scale.rb 0.2.10 → 0.2.12

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dba27c359003af9a2e6b3f23fd5baff9f971c3368909d104bbb3984cea64cea9
4
- data.tar.gz: 60c1083aa471562ce96a309046ea8372a404ea240fae53a45f0a1776ff3901b7
3
+ metadata.gz: fcfbe30b0f6a51793d9e977e362c34b213e04ee040f7d6961cde355b1e6cb8d0
4
+ data.tar.gz: 310f9fcab5186b0366e4a12da5790f77bfe81aa148684d1946d3a7f07ef57c60
5
5
  SHA512:
6
- metadata.gz: 55f235ae271e8c142b3a4e4a1e6b4dae0ee9c8cdc2a5c0774657c9d3d6ace9221de55714c5b2578eca48587e9f4da5b2e5d9ccaea6b9578c94c973659c4a8df0
7
- data.tar.gz: 2be28d69e6524cf422c8aabf7bb89128c59551d60146407673fbef97263148292e6b447853f9c74ce459920827b1082646a36021df1492f078573fc4738b0714
6
+ metadata.gz: 14545046f43e403d84a492905b9aa9928024b0310aa2ae8dca927dd98e9d0b4415dcffa5a3364e85af2e13ef1ef4808fdb704462c8687fcb30336fa631a2ca9f
7
+ data.tar.gz: 23224a2ff2578fb4c49a12bc1317ae645fb5a2237f4f8f9f312585a53b849abd78ca455c86c68f9f6c2f22c3e6dd837638e6644245c66e25674b701e39de08e5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scale.rb (0.2.10)
4
+ scale.rb (0.2.12)
5
5
  activesupport (>= 4.0.0)
6
6
  json (~> 2.3.0)
7
7
  substrate_common.rb (~> 0.1.9)
@@ -225,59 +225,71 @@ module Scale
225
225
  end
226
226
  end
227
227
 
228
- class Address
228
+ class GenericAddress
229
229
  include SingleValue
230
- attr_accessor :account_length, :account_index, :account_id, :account_idx
231
230
 
231
+ # https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)
232
+ # base58encode ( concat ( <address-type>, <address>, <checksum> ) )
233
+ # ^^^^^^^^^
234
+ # the <address> is 32 byte account id or 1, 2, 4, 8 byte account index
235
+ # scale_bytes: account length byte + <address>'s bytes
232
236
  def self.decode(scale_bytes)
233
237
  account_length = scale_bytes.get_next_bytes(1).first
234
238
 
235
- if account_length == 0xff # 255
239
+ if account_length == 0xff # 32 bytes address(Public key)
236
240
  account_id = scale_bytes.get_next_bytes(32).bytes_to_hex
237
- account_length = account_length.to_s(16)
238
- Address.new(account_id)
241
+ account_length = [account_length].bytes_to_hex
242
+
243
+ Address.new({
244
+ account_id: account_id,
245
+ account_length: account_length
246
+ })
239
247
  else
240
248
  account_index =
241
- if account_length == 0xfc
249
+ if account_length == 0xfc # 2 bytes address(account index)
242
250
  scale_bytes.get_next_bytes(2).bytes_to_hex
243
- elsif account_length == 0xfd
251
+ elsif account_length == 0xfd # 4 bytes address(account index)
244
252
  scale_bytes.get_next_bytes(4).bytes_to_hex
245
- elsif account_length == 0xfe
253
+ elsif account_length == 0xfe # 8 bytes address(account index)
246
254
  scale_bytes.get_next_bytes(8).bytes_to_hex
247
255
  else
248
256
  [account_length].bytes_to_hex
249
257
  end
250
- # account_idx =
251
- account_length = account_length.to_s(16)
252
- Address.new(account_index)
258
+ # TODO: add account_idx
259
+ account_length = [account_length].bytes_to_hex
260
+
261
+ Address.new({
262
+ account_index: account_index,
263
+ account_length: account_length
264
+ })
253
265
  end
254
266
  end
255
267
 
256
- def encode(ss58=false, addr_type=42)
257
- if value.start_with?("0x")
258
- if ss58 === true
259
- ::Address.encode(value, addr_type)
260
- else
261
- prefix = if value.length == 66
262
- "ff"
263
- elsif value.length == 6
264
- "fc"
265
- elsif value.length == 10
266
- "fd"
267
- elsif value.length == 18
268
- "fe"
269
- else
270
- ""
271
- end
272
- "#{prefix}#{value[2..]}"
273
- end
268
+ def encode
269
+ if self.value[:account_id]
270
+ "#{self.value[:account_length][2..]}#{self.value[:account_id][2..]}"
274
271
  else
275
- raise "Format error"
272
+ "#{self.value[:account_length][2..]}#{self.value[:account_index][2..]}"
276
273
  end
277
274
  end
278
275
  end
279
276
 
280
- class RawAddress < Address; end
277
+ class Address < GenericAddress; end
278
+
279
+ class RawAddress < GenericAddress; end
280
+
281
+ class AccountIdAddress < GenericAddress
282
+ def self.decode(scale_bytes)
283
+ AccountIdAddress.new({
284
+ account_id: AccountId.decode(scale_bytes).value,
285
+ account_length: "0xff"
286
+ })
287
+ end
288
+
289
+ def encode
290
+ "ff#{self.value[:account_id][2..]}"
291
+ end
292
+ end
281
293
 
282
294
  class AccountId < H256; end
283
295
 
@@ -1,3 +1,3 @@
1
1
  module Scale
2
- VERSION = "0.2.10".freeze
2
+ VERSION = "0.2.12".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wu Minzhe