-
Notifications
You must be signed in to change notification settings - Fork 9
/
account_key.go
69 lines (53 loc) · 1.29 KB
/
account_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
package bip44
import (
"github.com/btcsuite/btcutil/hdkeychain"
)
type AccountKey struct {
extendedKey *hdkeychain.ExtendedKey
startPath HDStartPath
}
func NewAccountKeyFromXPubKey(value string) (*AccountKey, error) {
xKey, err := hdkeychain.NewKeyFromString(value)
if err != nil {
return nil, err
}
return &AccountKey{
extendedKey: xKey,
}, nil
}
func (k *AccountKey) DeriveP2PKAddress(changeType ChangeType, index uint32, network Network) (*Address, error) {
if k.extendedKey.IsPrivate() {
changeType = HardenedKeyZeroIndex + changeType
index = HardenedKeyZeroIndex + index
}
var changeTypeIndex = uint32(changeType)
changeTypeK, err := k.extendedKey.Child(changeTypeIndex)
if err != nil {
return nil, err
}
addressK, err := changeTypeK.Child(index)
if err != nil {
return nil, err
}
netParam, err := networkToChainConfig(network)
if err != nil {
return nil, err
}
a, err := addressK.Address(netParam)
if err != nil {
return nil, err
}
address := &Address{
HDStartPath: HDStartPath{
PurposeIndex: k.startPath.PurposeIndex,
CoinTypeIndex: k.startPath.CoinTypeIndex,
AccountIndex: k.startPath.AccountIndex,
},
HDEndPath: HDEndPath{
ChangeIndex: changeTypeIndex,
AddressIndex: index,
},
Value: a.EncodeAddress(),
}
return address, nil
}