-
Notifications
You must be signed in to change notification settings - Fork 18
/
hdnode.go
280 lines (233 loc) · 6.75 KB
/
hdnode.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
package ethwallet
import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"github.com/0xsequence/ethkit/go-ethereum/accounts"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/crypto"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/tyler-smith/go-bip39"
)
// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
// are incremented. As such, the first account will be at m/44'/60'/0'/0/0, the second
// at m/44'/60'/0'/0/1, etc.
var DefaultBaseDerivationPath = accounts.DefaultBaseDerivationPath
// Entropy bit size constants for 12 and 24 word mnemonics
const (
EntropyBitSize12WordMnemonic = 128
EntropyBitSize24WordMnemonic = 256
)
type HDNode struct {
masterKey *hdkeychain.ExtendedKey
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
entropy []byte
mnemonic string
derivationPath accounts.DerivationPath
address common.Address
}
func NewHDNodeFromPrivateKey(privateKey string) (*HDNode, error) {
key, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
return &HDNode{
privateKey: key,
publicKey: &key.PublicKey,
address: crypto.PubkeyToAddress(key.PublicKey),
}, nil
}
func NewHDNodeFromMnemonic(mnemonic string, path *accounts.DerivationPath) (*HDNode, error) {
entropy, err := MnemonicToEntropy(mnemonic)
if err != nil {
return nil, err
}
hdnode, err := NewHDNodeFromEntropy(entropy, path)
if err != nil {
return nil, err
}
hdnode.mnemonic = mnemonic
return hdnode, nil
}
func NewHDNodeFromEntropy(entropy []byte, path *accounts.DerivationPath) (*HDNode, error) {
mnemonic, err := EntropyToMnemonic(entropy)
if err != nil {
return nil, err
}
seed, err := NewSeedFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, err
}
var derivationPath accounts.DerivationPath
if path == nil {
derivationPath = DefaultBaseDerivationPath
} else {
derivationPath = *path
}
privateKey, err := derivePrivateKey(masterKey, derivationPath)
if err != nil {
return nil, err
}
publicKey, err := derivePublicKey(masterKey, derivationPath)
if err != nil {
return nil, err
}
address, err := deriveAddress(masterKey, derivationPath)
if err != nil {
return nil, err
}
return &HDNode{
masterKey: masterKey,
privateKey: privateKey,
publicKey: publicKey,
entropy: entropy,
mnemonic: mnemonic,
derivationPath: derivationPath,
address: address,
}, nil
}
func NewHDNodeFromRandomEntropy(bitSize int, path *accounts.DerivationPath) (*HDNode, error) {
entropy, err := RandomEntropy(bitSize)
if err != nil {
return nil, err
}
return NewHDNodeFromEntropy(entropy, path)
}
// NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic.
func NewSeedFromMnemonic(mnemonic string) ([]byte, error) {
if mnemonic == "" {
return nil, fmt.Errorf("mnemonic is required")
}
return bip39.NewSeedWithErrorChecking(mnemonic, "")
}
func MnemonicToEntropy(mnemonic string) ([]byte, error) {
return bip39.MnemonicToByteArray(mnemonic, true)
}
func EntropyToMnemonic(entropy []byte) (string, error) {
return bip39.NewMnemonic(entropy)
}
func RandomEntropy(bitSize ...int) ([]byte, error) {
if len(bitSize) > 0 {
return bip39.NewEntropy(bitSize[0])
} else {
return bip39.NewEntropy(EntropyBitSize12WordMnemonic) // default
}
}
// RandomSeed returns a randomly generated BIP-39 seed.
func RandomSeed() ([]byte, error) {
b := make([]byte, 64)
_, err := rand.Read(b)
return b, err
}
func IsValidMnemonic(mnemonic string) bool {
return bip39.IsMnemonicValid(mnemonic)
}
// ParseDerivationPath parses the derivation path in string format into []uint32
func ParseDerivationPath(path string) (accounts.DerivationPath, error) {
return accounts.ParseDerivationPath(path)
}
func (h *HDNode) Mnemonic() string {
return h.mnemonic
}
func (h *HDNode) Entropy() []byte {
return h.entropy
}
func (h *HDNode) DerivationPath() accounts.DerivationPath {
return h.derivationPath
}
func (h *HDNode) Address() common.Address {
return h.address
}
func (h *HDNode) PrivateKey() *ecdsa.PrivateKey {
return h.privateKey
}
func (h *HDNode) PublicKey() *ecdsa.PublicKey {
return h.publicKey
}
func (h *HDNode) DerivePathFromString(path string) error {
derivationPath, err := ParseDerivationPath(path)
if err != nil {
return err
}
return h.DerivePath(derivationPath)
}
func (h *HDNode) DerivePath(derivationPath accounts.DerivationPath) error {
privateKey, err := derivePrivateKey(h.masterKey, derivationPath)
if err != nil {
return err
}
publicKey, err := derivePublicKey(h.masterKey, derivationPath)
if err != nil {
return err
}
address, err := deriveAddress(h.masterKey, derivationPath)
if err != nil {
return err
}
h.derivationPath = derivationPath
h.privateKey = privateKey
h.publicKey = publicKey
h.address = address
return nil
}
func (h *HDNode) DeriveAccountIndex(accountIndex uint32) error {
x := len(h.derivationPath)
if x < 4 {
return fmt.Errorf("invalid account derivation path")
}
// copy + update
updatedPath := make(accounts.DerivationPath, len(h.derivationPath))
copy(updatedPath, h.derivationPath)
updatedPath[x-1] = accountIndex
return h.DerivePath(updatedPath)
}
func (h *HDNode) Clone() (*HDNode, error) {
derivationPath := make(accounts.DerivationPath, len(h.derivationPath))
copy(derivationPath, h.derivationPath)
return NewHDNodeFromMnemonic(h.Mnemonic(), &derivationPath)
}
// DerivePrivateKey derives the private key of the derivation path.
func derivePrivateKey(masterKey *hdkeychain.ExtendedKey, path accounts.DerivationPath) (*ecdsa.PrivateKey, error) {
var err error
key := masterKey
for _, n := range path {
key, err = key.Child(n)
if err != nil {
return nil, err
}
}
privateKey, err := key.ECPrivKey()
if err != nil {
return nil, err
}
privateKeyECDSA := privateKey.ToECDSA()
return privateKeyECDSA, nil
}
// DerivePublicKey derives the public key of the derivation path.
func derivePublicKey(masterKey *hdkeychain.ExtendedKey, path accounts.DerivationPath) (*ecdsa.PublicKey, error) {
privateKeyECDSA, err := derivePrivateKey(masterKey, path)
if err != nil {
return nil, err
}
publicKey := privateKeyECDSA.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("failed to get public key")
}
return publicKeyECDSA, nil
}
// DeriveAddress derives the account address of the derivation path.
func deriveAddress(masterKey *hdkeychain.ExtendedKey, path accounts.DerivationPath) (common.Address, error) {
publicKeyECDSA, err := derivePublicKey(masterKey, path)
if err != nil {
return common.Address{}, err
}
address := crypto.PubkeyToAddress(*publicKeyECDSA)
return address, nil
}