scale_rb 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d53289f0b86d457c262f850a693fd75c8c007fe6fe6fb716241d30897c7f57c
4
- data.tar.gz: 3784af581e51a87e7bdc10d6da633d719189e328f0d373ddfb93f1d120e6a44e
3
+ metadata.gz: 241f9857282220d19480bed1cf2a1bcaeacd0948963a65455ee1c57b708ed4d6
4
+ data.tar.gz: 8ab8fad9f853c7f3d1d57bf9f283b2fbe16a6d17bb2f1221b84ff3a78feff34c
5
5
  SHA512:
6
- metadata.gz: a6fa663c9058b64cb9b5f77450cfddf2eac1ddd582ce1c56445d0d255bddd479339b644f95cb436cb53e5e1e46698593dc862e7a6d6e77515ea17798427ec029
7
- data.tar.gz: baf73201617384641f980d6aa8d83650edc483a60969c240c78ef935f22fce267b5084e9de30487a5b4a79bc4333f1b240f00003991ded04e3b32c94bca3df9d
6
+ metadata.gz: b712d5a8dc5a9c6c90580467f81939b843e876bc4bbf7f6b53bccbeb12a411e8104acffdfd3419ad70d760c4f375862c09de1b6964c5e4ecf8a92cdc4efdfdb2
7
+ data.tar.gz: 2b9cb6bb007b0dbff88839e3e811628241290de7383e1f85cfc68ba7a7936ea85d0a479d54035854d18e3cf79685aba1bf8f7f0d7b519ab4089cd75048f0262a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scale_rb (0.1.7)
4
+ scale_rb (0.1.8)
5
5
  base58
6
6
  blake2b_rs (~> 0.1.4)
7
7
  xxhash
data/lib/address.rb CHANGED
@@ -73,6 +73,8 @@ class Address
73
73
 
74
74
  u8_array.push(*checksum)
75
75
 
76
+ u8_array = u8_array.map { |i| if i.is_a?(String) then i.to_i(16) else i end }
77
+ # u8_array = [42, 202, 122, 179, 154, 86, 153, 242, 157, 207, 38, 115, 170, 163, 73, 75, 72, 81, 26, 186, 224, 220, 60, 101, 15, 243, 152, 246, 95, 229, 225, 18, 56, 0x7e]
76
78
  input = u8_array.pack("C*")
77
79
 
78
80
  Base58.binary_to_base58(input, :bitcoin)
@@ -14,12 +14,79 @@ module Metadata
14
14
  MetadataV14.build_registry(metadata_v14)
15
15
  end
16
16
 
17
+ def get_module(pallet_name, metadata)
18
+ version = metadata._get(:metadata).keys.first
19
+ raise ScaleRb::NotImplemented, metadata._get(:metadata).keys.first unless %w[v9 v10 v11 v12 v13 v14].include?(version.to_s)
20
+
21
+ Metadata.const_get("Metadata#{version.upcase}").get_module(pallet_name, metadata)
22
+ end
23
+
24
+ def get_module_by_index(pallet_index, metadata)
25
+ version = metadata._get(:metadata).keys.first
26
+ raise ScaleRb::NotImplemented, metadata._get(:metadata).keys.first unless %w[v9 v10 v11 v12 v13 v14].include?(version.to_s)
27
+
28
+ Metadata.const_get("Metadata#{version.upcase}").get_module_by_index(pallet_index, metadata)
29
+ end
30
+
17
31
  def get_storage_item(pallet_name, item_name, metadata)
18
32
  version = metadata._get(:metadata).keys.first
19
33
  raise ScaleRb::NotImplemented, metadata._get(:metadata).keys.first unless %w[v9 v10 v11 v12 v13 v14].include?(version.to_s)
20
34
 
21
35
  Metadata.const_get("Metadata#{version.upcase}").get_storage_item(pallet_name, item_name, metadata)
22
36
  end
37
+
38
+ def get_calls_type(pallet_name, metadata)
39
+ version = metadata._get(:metadata).keys.first
40
+ raise ScaleRb::NotImplemented, metadata._get(:metadata).keys.first unless %w[v9 v10 v11 v12 v13 v14].include?(version.to_s)
41
+
42
+ Metadata.const_get("Metadata#{version.upcase}").get_calls_type(pallet_name, metadata)
43
+ end
44
+
45
+ def get_calls_type_id(pallet_name, metadata)
46
+ version = metadata._get(:metadata).keys.first
47
+ raise ScaleRb::NotImplemented, metadata._get(:metadata).keys.first unless %w[v9 v10 v11 v12 v13 v14].include?(version.to_s)
48
+
49
+ Metadata.const_get("Metadata#{version.upcase}").get_calls_type_id(pallet_name, metadata)
50
+ end
51
+
52
+ # call examples:
53
+ # {:pallet_name=>"Deposit", :call_name=>"Claim", :call=>["claim", []]}
54
+ # {:pallet_name=>"Balances", :call_name=>"Transfer", :call=>[{:transfer=>{:dest=>[10, 18, 135, 151, 117, 120, 248, 136, 189, 193, 199, 98, 119, 129, 175, 28, 192, 0, 230, 171], :value=>11000000000000000000}}, []]}
55
+ def encode_call(call, metadata)
56
+ calls_type_id = get_calls_type_id(call[:pallet_name], metadata)
57
+ pallet_index = get_module(call[:pallet_name], metadata)._get(:index)
58
+ [pallet_index] + PortableCodec.encode(calls_type_id, call[:call].first, build_registry(metadata))
59
+ end
60
+
61
+ # callbytes's structure is: pallet_index + call_index + argsbytes
62
+ #
63
+ # callbytes examples:
64
+ # "0x0901".to_bytes
65
+ # "0x05000a1287977578f888bdc1c7627781af1cc000e6ab1300004c31b8d9a798".to_bytes
66
+ def decode_call(callbytes, metadata)
67
+ pallet_index = callbytes[0]
68
+ pallet = get_module_by_index(pallet_index, metadata)
69
+
70
+ pallet_name = pallet._get(:name)
71
+
72
+ # Remove the pallet_index
73
+ # The callbytes we used below should not contain the pallet index.
74
+ # This is because the pallet index is not part of the call type.
75
+ # Its structure is: call_index + call_args
76
+ callbytes_without_pallet_index = callbytes[1..]
77
+ calls_type_id = pallet._get(:calls)._get(:type)
78
+ decoded = PortableCodec.decode(
79
+ calls_type_id,
80
+ callbytes_without_pallet_index,
81
+ build_registry(metadata)
82
+ )
83
+
84
+ {
85
+ pallet_name: pallet_name,
86
+ call_name: decoded.first.is_a?(String) ? decoded.first.to_camel : decoded.first.keys.first.to_s.to_camel,
87
+ call: decoded
88
+ }
89
+ end
23
90
  end
24
91
 
25
92
  TYPES = {
@@ -14,6 +14,12 @@ module Metadata
14
14
  end
15
15
  end
16
16
 
17
+ def get_module_by_index(pallet_index, metadata)
18
+ metadata._get(:metadata)._get(:v14)._get(:pallets).find do |p|
19
+ p._get(:index) == pallet_index
20
+ end
21
+ end
22
+
17
23
  def get_storage_item(pallet_name, item_name, metadata)
18
24
  pallet = get_module(pallet_name, metadata)
19
25
  raise "Pallet `#{pallet_name}` not found" if pallet.nil?
@@ -21,6 +27,19 @@ module Metadata
21
27
  item._get(:name) == item_name
22
28
  end
23
29
  end
30
+
31
+ def get_calls_type(pallet_name, metadata)
32
+ type_id = get_calls_type_id(pallet_name, metadata)
33
+ metadata._get(:metadata)._get(:v14)._get(:lookup)._get(:types).find do |type|
34
+ type._get(:id) == type_id
35
+ end
36
+ end
37
+
38
+ def get_calls_type_id(pallet_name, metadata)
39
+ pallet = get_module(pallet_name, metadata)
40
+ raise "Pallet `#{pallet_name}` not found" if pallet.nil?
41
+ pallet._get(:calls)._get(:type)
42
+ end
24
43
  end
25
44
 
26
45
  TYPES = {
@@ -10,6 +10,15 @@ module PortableCodec
10
10
  class VariantIndexOutOfRange < Error; end
11
11
  class VariantInvalidValue < Error; end
12
12
 
13
+ class << self
14
+ def u256(value)
15
+ bytes = ScaleRb.encode('u256', value)
16
+ bytes.each_slice(8).map do |slice|
17
+ ScaleRb.decode('u64', slice).first
18
+ end
19
+ end
20
+ end
21
+
13
22
  class << self
14
23
  # registry:
15
24
  # {
@@ -171,7 +180,7 @@ module PortableCodec
171
180
  def encode_sequence(sequence_type, value, registry)
172
181
  inner_type_id = sequence_type._get(:type)
173
182
  length_bytes = encode_compact(value.length)
174
- length_bytes + _encode_types([inner_type_id] * array.length, value, registry)
183
+ length_bytes + _encode_types([inner_type_id] * value.length, value, registry)
175
184
  end
176
185
 
177
186
  # tuple_type: [type_id1, type_id2, ...]
@@ -230,7 +239,10 @@ module PortableCodec
230
239
 
231
240
  item = variants.find { |var| var._get(:name) == name }
232
241
  raise VariantItemNotFound, "type: #{variant_type}, name: #{name}" if item.nil?
233
- raise VariantInvalidValue, "type: #{variant_type}, v: #{v}" if item._get(:fields).length != v.length
242
+
243
+ # if the variant item has more than one field, the value must be a hash with the same length.
244
+ # if the variant item has only one field, that means the field is a type id point to a composite. TODO: check the type's fields length
245
+ raise VariantFieldsLengthNotMatch, "type: #{variant_type}, \nvalue: #{v}" if item._get(:fields).length > 1 && item._get(:fields).length != v.length
234
246
 
235
247
  ScaleRb.encode_uint('u8', item._get(:index)) + encode_composite(item, v, registry)
236
248
  end
@@ -1,3 +1,3 @@
1
1
  module ScaleRb
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-04 00:00:00.000000000 Z
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blake2b_rs