Class: Scale::Types::GenericAddress

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/scale/types.rb

Direct Known Subclasses

AccountIdAddress, Address, RawAddress

Instance Attribute Summary

Attributes included from Base

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#==, included, #initialize, #to_human

Class Method Details

.decode(scale_bytes) ⇒ Object

github.com/paritytech/substrate/wiki/External-Address-Format-(SS58) base58encode ( concat ( <address-type>, <address>, <checksum> ) )

^^^^^^^^^

the <address> is 32 byte account id or 1, 2, 4, 8 byte account index scale_bytes: account length byte + <address>‘s bytes



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
# File 'lib/scale/types.rb', line 294

def self.decode(scale_bytes)
  puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
   = scale_bytes.get_next_bytes(1).first

  if  == 0xff # 32 bytes address(Public key)
     = scale_bytes.get_next_bytes(32).bytes_to_hex
     = [].bytes_to_hex

    puts "  END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
    new({
      account_id: ,
      account_length: 
    })
  else
     =
      if  == 0xfc # 2 bytes address(account index)
        scale_bytes.get_next_bytes(2).bytes_to_hex
      elsif  == 0xfd # 4 bytes address(account index)
        scale_bytes.get_next_bytes(4).bytes_to_hex
      elsif  == 0xfe # 8 bytes address(account index)
        scale_bytes.get_next_bytes(8).bytes_to_hex
      else
        [].bytes_to_hex
      end
    # TODO: add account_idx 
     = [].bytes_to_hex

    puts "  END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
    new({
      account_index: ,
      account_length: 
    })
  end
end

Instance Method Details

#encodeObject



329
330
331
332
333
334
335
# File 'lib/scale/types.rb', line 329

def encode
  if self.value[:account_id]
    "#{self.value[:account_length][2..]}#{self.value[:account_id][2..]}"
  else
    "#{self.value[:account_length][2..]}#{self.value[:account_index][2..]}"
  end
end