forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.go
427 lines (335 loc) · 8.83 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
// 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"
"fmt"
"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"
)
type addressStringType byte
const (
InvalidAddress addressStringType = iota
FactoidPub
FactoidSec
ECPub
ECSec
)
const (
AddressLength = 38
PrefixLength = 2
ChecksumLength = 4
BodyLength = AddressLength - ChecksumLength
)
var (
ecPubPrefix = []byte{0x59, 0x2a}
ecSecPrefix = []byte{0x5d, 0xb6}
fcPubPrefix = []byte{0x5f, 0xb1}
fcSecPrefix = []byte{0x64, 0x78}
)
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
}
}
func IsValidAddress(s string) bool {
p := base58.Decode(s)
if len(p) != AddressLength {
return false
}
prefix := p[:PrefixLength]
switch {
case bytes.Equal(prefix, ecPubPrefix):
break
case bytes.Equal(prefix, ecSecPrefix):
break
case bytes.Equal(prefix, fcPubPrefix):
break
case bytes.Equal(prefix, fcSecPrefix):
break
default:
return false
}
// verify the address checksum
body := p[:BodyLength]
check := p[AddressLength-ChecksumLength:]
if bytes.Equal(shad(body)[:ChecksumLength], check) {
return true
}
return false
}
type ECAddress struct {
Pub *[ed.PublicKeySize]byte
Sec *[ed.PrivateKeySize]byte
}
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
}
func (a *ECAddress) UnmarshalBinaryData(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, fmt.Errorf("secret key portion must be 32 bytes")
}
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 takes a private address string (Es...) and returns an ECAddress.
func GetECAddress(s string) (*ECAddress, error) {
if !IsValidAddress(s) {
return nil, fmt.Errorf("Invalid Address")
}
p := base58.Decode(s)
if !bytes.Equal(p[:PrefixLength], ecSecPrefix) {
return nil, fmt.Errorf("Invalid Entry Credit Private Address")
}
return MakeECAddress(p[PrefixLength:BodyLength])
}
func MakeECAddress(sec []byte) (*ECAddress, error) {
if len(sec) != 32 {
return nil, fmt.Errorf("secret key portion must be 32 bytes")
}
a := NewECAddress()
err := a.UnmarshalBinary(sec)
if err != nil {
return nil, err
}
return a, nil
}
// PubBytes returns the []byte representation of the public key
func (a *ECAddress) PubBytes() []byte {
return a.Pub[:]
}
// PubFixed returns the fixed size public key
func (a *ECAddress) PubFixed() *[ed.PublicKeySize]byte {
return a.Pub
}
// PubString returns the string encoding of the public key
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
func (a *ECAddress) SecFixed() *[ed.PrivateKeySize]byte {
return a.Sec
}
// SecString returns the string encoding of the secret key
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 private key
func (a *ECAddress) Sign(msg []byte) *[ed.SignatureSize]byte {
return ed.Sign(a.SecFixed(), msg)
}
func (a *ECAddress) String() string {
return a.PubString()
}
type FactoidAddress struct {
RCD RCD
Sec *[ed.PrivateKeySize]byte
}
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
}
func (t *FactoidAddress) UnmarshalBinary(data []byte) error {
_, err := t.UnmarshalBinaryData(data)
return err
}
func (t *FactoidAddress) UnmarshalBinaryData(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, fmt.Errorf("secret key portion must be 32 bytes")
}
if t.Sec == nil {
t.Sec = new([ed.PrivateKeySize]byte)
}
copy(t.Sec[:], data[:32])
r := NewRCD1()
r.Pub = ed.GetPublicKey(t.Sec)
t.RCD = r
return data[32:], nil
}
func (t *FactoidAddress) MarshalBinary() ([]byte, error) {
return t.SecBytes()[:32], nil
}
// GetFactoidAddress takes a private address string (Fs...) and returns a
// FactoidAddress.
func GetFactoidAddress(s string) (*FactoidAddress, error) {
if !IsValidAddress(s) {
return nil, fmt.Errorf("Invalid Address")
}
p := base58.Decode(s)
if !bytes.Equal(p[:PrefixLength], fcSecPrefix) {
return nil, fmt.Errorf("Invalid Factoid Private Address")
}
return MakeFactoidAddress(p[PrefixLength:BodyLength])
}
func MakeFactoidAddress(sec []byte) (*FactoidAddress, error) {
if len(sec) != 32 {
return nil, fmt.Errorf("secret key portion must be 32 bytes")
}
a := NewFactoidAddress()
err := a.UnmarshalBinary(sec)
if err != nil {
return nil, err
}
return a, nil
}
func ParseAndValidateMnemonic(mnemonic string) (string, error) {
if l := len(strings.Fields(mnemonic)); l != 12 {
return "", fmt.Errorf("Incorrect mnemonic length. Expecitng 12 words, found %d", l)
}
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
}
// MakeFactoidAddressFromKoinify takes the 12 word string used in the Koinify
// sale and returns a Factoid Address.
func MakeFactoidAddressFromKoinify(mnemonic string) (*FactoidAddress, error) {
mnemonic, err := ParseAndValidateMnemonic(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 MakeBIP44FactoidAddress(mnemonic string, account, chain, address uint32) (*FactoidAddress, error) {
mnemonic, err := ParseAndValidateMnemonic(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)
}
func MakeBIP44ECAddress(mnemonic string, account, chain, address uint32) (*ECAddress, error) {
mnemonic, err := ParseAndValidateMnemonic(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)
}
func (a *FactoidAddress) RCDHash() []byte {
return a.RCD.Hash()
}
func (a *FactoidAddress) RCDType() uint8 {
return a.RCD.Type()
}
func (a *FactoidAddress) PubBytes() []byte {
return a.RCD.(*RCD1).PubBytes()
}
func (a *FactoidAddress) SecBytes() []byte {
return a.Sec[:]
}
func (a *FactoidAddress) SecFixed() *[ed.PrivateKeySize]byte {
return a.Sec
}
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())
}