/
hdwallet.go
340 lines (325 loc) · 9.29 KB
/
hdwallet.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
package main
import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip39"
"math/big"
"math/rand"
"sync"
)
// DefaultRootDerivationPath is the root path to which custom derivation endpoints
// are appended. As such, the first account will be at m/44'/60'/0'/0, the second
// at m/44'/60'/0'/1, etc.
var DefaultRootDerivationPath = accounts.DefaultRootDerivationPath
// 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, the second
// at m/44'/60'/0'/1, etc
var DefaultBaseDerivationPath = accounts.DefaultBaseDerivationPath
type Wallet struct {
mnemonic string
masterKey *hdkeychain.ExtendedKey
seed []byte
url accounts.URL
paths map[common.Address]accounts.DerivationPath
accounts []accounts.Account
stateLock sync.RWMutex
}
func newWallet(seed []byte) (*Wallet, error) {
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
}
return &Wallet{
masterKey: masterKey,
seed: seed,
accounts: []accounts.Account{},
paths: map[common.Address]accounts.DerivationPath{},
}, nil
}
func NewFromMnemonic(mnemonic, password string) (*Wallet, error) {
if mnemonic == "" {
return nil, errors.New("mnemonic is required")
}
if !bip39.IsMnemonicValid(mnemonic) {
return nil, errors.New("mnemonic is invalid")
}
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, password)
if err != nil {
return nil, err
}
wallet, err := newWallet(seed)
if err != nil {
return nil, err
}
wallet.mnemonic = mnemonic
return wallet, nil
}
func NewFromSeed(seed []byte) (*Wallet, error) {
if len(seed) != 0 {
return nil, errors.New("seed is required")
}
return newWallet(seed)
}
func (w *Wallet) URL() accounts.URL {
return w.url
}
func (w *Wallet) Status() (string, error) {
return "ok", nil
}
func (w *Wallet) Open(passphrase string) error {
return nil
}
func (w *Wallet) Close() error {
return nil
}
func (w *Wallet) Contains(account accounts.Account) bool {
w.stateLock.RLock()
defer w.stateLock.RUnlock()
_, exists := w.paths[account.Address]
return exists
}
func (w *Wallet) Unpin(account accounts.Account) error {
w.stateLock.RLock()
defer w.stateLock.RUnlock()
for i, acct := range w.accounts {
if acct.Address.String() == account.Address.String() {
w.accounts = removeAtIndex(w.accounts, i)
delete(w.paths, account.Address)
return nil
}
}
return errors.New("account not found")
}
func (w *Wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
w.stateLock.RLock()
address, err := w.deriveAddress(path)
w.stateLock.RUnlock()
if err != nil {
return accounts.Account{}, err
}
account := accounts.Account{
Address: address,
URL: accounts.URL{
Scheme: "",
Path: path.String(),
},
}
if !pin {
return account, nil
}
w.stateLock.Lock()
defer w.stateLock.RUnlock()
if _, ok := w.paths[address]; !ok {
w.accounts = append(w.accounts, account)
w.paths[address] = path
}
return account, nil
}
// SelfDerive implements accounts.Wallet, trying to discover accounts that the
// user used previously (based on the chain state), but ones that he/she did not
// explicitly pin to the wallet manually. To avoid chain head monitoring, self
// derivation only runs during account listing (and even then throttled).
func (w *Wallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {
// TODO: self derivation
}
func (w *Wallet) derivePrivateKey(path accounts.DerivationPath, masterKey *hdkeychain.ExtendedKey) (*ecdsa.PrivateKey, error) {
var err error
key := masterKey
for _, n := range path {
//按照路径迭代获得最终key
key, err = key.Derive(n)
if err != nil {
return nil, err
}
}
//私钥
privKey, err := key.ECPrivKey()
PrivateKeyECDSA := privKey.ToECDSA()
if err != nil {
return nil, err
}
return PrivateKeyECDSA, nil
}
func (w *Wallet) DerivePrivateKey(path accounts.DerivationPath) (*ecdsa.PrivateKey, error) {
var err error
key := w.masterKey
for _, n := range path {
//按照路径迭代获得最终key
key, err = key.Derive(n)
if err != nil {
return nil, err
}
}
//私钥
privKey, err := key.ECPrivKey()
PrivateKeyECDSA := privKey.ToECDSA()
if err != nil {
return nil, err
}
return PrivateKeyECDSA, nil
}
func (w *Wallet) DerivePublicKey(path accounts.DerivationPath) (*ecdsa.PublicKey, error) {
privateKeyECDSA, err := w.DerivePrivateKey(path)
if err != nil {
return nil, err
}
publickey := privateKeyECDSA.Public()
publicKeyECDSA, ok := publickey.(*ecdsa.PublicKey)
if !ok {
return nil, err
}
return publicKeyECDSA, nil
}
func (w *Wallet) derivePublicKey(privateKey *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
publicKey := privateKey.Public()
key := publicKey.(*ecdsa.PublicKey)
return key, nil
}
func (w *Wallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) {
path := w.paths[account.Address]
privateKey, err := w.DerivePrivateKey(path)
if err != nil {
return nil, err
}
return crypto.Sign(hash, privateKey)
}
func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainId *big.Int) (*types.Transaction, error) {
w.stateLock.RLock()
defer w.stateLock.RUnlock()
path := w.paths[account.Address]
privateKey, err := w.DerivePrivateKey(path)
if err != nil {
return nil, err
}
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
if err != nil {
return nil, err
}
message, err := signedTx.AsMessage(types.HomesteadSigner{}, nil)
if err != nil {
return nil, err
}
sender := message.From()
if sender != account.Address {
return nil, errors.New(fmt.Sprintf("signer mismatch: expected %s,got %s", account.Address.Hex(), sender.Hex()))
}
return signedTx, nil
}
//不用passphrase 不鸡肋吗?
func (w *Wallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
return w.SignHash(account, hash)
}
func (w *Wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainId *big.Int) (*types.Transaction, error) {
return w.SignTx(account, tx, chainId)
}
func (w *Wallet) PrivateKey(account accounts.Account) (*ecdsa.PrivateKey, error) {
path, err := accounts.ParseDerivationPath(account.URL.Path)
if err != nil {
return nil, err
}
return w.DerivePrivateKey(path)
}
func (w *Wallet) PrivateKeyBytes(account accounts.Account) ([]byte, error) {
privateKey, err := w.PrivateKey(account)
if err != nil {
return nil, err
}
return crypto.FromECDSA(privateKey), nil
}
func (w *Wallet) PrivateKeyHex(account accounts.Account) (string, error) {
privateKeyBytes, err := w.PrivateKeyBytes(account)
if err != nil {
return "", err
}
return hexutil.Encode(privateKeyBytes)[2:], nil
}
func (w *Wallet) PublicKey(account accounts.Account) (*ecdsa.PublicKey, error) {
path, err := accounts.ParseDerivationPath(account.URL.Path)
if err != nil {
return nil, err
}
return w.DerivePublicKey(path)
}
func (w *Wallet) PublicKeyBytes(account accounts.Account) ([]byte, error) {
publicKey, err := w.PublicKey(account)
if err != nil {
return nil, err
}
return crypto.FromECDSAPub(publicKey), nil
}
func (w *Wallet) PublicKeyHex(account accounts.Account) (string, error) {
publicKeyBytes, err := w.PublicKeyBytes(account)
if err != nil {
return "", err
}
//todo 为啥是4
return hexutil.Encode(publicKeyBytes)[4:], nil
}
func (w *Wallet) Address(account accounts.Account) (common.Address, error) {
publicKey, err := w.PublicKey(account)
if err != nil {
return common.Address{}, err
}
return crypto.PubkeyToAddress(*publicKey), nil
}
func (w *Wallet) AddressBytes(account accounts.Account) ([]byte, error) {
address, err := w.Address(account)
if err != nil {
return nil, err
}
return address.Bytes(), nil
}
func (w *Wallet) AddressHex(account accounts.Account) (string, error) {
address, err := w.Address(account)
if err != nil {
return "", err
}
return address.Hex(), nil
}
func (w *Wallet) Path(account accounts.Account) (string, error) {
return account.URL.Path, nil
}
func ParseDerivationPath(path string) accounts.DerivationPath {
derivationPath, err := accounts.ParseDerivationPath(path)
if err != nil {
return nil
}
return derivationPath
}
func NewMnemonic(bits int) (string, error) {
entropy, err := bip39.NewEntropy(bits)
if err != nil {
return "", err
}
return bip39.NewMnemonic(entropy)
}
func NewSeed() ([]byte, error) {
b := make([]byte, 64)
_, err := rand.Read(b)
return b, err
}
func NewSeedFromMnemonic(mnemonic, password string) ([]byte, error) {
if mnemonic == "" {
return nil, errors.New("mneonic is required")
}
return bip39.NewSeedWithErrorChecking(mnemonic, password)
}
func (w *Wallet) deriveAddress(path accounts.DerivationPath) (common.Address, error) {
key, err := w.DerivePublicKey(path)
if err != nil {
return common.Address{}, err
}
address := crypto.PubkeyToAddress(*key)
return address, nil
}
func removeAtIndex(accts []accounts.Account, index int) []accounts.Account {
return append(accts[:index], accts[index+1:]...)
}