-
Notifications
You must be signed in to change notification settings - Fork 36
/
types.go
497 lines (401 loc) · 11.8 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
package types
import (
"bytes"
"fmt"
"io"
"math"
"strconv"
"strings"
"unicode/utf8"
"github.com/huandu/xstrings"
"github.com/itering/scale.go/types/scaleBytes"
"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"
)
type H160 struct{ ScaleDecoder }
type Other struct{ HexBytes }
type ChangesTrieRoot struct{ HexBytes }
type BTreeSet struct{ Vec }
type RuntimeEnvironmentUpdated struct{ Null }
type SlotNumber struct{ U64 }
type Null struct{ ScaleDecoder }
func (*Null) TypeStructString() string {
return "NULL"
}
type Empty struct{ ScaleDecoder }
func (e *Empty) Process() {
e.Value = "NULL"
}
func (h *H160) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(20)))
}
func (h *H160) Encode(value string) string {
return utiles.AddHex(strings.ToLower(value))
}
func (h *H160) TypeStructString() string {
return "H160"
}
type H256 struct {
ScaleDecoder
}
func (h *H256) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(32)))
}
func (h *H256) Encode(value string) string {
return utiles.AddHex(strings.ToLower(value))
}
func (h *H256) TypeStructString() string {
return "H256"
}
type H512 struct {
ScaleDecoder
}
func (h *H512) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(64)))
}
func (h *H512) Encode(value string) string {
return utiles.AddHex(strings.ToLower(value))
}
func (h *H512) TypeStructString() string {
return "H512"
}
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))
}
}
func (e *Era) Encode(era string) string {
return era
}
type EraExtrinsic struct{ Era }
type CompactMoment struct {
CompactU32
}
func (m *CompactMoment) Process() {
m.CompactU32.Process()
if m.Value.(int) > 10000000000 {
m.Value = m.Value.(int) / 1000
}
}
type Moment struct {
U64
}
func (m *Moment) Process() {
m.U64.Process()
if m.Value.(uint64) > 10000000000 {
m.Value = m.Value.(uint64) / 1000
}
}
type BlockNumber struct {
U32
}
type Address struct {
ScaleDecoder
AccountLength string `json:"account_length"`
}
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 AccountId struct {
ScaleDecoder
}
func (s *AccountId) Process() {
s.Value = utiles.AddHex(xstrings.RightJustify(utiles.BytesToHex(s.NextBytes(32)), 64, "0"))
}
func (s *AccountId) Encode(value string) string {
return value
}
func (s *AccountId) TypeStructString() string {
return "AccountId"
}
type Balance struct {
U128
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.NewFromInt32(-1)
return
}
b.Value = decimal.NewFromBigInt(uint128.FromBytes(c).Big(), 0)
}
type LogDigest struct{ Enum }
func (l *LogDigest) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOption) {
l.ValueList = []string{"Other", "AuthoritiesChange", "ChangesTrieRoot", "SealV0", "Consensus", "Seal", "PreRuntime", "ChangesTrieSignal", "RuntimeEnvironmentUpdated"}
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 AuthoritiesChange struct{ Vec }
func (l *AuthoritiesChange) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOption) {
option.SubType = "AccountId"
l.Vec.Init(data, option)
}
type SealV0 struct{ Struct }
func (s *SealV0) Init(data scaleBytes.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.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.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.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.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.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.ScaleBytes, option *ScaleDecoderOption) {
s.Struct.TypeMapping = &TypeMapping{Names: []string{"slotNumber"}, Types: []string{"u64"}}
s.Struct.Init(data, option)
}
type RawBabePreDigestPrimary struct{ Struct }
func (r *RawBabePreDigestPrimary) Init(data scaleBytes.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)
}
type RawBabePreDigestSecondary struct{ Struct }
func (r *RawBabePreDigestSecondary) Init(data scaleBytes.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.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 EcdsaSignature struct {
ScaleDecoder
}
func (e *EcdsaSignature) Process() {
e.Value = utiles.AddHex(utiles.BytesToHex(e.NextBytes(65)))
}
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.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{}{fmt.Sprintf("Raw%d", d.Index-1): string(data)}
} else {
d.Value = map[string]interface{}{fmt.Sprintf("Raw%d", d.Index-1): 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])}
}
}
func (d *Data) Encode(v map[string]interface{}) string {
key, val, err := utiles.GetEnumValue(v)
if err != nil {
panic(err)
}
if index := utiles.SliceIndex(key, d.TypeMapping.Names); index > -1 {
if key == "None" {
return Encode("U8", 0)
}
return Encode("U8", index+32) + Encode(d.TypeMapping.Types[index], val.(string))
}
// raw data
if strings.HasPrefix(key, "Raw") {
var l int
if _, err := fmt.Sscanf(key, "Raw%d", &l); err != nil {
panic("invalid data Raw data key")
} else {
l++
indexRaw := Encode("U8", l)
if l == len(val.(string)) {
return indexRaw + val.(string)
}
return indexRaw + utiles.BytesToHex([]byte(val.(string)))
}
}
panic("invalid enum key")
}
type VoteOutcome struct{ H256 }
type OpaqueCall struct {
Bytes
}
func (f *OpaqueCall) Process() {
f.Bytes.Process()
e := ScaleDecoder{}
option := ScaleDecoderOption{Metadata: f.Metadata, Spec: f.Spec}
e.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(f.Value.(string))}, &option)
value := e.ProcessAndUpdateData("Call")
f.InternalCall = append(f.InternalCall, e.InternalCall...)
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 := func(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
}(AccountLength)
e := ScaleDecoder{}
e.Init(scaleBytes.ScaleBytes{Data: g.Data.Data[offset : offset+length]}, nil)
g.Value = strconv.Itoa(int(e.ProcessAndUpdateData("U32").(uint32)))
}
type Box struct{ ScaleDecoder }
func (b *Box) Process() {
b.Value = b.ProcessAndUpdateData(b.SubType)
}
type WrapperOpaque struct{ ScaleDecoder }
func (w *WrapperOpaque) Process() {
w.ProcessAndUpdateData("Compact<u32>")
w.Value = w.ProcessAndUpdateData(w.SubType)
}
type Range struct{ ScaleDecoder }
type RangeInclusive struct{ Range }
func (r *Range) Process() {
subTypeSlice := strings.Split(r.SubType, ",")
start := r.SubType
end := r.SubType
if len(subTypeSlice) == 2 {
start = subTypeSlice[0]
end = subTypeSlice[1]
}
r.Value = map[string]interface{}{
"start": r.ProcessAndUpdateData(start),
"end": r.ProcessAndUpdateData(end),
}
}
type BitVec struct {
Compact
}
func (b *BitVec) Process() {
length := b.ProcessAndUpdateData("Compact<u32>").(int)
u8a := b.NextBytes(int(math.Ceil(float64(length) / 8)))
var value []string
for _, v := range u8a {
value = append(value, fmt.Sprintf("%08b", v))
}
b.Value = "0b" + strings.Join(value, "_")
}
func (b *BitVec) TypeStructString() string {
return "BitVec"
}