forked from vedhavyas/go-subkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derive.go
94 lines (75 loc) · 1.77 KB
/
derive.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
package subkey
import (
"encoding/binary"
"errors"
"regexp"
"strconv"
"strings"
"golang.org/x/crypto/blake2b"
)
const (
// DevPhrase is default phrase used for dev test accounts
DevPhrase = "bottom drive obey lake curtain smoke basket hold race lonely fit walk"
junctionIDLen = 32
)
var (
re = regexp.MustCompile(`^(?P<phrase>[\d\w ]+)?(?P<path>(//?[^/]+)*)(///(?P<password>.*))?$`)
reJunction = regexp.MustCompile(`/(/?[^/]+)`)
)
type DeriveJunction struct {
ChainCode [32]byte
IsHard bool
}
func deriveJunctions(codes []string) (djs []DeriveJunction, err error) {
for _, code := range codes {
dj, err := parseDeriveJunction(code)
if err != nil {
return nil, err
}
djs = append(djs, dj)
}
return djs, nil
}
func parseDeriveJunction(code string) (DeriveJunction, error) {
var jd DeriveJunction
if strings.HasPrefix(code, "/") {
jd.IsHard = true
code = strings.TrimPrefix(code, "/")
}
var bc []byte
u64, err := strconv.ParseUint(code, 10, 0)
if err == nil {
bc = make([]byte, 8)
binary.LittleEndian.PutUint64(bc, u64)
} else {
cl, err := compactUint(uint64(len(code)))
if err != nil {
return jd, err
}
bc = append(cl, code...)
}
if len(bc) > junctionIDLen {
b := blake2b.Sum256(bc)
bc = b[:]
}
copy(jd.ChainCode[:len(bc)], bc)
return jd, nil
}
func derivePath(path string) (parts []string) {
res := reJunction.FindAllStringSubmatch(path, -1)
for _, p := range res {
parts = append(parts, p[1])
}
return parts
}
func splitURI(suri string) (phrase string, pathMap string, password string, err error) {
res := re.FindStringSubmatch(suri)
if res == nil {
return phrase, pathMap, password, errors.New("invalid URI format")
}
phrase = res[1]
if phrase == "" {
phrase = DevPhrase
}
return phrase, res[2], res[5], nil
}