-
Notifications
You must be signed in to change notification settings - Fork 96
/
bip32.go
74 lines (62 loc) · 1.45 KB
/
bip32.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
package bip32
import (
bip32 "github.com/tyler-smith/go-bip32"
"strconv"
"strings"
)
const (
FirstHardenedChild = bip32.FirstHardenedChild
PublicKeyCompressedLength = bip32.PublicKeyCompressedLength
)
type Key struct {
*bip32.Key
}
func NewMasterKey(seed []byte) (*Key, error) {
k, err := bip32.NewMasterKey(seed)
if err != nil {
return nil, err
}
return &Key{k}, nil
}
func (key *Key) NewChildKeyByChainId(id uint32) (*Key, error) {
return key.NewChildKeyByPath(bip32.FirstHardenedChild+44, id|bip32.FirstHardenedChild, bip32.FirstHardenedChild, 0, 0)
}
func (key *Key) NewChildKeyByPath(childPath ...uint32) (*Key, error) {
currentKey := key.Key
for _, childIdx := range childPath {
newKey, err := currentKey.NewChildKey(childIdx)
if err != nil {
return nil, err
}
currentKey = newKey
}
return &Key{currentKey}, nil
}
func (key *Key) NewChildKeyByPathString(childPath string) (*Key, error) {
arr := strings.Split(childPath, "/")
currentKey := key.Key
for _, part := range arr {
if part == "m" {
continue
}
var harden = false
if strings.HasSuffix(part, "'") {
harden = true
part = strings.TrimSuffix(part, "'")
}
id, err := strconv.ParseUint(part, 10, 31)
if err != nil {
return nil, err
}
var uid = uint32(id)
if harden {
uid |= bip32.FirstHardenedChild
}
newKey, err := currentKey.NewChildKey(uid)
if err != nil {
return nil, err
}
currentKey = newKey
}
return &Key{currentKey}, nil
}