class Scale::Types::GenericAddress

Public Class Methods

decode(scale_bytes) click to toggle source

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

# File lib/scale/types.rb, line 294
def self.decode(scale_bytes)
  puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
  account_length = scale_bytes.get_next_bytes(1).first

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

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

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

Public Instance Methods

encode() click to toggle source
# 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