forked from RaghavSood/ripple
/
hash.go
319 lines (273 loc) · 5.48 KB
/
hash.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package data
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"github.com/coinhako/ripple/crypto"
)
type KeyType int
const (
ECDSA KeyType = 0
Ed25519 KeyType = 1
)
func (keyType KeyType) String() string {
switch keyType {
case ECDSA:
return "ECDSA"
case Ed25519:
return "Ed25519"
default:
return "unknown key type"
}
}
type Hash128 [16]byte
type Hash160 [20]byte
type Hash256 [32]byte
type Vector256 []Hash256
type VariableLength []byte
type PublicKey [33]byte
type Account [20]byte
type RegularKey [20]byte
type Seed [16]byte
var zero256 Hash256
var zeroAccount Account
var zeroPublicKey PublicKey
var zeroSeed Seed
func (h *Hash128) Bytes() []byte {
if h == nil {
return nil
}
return h[:]
}
func (h Hash128) String() string {
return string(b2h(h[:]))
}
func (h *Hash160) Bytes() []byte {
if h == nil {
return nil
}
return h[:]
}
func (h Hash160) String() string {
return string(b2h(h[:]))
}
func (h *Hash160) Account() *Account {
if h == nil {
return nil
}
var a Account
copy(a[:], h[:])
return &a
}
func (h *Hash160) Currency() *Currency {
if h == nil {
return nil
}
var c Currency
copy(c[:], h[:])
return &c
}
// Accepts either a hex string or a byte slice of length 32
func NewHash256(value interface{}) (*Hash256, error) {
var h Hash256
switch v := value.(type) {
case []byte:
if len(v) != 32 {
return nil, fmt.Errorf("NewHash256: Wrong length %X", value)
}
copy(h[:], v)
case string:
n, err := hex.Decode(h[:], []byte(v))
if err != nil {
return nil, err
}
if n != 32 {
return nil, fmt.Errorf("NewHash256: Wrong length %s", v)
}
default:
return nil, fmt.Errorf("NewHash256: Wrong type %+v", v)
}
return &h, nil
}
func (h Hash256) IsZero() bool {
return h == zero256
}
func (h Hash256) Xor(x Hash256) Hash256 {
var xor Hash256
for i := range h {
xor[i] = h[i] ^ x[i]
}
return x
}
func (h Hash256) Compare(x Hash256) int {
return bytes.Compare(h[:], x[:])
}
func (h *Hash256) Bytes() []byte {
if h == nil {
return nil
}
return h[:]
}
func (h Hash256) String() string {
return string(b2h(h[:]))
}
func (h Hash256) TruncatedString(length int) string {
return string(b2h(h[:length]))
}
func (v Vector256) String() string {
var s []string
for _, h := range v {
s = append(s, h.String())
}
return fmt.Sprintf("[%s]", strings.Join(s, ","))
}
func (v *VariableLength) String() string {
if v != nil {
b, _ := v.MarshalText()
return string(b)
}
return ""
}
func (v *VariableLength) Bytes() []byte {
if v != nil {
return []byte(*v)
}
return []byte(nil)
}
func (p PublicKey) NodePublicKey() string {
hash, err := crypto.NewNodePublicKey(p[:])
if err != nil {
return "Bad node public key"
}
return hash.String()
}
func (p PublicKey) String() string {
b, _ := p.MarshalText()
return string(b)
}
func (p PublicKey) IsZero() bool {
return p == zeroPublicKey
}
func (p *PublicKey) Bytes() []byte {
if p != nil {
return p[:]
}
return []byte(nil)
}
// Expects address in base58 form
func NewAccountFromAddress(s string) (*Account, error) {
hash, err := crypto.NewRippleHashCheck(s, crypto.RIPPLE_ACCOUNT_ID)
if err != nil {
return nil, err
}
var account Account
copy(account[:], hash.Payload())
return &account, nil
}
func (a Account) Hash() (crypto.Hash, error) {
return crypto.NewAccountId(a[:])
}
func (a Account) String() string {
address, err := a.Hash()
if err != nil {
return fmt.Sprintf("Bad Address: %s", b2h(a[:]))
}
return address.String()
}
func (a Account) IsZero() bool {
return a == zeroAccount
}
func (a *Account) Bytes() []byte {
if a != nil {
return a[:]
}
return []byte(nil)
}
func (a Account) Compare(b Account) int {
return bytes.Compare(a[:], b[:])
}
func (a Account) Less(b Account) bool {
return a.Compare(b) < 0
}
func (a Account) Equals(b Account) bool {
return a == b
}
func (a Account) Hash256() Hash256 {
var h Hash256
copy(h[:], a[:])
return h
}
// Expects address in base58 form
func NewRegularKeyFromAddress(s string) (*RegularKey, error) {
hash, err := crypto.NewRippleHashCheck(s, crypto.RIPPLE_ACCOUNT_ID)
if err != nil {
return nil, err
}
var regKey RegularKey
copy(regKey[:], hash.Payload())
return ®Key, nil
}
func (r RegularKey) Hash() (crypto.Hash, error) {
return crypto.NewAccountId(r[:])
}
func (r RegularKey) String() string {
address, err := r.Hash()
if err != nil {
return fmt.Sprintf("Bad Address: %s", b2h(r[:]))
}
return address.String()
}
func (r *RegularKey) Bytes() []byte {
if r != nil {
return r[:]
}
return []byte(nil)
}
// Expects address in base58 form
func NewSeedFromAddress(s string) (*Seed, error) {
hash, err := crypto.NewRippleHashCheck(s, crypto.RIPPLE_FAMILY_SEED)
if err != nil {
return nil, err
}
var seed Seed
copy(seed[:], hash.Payload())
return &seed, nil
}
func (s Seed) Hash() (crypto.Hash, error) {
return crypto.NewFamilySeed(s[:])
}
func (s Seed) String() string {
address, err := s.Hash()
if err != nil {
return fmt.Sprintf("Bad Address: %s", b2h(s[:]))
}
return address.String()
}
func (s *Seed) Bytes() []byte {
if s != nil {
return s[:]
}
return []byte(nil)
}
func (s *Seed) Key(keyType KeyType) crypto.Key {
var (
key crypto.Key
err error
)
switch keyType {
case Ed25519:
key, err = crypto.NewEd25519Key(s[:])
case ECDSA:
key, err = crypto.NewECDSAKey(s[:])
}
if err != nil {
panic(fmt.Sprintf("bad seed: %v", err))
}
return key
}
func (s *Seed) AccountId(keyType KeyType, sequence *uint32) Account {
var account Account
copy(account[:], s.Key(keyType).Id(sequence))
return account
}