-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivation_path.go
212 lines (177 loc) · 5 KB
/
derivation_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package go_mhda
import (
"errors"
"fmt"
"regexp"
"strconv"
)
const (
ROOT = DerivationType(`root`)
BIP32 = DerivationType(`bip32`)
BIP44 = DerivationType(`bip44`)
BIP84 = DerivationType(`bip84`)
CIP11 = DerivationType(`cip11`)
ZIP32 = DerivationType(`zip32`)
ChargeExternal = ChargeType(0)
ChargeInternal = ChargeType(1)
)
type DerivationType string
type AccountIndex uint32
type ChargeType uint8
type AddressIndex struct {
Index uint32
IsHardened bool
}
type DerivationPath struct {
derivationType DerivationType
coin CoinType
account AccountIndex
charge ChargeType
index AddressIndex
}
func NewDerivationPath(derivationType DerivationType, coin CoinType, account AccountIndex, charge ChargeType, index AddressIndex) *DerivationPath {
return &DerivationPath{
derivationType: derivationType,
coin: coin,
account: account,
charge: charge,
index: index,
}
}
func ParseDerivationPath(dt DerivationType, path string) (*DerivationPath, error) {
rx, ok := derivationIndex[dt]
if !ok {
return nil, errors.New("wrong derivation type")
}
if !rx.MatchString(path) {
return nil, errors.New("incorrect derivation path")
}
dPath := &DerivationPath{
derivationType: dt,
}
err := dPath.ParsePath(path)
if err != nil {
return nil, err
}
return dPath, nil
}
func (dp *DerivationPath) DerivationType() DerivationType {
return dp.derivationType
}
func (dp *DerivationPath) Coin() CoinType {
return dp.coin
}
func (dp *DerivationPath) Account() AccountIndex {
return dp.account
}
func (dp *DerivationPath) Charge() ChargeType {
return dp.charge
}
func (dp *DerivationPath) AddressIndex() AddressIndex {
return dp.index
}
func (dp *DerivationPath) IsHardenedAddress() bool {
return dp.index.IsHardened
}
var (
rxRoot = regexp.MustCompile("")
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
// m / account ' / charge / address
rxBip32 = regexp.MustCompile(`^m/([0-9]+)[Hh']/(0|1)/([0-9]+)([Hh'])?$`)
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
// m / 44 ' / coin ' / account ' / charge / address
rxBip44 = regexp.MustCompile(`^m/44[Hh']/([0-9]+)[Hh']/([0-9]+)[Hh']/(0|1)/([0-9]+)([Hh'])?$`)
// https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
// m / 84 ' / 0 ' / account ' / charge / address
// TODO: Check for hardened index
rxBip84 = regexp.MustCompile(`^m/84[Hh']/0[Hh']/([0-9]+)[Hh']/(0|1)/([0-9]+)?$`)
// https://github.com/confio/cosmos-hd-key-derivation-spec
// m / 44 ' / 118 ' / account ' / charge_extra / address
rxCip11 = regexp.MustCompile(`^m/44[Hh']/118[Hh']/([0-9]+)[Hh']/([0-9]+)/([0-9]+)([Hh'])?$`)
// https://zips.z.cash/zip-0032
// m / 32 ' / 133 ' / account '
// m / 32 ' / 133 ' / account ' / address
// m / 32 ' / 133 ' / account ' / address '
rxZip32 = regexp.MustCompile("")
derivationIndex = map[DerivationType]*regexp.Regexp{
ROOT: rxRoot,
BIP32: rxBip32,
BIP44: rxBip44,
BIP84: rxBip84,
CIP11: rxCip11,
ZIP32: rxZip32,
}
)
func (dp *DerivationPath) ParsePath(path string) error {
var isAddressHardened = false
matches := derivationIndex[dp.derivationType].FindStringSubmatch(path)
// TODO: Fix serialization for different length
if len(matches) < 5 {
return errors.New(fmt.Sprintf("cannot parse path: %s", path))
}
coinType, err := strconv.ParseUint(matches[1], 10, 32)
if err != nil {
return errors.New("cannot parse coin")
}
accountIndex, err := strconv.ParseUint(matches[2], 10, 32)
if err != nil {
return errors.New("cannot parse account")
}
chargeType, err := strconv.ParseUint(matches[3], 10, 32)
if err != nil {
return errors.New("cannot parse charge")
}
addressIndex, err := strconv.ParseUint(matches[4], 10, 32)
if err != nil {
return errors.New("cannot parse index")
}
if len(matches) == 6 && matches[5] != "" {
isAddressHardened = true
}
dp.coin = CoinType(coinType)
dp.account = AccountIndex(accountIndex)
dp.charge = ChargeType(chargeType)
dp.index = AddressIndex{
Index: uint32(addressIndex),
IsHardened: isAddressHardened,
}
return nil
}
func (dp *DerivationPath) String() string {
var result string
switch dp.derivationType {
case ROOT:
return ``
case BIP32:
var format = "m/%d'/%d/%d"
if dp.index.IsHardened {
format += `'`
}
return fmt.Sprintf(format, dp.account, dp.charge, dp.index.Index)
case BIP44:
var format = "m/44'/%d'/%d'/%d/%d"
if dp.index.IsHardened {
format += `'`
}
return fmt.Sprintf(format, dp.coin, dp.account, dp.charge, dp.index.Index)
case BIP84:
var format = "m/84'/%d'/%d'/%d/%d"
if dp.index.IsHardened {
format += `'`
}
return fmt.Sprintf(format, dp.coin, dp.account, dp.charge, dp.index.Index)
case CIP11:
var format = "m/44'/133'/%d'/%d/%d"
if dp.index.IsHardened {
format += `'`
}
return fmt.Sprintf(format, dp.account, dp.charge, dp.index.Index)
case ZIP32:
var format = "m/32'/133'/%d'/%d"
if dp.index.IsHardened {
format += `'`
}
return fmt.Sprintf(format, dp.account, dp.index.Index)
}
return result
}