scale.rb 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,33 +17,53 @@ module Scale
17
17
  end
18
18
 
19
19
  def encode
20
- self.value === true ? "01" : "00"
20
+ value === true ? "01" : "00"
21
21
  end
22
22
  end
23
23
 
24
24
  class U8
25
25
  include FixedWidthUInt
26
- BYTES_LENGTH = 1
26
+ BYTE_LENGTH = 1
27
27
  end
28
28
 
29
29
  class U16
30
30
  include FixedWidthUInt
31
- BYTES_LENGTH = 2
31
+ BYTE_LENGTH = 2
32
32
  end
33
33
 
34
34
  class U32
35
35
  include FixedWidthUInt
36
- BYTES_LENGTH = 4
36
+ BYTE_LENGTH = 4
37
37
  end
38
38
 
39
39
  class U64
40
40
  include FixedWidthUInt
41
- BYTES_LENGTH = 8
41
+ BYTE_LENGTH = 8
42
42
  end
43
43
 
44
44
  class U128
45
45
  include FixedWidthUInt
46
- BYTES_LENGTH = 16
46
+ BYTE_LENGTH = 16
47
+ end
48
+
49
+ class I8
50
+ include FixedWidthInt
51
+ end
52
+
53
+ class I16
54
+ include FixedWidthInt
55
+ end
56
+
57
+ class I32
58
+ include FixedWidthInt
59
+ end
60
+
61
+ class I64
62
+ include FixedWidthInt
63
+ end
64
+
65
+ class I128
66
+ include FixedWidthInt
47
67
  end
48
68
 
49
69
  class Compact
@@ -51,34 +71,34 @@ module Scale
51
71
 
52
72
  def self.decode(scale_bytes)
53
73
  first_byte = scale_bytes.get_next_bytes(1)[0]
54
- first_byte_in_bin = first_byte.to_s(2).rjust(8, '0')
74
+ first_byte_in_bin = first_byte.to_s(2).rjust(8, "0")
55
75
 
56
76
  mode = first_byte_in_bin[6..7]
57
- value =
58
- if mode == '00'
77
+ value =
78
+ if mode == "00"
59
79
  first_byte >> 2
60
- elsif mode == '01'
80
+ elsif mode == "01"
61
81
  second_byte = scale_bytes.get_next_bytes(1)[0]
62
82
  [first_byte, second_byte]
63
83
  .reverse
64
- .map { |b| b.to_s(16).rjust(2, '0') }
84
+ .map { |b| b.to_s(16).rjust(2, "0") }
65
85
  .join
66
86
  .to_i(16) >> 2
67
- elsif mode == '10'
87
+ elsif mode == "10"
68
88
  remaining_bytes = scale_bytes.get_next_bytes(3)
69
89
  ([first_byte] + remaining_bytes)
70
90
  .reverse
71
- .map { |b| b.to_s(16).rjust(2, '0') }
91
+ .map { |b| b.to_s(16).rjust(2, "0") }
72
92
  .join
73
93
  .to_i(16) >> 2
74
94
  # or like this:
75
95
  # ['02093d00'].pack('H*').unpack('l').first / 4
76
- elsif mode == '11'
96
+ elsif mode == "11"
77
97
  remaining_length = 4 + (first_byte >> 2)
78
98
  remaining_bytes = scale_bytes.get_next_bytes(remaining_length)
79
99
  remaining_bytes
80
100
  .reverse
81
- .map { |b| b.to_s(16).rjust(2, '0') }
101
+ .map { |b| b.to_s(16).rjust(2, "0") }
82
102
  .join
83
103
  .to_i(16)
84
104
  end
@@ -87,22 +107,22 @@ module Scale
87
107
  end
88
108
 
89
109
  def encode
90
- if self.value >= 0 and self.value <= 63
91
- (value << 2).to_s(16).rjust(2, '0')
92
- elsif self.value > 63 and self.value <= (2**14 - 1)
93
- ((value << 2) + 1).to_s(16).rjust(4, '0').scan(/.{2}/).reverse.join
94
- elsif self.value > (2**14 - 1) and self.value <= (2**30 - 1)
95
- ((value << 2) + 2).to_s(16).rjust(8, '0').scan(/.{2}/).reverse.join
96
- elsif self.value > (2**30 - 1)
97
- value_in_hex = self.value.to_s(16)
110
+ if (value >= 0) && (value <= 63)
111
+ (value << 2).to_s(16).rjust(2, "0")
112
+ elsif (value > 63) && (value <= (2**14 - 1))
113
+ ((value << 2) + 1).to_s(16).rjust(4, "0").scan(/.{2}/).reverse.join
114
+ elsif (value > (2**14 - 1)) && (value <= (2**30 - 1))
115
+ ((value << 2) + 2).to_s(16).rjust(8, "0").scan(/.{2}/).reverse.join
116
+ elsif value > (2**30 - 1)
117
+ value_in_hex = value.to_s(16)
98
118
  length = if value_in_hex.length % 2 == 1
99
119
  value_in_hex.length + 1
100
120
  else
101
121
  value_in_hex.length
102
122
  end
103
123
 
104
- hex = value_in_hex.rjust(length, '0').scan(/.{2}/).reverse.join
105
- (((length/2 - 4) << 2) + 3).to_s(16).rjust(2, '0') + hex
124
+ hex = value_in_hex.rjust(length, "0").scan(/.{2}/).reverse.join
125
+ (((length/2 - 4) << 2) + 3).to_s(16).rjust(2, "0") + hex
106
126
  end
107
127
  end
108
128
  end
@@ -125,11 +145,11 @@ module Scale
125
145
  end
126
146
 
127
147
  def encode
128
- if self.value.start_with?("0x")
129
- length = Compact.new((self.value.length - 2)/2).encode
130
- "#{length}#{self.value[2..]}"
148
+ if value.start_with?("0x")
149
+ length = Compact.new((value.length - 2)/2).encode
150
+ "#{length}#{value[2..]}"
131
151
  else
132
- bytes = self.value.unpack("C*")
152
+ bytes = value.unpack("C*")
133
153
  hex_string = bytes.bytes_to_hex[2..]
134
154
  length = Compact.new(bytes.length).encode
135
155
  "#{length}#{hex_string}"
@@ -164,8 +184,8 @@ module Scale
164
184
  end
165
185
 
166
186
  def encode
167
- raise "Format error" if not self.value.start_with?("0x") || self.value.length != 42
168
- ScaleBytes.new self.value
187
+ raise "Format error" unless value.start_with?("0x") || value.length != 42
188
+ value[2..]
169
189
  end
170
190
  end
171
191
 
@@ -177,8 +197,8 @@ module Scale
177
197
  end
178
198
 
179
199
  def encode
180
- raise "Format error" if not self.value.start_with?("0x") || self.value.length != 66
181
- ScaleBytes.new self.value
200
+ raise "Format error" unless value.start_with?("0x") || value.length != 66
201
+ value[2..]
182
202
  end
183
203
  end
184
204
 
@@ -190,47 +210,56 @@ module Scale
190
210
  end
191
211
 
192
212
  def encode
193
- raise "Format error" if not self.value.start_with?("0x") || self.value.length != 130
194
- ScaleBytes.new self.value
213
+ raise "Format error" unless value.start_with?("0x") || value.length != 130
214
+ value[2..]
195
215
  end
196
216
  end
197
217
 
198
218
  class Address
199
219
  include SingleValue
220
+ attr_accessor :account_length, :account_index, :account_id, :account_idx
221
+
200
222
  def self.decode(scale_bytes)
201
- prefix = scale_bytes.get_next_bytes(1).first
202
- if prefix == 0xff
203
- value = scale_bytes.get_next_bytes(32).bytes_to_hex
204
- elsif prefix == 0xfc
205
- value = scale_bytes.get_next_bytes(2).bytes_to_hex
206
- elsif prefix == 0xfd
207
- value = scale_bytes.get_next_bytes(4).bytes_to_hex
208
- elsif prefix == 0xfe
209
- value = scale_bytes.get_next_bytes(8).bytes_to_hex
223
+ account_length = scale_bytes.get_next_bytes(1).first
224
+
225
+ if account_length == 0xff # 255
226
+ account_id = scale_bytes.get_next_bytes(32).bytes_to_hex
227
+ account_length = account_length.to_s(16)
228
+ Address.new(account_id)
210
229
  else
211
- value = [prefix].bytes_to_hex
230
+ account_index =
231
+ if account_length == 0xfc
232
+ scale_bytes.get_next_bytes(2).bytes_to_hex
233
+ elsif account_length == 0xfd
234
+ scale_bytes.get_next_bytes(4).bytes_to_hex
235
+ elsif account_length == 0xfe
236
+ scale_bytes.get_next_bytes(8).bytes_to_hex
237
+ else
238
+ [account_length].bytes_to_hex
239
+ end
240
+ # account_idx =
241
+ account_length = account_length.to_s(16)
242
+ Address.new(account_index)
212
243
  end
213
-
214
- Address.new(value)
215
244
  end
216
245
 
217
246
  def encode(ss58=false, addr_type=42)
218
- if self.value.start_with?("0x")
247
+ if value.start_with?("0x")
219
248
  if ss58 === true
220
- ::Address.encode(self.value, addr_type)
249
+ ::Address.encode(value, addr_type)
221
250
  else
222
- prefix = if self.value.length == 66
223
- "ff"
224
- elsif self.value.length == 6
225
- "fc"
226
- elsif self.value.length == 10
227
- "fd"
228
- elsif self.value.length == 18
229
- "fe"
230
- else
231
- ""
232
- end
233
- "#{prefix}#{self.value[2..]}"
251
+ prefix = if value.length == 66
252
+ "ff"
253
+ elsif value.length == 6
254
+ "fc"
255
+ elsif value.length == 10
256
+ "fd"
257
+ elsif value.length == 18
258
+ "fe"
259
+ else
260
+ ""
261
+ end
262
+ "#{prefix}#{value[2..]}"
234
263
  end
235
264
  else
236
265
  raise "Format error"
@@ -238,6 +267,8 @@ module Scale
238
267
  end
239
268
  end
240
269
 
270
+ class RawAddress < Address; end
271
+
241
272
  class AccountId < H256; end
242
273
 
243
274
  class Balance < U128; end
@@ -269,7 +300,7 @@ module Scale
269
300
  def self.decode(scale_bytes)
270
301
  value = Compact.decode(scale_bytes).value
271
302
  if value > 10000000000
272
- value = value / 1000
303
+ value /= 1000
273
304
  end
274
305
 
275
306
  CompactMoment.new Time.at(seconds_since_epoch_integer).to_datetime
@@ -280,26 +311,26 @@ module Scale
280
311
  include Struct
281
312
  items(
282
313
  proposal: "Hex",
283
- registredBy: "AccountId",
314
+ registred_by: "AccountId",
284
315
  deposit: "BalanceOf",
285
316
  blockNumber: "BlockNumber"
286
317
  )
287
318
  end
288
319
 
289
320
  class RewardDestination
290
- include Enum
321
+ include Enum
291
322
  values "Staked", "Stash", "Controller"
292
323
  end
293
324
 
294
325
  class WithdrawReasons
295
326
  include Set
296
- values(
327
+ items(
297
328
  {
298
- "TransactionPayment" => 1,
299
- "Transfer" => 2,
300
- "Reserve" => 4,
301
- "Fee" => 8,
302
- "Tip" => 16
329
+ TransactionPayment: 1,
330
+ Transfer: 2,
331
+ Reserve: 4,
332
+ Fee: 8,
333
+ Tip: 16
303
334
  }, 1
304
335
  )
305
336
  end
@@ -372,7 +403,7 @@ module Scale
372
403
 
373
404
  class VoteThreshold
374
405
  include Enum
375
- values 'SuperMajorityApprove', 'SuperMajorityAgainst', 'SimpleMajority'
406
+ values "SuperMajorityApprove", "SuperMajorityAgainst", "SimpleMajority"
376
407
  end
377
408
 
378
409
  class Null
@@ -391,7 +422,7 @@ module Scale
391
422
  class LockPeriods < U8; end
392
423
 
393
424
  class Hash < H256; end
394
-
425
+
395
426
  class VoteIndex < U32; end
396
427
 
397
428
  class ProposalIndex < U32; end
@@ -422,7 +453,7 @@ module Scale
422
453
 
423
454
  class StorageHasher
424
455
  include Enum
425
- values 'Blake2_128', 'Blake2_256', 'Blake2_128Concat', 'Twox128', 'Twox256', 'Twox64Concat'
456
+ values "Blake2_128", "Blake2_256", "Blake2_128Concat", "Twox128", "Twox256", "Twox64Concat", "Identity"
426
457
  end
427
458
 
428
459
  class VoterInfo
@@ -459,7 +490,7 @@ module Scale
459
490
  end
460
491
 
461
492
  class OpaquePeerId < Bytes; end
462
-
493
+
463
494
  class OpaqueMultiaddr < Bytes; end
464
495
 
465
496
  class SessionKeysSubstrate
@@ -478,7 +509,7 @@ module Scale
478
509
  babe: "AccountId"
479
510
  )
480
511
  end
481
-
512
+
482
513
  class EdgewareKeys
483
514
  include Struct
484
515
  items(
@@ -516,35 +547,35 @@ module Scale
516
547
  end
517
548
 
518
549
  class VecU8Length2
519
- include VecU8FixedLength
550
+ include VecU8FixedLength
520
551
  end
521
552
 
522
553
  class VecU8Length3
523
- include VecU8FixedLength
554
+ include VecU8FixedLength
524
555
  end
525
556
 
526
557
  class VecU8Length4
527
- include VecU8FixedLength
558
+ include VecU8FixedLength
528
559
  end
529
560
 
530
561
  class VecU8Length8
531
- include VecU8FixedLength
562
+ include VecU8FixedLength
532
563
  end
533
564
 
534
565
  class VecU8Length16
535
- include VecU8FixedLength
566
+ include VecU8FixedLength
536
567
  end
537
568
 
538
569
  class VecU8Length20
539
- include VecU8FixedLength
570
+ include VecU8FixedLength
540
571
  end
541
572
 
542
573
  class VecU8Length32
543
- include VecU8FixedLength
574
+ include VecU8FixedLength
544
575
  end
545
-
576
+
546
577
  class VecU8Length64
547
- include VecU8FixedLength
578
+ include VecU8FixedLength
548
579
  end
549
580
 
550
581
  class BalanceLock
@@ -557,5 +588,208 @@ module Scale
557
588
  )
558
589
  end
559
590
 
591
+ class EthereumAddress
592
+ include SingleValue
593
+
594
+ def self.decode(scale_bytes)
595
+ bytes = scale_bytes.get_next_bytes(20)
596
+ EthereumAddress.new(bytes.bytes_to_hex)
597
+ end
598
+
599
+ def encode
600
+ if value.start_with?("0x") && value.length == 42
601
+ value[2..]
602
+ else
603
+ raise 'Value should start with "0x" and must be 20 bytes long'
604
+ end
605
+ end
606
+ end
607
+
608
+ class EcdsaSignature
609
+ include SingleValue
610
+
611
+ def self.decode(scale_bytes)
612
+ bytes = scale_bytes.get_next_bytes(65)
613
+ EcdsaSignature.new(bytes.bytes_to_hex)
614
+ end
615
+
616
+ def encode
617
+ if value.start_with?("0x") && value.length == 132
618
+ value[2..]
619
+ else
620
+ raise 'Value should start with "0x" and must be 65 bytes long'
621
+ end
622
+ end
623
+ end
624
+
625
+ class Bidder
626
+ include Enum
627
+ values "NewBidder", "ParaId"
628
+ end
629
+
630
+ class BlockAttestations
631
+ include Struct
632
+ items(
633
+ receipt: "CandidateReceipt",
634
+ valid: "Vec<AccountId>",
635
+ invalid: "Vec<AccountId>"
636
+ )
637
+ end
638
+
639
+ class IncludedBlocks
640
+ include Struct
641
+ items(
642
+ actual_number: "BlockNumber",
643
+ session: "SessionIndex",
644
+ random_seed: "H256",
645
+ active_parachains: "Vec<ParaId>",
646
+ para_blocks: "Vec<Hash>"
647
+ )
648
+ end
649
+
650
+ class HeadData < Bytes; end
651
+
652
+ class Conviction
653
+ include Enum
654
+ values "None", "Locked1x", "Locked2x", "Locked3x", "Locked4x", "Locked5x", "Locked6x"
655
+ end
656
+
657
+ class EraRewards
658
+ include Struct
659
+ items(
660
+ total: "U32",
661
+ rewards: "Vec<U32>"
662
+ )
663
+ end
664
+
665
+ class SlashJournalEntry
666
+ include Struct
667
+ items(
668
+ who: "AccountId",
669
+ amount: "Balance",
670
+ ownSlash: "Balance"
671
+ )
672
+ end
673
+
674
+ class UpwardMessage
675
+ include Struct
676
+ items(
677
+ origin: "ParachainDispatchOrigin",
678
+ data: "Bytes"
679
+ )
680
+ end
681
+
682
+ class ParachainDispatchOrigin
683
+ include Enum
684
+ values "Signed", "Parachain"
685
+ end
686
+
687
+ class StoredState
688
+ include Enum
689
+ values "Live", "PendingPause", "Paused", "PendingResume"
690
+ end
691
+
692
+ class Votes
693
+ include Struct
694
+ items(
695
+ index: "ProposalIndex",
696
+ threshold: "MemberCount",
697
+ ayes: "Vec<AccountId>",
698
+ nays: "Vec<AccountId>"
699
+ )
700
+ end
701
+
702
+ class WinningDataEntry
703
+ include Struct
704
+ items(
705
+ account_id: "AccountId",
706
+ para_id_of: "ParaIdOf",
707
+ balance_of: "BalanceOf"
708
+ )
709
+ end
710
+
711
+ class IdentityType < Bytes; end
712
+
713
+ class VoteType
714
+ include Enum
715
+ values "Binary", "MultiOption"
716
+ end
717
+
718
+ class VoteOutcome
719
+ include SingleValue
720
+ def self.decode(scale_bytes)
721
+ new(scale_bytes.get_next_bytes(32))
722
+ end
723
+ end
724
+
725
+ class Identity < Bytes; end
726
+
727
+ class ProposalTitle < Bytes; end
728
+
729
+ class ProposalContents < Bytes; end
730
+
731
+ class ProposalStage
732
+ include Enum
733
+ values "PreVoting", "Voting", "Completed"
734
+ end
735
+
736
+ class ProposalCategory
737
+ include Enum
738
+ values "Signaling"
739
+ end
740
+
741
+ class VoteStage
742
+ include Enum
743
+ values "PreVoting", "Commit", "Voting", "Completed"
744
+ end
745
+
746
+ class TallyType
747
+ include Enum
748
+ values "OnePerson", "OneCoin"
749
+ end
750
+
751
+ class Attestation < Bytes; end
752
+
753
+ class VecNextAuthority
754
+ include Vec
755
+ inner_type "NextAuthority"
756
+ end
757
+
758
+ class BoxProposal
759
+ include SingleValue
760
+
761
+ def self.decode(scale_bytes, metadata, chain_spec)
762
+ call_index = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
763
+ call_module, call = metadata.value.call_index[call_index]
764
+
765
+ call_args = call[:args].map do |arg|
766
+ arg_obj = Scale::Types.get(arg[:type], chain_spec).decode(scale_bytes)
767
+ {name: arg[:name], type: arg[:type], value: arg_obj.encode, value_raw: arg_obj.value}
768
+ end
769
+
770
+ self.new({
771
+ call_index: call_index,
772
+ call_function: call[:name],
773
+ call_module: call_module[:name],
774
+ call_args: call_args
775
+ })
776
+ end
777
+
778
+ def encode
779
+ value[:call_index] +
780
+ value[:call_args]
781
+ .map {|call_arg| call_arg[:value]}
782
+ .join("")
783
+ end
784
+ end
785
+
786
+ class ValidatorPrefsLegacy
787
+ include Struct
788
+ items(
789
+ unstake_threshold: "Compact",
790
+ validator_payment: "Compact"
791
+ )
792
+ end
793
+
560
794
  end
561
795
  end