-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkey.go
95 lines (71 loc) · 1.8 KB
/
key.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
package bip44
import (
"encoding/hex"
"github.com/btcsuite/btcutil/hdkeychain"
)
type ExtendedKey struct {
Key *hdkeychain.ExtendedKey
}
func NewKeyFromSeedHex(seed string, net Network) (*ExtendedKey, error) {
pk, err := hex.DecodeString(seed)
if err != nil {
return nil, err
}
return NewKeyFromSeedBytes(pk, net)
}
func NewKeyFromSeedBytes(seed []byte, net Network) (*ExtendedKey, error) {
n, err := networkToChainConfig(net)
if err != nil {
return nil, err
}
xKey, err := hdkeychain.NewMaster(seed, n)
if err != nil {
return nil, err
}
return &ExtendedKey{
Key: xKey,
}, nil
}
func (e *ExtendedKey) BIP44AccountKey(coinType CoinType, accIndex uint32, includePrivateKey bool) (*AccountKey, error) {
return e.baseDeriveAccount(BIP44Purpose, coinType, accIndex, includePrivateKey)
}
func (e *ExtendedKey) baseDeriveAccount(purpose Purpose, coinType CoinType, accIndex uint32, includePrivateKey bool) (*AccountKey, error) {
var purposeIndex = uint32(purpose)
var coinTypeIndex = uint32(coinType)
if e.Key.IsPrivate() {
purposeIndex = HardenedKeyZeroIndex + purposeIndex
coinTypeIndex = HardenedKeyZeroIndex + coinTypeIndex
accIndex = HardenedKeyZeroIndex + accIndex
}
purposeK, err := e.Key.Child(purposeIndex)
if err != nil {
return nil, err
}
cTypeK, err := purposeK.Child(coinTypeIndex)
if err != nil {
return nil, err
}
accK, err := cTypeK.Child(accIndex)
if err != nil {
return nil, err
}
hdStartPath := HDStartPath{
PurposeIndex: purposeIndex,
CoinTypeIndex: coinTypeIndex,
AccountIndex: accIndex,
}
if includePrivateKey {
return &AccountKey{
extendedKey: accK,
startPath: hdStartPath,
}, nil
}
pub, err := accK.Neuter()
if err != nil {
return nil, err
}
return &AccountKey{
extendedKey: pub,
startPath: hdStartPath,
}, nil
}