-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhda.go
240 lines (190 loc) · 4.56 KB
/
mhda.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package go_mhda
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
)
type MHDA interface {
Chain() *Chain
// DerivationType() DerivationType
DerivationPath() *DerivationPath
Algorithm() Algorithm
Format() Format
NSS() string
String() string
Hash() string
NSSHash() string
}
type Address struct {
chain *Chain
path *DerivationPath
addressAlgorithm Algorithm
addressFormat Format
addressPrefix string
addressSuffix string
}
// NewAddress add optional params: aa, af, ap, as
func NewAddress(chain *Chain, path *DerivationPath, params ...string) *Address {
return &Address{chain: chain, path: path}
}
func parseAddress(m map[string]string) (MHDA, error) {
var err error
chain, err := parseChain(m)
if err != nil {
return nil, err
}
mhda := &Address{
chain: chain,
}
err = mhda.SetDerivationType(m[compDerivationType])
if err != nil {
return nil, err
}
// TODO: Add dp validation
err = mhda.SetDerivationPath(m[compDerivationPath])
if err != nil {
return nil, err
}
err = mhda.SetAddressAlgorithm(m[compAddressAlgorithm])
if err != nil {
return nil, err
}
err = mhda.SetAddressFormat(m[compAddressFormat])
if err != nil {
return nil, err
}
err = mhda.SetAddressPrefix(m[compAddressPrefix])
if err != nil {
return nil, err
}
err = mhda.SetAddressSuffix(m[compAddressSuffix])
if err != nil {
return nil, err
}
return mhda, nil
}
func (a *Address) Chain() *Chain {
return a.chain
}
/*func (a *Address) DerivationType() DerivationType {
return a.path.derivationType
}*/
func (a *Address) DerivationPath() *DerivationPath {
return a.path
}
func (a *Address) Algorithm() Algorithm {
return a.addressAlgorithm
}
func (a *Address) Format() Format {
return a.addressFormat
}
func (a *Address) SetDerivationType(dt string) error {
dt = strings.TrimSpace(dt)
dt = strings.ToLower(dt)
if a.path == nil {
a.path = &DerivationPath{}
}
if dt != `` {
if _, ok := derivationIndex[DerivationType(dt)]; !ok {
return errors.New(fmt.Sprintf(`"dt" param has wrong value "%s"`, dt))
}
a.path.derivationType = DerivationType(dt)
} else {
a.path.derivationType = ROOT
}
return nil
}
func (a *Address) SetDerivationPath(dp string) error {
if a.path.derivationType == ROOT {
return nil
}
rx, ok := derivationIndex[a.path.derivationType]
if !ok {
return errors.New(`incorrect "dp" param`)
}
dp = strings.TrimSpace(dp)
dp = strings.ToLower(dp)
if !rx.MatchString(dp) {
return errors.New(fmt.Sprintf(`"dp" param has wrong value "%s"`, dp))
}
return a.path.ParsePath(dp)
}
func (a *Address) SetCoinType(ct string) error {
ct = strings.TrimSpace(ct)
// TODO: Check coin type extraction from derivation path??? subnets???
if ct == `` {
return errors.New(fmt.Sprintf(`"ct" required for "ct=%s"`, a.chain.networkType))
}
coinType, err := strconv.ParseUint(ct, 0, 32)
if err != nil {
return errors.New(`cannot parse "ct"`)
}
a.chain.coinType = CoinType(coinType)
return nil
}
func (a *Address) SetAddressAlgorithm(aa string) error {
aa = strings.TrimSpace(aa)
aa = strings.ToLower(aa)
if aa == `` {
// set default
switch a.chain.networkType {
case Bitcoin, EthereumVM, AvalancheVM, TronVM, Cosmos:
a.addressAlgorithm = Secp256k1
case Solana:
a.addressAlgorithm = Ed25519
}
} else {
if _, ok := indexAlgorithms[Algorithm(aa)]; !ok {
return errors.New(`incorrect "aa" param`)
}
a.addressAlgorithm = Algorithm(aa)
}
return nil
}
func (a *Address) SetAddressFormat(af string) error {
af = strings.TrimSpace(af)
if af != `` {
a.addressFormat = Format(af)
}
return nil
}
func (a *Address) SetAddressPrefix(ap string) error {
ap = strings.TrimSpace(ap)
if ap != `` {
a.addressPrefix = ap
}
return nil
}
func (a *Address) SetAddressSuffix(as string) error {
as = strings.TrimSpace(as)
if as != `` {
a.addressSuffix = as
}
return nil
}
func (a *Address) String() string {
return fmt.Sprintf(`urn:mhda:%s`, a.NSS())
}
func (a *Address) NSS() string {
result := fmt.Sprintf(`nt:%s`, a.chain.networkType)
if a.path.derivationType != ROOT {
result += fmt.Sprintf(`:dt:%s:dp:%s`, a.path.derivationType, a.path.String())
}
result += fmt.Sprintf(`:ct:%d:ci:%s`, a.chain.coinType, a.chain.chainId)
// TODO: add full mode
// TODO: use additional params, when address has non-default values
return result
}
func (a *Address) Hash() string {
h := sha1.New()
h.Write([]byte(a.String()))
return hex.EncodeToString(h.Sum(nil))
}
func (a *Address) NSSHash() string {
h := sha1.New()
h.Write([]byte(a.NSS()))
return hex.EncodeToString(h.Sum(nil))
}