-
Notifications
You must be signed in to change notification settings - Fork 19
/
addresses.go
451 lines (362 loc) · 10.7 KB
/
addresses.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"bytes"
"errors"
"strings"
"github.com/FactomProject/btcutil/base58"
ed "github.com/FactomProject/ed25519"
"github.com/FactomProject/go-bip32"
"github.com/FactomProject/go-bip39"
"github.com/FactomProject/go-bip44"
)
// Common Address errors
var (
ErrInvalidAddress = errors.New("invalid address")
ErrInvalidFactoidSec = errors.New("invalid Factoid secret address")
ErrInvalidECSec = errors.New("invalid Entry Credit secret address")
ErrSecKeyLength = errors.New("secret key portion must be 32 bytes")
ErrMnemonicLength = errors.New("mnemonic must be 12 words")
)
type addressStringType byte
const (
InvalidAddress addressStringType = iota
FactoidPub
FactoidSec
ECPub
ECSec
)
const (
AddressLength = 38
PrefixLength = 2
ChecksumLength = 4
BodyLength = AddressLength - ChecksumLength
)
var (
fcPubPrefix = []byte{0x5f, 0xb1}
fcSecPrefix = []byte{0x64, 0x78}
ecPubPrefix = []byte{0x59, 0x2a}
ecSecPrefix = []byte{0x5d, 0xb6}
)
// AddressStringType determin the type of address from the given string.
// AddressStringType must return one of the defined address types;
// InvalidAddress, FactoidPub, FactoidSec, ECPub, or ECSec.
func AddressStringType(s string) addressStringType {
p := base58.Decode(s)
if len(p) != AddressLength {
return InvalidAddress
}
// verify the address checksum
body := p[:BodyLength]
check := p[AddressLength-ChecksumLength:]
if !bytes.Equal(shad(body)[:ChecksumLength], check) {
return InvalidAddress
}
prefix := p[:PrefixLength]
switch {
case bytes.Equal(prefix, ecPubPrefix):
return ECPub
case bytes.Equal(prefix, ecSecPrefix):
return ECSec
case bytes.Equal(prefix, fcPubPrefix):
return FactoidPub
case bytes.Equal(prefix, fcSecPrefix):
return FactoidSec
default:
return InvalidAddress
}
}
// IsValidAddress checks that a string is a valid address of one of the defined
// address types.
//
// For an address to be valid it must be the correct length, it must begin with
// one of the defined address prefixes, and the address checksum must match the
// address body.
func IsValidAddress(s string) bool {
if AddressStringType(s) != InvalidAddress {
return true
}
return false
}
// ECAddress is an Entry Credit public/secret key pair.
type ECAddress struct {
Pub *[ed.PublicKeySize]byte
Sec *[ed.PrivateKeySize]byte
}
// NewECAddress creates a blank public/secret key pair for an Entry Credit
// Address.
func NewECAddress() *ECAddress {
a := new(ECAddress)
a.Pub = new([ed.PublicKeySize]byte)
a.Sec = new([ed.PrivateKeySize]byte)
return a
}
func (a *ECAddress) UnmarshalBinary(data []byte) error {
_, err := a.UnmarshalBinaryData(data)
return err
}
// UnmarshalBinaryData reads an ECAddress from a byte stream and returns the
// remainder of the byte stream.
func (a *ECAddress) UnmarshalBinaryData(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, ErrSecKeyLength
}
if a.Sec == nil {
a.Sec = new([ed.PrivateKeySize]byte)
}
copy(a.Sec[:], data[:32])
a.Pub = ed.GetPublicKey(a.Sec)
return data[32:], nil
}
func (a *ECAddress) MarshalBinary() ([]byte, error) {
return a.SecBytes()[:32], nil
}
// GetECAddress creates an Entry Credit Address public/secret key pair from a
// secret Entry Credit Address string i.e. Es...
func GetECAddress(s string) (*ECAddress, error) {
if !IsValidAddress(s) {
return nil, ErrInvalidAddress
}
p := base58.Decode(s)
if !bytes.Equal(p[:PrefixLength], ecSecPrefix) {
return nil, ErrInvalidECSec
}
return MakeECAddress(p[PrefixLength:BodyLength])
}
// MakeECAddress creates an Entry Credit Address public/secret key pair from a
// secret key []byte.
func MakeECAddress(sec []byte) (*ECAddress, error) {
if len(sec) != 32 {
return nil, ErrSecKeyLength
}
a := NewECAddress()
err := a.UnmarshalBinary(sec)
if err != nil {
return nil, err
}
return a, nil
}
// MakeBIP44ECAddress generates an Entry Credit Address from a 12 word mnemonic,
// an account index, a chain index, and an address index, according to the bip44
// standard for multicoin wallets.
func MakeBIP44ECAddress(mnemonic string, account, chain, address uint32) (*ECAddress, error) {
mnemonic, err := ParseMnemonic(mnemonic)
if err != nil {
return nil, err
}
child, err := bip44.NewKeyFromMnemonic(mnemonic, bip44.TypeFactomEntryCredits, account, chain, address)
if err != nil {
return nil, err
}
return MakeECAddress(child.Key)
}
// PubBytes returns the []byte representation of the public key.
func (a *ECAddress) PubBytes() []byte {
return a.Pub[:]
}
// PubFixed returns the fixed size public key ([32]byte).
func (a *ECAddress) PubFixed() *[ed.PublicKeySize]byte {
return a.Pub
}
// PubString returns the string encoding of the public key i.e. EC...
func (a *ECAddress) PubString() string {
buf := new(bytes.Buffer)
// EC address prefix
buf.Write(ecPubPrefix)
// Public key
buf.Write(a.PubBytes())
// Checksum
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
// SecBytes returns the []byte representation of the secret key.
func (a *ECAddress) SecBytes() []byte {
return a.Sec[:]
}
// SecFixed returns the fixed size secret key ([64]byte).
func (a *ECAddress) SecFixed() *[ed.PrivateKeySize]byte {
return a.Sec
}
// SecString returns the string encoding of the secret key i.e. Es...
func (a *ECAddress) SecString() string {
buf := new(bytes.Buffer)
// EC address prefix
buf.Write(ecSecPrefix)
// Secret key
buf.Write(a.SecBytes()[:32])
// Checksum
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
// Sign the message with the ECAddress secret key.
func (a *ECAddress) Sign(msg []byte) *[ed.SignatureSize]byte {
return ed.Sign(a.SecFixed(), msg)
}
func (a *ECAddress) String() string {
return a.PubString()
}
// FactoidAddress is a Factoid Redeem Condition Datastructure (a type 1 RCD is
// just the public key) and a corresponding secret key.
type FactoidAddress struct {
RCD RCD
Sec *[ed.PrivateKeySize]byte
}
// NewFactoidAddress creates a blank rcd/secret key pair for a Factoid Address.
func NewFactoidAddress() *FactoidAddress {
a := new(FactoidAddress)
r := NewRCD1()
r.Pub = new([ed.PublicKeySize]byte)
a.RCD = r
a.Sec = new([ed.PrivateKeySize]byte)
return a
}
// GetFactoidAddress creates a Factoid Address rcd/secret key pair from a secret
// Factoid Address string i.e. Fs...
func GetFactoidAddress(s string) (*FactoidAddress, error) {
if !IsValidAddress(s) {
return nil, ErrInvalidAddress
}
p := base58.Decode(s)
if !bytes.Equal(p[:PrefixLength], fcSecPrefix) {
return nil, ErrInvalidFactoidSec
}
return MakeFactoidAddress(p[PrefixLength:BodyLength])
}
// MakeFactoidAddress creates a Factoid Address rcd/secret key pair from a
// secret key []byte.
func MakeFactoidAddress(sec []byte) (*FactoidAddress, error) {
if len(sec) != 32 {
return nil, ErrSecKeyLength
}
a := NewFactoidAddress()
err := a.UnmarshalBinary(sec)
if err != nil {
return nil, err
}
return a, nil
}
// MakeBIP44FactoidAddress generates a Factoid Address from a 12 word mnemonic,
// an account index, a chain index, and an address index, according to the bip44
// standard for multicoin wallets.
func MakeBIP44FactoidAddress(mnemonic string, account, chain, address uint32) (*FactoidAddress, error) {
mnemonic, err := ParseMnemonic(mnemonic)
if err != nil {
return nil, err
}
child, err := bip44.NewKeyFromMnemonic(mnemonic, bip44.TypeFactomFactoids, account, chain, address)
if err != nil {
return nil, err
}
return MakeFactoidAddress(child.Key)
}
// MakeFactoidAddressFromKoinify takes the 12 word string used in the Koinify
// sale and returns a Factoid Address.
func MakeFactoidAddressFromKoinify(mnemonic string) (*FactoidAddress, error) {
mnemonic, err := ParseMnemonic(mnemonic)
if err != nil {
return nil, err
}
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return nil, err
}
masterKey, err := bip32.NewMasterKey(seed)
if err != nil {
return nil, err
}
child, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 7)
if err != nil {
return nil, err
}
return MakeFactoidAddress(child.Key)
}
func (a *FactoidAddress) UnmarshalBinary(data []byte) error {
_, err := a.UnmarshalBinaryData(data)
return err
}
func (a *FactoidAddress) UnmarshalBinaryData(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, ErrSecKeyLength
}
if a.Sec == nil {
a.Sec = new([ed.PrivateKeySize]byte)
}
copy(a.Sec[:], data[:32])
r := NewRCD1()
r.Pub = ed.GetPublicKey(a.Sec)
a.RCD = r
return data[32:], nil
}
func (a *FactoidAddress) MarshalBinary() ([]byte, error) {
return a.SecBytes()[:32], nil
}
// RCDHash returns the Hash of the Redeem Condition Datastructure from a Factoid
// Address.
func (a *FactoidAddress) RCDHash() []byte {
return a.RCD.Hash()
}
// RCDType returns the Redeem Condition Datastructure type used by the Factoid
// Address.
func (a *FactoidAddress) RCDType() uint8 {
return a.RCD.Type()
}
// PubBytes returns the []byte representation of the Redeem Condition
// Datastructure.
func (a *FactoidAddress) PubBytes() []byte {
return a.RCD.(*RCD1).PubBytes()
}
// SecBytes returns the []byte representation of the secret key.
func (a *FactoidAddress) SecBytes() []byte {
return a.Sec[:]
}
// SecFixed returns the fixed size secret key ([64]byte).
func (a *FactoidAddress) SecFixed() *[ed.PrivateKeySize]byte {
return a.Sec
}
// SecString returns the string encoding of the secret key i.e. Es...
func (a *FactoidAddress) SecString() string {
buf := new(bytes.Buffer)
// Factoid address prefix
buf.Write(fcSecPrefix)
// Secret key
buf.Write(a.SecBytes()[:32])
// Checksum
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
func (a *FactoidAddress) String() string {
buf := new(bytes.Buffer)
// FC address prefix
buf.Write(fcPubPrefix)
// RCD Hash
buf.Write(a.RCDHash())
// Checksum
check := shad(buf.Bytes())[:ChecksumLength]
buf.Write(check)
return base58.Encode(buf.Bytes())
}
// ParseMnemonic parse and validate a bip39 mnumonic string. Remove extra
// spaces, capitalization, etc. Return an error if the string is invalid.
func ParseMnemonic(mnemonic string) (string, error) {
if l := len(strings.Fields(mnemonic)); l != 12 {
return "", ErrMnemonicLength
}
mnemonic = strings.ToLower(strings.TrimSpace(mnemonic))
split := strings.Split(mnemonic, " ")
for i := len(split) - 1; i >= 0; i-- {
if split[i] == "" {
split = append(split[:i], split[i+1:]...)
}
}
mnemonic = strings.Join(split, " ")
_, err := bip39.MnemonicToByteArray(mnemonic)
if err != nil {
return "", err
}
return mnemonic, nil
}