-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.go
783 lines (650 loc) · 17.9 KB
/
types.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
package stafi_decoder
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"math/big"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/huandu/xstrings"
"github.com/itering/scale.go/utiles"
"github.com/itering/scale.go/utiles/crypto/ethereum"
"github.com/itering/scale.go/utiles/uint128"
"github.com/shopspring/decimal"
commonTypes "github.com/stafiprotocol/go-substrate-rpc-client/types/common"
)
type HexBytes struct {
ScaleDecoder
}
func (h *HexBytes) Process() {
length := h.ProcessAndUpdateData("Compact<u32>").(int)
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(int(length))))
}
type U8 struct {
ScaleDecoder
}
func (u *U8) Process() {
u.Value = u.GetNextU8()
}
type U16 struct {
Reader io.Reader
ScaleDecoder
}
func (u *U16) Process() {
buf := &bytes.Buffer{}
u.Reader = buf
_, _ = buf.Write(u.NextBytes(2))
c := make([]byte, 2)
_, _ = u.Reader.Read(c)
u.Value = binary.LittleEndian.Uint16(c)
}
type U32 struct {
Reader io.Reader
ScaleDecoder
}
func (u *U32) Process() {
buf := &bytes.Buffer{}
u.Reader = buf
_, _ = buf.Write(u.NextBytes(4))
c := make([]byte, 4)
_, _ = u.Reader.Read(c)
u.Value = binary.LittleEndian.Uint32(c)
}
func (u *U32) Encode(value int) {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(value))
u.Data = ScaleBytes{Data: bs}
}
type U64 struct {
ScaleDecoder
Reader io.Reader
}
func (u *U64) Process() {
buf := &bytes.Buffer{}
u.Reader = buf
_, _ = buf.Write(u.NextBytes(8))
c := make([]byte, 8)
_, _ = u.Reader.Read(c)
u.Value = binary.LittleEndian.Uint64(c)
}
type U128 struct {
ScaleDecoder
}
func (u *U128) Process() {
elementBytes := u.NextBytes(16)
if len(elementBytes) < 16 {
elementBytes = utiles.HexToBytes(xstrings.LeftJustify(utiles.BytesToHex(elementBytes), 32, "0"))
}
u.Value = uint128.FromBytes(elementBytes).String()
}
type H256 struct {
ScaleDecoder
}
func (h *H256) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(32)))
}
type H512 struct {
ScaleDecoder
}
func (h *H512) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(64)))
}
type Era struct {
ScaleDecoder
}
func (e *Era) Process() {
optionHex := utiles.BytesToHex(e.NextBytes(1))
if optionHex == "00" {
e.Value = optionHex
} else {
e.Value = optionHex + utiles.BytesToHex(e.NextBytes(1))
}
}
type EraExtrinsic struct {
ScaleDecoder
}
func (e *EraExtrinsic) Process() {
optionHex := utiles.BytesToHex(e.NextBytes(1))
if optionHex == "00" {
e.Value = optionHex
} else {
e.Value = optionHex + utiles.BytesToHex(e.NextBytes(1))
}
}
type Bool struct {
ScaleDecoder
}
func (b *Bool) Process() {
b.Value = b.getNextBool()
}
type Moment struct {
CompactU32
}
func (m *Moment) Init(data ScaleBytes, option *ScaleDecoderOption) {
m.TypeString = "Compact<Moment>"
m.ScaleDecoder.Init(data, option)
}
func (m *Moment) Process() {
m.CompactU32.Process()
if m.Value.(int) > 10000000000 {
m.Value = m.Value.(int) / 1000
}
}
type Struct struct {
ScaleDecoder
}
func (s *Struct) Process() {
result := make(map[string]interface{})
if s.TypeMapping != nil {
for k, v := range s.TypeMapping.Names {
result[v] = s.ProcessAndUpdateData(s.TypeMapping.Types[k])
}
}
s.Value = result
}
type BlockNumber struct {
U32
}
type Vec struct {
ScaleDecoder
}
func (v *Vec) Init(data ScaleBytes, option *ScaleDecoderOption) {
if v.SubType != "" && option != nil {
option.SubType = v.SubType
}
v.ScaleDecoder.Init(data, option)
}
func (v *Vec) Process() {
elementCount := v.ProcessAndUpdateData("Compact<u32>").(int)
var result []interface{}
if elementCount > 20000 {
panic(fmt.Sprintf("Vec length %d exceeds %d", elementCount, 1000))
}
for i := 0; i < elementCount; i++ {
element := v.ProcessAndUpdateData(v.SubType)
result = append(result, element)
}
v.Value = result
}
type Address struct {
ScaleDecoder
AccountLength string `json:"account_length"`
}
// only support latest address type
func (a *Address) Process() {
AccountLength := a.NextBytes(1)
a.AccountLength = utiles.BytesToHex(AccountLength)
if a.AccountLength == "ff" {
a.Value = utiles.BytesToHex(a.NextBytes(32))
return
}
a.Value = utiles.BytesToHex(append(AccountLength, a.NextBytes(31)...))
}
type GenericAddress struct {
ScaleDecoder
AccountLength string `json:"account_length"`
}
func (a *GenericAddress) Process() {
AccountLength := a.NextBytes(1)
a.AccountLength = utiles.BytesToHex(AccountLength)
if a.AccountLength == "ff" {
a.Value = utiles.BytesToHex(a.NextBytes(32))
return
}
switch a.AccountLength {
case "fc":
a.NextBytes(2)
case "fd":
a.NextBytes(4)
case "fe":
a.NextBytes(8)
default:
a.Value = utiles.BytesToHex(append(AccountLength, a.NextBytes(31)...))
}
}
type Signature struct {
ScaleDecoder
}
func (s *Signature) Process() {
s.Value = utiles.BytesToHex(s.NextBytes(64))
}
type Enum struct {
ScaleDecoder
ValueList []string `json:"value_list"`
Index int `json:"index"`
}
func (e *Enum) Init(data ScaleBytes, option *ScaleDecoderOption) {
e.Index = 0
if option != nil && len(e.ValueList) == 0 {
e.ValueList = option.ValueList
}
e.ScaleDecoder.Init(data, option)
}
func (e *Enum) Process() {
index := utiles.BytesToHex(e.NextBytes(1))
if utiles.U256(index) != nil {
e.Index = int(utiles.U256(index).Uint64())
}
if e.TypeMapping != nil {
// check c-like enum
isCLikeEnum := true
for _, subType := range e.TypeMapping.Types {
if !regexp.MustCompile("^[0-9]+$").MatchString(subType) {
isCLikeEnum = false
break
}
}
rustEnum := make(map[int]string)
if isCLikeEnum {
for index, v := range e.TypeMapping.Names {
rustEnum[utiles.StringToInt(e.TypeMapping.Types[index])] = v
}
e.Value = rustEnum[e.Index]
return
}
if subType := e.TypeMapping.Types[e.Index]; subType != "" {
e.Value = map[string]interface{}{e.TypeMapping.Names[e.Index]: e.ProcessAndUpdateData(subType)}
return
}
}
if e.ValueList[e.Index] != "" {
e.Value = e.ValueList[e.Index]
}
}
type StorageHasher struct {
Enum
}
func (s *StorageHasher) Init(data ScaleBytes, option *ScaleDecoderOption) {
option.ValueList = []string{"Blake2_128", "Blake2_256", "Blake2_128Concat", "Twox128", "Twox256", "Twox64Concat", "Identity"}
s.Enum.Init(data, option)
}
type Null struct {
ScaleDecoder
}
type VecU8FixedLength struct {
ScaleDecoder
FixedLength int
}
func (s *VecU8FixedLength) Process() {
s.Value = utiles.AddHex(utiles.BytesToHex(s.NextBytes(s.FixedLength)))
}
type AccountId struct {
ScaleDecoder
}
func (s *AccountId) Process() {
s.Value = xstrings.RightJustify(utiles.BytesToHex(s.NextBytes(32)), 64, "0")
}
type BoxProposal struct {
ScaleDecoder
}
func (s *BoxProposal) Process() {
callIndex := utiles.BytesToHex(s.NextBytes(2))
callModule := s.Metadata.CallIndex[callIndex]
result := map[string]interface{}{
"call_index": callIndex,
"call_name": callModule.Call.Name,
"call_module": callModule.Module.Name,
}
var param []commonTypes.ExtrinsicParam
for _, arg := range callModule.Call.Args {
param = append(param, commonTypes.ExtrinsicParam{
Name: arg.Name,
Type: arg.Type,
Value: s.ProcessAndUpdateData(arg.Type),
})
}
result["params"] = param
s.Value = result
}
type Balance struct {
ScaleDecoder
Reader io.Reader
}
func (b *Balance) Process() {
buf := &bytes.Buffer{}
b.Reader = buf
_, _ = buf.Write(b.NextBytes(16))
c := make([]byte, 16)
_, _ = b.Reader.Read(c)
if utiles.BytesToHex(c) == "ffffffffffffffffffffffffffffffff" {
b.Value = decimal.Zero
return
}
b.Value = decimal.NewFromBigInt(uint128.FromBytes(c).Big(), 0)
}
type Index struct{ U64 }
type SessionIndex struct{ U32 }
type EraIndex struct{ U32 }
type ParaId struct{ U32 }
type Set struct {
ScaleDecoder
SetValue int
ValueList []string
BitLength int
}
func (s *Set) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.SetValue = 0
if option.ValueList != nil {
s.ValueList = option.ValueList
}
s.ScaleDecoder.Init(data, option)
}
func (s *Set) Process() {
setValue := s.ProcessAndUpdateData(fmt.Sprintf("U%d", s.BitLength))
switch v := setValue.(type) {
case uint64:
s.SetValue = int(v)
case uint8:
s.SetValue = int(v)
case uint32:
s.SetValue = int(v)
case uint16:
s.SetValue = int(v)
}
var result []string
if s.SetValue > 0 {
for k, value := range s.ValueList {
if s.SetValue&int(math.Pow(2, float64(k))) > 0 {
result = append(result, value)
}
}
}
s.Value = result
}
type LogDigest struct{ Enum }
func (l *LogDigest) Init(data ScaleBytes, option *ScaleDecoderOption) {
l.ValueList = []string{"Other", "AuthoritiesChange", "ChangesTrieRoot", "SealV0", "Consensus", "Seal", "PreRuntime"}
l.Enum.Init(data, option)
}
func (l *LogDigest) Process() {
index := utiles.BytesToHex(l.NextBytes(1))
if utiles.U256(index) != nil {
l.Index = int(utiles.U256(index).Uint64())
}
indexType := l.ValueList[l.Index]
if indexType == "" {
panic(fmt.Sprintf("LogDigest index %d not in list", l.Index))
}
l.Value = map[string]interface{}{
"type": indexType,
"value": l.ProcessAndUpdateData(indexType),
}
}
type Other struct{ HexBytes }
type ChangesTrieRoot struct{ HexBytes }
type AuthoritiesChange struct{ Vec }
func (l *AuthoritiesChange) Init(data ScaleBytes, option *ScaleDecoderOption) {
option.SubType = "AccountId"
l.Vec.Init(data, option)
}
type SealV0 struct{ Struct }
func (s *SealV0) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"slot", "signature"}, Types: []string{"u64", "Signature"}}
s.Struct.Init(data, option)
}
type Consensus struct{ Struct }
func (s *Consensus) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"engine", "data"}, Types: []string{"u32", "Vec<u8>"}}
s.Struct.Init(data, option)
}
type Seal struct{ Struct }
func (s *Seal) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"engine", "data"}, Types: []string{"u32", "HexBytes"}}
s.Struct.Init(data, option)
}
type PreRuntime struct{ Struct }
func (s *PreRuntime) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"engine", "data"}, Types: []string{"u32", "HexBytes"}}
s.Struct.Init(data, option)
}
type Exposure struct{ Struct }
func (s *Exposure) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"total", "own", "others"}, Types: []string{"Compact<Balance>", "Compact<Balance>", "Vec<IndividualExposure<AccountId, Balance>>"}}
s.Struct.Init(data, option)
}
type IndividualExposure struct{ Struct }
func (s *IndividualExposure) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"who", "value"}, Types: []string{"AccountId", "Compact<Balance>"}}
s.Struct.Init(data, option)
}
type RawAuraPreDigest struct{ Struct }
func (s *RawAuraPreDigest) Init(data ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"slotNumber"}, Types: []string{"u64"}}
s.Struct.Init(data, option)
}
type RawBabePreDigest struct {
Struct
}
func (r *RawBabePreDigest) Init(data ScaleBytes, option *ScaleDecoderOption) {
r.Struct.TypeMapping = &TypeMapping{Names: []string{"isPhantom", "Primary", "Secondary", "VRF"}, Types: []string{"bool", "RawBabePreDigestPrimary", "RawBabePreDigestSecondary", "RawBabePreDigestSecondaryVRF"}}
r.Struct.Init(data, option)
}
func (r *RawBabePreDigest) Process() {
label := r.ProcessAndUpdateData("RawBabeLabel").(string)
for k, name := range r.Struct.TypeMapping.Names {
if name == label {
r.Value = map[string]interface{}{label: r.ProcessAndUpdateData(r.TypeMapping.Types[k])}
break
}
}
}
type RawBabeLabel struct {
Enum
}
func (s *RawBabeLabel) Init(data ScaleBytes, option *ScaleDecoderOption) {
option.ValueList = []string{"isPhantom", "Primary", "Secondary", "VRF"}
s.Enum.Init(data, option)
}
type RawBabePreDigestPrimary struct{ Struct }
type RawBabePreDigestSecondary struct{ Struct }
type SlotNumber struct{ U64 }
type BabeBlockWeight struct{ U32 }
func (r *RawBabePreDigestPrimary) Init(data ScaleBytes, option *ScaleDecoderOption) {
r.Struct.TypeMapping = &TypeMapping{
Names: []string{"authorityIndex", "slotNumber", "weight", "vrfOutput", "vrfProof"},
Types: []string{"u32", "SlotNumber", "BabeBlockWeight", "H256", "H256"},
}
r.Struct.Init(data, option)
}
func (r *RawBabePreDigestSecondary) Init(data ScaleBytes, option *ScaleDecoderOption) {
r.Struct.TypeMapping = &TypeMapping{Names: []string{"authorityIndex", "slotNumber", "weight"}, Types: []string{"u32", "SlotNumber", "BabeBlockWeight"}}
r.Struct.Init(data, option)
}
type RawBabePreDigestSecondaryVRF struct{ Struct }
func (r *RawBabePreDigestSecondaryVRF) Init(data ScaleBytes, option *ScaleDecoderOption) {
r.Struct.TypeMapping = &TypeMapping{Names: []string{"authorityIndex", "slotNumber", "vrfOutput", "vrfProof"}, Types: []string{"u32", "SlotNumber", "VrfData", "VrfProof"}}
r.Struct.Init(data, option)
}
type LockIdentifier struct {
ScaleDecoder
}
func (l *LockIdentifier) Process() {
l.Value = utiles.AddHex(utiles.BytesToHex(l.NextBytes(8)))
}
type AccountIndex struct{ U32 }
type FixedLengthArray struct {
ScaleDecoder
FixedLength int
SubType string
}
func (f *FixedLengthArray) Init(data ScaleBytes, option *ScaleDecoderOption) {
if option != nil && option.FixedLength != 0 {
f.FixedLength = option.FixedLength
}
f.ScaleDecoder.Init(data, option)
}
func (f *FixedLengthArray) Process() {
var result []interface{}
if f.FixedLength > 0 {
for i := 0; i < f.FixedLength; i++ {
result = append(result, f.ProcessAndUpdateData(f.SubType))
}
} else {
f.GetNextU8()
}
f.Value = result
}
type AuthorityId struct{ H256 }
type EcdsaSignature struct {
ScaleDecoder
}
func (e *EcdsaSignature) Process() {
e.Value = utiles.AddHex(utiles.BytesToHex(e.NextBytes(65)))
}
type Call struct {
ScaleDecoder
}
func (s *Call) Process() {
callIndex := utiles.BytesToHex(s.NextBytes(2))
callModule := s.Metadata.CallIndex[callIndex]
result := map[string]interface{}{
"call_index": callIndex,
"call_name": callModule.Call.Name,
"call_module": callModule.Module.Name,
}
var param []commonTypes.ExtrinsicParam
for _, arg := range callModule.Call.Args {
param = append(param, commonTypes.ExtrinsicParam{
Name: arg.Name,
Type: arg.Type,
Value: s.ProcessAndUpdateData(arg.Type),
})
}
result["params"] = param
s.Value = result
}
type ReferendumIndex struct{ U32 }
type PropIndex struct{ U32 }
type EthereumAddress struct {
ScaleDecoder
}
func (e *EthereumAddress) Process() {
e.Value = ethereum.Encode(utiles.BytesToHex(e.NextBytes(20)))
}
type Data struct {
Enum
}
func (d *Data) Init(data ScaleBytes, option *ScaleDecoderOption) {
d.TypeMapping = &TypeMapping{
Names: []string{"None", "Raw", "BlakeTwo256", "Sha256", "Keccak256", "ShaThree256"},
Types: []string{"Null", "String", "H256", "H256", "H256", "H256"},
}
d.Enum.Init(data, option)
}
func (d *Data) Process() {
c := utiles.BytesToHex(d.NextBytes(1))
if c == "" || utiles.U256(c).Uint64() == 0 {
d.Value = map[string]interface{}{"None": nil}
return
}
d.Index = int(utiles.U256(c).Uint64())
if d.Index >= 1 && d.Index <= 33 {
data := d.NextBytes(d.Index - 1)
if utf8.Valid(data) {
d.Value = map[string]interface{}{"Raw": string(data)}
} else {
d.Value = map[string]interface{}{"Raw": utiles.BytesToHex(data)}
}
}
if d.Index >= 34 && d.Index <= 37 {
enumKey := d.TypeMapping.Names[d.Index-32]
d.Value = map[string]interface{}{enumKey: d.ProcessAndUpdateData(d.TypeMapping.Types[d.Index-32])}
}
}
type Vote struct {
U8
}
type VoteOutcome struct {
ScaleDecoder
}
func (v *VoteOutcome) Process() {
v.Value = utiles.BytesToHex(v.NextBytes(32))
}
type H160 struct{ ScaleDecoder }
func (h *H160) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(20)))
}
type IntFixed struct {
ScaleDecoder
FixedLength int
Reader io.Reader
}
func (f *IntFixed) Init(data ScaleBytes, option *ScaleDecoderOption) {
if option != nil && option.FixedLength != 0 {
f.FixedLength = option.FixedLength
}
f.ScaleDecoder.Init(data, option)
}
func (f *IntFixed) Process() {
value := utiles.U256(utiles.BytesToHex(utiles.ReverseBytes(f.NextBytes(f.FixedLength))))
var i, e, b = big.NewInt(2), big.NewInt(int64(f.FixedLength*8) - 1), big.NewInt(int64(f.FixedLength * 8))
unsignedMid := big.NewInt(2).Exp(i, e, nil)
ceiling := big.NewInt(2).Exp(i, b, nil)
if value.Cmp(unsignedMid) > 0 {
f.Value = value.Sub(value, ceiling)
return
}
f.Value = value
}
type Key struct{ Bytes }
type OpaqueCall struct {
Bytes
}
func (f *OpaqueCall) Process() {
f.Bytes.Process()
e := ScaleDecoder{}
option := ScaleDecoderOption{Metadata: f.Metadata, Spec: f.Spec}
e.Init(ScaleBytes{Data: utiles.HexToBytes(f.Value.(string))}, &option)
value := e.ProcessAndUpdateData("Call")
f.Value = value
}
type GenericLookupSource struct {
ScaleDecoder
}
func (g *GenericLookupSource) Process() {
if len(g.Data.Data) == 32 {
g.Value = utiles.BytesToHex(g.NextBytes(32))
}
AccountLength := g.NextBytes(1)
accountLength := utiles.BytesToHex(AccountLength)
if accountLength == "ff" {
g.Value = utiles.BytesToHex(g.NextBytes(32))
return
}
offset, length := readLength(AccountLength)
e := ScaleDecoder{}
e.Init(ScaleBytes{Data: g.Data.Data[offset : offset+length]}, nil)
g.Value = strconv.Itoa(int(e.ProcessAndUpdateData("U32").(uint32)))
}
func readLength(value []byte) (int, int) {
first := value[0]
switch first {
case 0xfc:
return 1, 2
case 0xfd:
return 1, 4
case 0xfe:
return 1, 8
}
return 0, 1
}
type BTreeMap struct{ ScaleDecoder }
func (b *BTreeMap) Process() {
elementCount := b.ProcessAndUpdateData("Compact<u32>").(int)
var result []interface{}
if elementCount > 1000 {
panic(fmt.Sprintf("BTreeMap length %d exceeds %d", elementCount, 1000))
}
for i := 0; i < elementCount; i++ {
subType := strings.Split(b.SubType, ",")
key := utiles.ToString(b.ProcessAndUpdateData(subType[0]))
result = append(result, map[string]interface{}{
key: b.ProcessAndUpdateData(subType[1]),
})
}
b.Value = result
}