forked from decred/dcrdex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.go
53 lines (46 loc) · 1.37 KB
/
keygen.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
package keygen
import (
"fmt"
"github.com/decred/dcrd/hdkeychain/v3"
)
// RootKeyParams implements hdkeychain.NetworkParams for master
// hdkeychain.ExtendedKey creation.
type RootKeyParams struct{}
func (*RootKeyParams) HDPrivKeyVersion() [4]byte {
return [4]byte{0x74, 0x61, 0x63, 0x6f} // ASCII "taco"
}
func (*RootKeyParams) HDPubKeyVersion() [4]byte {
return [4]byte{0x64, 0x65, 0x78, 0x63} // ASCII "dexc"
}
// GenDeepChild derives the leaf of a path of children from a root extended key.
func GenDeepChild(seed []byte, kids []uint32) (*hdkeychain.ExtendedKey, error) {
root, err := hdkeychain.NewMaster(seed, &RootKeyParams{})
defer root.Zero()
if err != nil {
return nil, err
}
genChild := func(parent *hdkeychain.ExtendedKey, childIdx uint32) (*hdkeychain.ExtendedKey, error) {
err := hdkeychain.ErrInvalidChild
for err == hdkeychain.ErrInvalidChild {
var kid *hdkeychain.ExtendedKey
kid, err = parent.ChildBIP32Std(childIdx)
if err == nil {
return kid, nil
}
fmt.Printf("Child derive skipped a key index %d -> %d", childIdx, childIdx+1) // < 1 in 2^127 chance
childIdx++
}
return nil, err
}
extKey := root
for _, childIdx := range kids {
childExtKey, err := genChild(extKey, childIdx)
extKey.Zero()
extKey = childExtKey
if err != nil {
extKey.Zero()
return nil, fmt.Errorf("genChild error: %w", err)
}
}
return extKey, nil
}