class Eth::Tx::Eip1559
def decode(hex)
-
(DecoderError)
- if transaction decoding fails. -
(ParameterError)
- if transaction is missing fields. -
(TransactionTypeError)
- if transaction type is invalid.
Returns:
-
(Eth::Tx::Eip1559)
- 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_1559 bin = Util.hex_to_bin hex[2..] tx = Rlp.decode bin # decoded transactions always have 9 + 3 fields, even if they are empty or zero raise ParameterError, "Transaction missing fields!" if tx.size < 9 # populate the 9 payload fields chain_id = Util.deserialize_big_endian_to_int tx[0] nonce = Util.deserialize_big_endian_to_int tx[1] priority_fee = Util.deserialize_big_endian_to_int tx[2] max_gas_fee = Util.deserialize_big_endian_to_int tx[3] gas_limit = Util.deserialize_big_endian_to_int tx[4] to = Util.bin_to_hex tx[5] value = Util.deserialize_big_endian_to_int tx[6] data = tx[7] access_list = tx[8] # populate class attributes @chain_id = chain_id.to_i @signer_nonce = nonce.to_i @max_priority_fee_per_gas = priority_fee.to_i @max_fee_per_gas = max_gas_fee.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 == 9 _set_signature(nil, 0, 0) elsif tx.size == 12 recovery_id = Util.bin_to_hex(tx[9]).to_i(16) r = Util.bin_to_hex tx[10] s = Util.bin_to_hex tx[11] # 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-1559 payload!" end # last but not least, set the type. @type = TYPE_1559 # recover sender address v = Chain.to_v recovery_id, chain_id public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v.to_s(16)}", chain_id) address = Util.public_key_to_address(public_key).to_s @sender = Tx.sanitize_address address end