forked from umbracle/ethgo
/
wallet_hd.go
92 lines (78 loc) · 1.97 KB
/
wallet_hd.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
package wallet
import (
"crypto/ecdsa"
"fmt"
"math/big"
"strings"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/tyler-smith/go-bip39"
)
type DerivationPath []uint32
// 0x800000
var decVal = big.NewInt(2147483648)
// DefaultDerivationPath is the default derivation path for Ethereum addresses
var DefaultDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}
func (d *DerivationPath) Derive(master *hdkeychain.ExtendedKey) (*ecdsa.PrivateKey, error) {
var err error
key := master
for _, n := range *d {
key, err = key.Child(n)
if err != nil {
return nil, err
}
}
priv, err := key.ECPrivKey()
if err != nil {
return nil, err
}
return priv.ToECDSA(), nil
}
func parseDerivationPath(path string) (*DerivationPath, error) {
parts := strings.Split(path, "/")
if len(parts) == 0 {
return nil, fmt.Errorf("no derivation path")
}
// clean all the parts of any trim spaces
for indx := range parts {
parts[indx] = strings.TrimSpace(parts[indx])
}
// first part has to be an 'm'
if parts[0] != "m" {
return nil, fmt.Errorf("first has to be m")
}
result := DerivationPath{}
for _, p := range parts[1:] {
val := new(big.Int)
if strings.HasSuffix(p, "'") {
p = strings.TrimSuffix(p, "'")
val.Add(val, decVal)
}
bigVal, ok := new(big.Int).SetString(p, 0)
if !ok {
return nil, fmt.Errorf("invalid path")
}
val.Add(val, bigVal)
// TODO, limit to uint32
if !val.IsUint64() {
return nil, fmt.Errorf("bad")
}
result = append(result, uint32(val.Uint64()))
}
return &result, nil
}
func NewWalletFromMnemonic(mnemonic string) (*Key, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return nil, err
}
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, err
}
priv, err := DefaultDerivationPath.Derive(masterKey)
if err != nil {
return nil, err
}
return newKey(priv), nil
}