forked from karlsen-network/karlsend
/
bip32.go
46 lines (37 loc) · 1.11 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
package bip32
import "crypto/rand"
// GenerateSeed generates seed that can be used to initialize a master key.
func GenerateSeed() ([]byte, error) {
randBytes := make([]byte, 32)
_, err := rand.Read(randBytes)
if err != nil {
return nil, err
}
return randBytes, nil
}
// NewMasterWithPath returns a new master key based on the given seed and version, with a derivation
// to the given path.
func NewMasterWithPath(seed []byte, version [4]byte, pathString string) (*ExtendedKey, error) {
masterKey, err := NewMaster(seed, version)
if err != nil {
return nil, err
}
return masterKey.DeriveFromPath(pathString)
}
// NewPublicMasterWithPath returns a new public master key based on the given seed and version, with a derivation
// to the given path.
func NewPublicMasterWithPath(seed []byte, version [4]byte, pathString string) (*ExtendedKey, error) {
masterKey, err := NewMaster(seed, version)
if err != nil {
return nil, err
}
path, err := parsePath(pathString)
if err != nil {
return nil, err
}
descendantKey, err := masterKey.path(path)
if err != nil {
return nil, err
}
return descendantKey.Public()
}