This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
key.go
536 lines (504 loc) · 12.5 KB
/
key.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
"time"
"blockwatch.cc/tzgo/tezos"
"golang.org/x/crypto/blake2b"
)
// Comparable key as used in bigmaps and maps
type Key struct {
Type Type
// TODO: refactor into simple Prim
IntKey *big.Int
StringKey string
BytesKey []byte
BoolKey bool
AddrKey tezos.Address
KeyKey tezos.Key
SignatureKey tezos.Signature
TimeKey time.Time
PrimKey Prim
}
func NewKey(typ Type, key Prim) (Key, error) {
k := Key{
Type: typ,
}
switch typ.OpCode {
case T_INT, T_NAT, T_MUTEZ:
k.IntKey = key.Int
case T_STRING:
if isASCII(key.String) {
k.StringKey = key.String
} else {
key.Bytes = []byte(key.String)
}
// convert empty and non-ascii strings to bytes
if len(k.StringKey) == 0 && len(key.Bytes) > 0 {
k.Type.OpCode = T_BYTES
k.BytesKey = key.Bytes
}
case T_BYTES:
k.BytesKey = key.Bytes
case T_BOOL:
switch key.OpCode {
case D_TRUE:
k.BoolKey = true
case D_FALSE:
k.BoolKey = false
default:
return Key{}, fmt.Errorf("micheline: invalid bool big_map key opcode %s (%[1]d)", key.OpCode)
}
case T_TIMESTAMP:
// in some cases (originated contract storage) timestamps are strings
if key.Int == nil {
t, err := time.Parse(time.RFC3339, key.String)
if err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for string timestamp: %v", err)
}
k.TimeKey = t
} else {
k.TimeKey = time.Unix(key.Int.Int64(), 0).UTC()
}
case T_KEY_HASH, T_ADDRESS:
// in some cases (originated contract storage) addresses are strings
if len(key.Bytes) == 0 && len(key.String) > 0 {
a, err := tezos.ParseAddress(strings.Split(key.String, "%")[0])
if err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for string type address: %v", err)
}
k.AddrKey = a
} else {
a := tezos.Address{}
if err := a.UnmarshalBinary(key.Bytes); err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for type address: %v", err)
}
k.AddrKey = a
}
case T_KEY:
if len(key.Bytes) == 0 && len(key.String) > 0 {
kk, err := tezos.ParseKey(key.String)
if err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for string type key: %v", err)
}
k.KeyKey = kk
} else {
kk := tezos.Key{}
if err := kk.UnmarshalBinary(key.Bytes); err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for type key: %v", err)
}
k.KeyKey = kk
}
case T_SIGNATURE:
if len(key.Bytes) == 0 && len(key.String) > 0 {
sk, err := tezos.ParseSignature(key.String)
if err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for string type signature: %v", err)
}
k.SignatureKey = sk
} else {
sk := tezos.Signature{}
if err := sk.UnmarshalBinary(key.Bytes); err != nil {
return Key{}, fmt.Errorf("micheline: invalid big_map key for type signature: %v", err)
}
k.SignatureKey = sk
}
case T_PAIR, T_OPTION, T_OR, T_CHAIN_ID, T_UNIT:
k.PrimKey = key
// build type details when missing
if len(k.Type.Args) == 0 {
k.Type = key.BuildType()
}
default:
k.PrimKey = key
// build type details when missing
if len(k.Type.Args) == 0 {
k.Type = key.BuildType()
}
return k, fmt.Errorf("micheline: big_map key type '%s' is not implemented", typ.OpCode)
}
return k, nil
}
func NewKeyPtr(typ Type, key Prim) (*Key, error) {
k, err := NewKey(typ, key)
return &k, err
}
func (k Key) IsPacked() bool {
return k.Type.OpCode == T_BYTES && len(k.BytesKey) > 1 && k.BytesKey[0] == 0x5
}
func (k Key) UnpackPrim() (p Prim, err error) {
p = Prim{}
if !k.IsPacked() {
return p, fmt.Errorf("key is not packed")
}
defer func() {
if e := recover(); e != nil {
p = Prim{}
err = fmt.Errorf("prim is not packed")
}
}()
if err = p.UnmarshalBinary(k.BytesKey[1:]); err != nil {
return p, err
}
return p, nil
}
func (k Key) Unpack() (Key, error) {
if !k.IsPacked() {
return k, nil
}
p, err := k.UnpackPrim()
if err != nil {
return Key{}, err
}
return Key{
Type: p.BuildType(),
IntKey: p.Int,
StringKey: p.String,
BytesKey: p.Bytes,
PrimKey: p,
}, nil
}
func ParseKeyType(typ string) (OpCode, error) {
t, err := ParseOpCode(typ)
if err != nil {
return t, fmt.Errorf("micheline: invalid big_map key type '%s'", typ)
}
switch t {
case T_INT, T_NAT, T_MUTEZ, T_STRING, T_BYTES, T_BOOL,
T_KEY_HASH, T_TIMESTAMP, T_ADDRESS, T_PAIR, T_KEY, T_SIGNATURE,
T_OPTION, T_OR, T_CHAIN_ID, T_UNIT:
return t, nil
default:
return t, fmt.Errorf("micheline: unsupported big_map key type %s", t)
}
}
// query string parsing used for lookup
func ParseKey(typ OpCode, val string) (Key, error) {
key := Key{Type: Type{}}
if typ.IsTypeCode() {
key.Type.OpCode = typ
} else {
key.Type.OpCode = InferKeyType(val)
}
var err error
switch key.Type.OpCode {
case T_INT, T_NAT, T_MUTEZ:
key.Type.Type = PrimInt
key.IntKey = big.NewInt(0)
err = key.IntKey.UnmarshalText([]byte(val))
case T_STRING:
key.Type.Type = PrimString
key.StringKey = val
case T_BYTES:
key.Type.Type = PrimBytes
key.BytesKey, err = hex.DecodeString(val)
case T_BOOL:
key.Type.Type = PrimNullary
key.BoolKey, err = strconv.ParseBool(val)
case T_TIMESTAMP:
// either RFC3339 or UNIX seconds
key.Type.Type = PrimInt
if strings.Contains(val, "T") {
key.TimeKey, err = time.Parse(time.RFC3339, val)
} else {
var i int64
i, err = strconv.ParseInt(val, 10, 64)
key.TimeKey = time.Unix(i, 0).UTC()
}
case T_KEY_HASH, T_ADDRESS:
key.Type.Type = PrimBytes
key.AddrKey, err = tezos.ParseAddress(val)
case T_KEY:
key.Type.Type = PrimBytes
key.KeyKey, err = tezos.ParseKey(val)
case T_SIGNATURE:
key.Type.Type = PrimBytes
key.SignatureKey, err = tezos.ParseSignature(val)
case T_PAIR:
// parse comma-separated list into a right-hand pair tree
prims := []Prim{}
for _, v := range strings.Split(val, ",") {
parsed, err := ParseKey(InferKeyType(v), v)
if err != nil {
return Key{}, fmt.Errorf("micheline: decoding bigmap pair key element %s: %v", v, err)
}
prims = append(prims, parsed.Prim())
}
switch len(prims) {
case 0:
return Key{}, fmt.Errorf("micheline: empty bigmap pair key: %s", val)
case 1:
return Key{}, fmt.Errorf("micheline: single-value bigmap pair key: %s", val)
default:
key.PrimKey = NewSeq(prims...).FoldPair()
key.Type.Type = PrimBinary
}
case T_UNIT:
if val != D_UNIT.String() {
return Key{}, fmt.Errorf("micheline: invalid bigmap pair key for Unit type: %s", val)
}
key.PrimKey = NewCode(D_UNIT)
key.Type.Type = PrimNullary
default:
return Key{}, fmt.Errorf("micheline: unsupported big_map key type %s", typ)
}
if err != nil {
return Key{}, fmt.Errorf("micheline: decoding bigmap key %s as %s: %v", val, typ, err)
}
return key, nil
}
func InferKeyType(val string) OpCode {
if val == D_UNIT.String() {
return T_UNIT
}
if _, err := tezos.ParseAddress(val); err == nil {
// Note: can also be KEY_HASH, but used inconsistently
return T_ADDRESS
}
if _, err := tezos.ParseKey(val); err == nil {
return T_KEY
}
if _, err := tezos.ParseSignature(val); err == nil {
return T_SIGNATURE
}
if _, err := time.Parse(time.RFC3339, val); err == nil {
return T_TIMESTAMP
}
i := big.NewInt(0)
if err := i.UnmarshalText([]byte(val)); err == nil {
// can also be T_MUTEZ, T_NAT
return T_INT
}
if _, err := hex.DecodeString(val); err == nil {
return T_BYTES
}
if strings.Contains(val, ",") {
return T_PAIR
}
if _, err := strconv.ParseBool(val); err == nil {
return T_BOOL
}
return T_STRING
}
func DecodeKey(typ Type, b []byte) (Key, error) {
key := Prim{}
if err := key.UnmarshalBinary(b); err != nil {
return Key{}, err
}
return NewKey(typ, key)
}
func (k Key) Bytes() []byte {
p := Prim{}
switch k.Type.OpCode {
case T_INT, T_NAT, T_MUTEZ:
p.Type = PrimInt
p.Int = k.IntKey
case T_STRING:
p.Type = PrimString
p.String = k.StringKey
case T_BYTES:
p.Type = PrimBytes
p.Bytes = k.BytesKey
case T_BOOL:
p.Type = PrimNullary
if k.BoolKey {
p.OpCode = D_TRUE
} else {
p.OpCode = D_FALSE
}
case T_TIMESTAMP:
var z Z
z.SetInt64(k.TimeKey.Unix())
p.Type = PrimInt
p.Int = z.Big()
case T_ADDRESS:
p.Type = PrimBytes
p.Bytes, _ = k.AddrKey.MarshalBinary()
case T_KEY_HASH:
p.Type = PrimBytes
b, _ := k.AddrKey.MarshalBinary()
p.Bytes = b[1:] // strip leading flag
case T_KEY:
p.Type = PrimBytes
p.Bytes, _ = k.KeyKey.MarshalBinary()
case T_SIGNATURE:
p.Type = PrimBytes
p.Bytes, _ = k.SignatureKey.MarshalBinary()
default:
// anthing else comes from prim tree
if !k.PrimKey.IsValid() {
return nil
}
b, _ := k.PrimKey.MarshalBinary()
return b
}
buf, _ := p.MarshalBinary()
return buf
}
func (k Key) MarshalBinary() ([]byte, error) {
return k.Bytes(), nil
}
func (k Key) Hash() tezos.ExprHash {
return KeyHash(k.Bytes())
}
func KeyHash(buf []byte) tezos.ExprHash {
// blake2b with digest size 32 byte
h, _ := blake2b.New(32, nil)
// encode with pack byte
h.Write([]byte{0x5})
h.Write(buf)
// wrap in exprhash
return tezos.NewExprHash(h.Sum(nil))
}
func (k Key) String() string {
switch k.Type.OpCode {
case T_INT, T_NAT, T_MUTEZ:
return k.IntKey.Text(10)
case T_STRING:
return k.StringKey
case T_BYTES:
return hex.EncodeToString(k.BytesKey)
case T_BOOL:
return strconv.FormatBool(k.BoolKey)
case T_TIMESTAMP:
return k.TimeKey.Format(time.RFC3339)
case T_KEY_HASH, T_ADDRESS:
return k.AddrKey.String()
case T_KEY:
return k.KeyKey.String()
case T_SIGNATURE:
return k.SignatureKey.String()
case T_PAIR:
type lv struct {
l string
v string
}
parts := make([]lv, 0)
val := Value{
Type: k.Type, // real or guessed type tree
Value: k.PrimKey,
}
// walk produces a non-deterministic order
val.Walk("", func(label string, v interface{}) error {
part := lv{l: label}
if stringer, ok := v.(fmt.Stringer); ok {
part.v = stringer.String()
} else {
if str, ok := v.(string); ok {
part.v = str
} else {
part.v = fmt.Sprint(v)
}
}
parts = append(parts, part)
return nil
})
// sort by label (works up to 10 pair values)
sort.Slice(parts, func(i, j int) bool { return parts[i].l < parts[j].l })
var b strings.Builder
for i, v := range parts {
if i > 0 {
b.WriteRune(',')
}
b.WriteString(v.v)
}
return b.String()
// TODO: simpler, but requires value tree decoration
// return fmt.Sprint(k.PrimKey.Value(T_PAIR)
case T_UNIT:
return D_UNIT.String()
default:
if k.PrimKey.IsValid() {
return k.PrimKey.OpCode.String()
}
return ""
}
}
func (k Key) Prim() Prim {
p := Prim{}
switch k.Type.OpCode {
case T_INT, T_NAT, T_MUTEZ:
p.Int = k.IntKey
p.Type = PrimInt
case T_TIMESTAMP:
p.Int = big.NewInt(k.TimeKey.Unix())
p.Type = PrimInt
case T_STRING:
p.String = k.StringKey
p.Type = PrimString
case T_BYTES:
p.Bytes = k.BytesKey
p.Type = PrimBytes
case T_ADDRESS, T_KEY_HASH:
p.Bytes, _ = k.AddrKey.MarshalBinary()
p.Type = PrimBytes
case T_KEY:
p.Bytes, _ = k.KeyKey.MarshalBinary()
p.Type = PrimBytes
case T_SIGNATURE:
p.Bytes, _ = k.SignatureKey.MarshalBinary()
p.Type = PrimBytes
case T_BOOL:
p.Type = PrimNullary
if k.BoolKey {
p.OpCode = D_TRUE
} else {
p.OpCode = D_FALSE
}
case T_PAIR, T_UNIT:
p = k.PrimKey
default:
if k.PrimKey.IsValid() {
p = k.PrimKey
break
}
if k.BytesKey != nil {
if err := p.UnmarshalBinary(k.BytesKey); err == nil {
break
}
}
p.Bytes = k.Bytes()
p.Type = PrimBytes
}
return p
}
func (k Key) PrimPtr() *Prim {
p := k.Prim()
return &p
}
func (k Key) MarshalJSON() ([]byte, error) {
switch k.Type.OpCode {
case T_INT, T_NAT, T_MUTEZ:
return []byte(strconv.Quote(k.IntKey.Text(10))), nil
case T_STRING:
return []byte(strconv.Quote(k.StringKey)), nil
case T_BYTES:
return []byte(strconv.Quote(hex.EncodeToString(k.BytesKey))), nil
case T_BOOL:
return []byte(strconv.FormatBool(k.BoolKey)), nil
case T_TIMESTAMP:
if y := k.TimeKey.Year(); y < 0 || y >= 10000 {
return []byte(strconv.Quote(strconv.FormatInt(k.TimeKey.Unix(), 10))), nil
}
return []byte(strconv.Quote(k.TimeKey.Format(time.RFC3339))), nil
case T_KEY_HASH, T_ADDRESS:
return []byte(strconv.Quote(k.AddrKey.String())), nil
case T_KEY:
return []byte(strconv.Quote(k.KeyKey.String())), nil
case T_SIGNATURE:
return []byte(strconv.Quote(k.SignatureKey.String())), nil
default:
val := &Value{
Type: k.Type,
Value: k.PrimKey,
}
return json.Marshal(val)
}
}