class Eth::Tx::Eip2930

def decode(hex)

Raises:
  • (DecoderError) - if transaction decoding fails.
  • (ParameterError) - if transaction is missing fields.
  • (TransactionTypeError) - if transaction type is invalid.

Returns:
  • (Eth::Tx::Eip2930) - transaction payload.

Parameters:
  • hex (String) -- the raw transaction hex-string.
def decode(hex)
  hex = Util.remove_hex_prefix hex
  type = hex[0, 2]
  raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_2930
  bin = Util.hex_to_bin hex[2..]
  tx = Rlp.decode bin
  # decoded transactions always have 8 + 3 fields, even if they are empty or zero
  raise ParameterError, "Transaction missing fields!" if tx.size < 8
  # populate the 8 payload fields
  chain_id = Util.deserialize_big_endian_to_int tx[0]
  nonce = Util.deserialize_big_endian_to_int tx[1]
  gas_price = Util.deserialize_big_endian_to_int tx[2]
  gas_limit = Util.deserialize_big_endian_to_int tx[3]
  to = Util.bin_to_hex tx[4]
  value = Util.deserialize_big_endian_to_int tx[5]
  data = tx[6]
  access_list = tx[7]
  # populate class attributes
  @chain_id = chain_id.to_i
  @signer_nonce = nonce.to_i
  @gas_price = gas_price.to_i
  @gas_limit = gas_limit.to_i
  @destination = to.to_s
  @amount = value.to_i
  @payload = data
  @access_list = access_list
  # populate the 3 signature fields
  if tx.size == 8
    _set_signature(nil, 0, 0)
  elsif tx.size == 11
    recovery_id = Util.bin_to_hex(tx[8]).to_i(16)
    r = Util.bin_to_hex tx[9]
    s = Util.bin_to_hex tx[10]
    # allows us to force-setting a signature if the transaction is signed already
    _set_signature(recovery_id, r, s)
  else
    raise_error DecoderError, "Cannot decode EIP-2930 payload!"
  end
  # last but not least, set the type.
  @type = TYPE_2930
  # recover sender address
  v = Chain.to_v recovery_id, chain_id
  public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
  address = Util.public_key_to_address(public_key).to_s
  @sender = Tx.sanitize_address address
end