class Eth::Tx::Legacy

def decode(hex)

Raises:
  • (ParameterError) - if transaction misses fields.

Returns:
  • (Eth::Tx::Legacy) - transaction object.

Parameters:
  • hex (String) -- the raw transaction hex-string.
def decode(hex)
  bin = Util.hex_to_bin hex
  tx = Rlp.decode bin
  # decoded transactions always have 9 fields, even if they are empty or zero
  raise ParameterError, "Transaction missing fields!" if tx.size < 9
  # populate the 9 fields
  nonce = Util.deserialize_rlp_int tx[0]
  gas_price = Util.deserialize_rlp_int tx[1]
  gas_limit = Util.deserialize_rlp_int tx[2]
  to = Util.bin_to_hex tx[3]
  value = Util.deserialize_rlp_int tx[4]
  data = tx[5]
  v = Util.bin_to_hex tx[6]
  r = Util.bin_to_hex tx[7]
  s = Util.bin_to_hex tx[8]
  # try to recover the chain id from v
  chain_id = Chain.to_chain_id Util.deserialize_rlp_int tx[6]
  # populate class attributes
  @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
  @chain_id = chain_id
  # allows us to force-setting a signature if the transaction is signed already
  _set_signature(v, r, s)
  unless chain_id.nil?
    # recover sender address
    public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v}", chain_id)
    address = Util.public_key_to_address(public_key).to_s
    @sender = Tx.sanitize_address address
  else
    # keep the 'from' field blank
    @sender = Tx.sanitize_address nil
  end
  # last but not least, set the type.
  @type = TYPE_LEGACY
end