-
Notifications
You must be signed in to change notification settings - Fork 81
/
path.go
70 lines (57 loc) · 1.31 KB
/
path.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
package hdwallet
import (
"crypto/hmac"
"crypto/sha512"
"encoding/binary"
"fmt"
"regexp"
"strconv"
"strings"
)
type Key struct {
PrivateKey []byte
ChainCode []byte
}
func CKDPriv(key Key, index uint32) Key {
buffer := make([]byte, 0, 1+4+len(key.PrivateKey))
buffer = append(buffer, 0)
buffer = append(buffer, key.PrivateKey...)
v := make([]byte, 4)
binary.BigEndian.PutUint32(v, index)
buffer = append(buffer, v...)
I := hamc512(key.ChainCode, buffer)
return Key{
PrivateKey: I[:32],
ChainCode: I[32:],
}
}
func CreateMasterKey(seed []byte) Key {
I := hamc512([]byte("ed25519 seed"), seed)
return Key{
PrivateKey: I[:32],
ChainCode: I[32:],
}
}
func hamc512(key, data []byte) []byte {
hmac := hmac.New(sha512.New, key)
hmac.Write(data)
return hmac.Sum(nil)
}
func isValidPath(path string) bool {
r := regexp.MustCompile(`^m{1,1}(/\d+')*$`)
return r.MatchString(path)
}
func Derived(path string, seed []byte) (Key, error) {
if !isValidPath(path) {
return Key{}, fmt.Errorf("invalid path: %v", path)
}
key := CreateMasterKey(seed)
for _, s := range strings.Split(path, "/")[1:] {
v, err := strconv.ParseUint(s[:len(s)-1], 10, 32)
if err != nil {
return Key{}, fmt.Errorf("failed to parse %v as a uint, err: %v", v, err)
}
key = CKDPriv(key, uint32(v)+1<<31)
}
return key, nil
}