ethereum.forks.amsterdam.transactions

Transactions are atomic units of work created externally to Ethereum and submitted to be executed. If Ethereum is viewed as a state machine, transactions are the events that move between states.

IntrinsicGasCost

Intrinsic gas costs for a transaction, split by gas type.

40
@final
41
@dataclass
class IntrinsicGasCost:

execution

Execution gas (calldata, base cost, access list, etc.).

45
    execution: ExecutionGas

calldata_floor

Minimum gas cost based on calldata size per EIP-7623, including the access list data surcharge per EIP-7981.

48
    calldata_floor: ExecutionGas

BLOB_COUNT_LIMIT

Maximum number of blobs a single transaction may carry.

58
BLOB_COUNT_LIMIT = 6

VERSIONED_HASH_VERSION_KZG

Version byte that every blob versioned hash must start with.

63
VERSIONED_HASH_VERSION_KZG = b"\x01"

ACCESS_LIST_ADDRESS_FLOOR_TOKENS

Floor data tokens contributed by a single access list address per EIP-7981.

68
ACCESS_LIST_ADDRESS_FLOOR_TOKENS = Uint(80)

ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS

Floor data tokens contributed by a single access list storage key per EIP-7981.

76
ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS = Uint(128)

LegacyTransaction

Atomic operation performed on the block chain. This represents the original transaction format used before EIP-1559, EIP-2930, EIP-4844, and EIP-7702.

85
@final
86
@slotted_freezable
87
@dataclass
class LegacyTransaction:

nonce

A scalar value equal to the number of transactions sent by the sender.

100
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

105
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

110
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

115
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

121
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

126
    data: Bytes

v

The recovery id of the signature.

132
    v: U256

r

The first part of the signature.

137
    r: U256

s

The second part of the signature.

142
    s: U256

Access

A mapping from account address to storage slots that are pre-warmed as part of a transaction.

148
@final
149
@slotted_freezable
150
@dataclass
class Access:

account

The address of the account that is accessed.

157
    account: Address

slots

A tuple of storage slots that are accessed in the account.

162
    slots: Tuple[Bytes32, ...]

AccessListTransaction

The transaction type added in EIP-2930 to support access lists.

This transaction type extends the legacy transaction with an access list and chain ID. The access list specifies which addresses and storage slots the transaction will access.

168
@final
169
@slotted_freezable
170
@dataclass
class AccessListTransaction:

chain_id

The ID of the chain on which this transaction is executed.

182
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

187
    nonce: U256

gas_price

The price of gas for this transaction.

192
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

197
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

202
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

208
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

213
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

219
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

225
    y_parity: U256

r

The first part of the signature.

230
    r: U256

s

The second part of the signature.

235
    s: U256

FeeMarketTransaction

The transaction type added in EIP-1559.

This transaction type introduces a new fee market mechanism with two gas price parameters: max_priority_fee_per_gas and max_fee_per_gas.

241
@final
242
@slotted_freezable
243
@dataclass
class FeeMarketTransaction:

chain_id

The ID of the chain on which this transaction is executed.

254
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

259
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

264
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

269
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

275
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

280
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

286
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

291
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

297
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

303
    y_parity: U256

r

The first part of the signature.

308
    r: U256

s

The second part of the signature.

313
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

This transaction type extends the fee market transaction to support blob-carrying transactions.

319
@final
320
@slotted_freezable
321
@dataclass
class BlobTransaction:

chain_id

The ID of the chain on which this transaction is executed.

332
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

337
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

342
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

347
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

353
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

358
    to: Address

value

The amount of ether (in wei) to send with this transaction.

364
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

369
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

375
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

The maximum fee per blob gas that the sender is willing to pay.

381
    max_fee_per_blob_gas: U256

blob_versioned_hashes

A tuple of objects that represent the versioned hashes of the blobs included in the transaction.

386
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

392
    y_parity: U256

r

The first part of the signature.

397
    r: U256

s

The second part of the signature.

402
    s: U256

SetCodeTransaction

The transaction type added in EIP-7702.

This transaction type allows Ethereum Externally Owned Accounts (EOAs) to set code on their account, enabling them to act as smart contracts.

408
@final
409
@slotted_freezable
410
@dataclass
class SetCodeTransaction:

chain_id

The ID of the chain on which this transaction is executed.

421
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

426
    nonce: U64

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

431
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

436
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

442
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

447
    to: Address

value

The amount of ether (in wei) to send with this transaction.

453
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

458
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

464
    access_list: Tuple[Access, ...]

authorizations

A tuple of Authorization objects that specify what code the signer desires to execute in the context of their EOA.

470
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

476
    y_parity: U256

r

The first part of the signature.

481
    r: U256

s

The second part of the signature.

486
    s: U256

Transaction

Union type representing any valid transaction type.

492
Transaction = (
493
    LegacyTransaction
494
    | AccessListTransaction
495
    | FeeMarketTransaction
496
    | BlobTransaction
497
    | SetCodeTransaction
498
)

AccessListCapableTransaction

Transaction types that include an EIP-2930-style access list.

See has_access_list and Access for more details.

504
AccessListCapableTransaction = (
505
    AccessListTransaction
506
    | FeeMarketTransaction
507
    | BlobTransaction
508
    | SetCodeTransaction
509
)

FeeMarketCapableTransaction

Transaction types that include the EIP-1559-style fee structure.

See FeeMarketTransaction for more details.

521
FeeMarketCapableTransaction = (
522
    FeeMarketTransaction | BlobTransaction | SetCodeTransaction
523
)

encode_transaction

Encode a transaction into its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Legacy transactions are returned as-is, while other transaction types are prefixed with their type identifier and RLP encoded.

def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
535
    <snip>
542
    if isinstance(tx, LegacyTransaction):
543
        return tx
544
    elif isinstance(tx, AccessListTransaction):
545
        return b"\x01" + rlp.encode(tx)
546
    elif isinstance(tx, FeeMarketTransaction):
547
        return b"\x02" + rlp.encode(tx)
548
    elif isinstance(tx, BlobTransaction):
549
        return b"\x03" + rlp.encode(tx)
550
    elif isinstance(tx, SetCodeTransaction):
551
        return b"\x04" + rlp.encode(tx)
552
    else:
553
        raise Exception(f"Unable to encode transaction of type {type(tx)}")

decode_transaction

Decode a transaction from its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Accept a LegacyTransaction object (returned as-is) or raw bytes.

EIP-2718 states that the first byte distinguishes the format: [0x00, 0x7f] is a typed transaction, [0xc0, 0xfe] is a legacy transaction (RLP list prefix).

def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
557
    <snip>
568
    if isinstance(tx, Bytes):
569
        if tx[0] == 1:
570
            return rlp.decode_to(AccessListTransaction, tx[1:])
571
        elif tx[0] == 2:
572
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
573
        elif tx[0] == 3:
574
            return rlp.decode_to(BlobTransaction, tx[1:])
575
        elif tx[0] == 4:
576
            return rlp.decode_to(SetCodeTransaction, tx[1:])
577
        elif tx[0] >= 0xC0:
578
            assert tx[0] <= 0xFE
579
            return rlp.decode_to(LegacyTransaction, tx)
580
        else:
581
            raise TransactionTypeError(tx[0])
582
    else:
583
        return tx

validate_transaction

Verifies a transaction.

The gas in a transaction gets used to pay for the intrinsic cost of operations, therefore if there is insufficient gas then it would not be possible to execute a transaction and it will be declared invalid.

Additionally, the nonce of a transaction must not equal or exceed the limit defined in EIP-2681. In practice, defining the limit as 2**64-1 has no impact because sending 2**64-1 transactions is improbable. It's not strictly impossible though, 2**64-1 transactions is the entire capacity of the Ethereum blockchain at 2022 gas limits for a little over 22 years.

Also, the code size of a contract creation transaction must be within limits of the protocol.

This function takes a transaction and gas_limit as parameters and returns the intrinsic gas costs for the transaction after validation. It throws an InsufficientTransactionGasError exception if the transaction does not provide enough gas to cover the intrinsic cost, and a NonceOverflowError exception if the nonce overflows. It also raises an InitCodeTooLargeError if the code size of a contract creation transaction exceeds the maximum allowed size, and a PriorityFeeGreaterThanMaxFeeError if the maximum priority fee per gas of a fee market transaction exceeds its maximum fee per gas.

def validate_transaction(tx: Transaction, ​​sender: Address) -> IntrinsicGasCost:
587
    <snip>
618
    from .vm.gas import GasCosts
619
    from .vm.interpreter import MAX_INIT_CODE_SIZE
620
621
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
622
        raise NonceOverflowError("Nonce too high")
623
624
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
625
        raise InitCodeTooLargeError("Code size too large")
626
627
    if isinstance(tx, FeeMarketCapableTransaction):
628
        if tx.max_fee_per_gas < tx.max_priority_fee_per_gas:
629
            raise PriorityFeeGreaterThanMaxFeeError(
630
                "priority fee greater than max fee"
631
            )
632
633
    if isinstance(tx, BlobTransaction):
634
        blob_count = len(tx.blob_versioned_hashes)
635
        if blob_count == 0:
636
            raise NoBlobDataError("no blob data in transaction")
637
        if blob_count > BLOB_COUNT_LIMIT:
638
            raise BlobCountExceededError(
639
                f"Tx has {blob_count} blobs. Max allowed: {BLOB_COUNT_LIMIT}"
640
            )
641
        for blob_versioned_hash in tx.blob_versioned_hashes:
642
            if blob_versioned_hash[0:1] != VERSIONED_HASH_VERSION_KZG:
643
                raise InvalidBlobVersionedHashError(
644
                    "invalid blob versioned hash"
645
                )
646
647
    if isinstance(tx, (BlobTransaction, SetCodeTransaction)):
648
        if not isinstance(tx.to, Address):
649
            raise TransactionTypeContractCreationError(tx)
650
651
    if isinstance(tx, SetCodeTransaction):
652
        if not any(tx.authorizations):
653
            raise EmptyAuthorizationListError("empty authorization list")
654
655
    intrinsic = calculate_intrinsic_cost(tx, sender)
656
    intrinsic_gas = Uint(intrinsic.execution)
657
    if intrinsic_gas > tx.gas:
658
        raise InsufficientTransactionGasError("Insufficient intrinsic gas")
659
    if intrinsic.calldata_floor > tx.gas:
660
        raise InsufficientTransactionGasError("Insufficient calldata floor")
661
    if intrinsic.execution > GasCosts.TX_MAX_GAS_LIMIT:
662
        raise InsufficientTransactionGasError(
663
            "Intrinsic execution gas exceeds TX_MAX_GAS_LIMIT"
664
        )
665
    if intrinsic.calldata_floor > GasCosts.TX_MAX_GAS_LIMIT:
666
        raise InsufficientTransactionGasError(
667
            "Intrinsic calldata floor exceeds TX_MAX_GAS_LIMIT"
668
        )
669
670
    return intrinsic

calculate_intrinsic_cost

Calculate the gas charged before execution starts and the data floor.

The intrinsic cost of the transaction is charged before execution has begun. Functions/operations in the EVM cost money to execute so this intrinsic cost is for the operations that need to be paid for as part of the transaction. Data transfer, for example, is part of this intrinsic cost. It costs ether to send data over the wire and that ether is accounted for in the intrinsic cost calculated in this function. This intrinsic cost must be calculated and paid for before execution in order for all operations to be implemented.

The intrinsic cost includes:

  1. Sender cost (TX_BASE).

  2. Recipient cost (COLD_ACCOUNT_ACCESS for a non-self-transfer call, or CREATE_ACCESS for a contract creation). The created account's NEW_ACCOUNT state gas is state-dependent and is charged at the top frame, not here.

  3. Value cost (TX_VALUE_COST for a non-self-transfer call) when tx.value > 0.

  4. Calldata cost (zero and non-zero bytes).

  5. Access list entry charges and the data surcharge (if applicable).

  6. Authorizations (if applicable): only the state-independent base cost (EXECUTION_PER_AUTH_BASE_COST) per tuple. The state-dependent account-creation and delegation-write costs are charged at the top frame by set_delegation.

Self-transfers (sender == tx.to) skip the recipient and value charges.

This function takes a transaction and its sender as parameters and returns the intrinsic execution gas cost and the minimum (floor) gas cost based on the calldata size and access list data surcharge. The surcharge is added to both costs, so it is charged regardless of which side determines the gas used. The floor is anchored on the execution-gas portion of items 1 to 3 above rather than TX_BASE alone, so it never undercuts the transaction's own intrinsic base.

def calculate_intrinsic_cost(tx: Transaction, ​​sender: Address) -> IntrinsicGasCost:
676
    <snip>
714
    from .vm.gas import GasCosts, init_code_cost
715
716
    tokens_in_calldata = count_tokens_in_data(tx.data)
717
718
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
719
720
    is_create = tx.to == Bytes0(b"")
721
    is_self_transfer = tx.to == sender
722
723
    recipient_execution_gas = Uint(0)
724
    init_code_gas = Uint(0)
725
    if is_create:
726
        recipient_execution_gas = GasCosts.CREATE_ACCESS
727
        init_code_gas = init_code_cost(ulen(tx.data))
728
    elif not is_self_transfer:
729
        recipient_execution_gas = GasCosts.COLD_ACCOUNT_ACCESS
730
        if tx.value > U256(0):
731
            recipient_execution_gas += GasCosts.TX_VALUE_COST
732
733
    access_list_cost = Uint(0)
734
    tokens_in_access_list = Uint(0)
735
    if has_access_list(tx):
736
        for access in tx.access_list:
737
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
738
            access_list_cost += (
739
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
740
            )
741
            tokens_in_access_list += ACCESS_LIST_ADDRESS_FLOOR_TOKENS
742
            tokens_in_access_list += (
743
                ulen(access.slots) * ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS
744
            )
745
746
    # Charge the access list data surcharge on both sides of the gas-used
747
    # maximum, independently of the existing per-entry access charges.
748
    access_list_data_cost = (
749
        tokens_in_access_list * GasCosts.TX_DATA_TOKEN_FLOOR
750
    )
751
752
    auth_cost = Uint(0)
753
    if isinstance(tx, SetCodeTransaction):
754
        auth_cost = GasCosts.EXECUTION_PER_AUTH_BASE_COST * ulen(
755
            tx.authorizations
756
        )
757
758
    # EIP-7976 floor tokens: all calldata bytes count uniformly.
759
    floor_tokens_in_calldata = ulen(tx.data) * GasCosts.TX_DATA_TOKEN_STANDARD
760
761
    # Decomposed execution-gas intrinsic base (EIP-2780), which also
762
    # anchors the calldata floor.
763
    base_execution_gas = GasCosts.TX_BASE + recipient_execution_gas
764
765
    # Floor gas cost (EIP-7623: minimum gas for data-heavy transactions).
766
    data_floor_gas_cost = (
767
        base_execution_gas
768
        + floor_tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR
769
        + access_list_data_cost
770
    )
771
772
    return IntrinsicGasCost(
773
        execution=ExecutionGas(
774
            base_execution_gas
775
            + init_code_gas
776
            + data_cost
777
            + access_list_cost
778
            + access_list_data_cost
779
            + auth_cost
780
        ),
781
        calldata_floor=ExecutionGas(data_floor_gas_cost),
782
    )

count_tokens_in_data

Count the data tokens in arbitrary input bytes.

Zero bytes count as 1 token; non-zero bytes count as 4 tokens.

def count_tokens_in_data(data: bytes) -> Uint:
786
    <snip>
791
    num_zeros = Uint(data.count(0))
792
    num_non_zeros = ulen(data) - num_zeros
793
794
    return num_zeros + num_non_zeros * Uint(4)

calculate_effective_gas_price

Calculate the price per unit of gas the transaction actually pays.

A fee-market transaction pays the base fee plus a priority fee capped by both of its fee caps; its maximum fee must cover the base fee, or an InsufficientMaxFeePerGasError is raised. A transaction priced with a plain gas price pays that price outright, which must likewise cover the base fee.

def calculate_effective_gas_price(tx: Transaction, ​​base_fee_per_gas: Uint) -> Uint:
800
    <snip>
809
    if isinstance(tx, FeeMarketCapableTransaction):
810
        if tx.max_fee_per_gas < base_fee_per_gas:
811
            raise InsufficientMaxFeePerGasError(
812
                tx.max_fee_per_gas, base_fee_per_gas
813
            )
814
815
        priority_fee_per_gas = min(
816
            tx.max_priority_fee_per_gas,
817
            tx.max_fee_per_gas - base_fee_per_gas,
818
        )
819
        return priority_fee_per_gas + base_fee_per_gas
820
821
    if tx.gas_price < base_fee_per_gas:
822
        raise InvalidBlock
823
    return tx.gas_price

calculate_max_gas_fee

Calculate the largest execution-gas fee the transaction can incur: gas_limit priced at the transaction's fee cap.

def calculate_max_gas_fee(tx: Transaction, ​​gas_limit: Uint) -> Uint:
827
    <snip>
831
    if isinstance(tx, FeeMarketCapableTransaction):
832
        return gas_limit * tx.max_fee_per_gas
833
    return gas_limit * tx.gas_price

check_nonce

Check that the transaction's nonce equals the sender's next nonce.

def check_nonce(tx: Transaction, ​​sender_nonce: Uint) -> None:
837
    <snip>
840
    if sender_nonce > Uint(tx.nonce):
841
        raise NonceMismatchError("nonce too low")
842
    elif sender_nonce < Uint(tx.nonce):
843
        raise NonceMismatchError("nonce too high")

chain_id

Extract the chain identifier from a transaction. See EIP-155.

def chain_id(tx: Transaction) -> None | U64:
847
    <snip>
852
    if isinstance(tx, LegacyTransaction):
853
        if tx.v == 27 or tx.v == 28:
854
            return None
855
856
        if tx.v < U256(35):
857
            raise InvalidSignatureError("bad v")
858
859
        return U64((tx.v - U256(35)) >> U256(1))
860
    else:
861
        return tx.chain_id

recover_sender

Extracts the sender address from a transaction.

The v, r, and s values are the three parts that make up the signature of a transaction. In order to recover the sender of a transaction the two components needed are the signature (v, r, and s) and the signing hash of the transaction. The sender's public key can be obtained with these two values and therefore the sender address can be retrieved.

This function takes chain_id and a transaction as parameters and returns the address of the sender of the transaction. It raises an InvalidSignatureError if the signature values (r, s, v) are invalid.

def recover_sender(tx: Transaction) -> Address:
865
    <snip>
878
    r, s = tx.r, tx.s
879
    if U256(0) >= r or r >= SECP256K1N:
880
        raise InvalidSignatureError("bad r")
881
    if U256(0) >= s or s > SECP256K1N // U256(2):
882
        raise InvalidSignatureError("bad s")
883
884
    if isinstance(tx, LegacyTransaction):
885
        v = tx.v
886
        if v == 27 or v == 28:
887
            public_key = secp256k1_recover(
888
                r, s, v - U256(27), signing_hash_pre155(tx)
889
            )
890
        else:
891
            assert v >= U256(35), "call chain_id before recover_sender"
892
            tx_chain_id = U64((v - U256(35)) >> U256(1))
893
            v = (v - U256(35)) & U256(1)
894
            public_key = secp256k1_recover(
895
                r,
896
                s,
897
                v,
898
                signing_hash_155(tx, tx_chain_id),
899
            )
900
    elif isinstance(tx, AccessListTransaction):
901
        if tx.y_parity not in (U256(0), U256(1)):
902
            raise InvalidSignatureError("bad y_parity")
903
        public_key = secp256k1_recover(
904
            r, s, tx.y_parity, signing_hash_2930(tx)
905
        )
906
    elif isinstance(tx, FeeMarketTransaction):
907
        if tx.y_parity not in (U256(0), U256(1)):
908
            raise InvalidSignatureError("bad y_parity")
909
        public_key = secp256k1_recover(
910
            r, s, tx.y_parity, signing_hash_1559(tx)
911
        )
912
    elif isinstance(tx, BlobTransaction):
913
        if tx.y_parity not in (U256(0), U256(1)):
914
            raise InvalidSignatureError("bad y_parity")
915
        public_key = secp256k1_recover(
916
            r, s, tx.y_parity, signing_hash_4844(tx)
917
        )
918
    elif isinstance(tx, SetCodeTransaction):
919
        if tx.y_parity not in (U256(0), U256(1)):
920
            raise InvalidSignatureError("bad y_parity")
921
        public_key = secp256k1_recover(
922
            r, s, tx.y_parity, signing_hash_7702(tx)
923
        )
924
925
    return Address(keccak256(public_key)[12:32])

signing_hash_pre155

Compute the hash of a transaction used in a legacy (pre EIP-155) signature.

This function takes a legacy transaction as a parameter and returns the signing hash of the transaction.

def signing_hash_pre155(tx: LegacyTransaction) -> Hash32:
929
    <snip>
938
    return keccak256(
939
        rlp.encode(
940
            (
941
                tx.nonce,
942
                tx.gas_price,
943
                tx.gas,
944
                tx.to,
945
                tx.value,
946
                tx.data,
947
            )
948
        )
949
    )

signing_hash_155

Compute the hash of a transaction used in a EIP-155 signature.

This function takes a legacy transaction and a chain ID as parameters and returns the hash of the transaction used in an EIP-155 signature.

def signing_hash_155(tx: LegacyTransaction, ​​chain_id: U64) -> Hash32:
953
    <snip>
961
    return keccak256(
962
        rlp.encode(
963
            (
964
                tx.nonce,
965
                tx.gas_price,
966
                tx.gas,
967
                tx.to,
968
                tx.value,
969
                tx.data,
970
                chain_id,
971
                Uint(0),
972
                Uint(0),
973
            )
974
        )
975
    )

signing_hash_2930

Compute the hash of a transaction used in a EIP-2930 signature.

This function takes an access list transaction as a parameter and returns the hash of the transaction used in an EIP-2930 signature.

def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
979
    <snip>
987
    return keccak256(
988
        b"\x01"
989
        + rlp.encode(
990
            (
991
                tx.chain_id,
992
                tx.nonce,
993
                tx.gas_price,
994
                tx.gas,
995
                tx.to,
996
                tx.value,
997
                tx.data,
998
                tx.access_list,
999
            )
1000
        )
1001
    )

signing_hash_1559

Compute the hash of a transaction used in an EIP-1559 signature.

This function takes a fee market transaction as a parameter and returns the hash of the transaction used in an EIP-1559 signature.

def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
1005
    <snip>
1013
    return keccak256(
1014
        b"\x02"
1015
        + rlp.encode(
1016
            (
1017
                tx.chain_id,
1018
                tx.nonce,
1019
                tx.max_priority_fee_per_gas,
1020
                tx.max_fee_per_gas,
1021
                tx.gas,
1022
                tx.to,
1023
                tx.value,
1024
                tx.data,
1025
                tx.access_list,
1026
            )
1027
        )
1028
    )

signing_hash_4844

Compute the hash of a transaction used in an EIP-4844 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in an EIP-4844 signature.

def signing_hash_4844(tx: BlobTransaction) -> Hash32:
1032
    <snip>
1040
    return keccak256(
1041
        b"\x03"
1042
        + rlp.encode(
1043
            (
1044
                tx.chain_id,
1045
                tx.nonce,
1046
                tx.max_priority_fee_per_gas,
1047
                tx.max_fee_per_gas,
1048
                tx.gas,
1049
                tx.to,
1050
                tx.value,
1051
                tx.data,
1052
                tx.access_list,
1053
                tx.max_fee_per_blob_gas,
1054
                tx.blob_versioned_hashes,
1055
            )
1056
        )
1057
    )

signing_hash_7702

Compute the hash of a transaction used in a EIP-7702 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in a EIP-7702 signature.

def signing_hash_7702(tx: SetCodeTransaction) -> Hash32:
1061
    <snip>
1069
    return keccak256(
1070
        b"\x04"
1071
        + rlp.encode(
1072
            (
1073
                tx.chain_id,
1074
                tx.nonce,
1075
                tx.max_priority_fee_per_gas,
1076
                tx.max_fee_per_gas,
1077
                tx.gas,
1078
                tx.to,
1079
                tx.value,
1080
                tx.data,
1081
                tx.access_list,
1082
                tx.authorizations,
1083
            )
1084
        )
1085
    )

get_transaction_hash

Compute the hash of a transaction.

This function takes a transaction as a parameter and returns the keccak256 hash of the transaction. It can handle both legacy transactions and typed transactions (AccessListTransaction, FeeMarketTransaction, etc.).

def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
1089
    <snip>
1097
    assert isinstance(tx, (LegacyTransaction, Bytes))
1098
    if isinstance(tx, LegacyTransaction):
1099
        return keccak256(rlp.encode(tx))
1100
    else:
1101
        return keccak256(tx)

has_access_list

Return whether the transaction has an EIP-2930-style access list.

def has_access_list(tx: Transaction) -> TypeGuard[AccessListCapableTransaction]:
1107
    <snip>
1112
    return isinstance(
1113
        tx,
1114
        AccessListCapableTransaction,
1115
    )