-
Notifications
You must be signed in to change notification settings - Fork 19
/
identityKeys.go
219 lines (174 loc) · 4.43 KB
/
identityKeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package factom
import (
"bytes"
"fmt"
"github.com/FactomProject/btcutil/base58"
ed "github.com/FactomProject/ed25519"
"github.com/FactomProject/go-bip44"
)
type identityKeyStringType byte
const (
InvalidIdentityKey identityKeyStringType = iota
IDPub
IDSec
)
const (
IDKeyLength = 41
IDKeyPrefixLength = 5
IDKeyBodyLength = IDKeyLength - ChecksumLength
)
var (
idPubPrefix = []byte{0x03, 0x45, 0xef, 0x9d, 0xe0}
idSecPrefix = []byte{0x03, 0x45, 0xf3, 0xd0, 0xd6}
)
func IdentityKeyStringType(s string) identityKeyStringType {
p := base58.Decode(s)
if len(p) != IDKeyLength {
return InvalidIdentityKey
}
// verify the address checksum
body := p[:IDKeyBodyLength]
check := p[IDKeyLength-ChecksumLength:]
if !bytes.Equal(shad(body)[:ChecksumLength], check) {
return InvalidIdentityKey
}
prefix := p[:IDKeyPrefixLength]
switch {
case bytes.Equal(prefix, idPubPrefix):
return IDPub
case bytes.Equal(prefix, idSecPrefix):
return IDSec
default:
return InvalidIdentityKey
}
}
func IsValidIdentityKey(s string) bool {
p := base58.Decode(s)
if len(p) != IDKeyLength {
return false
}
prefix := p[:IDKeyPrefixLength]
switch {
case bytes.Equal(prefix, idPubPrefix):
break
case bytes.Equal(prefix, idSecPrefix):
break
default:
return false
}
// verify the address checksum
body := p[:IDKeyBodyLength]
check := p[IDKeyLength-ChecksumLength:]
if bytes.Equal(shad(body)[:ChecksumLength], check) {
return true
}
return false
}
type IdentityKey struct {
Pub *[ed.PublicKeySize]byte
Sec *[ed.PrivateKeySize]byte
}
func NewIdentityKey() *IdentityKey {
k := new(IdentityKey)
k.Pub = new([ed.PublicKeySize]byte)
k.Sec = new([ed.PrivateKeySize]byte)
return k
}
func (k *IdentityKey) UnmarshalBinary(data []byte) error {
_, err := k.UnmarshalBinaryData(data)
return err
}
func (k *IdentityKey) UnmarshalBinaryData(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, fmt.Errorf("secret key portion must be 32 bytes")
}
if k.Sec == nil {
k.Sec = new([ed.PrivateKeySize]byte)
}
copy(k.Sec[:], data[:32])
k.Pub = ed.GetPublicKey(k.Sec)
return data[32:], nil
}
func (k *IdentityKey) MarshalBinary() ([]byte, error) {
return k.SecBytes()[:32], nil
}
// GetIdentityKey takes a private key string and returns an IdentityKey.
func GetIdentityKey(s string) (*IdentityKey, error) {
if !IsValidIdentityKey(s) {
return nil, fmt.Errorf("invalid Identity Private Key")
}
p := base58.Decode(s)
if !bytes.Equal(p[:IDKeyPrefixLength], idSecPrefix) {
return nil, fmt.Errorf("invalid Identity Private Key")
}
return MakeIdentityKey(p[IDKeyPrefixLength:IDKeyBodyLength])
}
func MakeIdentityKey(sec []byte) (*IdentityKey, error) {
if len(sec) != 32 {
return nil, ErrSecKeyLength
}
k := NewIdentityKey()
err := k.UnmarshalBinary(sec)
if err != nil {
return nil, err
}
return k, nil
}
func MakeBIP44IdentityKey(mnemonic string, account, chain, address uint32) (*IdentityKey, error) {
mnemonic, err := ParseMnemonic(mnemonic)
if err != nil {
return nil, err
}
child, err := bip44.NewKeyFromMnemonic(
mnemonic,
bip44.TypeFactomIdentity,
account,
chain,
address,
)
if err != nil {
return nil, err
}
return MakeIdentityKey(child.Key)
}
// PubBytes returns the []byte representation of the public key
func (k *IdentityKey) PubBytes() []byte {
return k.Pub[:]
}
// PubFixed returns the fixed size public key
func (k *IdentityKey) PubFixed() *[ed.PublicKeySize]byte {
return k.Pub
}
// PubString returns the string encoding of the public key
func (k *IdentityKey) PubString() string {
buf := new(bytes.Buffer)
buf.Write(idPubPrefix)
buf.Write(k.PubBytes())
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
// SecBytes returns the []byte representation of the secret key
func (k *IdentityKey) SecBytes() []byte {
return k.Sec[:]
}
// SecFixed returns the fixed size secret key
func (k *IdentityKey) SecFixed() *[ed.PrivateKeySize]byte {
return k.Sec
}
// SecString returns the string encoding of the secret key
func (k *IdentityKey) SecString() string {
buf := new(bytes.Buffer)
buf.Write(idSecPrefix)
buf.Write(k.SecBytes()[:32])
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
// Sign the message with the Identity private key
func (k *IdentityKey) Sign(msg []byte) *[ed.SignatureSize]byte {
return ed.Sign(k.SecFixed(), msg)
}
func (k *IdentityKey) String() string {
return k.PubString()
}