-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.go
522 lines (447 loc) · 14.9 KB
/
main.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (c) 2015-2020 The Decred Developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"runtime"
"strings"
"decred.org/dcrwallet/walletseed"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrec"
"github.com/decred/dcrd/dcrec/secp256k1/v3"
"github.com/decred/dcrd/dcrutil/v3"
"github.com/decred/dcrd/hdkeychain/v3"
)
// The hierarchy described by BIP0043 is:
// m/<purpose>'/*
// This is further extended by BIP0044 to:
// m/44'/<coin type>'/<account>'/<branch>/<address index>
//
// The branch is 0 for external addresses and 1 for internal addresses.
// maxCoinType is the maximum allowed coin type used when structuring
// the BIP0044 multi-account hierarchy. This value is based on the
// limitation of the underlying hierarchical deterministic key
// derivation.
const maxCoinType = hdkeychain.HardenedKeyStart - 1
// MaxAccountNum is the maximum allowed account number. This value was
// chosen because accounts are hardened children and therefore must
// not exceed the hardened child range of extended keys and it provides
// a reserved account at the top of the range for supporting imported
// addresses.
const MaxAccountNum = hdkeychain.HardenedKeyStart - 2 // 2^31 - 2
// ExternalBranch is the child number to use when performing BIP0044
// style hierarchical deterministic key derivation for the external
// branch.
const ExternalBranch uint32 = 0
// InternalBranch is the child number to use when performing BIP0044
// style hierarchical deterministic key derivation for the internal
// branch.
const InternalBranch uint32 = 1
var params = chaincfg.MainNetParams()
// Flag arguments.
var getHelp = flag.Bool("h", false, "Print help message")
var testnet = flag.Bool("testnet", false, "")
var simnet = flag.Bool("simnet", false, "")
var noseed = flag.Bool("noseed", false, "Generate a single keypair instead of "+
"an HD extended seed")
var verify = flag.Bool("verify", false, "Verify a seed by generating the first "+
"address")
func setupFlags(msg func(), f *flag.FlagSet) {
f.Usage = msg
}
var newLine = "\n"
// writeNewFile writes data to a file named by filename.
// Error is returned if the file does exist. Otherwise writeNewFile creates the file with permissions perm;
// Based on ioutil.WriteFile, but produces an err if the file exists.
func writeNewFile(filename string, data []byte, perm os.FileMode) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
if err != nil {
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
// There was no error, but not all the data was written, so report an error.
err = io.ErrShortWrite
}
if err != nil {
// There was an error, so close file (ignoring any further errors) and return the error.
f.Close()
return err
}
return f.Close()
}
// generateKeyPair generates and stores a secp256k1 keypair in a file.
func generateKeyPair(filename string) error {
priv, err := secp256k1.GeneratePrivateKey()
if err != nil {
return err
}
pub := priv.PubKey()
addr, err := dcrutil.NewAddressPubKeyHash(
dcrutil.Hash160(pub.SerializeCompressed()),
params,
dcrec.STEcdsaSecp256k1)
if err != nil {
return err
}
privWif, err := dcrutil.NewWIF(priv.Serialize(), params.PrivateKeyID, dcrec.STEcdsaSecp256k1)
if err != nil {
return err
}
var buf bytes.Buffer
buf.WriteString("Address: ")
buf.WriteString(addr.Address())
buf.WriteString(newLine)
buf.WriteString("Private key: ")
buf.WriteString(privWif.String())
buf.WriteString(newLine)
return writeNewFile(filename, buf.Bytes(), 0600)
}
// deriveCoinTypeKey derives the cointype key which can be used to derive the
// extended key for an account according to the hierarchy described by BIP0044
// given the coin type key.
//
// In particular this is the hierarchical deterministic extended key path:
// m/44'/<coin type>'
func deriveCoinTypeKey(masterNode *hdkeychain.ExtendedKey,
coinType uint32) (*hdkeychain.ExtendedKey, error) {
// Enforce maximum coin type.
if coinType > maxCoinType {
return nil, fmt.Errorf("bad coin type")
}
// The hierarchy described by BIP0043 is:
// m/<purpose>'/*
// This is further extended by BIP0044 to:
// m/44'/<coin type>'/<account>'/<branch>/<address index>
//
// The branch is 0 for external addresses and 1 for internal addresses.
// Derive the purpose key as a child of the master node.
purpose, err := masterNode.Child(44 + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}
// Derive the coin type key as a child of the purpose key.
coinTypeKey, err := purpose.Child(coinType + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}
return coinTypeKey, nil
}
// deriveAccountKey derives the extended key for an account according to the
// hierarchy described by BIP0044 given the master node.
//
// In particular this is the hierarchical deterministic extended key path:
// m/44'/<coin type>'/<account>'
func deriveAccountKey(coinTypeKey *hdkeychain.ExtendedKey,
account uint32) (*hdkeychain.ExtendedKey, error) {
// Enforce maximum account number.
if account > MaxAccountNum {
return nil, fmt.Errorf("account num too high")
}
// Derive the account key as a child of the coin type key.
return coinTypeKey.Child(account + hdkeychain.HardenedKeyStart)
}
// checkBranchKeys ensures deriving the extended keys for the internal and
// external branches given an account key does not result in an invalid child
// error which means the chosen seed is not usable. This conforms to the
// hierarchy described by BIP0044 so long as the account key is already derived
// accordingly.
//
// In particular this is the hierarchical deterministic extended key path:
// m/44'/<coin type>'/<account>'/<branch>
//
// The branch is 0 for external addresses and 1 for internal addresses.
func checkBranchKeys(acctKey *hdkeychain.ExtendedKey) error {
// Derive the external branch as the first child of the account key.
if _, err := acctKey.Child(ExternalBranch); err != nil {
return err
}
// Derive the external branch as the second child of the account key.
_, err := acctKey.Child(InternalBranch)
return err
}
// generateSeed derives an address from an HDKeychain for use in wallet. It
// outputs the seed, address, and extended public key to the file specified.
func generateSeed(filename string) error {
seed, err := hdkeychain.GenerateSeed(hdkeychain.RecommendedSeedLen)
if err != nil {
return err
}
// Derive the master extended key from the seed.
root, err := hdkeychain.NewMaster(seed, params)
if err != nil {
return err
}
defer root.Zero()
// Derive the cointype key according to BIP0044.
coinTypeKeyPriv, err := deriveCoinTypeKey(root, params.SLIP0044CoinType)
if err != nil {
return err
}
defer coinTypeKeyPriv.Zero()
// Derive the account key for the first account according to BIP0044.
acctKeyPriv, err := deriveAccountKey(coinTypeKeyPriv, 0)
if err != nil {
// The seed is unusable if the any of the children in the
// required hierarchy can't be derived due to invalid child.
if err == hdkeychain.ErrInvalidChild {
return fmt.Errorf("the provided seed is unusable")
}
return err
}
// Ensure the branch keys can be derived for the provided seed according
// to BIP0044.
if err := checkBranchKeys(acctKeyPriv); err != nil {
// The seed is unusable if the any of the children in the
// required hierarchy can't be derived due to invalid child.
if err == hdkeychain.ErrInvalidChild {
return fmt.Errorf("the provided seed is unusable")
}
return err
}
// The address manager needs the public extended key for the account.
acctKeyPub := acctKeyPriv.Neuter()
index := uint32(0) // First address
branch := uint32(0) // External
// The next address can only be generated for accounts that have already
// been created.
acctKey := acctKeyPub
defer acctKey.Zero()
// Derive the appropriate branch key and ensure it is zeroed when done.
branchKey, err := acctKey.Child(branch)
if err != nil {
return err
}
defer branchKey.Zero() // Ensure branch key is zeroed when done.
key, err := branchKey.Child(index)
if err != nil {
return err
}
defer key.Zero()
pk := key.SerializedPubKey()
pkHash := dcrutil.Hash160(pk)
addr, err := dcrutil.NewAddressPubKeyHash(pkHash, params, dcrec.STEcdsaSecp256k1)
if err != nil {
return err
}
// Require the user to write down the seed.
reader := bufio.NewReader(os.Stdin)
seedStr := walletseed.EncodeMnemonic(seed)
seedStrSplit := strings.Split(seedStr, " ")
fmt.Println("WRITE DOWN THE SEED GIVEN BELOW. YOU WILL NOT BE GIVEN " +
"ANOTHER CHANCE TO.\n")
fmt.Printf("Your wallet generation seed is:\n\n")
for i := 0; i < hdkeychain.RecommendedSeedLen+1; i++ {
fmt.Printf("%v ", seedStrSplit[i])
if (i+1)%6 == 0 {
fmt.Printf("\n")
}
}
fmt.Printf("\n\nHex: %x\n", seed)
fmt.Println("IMPORTANT: Keep the seed in a safe place as you\n" +
"will NOT be able to restore your wallet without it.")
fmt.Println("Please keep in mind that anyone who has access\n" +
"to the seed can also restore your wallet thereby\n" +
"giving them access to all your funds, so it is\n" +
"imperative that you keep it in a secure location.\n")
for {
fmt.Print("Once you have stored the seed in a safe \n" +
"and secure location, enter OK here to erase the \n" +
"seed and all derived keys from memory. Derived \n" +
"public keys and an address will be stored in the \n" +
"file specified (default: keys.txt): ")
confirmSeed, err := reader.ReadString('\n')
if err != nil {
return err
}
confirmSeed = strings.TrimSpace(confirmSeed)
confirmSeed = strings.Trim(confirmSeed, `"`)
if confirmSeed == "OK" {
break
}
}
var buf bytes.Buffer
buf.WriteString("First address: ")
buf.WriteString(addr.Address())
buf.WriteString(newLine)
buf.WriteString("Extended public key: ")
buf.WriteString(acctKey.String())
buf.WriteString(newLine)
// Zero the seed array.
copy(seed[:], bytes.Repeat([]byte{0x00}, 32))
return writeNewFile(filename, buf.Bytes(), 0600)
}
// promptSeed is used to prompt for the wallet seed which maybe required during
// upgrades.
func promptSeed(seedA *[32]byte) error {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter existing wallet seed: ")
seedStr, err := reader.ReadString('\n')
if err != nil {
return err
}
seedStrTrimmed := strings.TrimSpace(seedStr)
seed, err := walletseed.DecodeUserInput(seedStrTrimmed)
if err != nil || len(seed) < hdkeychain.MinSeedBytes ||
len(seed) > hdkeychain.MaxSeedBytes {
if err != nil {
fmt.Printf("Input error: %v\n", err.Error())
}
fmt.Printf("Invalid seed specified. Must be "+
"the words of the seed and at least %d bits and "+
"at most %d bits\n", hdkeychain.MinSeedBytes*8,
hdkeychain.MaxSeedBytes*8)
continue
}
copy(seedA[:], seed[:])
// Zero the seed slice.
copy(seed[:], bytes.Repeat([]byte{0x00}, 32))
return nil
}
}
func verifySeed() error {
seed := new([32]byte)
err := promptSeed(seed)
if err != nil {
return err
}
// Derive the master extended key from the seed.
root, err := hdkeychain.NewMaster(seed[:], params)
if err != nil {
return err
}
defer root.Zero()
// Derive the cointype key according to BIP0044.
coinTypeKeyPriv, err := deriveCoinTypeKey(root, params.SLIP0044CoinType)
if err != nil {
return err
}
defer coinTypeKeyPriv.Zero()
// Derive the account key for the first account according to BIP0044.
acctKeyPriv, err := deriveAccountKey(coinTypeKeyPriv, 0)
if err != nil {
// The seed is unusable if the any of the children in the
// required hierarchy can't be derived due to invalid child.
if err == hdkeychain.ErrInvalidChild {
return fmt.Errorf("the provided seed is unusable")
}
return err
}
// Ensure the branch keys can be derived for the provided seed according
// to BIP0044.
if err := checkBranchKeys(acctKeyPriv); err != nil {
// The seed is unusable if the any of the children in the
// required hierarchy can't be derived due to invalid child.
if err == hdkeychain.ErrInvalidChild {
return fmt.Errorf("the provided seed is unusable")
}
return err
}
// The address manager needs the public extended key for the account.
acctKeyPub := acctKeyPriv.Neuter()
index := uint32(0) // First address
branch := uint32(0) // External
// The next address can only be generated for accounts that have already
// been created.
acctKey := acctKeyPub
defer acctKey.Zero()
// Derive the appropriate branch key and ensure it is zeroed when done.
branchKey, err := acctKey.Child(branch)
if err != nil {
return err
}
defer branchKey.Zero() // Ensure branch key is zeroed when done.
key, err := branchKey.Child(index)
if err != nil {
return err
}
defer key.Zero()
pk := key.SerializedPubKey()
pkHash := dcrutil.Hash160(pk)
addr, err := dcrutil.NewAddressPubKeyHash(pkHash, params, dcrec.STEcdsaSecp256k1)
if err != nil {
return err
}
fmt.Printf("First derived address of given seed: \n%v\n",
addr.Address())
// Zero the seed array.
copy(seed[:], bytes.Repeat([]byte{0x00}, 32))
return nil
}
func main() {
if runtime.GOOS == "windows" {
newLine = "\r\n"
}
helpMessage := func() {
fmt.Println(
"Usage: dcraddrgen [-testnet] [-simnet] [-noseed] [-verify] [-h] filename")
fmt.Println("Generate a Decred private and public key or wallet seed. \n" +
"These are output to the file 'filename'.\n")
fmt.Println(" -h \t\tPrint this message")
fmt.Println(" -testnet \tGenerate a testnet key instead of mainnet")
fmt.Println(" -simnet \tGenerate a simnet key instead of mainnet")
fmt.Println(" -noseed \tGenerate a single keypair instead of a seed")
fmt.Println(" -verify \tVerify a seed by generating the first address")
}
setupFlags(helpMessage, flag.CommandLine)
flag.Parse()
if *getHelp {
helpMessage()
return
}
if *verify {
err := verifySeed()
if err != nil {
fmt.Printf("Error verifying seed: %v\n", err.Error())
return
}
return
}
var fn string
if flag.Arg(0) != "" {
fn = flag.Arg(0)
} else {
fn = "keys.txt"
}
// Alter the globals to specified network.
if *testnet {
if *simnet {
fmt.Println("Error: Only specify one network.")
return
}
params = chaincfg.TestNet3Params()
}
if *simnet {
params = chaincfg.SimNetParams()
}
// Single keypair generation.
if *noseed {
err := generateKeyPair(fn)
if err != nil {
fmt.Printf("Error generating key pair: %v\n", err.Error())
return
}
fmt.Printf("Successfully generated keypair and stored it in %v.\n",
fn)
fmt.Printf("Your private key is used to spend your funds. Do not " +
"reveal it to anyone.\n")
return
}
// Derivation of an address from an HDKeychain for use in wallet.
err := generateSeed(fn)
if err != nil {
fmt.Printf("Error generating seed: %v\n", err.Error())
return
}
fmt.Printf("\nSuccessfully generated an extended public \n"+
"key and address and stored them in %v.\n", fn)
fmt.Printf("\nYour extended public key can be used to " +
"derive all your addresses. Keep it private.\n")
}