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.

41
@final
42
@dataclass
class IntrinsicGasCost:

execution

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

46
    execution: ExecutionGas

calldata_floor

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

49
    calldata_floor: ExecutionGas

BLOB_COUNT_LIMIT

Maximum number of blobs a single transaction may carry.

59
BLOB_COUNT_LIMIT = 6

VERSIONED_HASH_VERSION_KZG

Version byte that every blob versioned hash must start with.

64
VERSIONED_HASH_VERSION_KZG = b"\x01"

ACCESS_LIST_ADDRESS_FLOOR_TOKENS

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

69
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.

77
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.

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

nonce

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

101
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

106
    gas_price: Uint

gas

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

111
    gas: Uint

to

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

116
    to: Bytes0 | Address

value

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

122
    value: U256

data

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

127
    data: Bytes

v

The recovery id of the signature.

133
    v: U256

r

The first part of the signature.

138
    r: U256

s

The second part of the signature.

143
    s: U256

Access

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

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

account

The address of the account that is accessed.

158
    account: Address

slots

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

163
    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.

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

chain_id

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

183
    chain_id: U64

nonce

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

188
    nonce: U256

gas_price

The price of gas for this transaction.

193
    gas_price: Uint

gas

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

198
    gas: Uint

to

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

203
    to: Bytes0 | Address

value

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

209
    value: U256

data

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

214
    data: Bytes

access_list

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

220
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

226
    y_parity: U256

r

The first part of the signature.

231
    r: U256

s

The second part of the signature.

236
    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.

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

chain_id

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

255
    chain_id: U64

nonce

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

260
    nonce: U256

max_priority_fee_per_gas

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

265
    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.

270
    max_fee_per_gas: Uint

gas

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

276
    gas: Uint

to

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

281
    to: Bytes0 | Address

value

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

287
    value: U256

data

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

292
    data: Bytes

access_list

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

298
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

304
    y_parity: U256

r

The first part of the signature.

309
    r: U256

s

The second part of the signature.

314
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

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

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

chain_id

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

333
    chain_id: U64

nonce

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

338
    nonce: U256

max_priority_fee_per_gas

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

343
    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.

348
    max_fee_per_gas: Uint

gas

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

354
    gas: Uint

to

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

359
    to: Address

value

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

365
    value: U256

data

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

370
    data: Bytes

access_list

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

376
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

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

382
    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.

387
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

393
    y_parity: U256

r

The first part of the signature.

398
    r: U256

s

The second part of the signature.

403
    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.

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

chain_id

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

422
    chain_id: U64

nonce

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

427
    nonce: U64

max_priority_fee_per_gas

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

432
    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.

437
    max_fee_per_gas: Uint

gas

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

443
    gas: Uint

to

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

448
    to: Address

value

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

454
    value: U256

data

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

459
    data: Bytes

access_list

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

465
    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.

471
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

477
    y_parity: U256

r

The first part of the signature.

482
    r: U256

s

The second part of the signature.

487
    s: U256

Transaction

Union type representing any valid transaction type.

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

AccessListCapableTransaction

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

See has_access_list and Access for more details.

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

FeeMarketCapableTransaction

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

See FeeMarketTransaction for more details.

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

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:
536
    <snip>
543
    if isinstance(tx, LegacyTransaction):
544
        return tx
545
    elif isinstance(tx, AccessListTransaction):
546
        return b"\x01" + rlp.encode(tx)
547
    elif isinstance(tx, FeeMarketTransaction):
548
        return b"\x02" + rlp.encode(tx)
549
    elif isinstance(tx, BlobTransaction):
550
        return b"\x03" + rlp.encode(tx)
551
    elif isinstance(tx, SetCodeTransaction):
552
        return b"\x04" + rlp.encode(tx)
553
    else:
554
        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:
558
    <snip>
569
    if isinstance(tx, Bytes):
570
        if tx[0] == 1:
571
            return rlp.decode_to(AccessListTransaction, tx[1:])
572
        elif tx[0] == 2:
573
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
574
        elif tx[0] == 3:
575
            return rlp.decode_to(BlobTransaction, tx[1:])
576
        elif tx[0] == 4:
577
            return rlp.decode_to(SetCodeTransaction, tx[1:])
578
        elif tx[0] >= 0xC0:
579
            assert tx[0] <= 0xFE
580
            return rlp.decode_to(LegacyTransaction, tx)
581
        else:
582
            raise TransactionTypeError(tx[0])
583
    else:
584
        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.

The gas limit of a transaction may not exceed TX_MAX_TOTAL_GAS_LIMIT (EIP-8037). TX_MAX_GAS_LIMIT (EIP-7825) bounds only the execution-gas a transaction can spend, so it is checked against the intrinsic execution cost rather than against the gas limit.

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, a TransactionGasLimitExceededError if the gas limit exceeds TX_MAX_TOTAL_GAS_LIMIT, 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:
588
    <snip>  # noqa: E501
629
    from .vm.gas import GasCosts
630
    from .vm.interpreter import MAX_INIT_CODE_SIZE
631
632
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
633
        raise NonceOverflowError("Nonce too high")
634
635
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
636
        raise InitCodeTooLargeError("Code size too large")
637
638
    if tx.gas > GasCosts.TX_MAX_TOTAL_GAS_LIMIT:
639
        raise TransactionGasLimitExceededError("Gas limit too high")
640
641
    if isinstance(tx, FeeMarketCapableTransaction):
642
        if tx.max_fee_per_gas < tx.max_priority_fee_per_gas:
643
            raise PriorityFeeGreaterThanMaxFeeError(
644
                "priority fee greater than max fee"
645
            )
646
647
    if isinstance(tx, BlobTransaction):
648
        blob_count = len(tx.blob_versioned_hashes)
649
        if blob_count == 0:
650
            raise NoBlobDataError("no blob data in transaction")
651
        if blob_count > BLOB_COUNT_LIMIT:
652
            raise BlobCountExceededError(
653
                f"Tx has {blob_count} blobs. Max allowed: {BLOB_COUNT_LIMIT}"
654
            )
655
        for blob_versioned_hash in tx.blob_versioned_hashes:
656
            if blob_versioned_hash[0:1] != VERSIONED_HASH_VERSION_KZG:
657
                raise InvalidBlobVersionedHashError(
658
                    "invalid blob versioned hash"
659
                )
660
661
    if isinstance(tx, (BlobTransaction, SetCodeTransaction)):
662
        if not isinstance(tx.to, Address):
663
            raise TransactionTypeContractCreationError(tx)
664
665
    if isinstance(tx, SetCodeTransaction):
666
        if not any(tx.authorizations):
667
            raise EmptyAuthorizationListError("empty authorization list")
668
669
    intrinsic = calculate_intrinsic_cost(tx, sender)
670
    intrinsic_gas = Uint(intrinsic.execution)
671
    if intrinsic_gas > tx.gas:
672
        raise InsufficientTransactionGasError("Insufficient intrinsic gas")
673
    if intrinsic.calldata_floor > tx.gas:
674
        raise InsufficientTransactionGasError("Insufficient calldata floor")
675
    if intrinsic.execution > GasCosts.TX_MAX_GAS_LIMIT:
676
        raise InsufficientTransactionGasError(
677
            "Intrinsic execution gas exceeds TX_MAX_GAS_LIMIT"
678
        )
679
    if intrinsic.calldata_floor > GasCosts.TX_MAX_GAS_LIMIT:
680
        raise InsufficientTransactionGasError(
681
            "Intrinsic calldata floor exceeds TX_MAX_GAS_LIMIT"
682
        )
683
684
    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:
690
    <snip>
728
    from .vm.gas import GasCosts, init_code_cost
729
730
    tokens_in_calldata = count_tokens_in_data(tx.data)
731
732
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
733
734
    is_create = tx.to == Bytes0(b"")
735
    is_self_transfer = tx.to == sender
736
737
    recipient_execution_gas = Uint(0)
738
    init_code_gas = Uint(0)
739
    if is_create:
740
        recipient_execution_gas = GasCosts.CREATE_ACCESS
741
        init_code_gas = init_code_cost(ulen(tx.data))
742
    elif not is_self_transfer:
743
        recipient_execution_gas = GasCosts.COLD_ACCOUNT_ACCESS
744
        if tx.value > U256(0):
745
            recipient_execution_gas += GasCosts.TX_VALUE_COST
746
747
    access_list_cost = Uint(0)
748
    tokens_in_access_list = Uint(0)
749
    if has_access_list(tx):
750
        for access in tx.access_list:
751
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
752
            access_list_cost += (
753
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
754
            )
755
            tokens_in_access_list += ACCESS_LIST_ADDRESS_FLOOR_TOKENS
756
            tokens_in_access_list += (
757
                ulen(access.slots) * ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS
758
            )
759
760
    # Charge the access list data surcharge on both sides of the gas-used
761
    # maximum, independently of the existing per-entry access charges.
762
    access_list_data_cost = (
763
        tokens_in_access_list * GasCosts.TX_DATA_TOKEN_FLOOR
764
    )
765
766
    auth_cost = Uint(0)
767
    if isinstance(tx, SetCodeTransaction):
768
        auth_cost = GasCosts.EXECUTION_PER_AUTH_BASE_COST * ulen(
769
            tx.authorizations
770
        )
771
772
    # EIP-7976 floor tokens: all calldata bytes count uniformly.
773
    floor_tokens_in_calldata = ulen(tx.data) * GasCosts.TX_DATA_TOKEN_STANDARD
774
775
    # Decomposed execution-gas intrinsic base (EIP-2780), which also
776
    # anchors the calldata floor.
777
    base_execution_gas = GasCosts.TX_BASE + recipient_execution_gas
778
779
    # Floor gas cost (EIP-7623: minimum gas for data-heavy transactions).
780
    data_floor_gas_cost = (
781
        base_execution_gas
782
        + floor_tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR
783
        + access_list_data_cost
784
    )
785
786
    return IntrinsicGasCost(
787
        execution=ExecutionGas(
788
            base_execution_gas
789
            + init_code_gas
790
            + data_cost
791
            + access_list_cost
792
            + access_list_data_cost
793
            + auth_cost
794
        ),
795
        calldata_floor=ExecutionGas(data_floor_gas_cost),
796
    )

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:
800
    <snip>
805
    num_zeros = Uint(data.count(0))
806
    num_non_zeros = ulen(data) - num_zeros
807
808
    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:
814
    <snip>
823
    if isinstance(tx, FeeMarketCapableTransaction):
824
        if tx.max_fee_per_gas < base_fee_per_gas:
825
            raise InsufficientMaxFeePerGasError(
826
                tx.max_fee_per_gas, base_fee_per_gas
827
            )
828
829
        priority_fee_per_gas = min(
830
            tx.max_priority_fee_per_gas,
831
            tx.max_fee_per_gas - base_fee_per_gas,
832
        )
833
        return priority_fee_per_gas + base_fee_per_gas
834
835
    if tx.gas_price < base_fee_per_gas:
836
        raise InvalidBlock
837
    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:
841
    <snip>
845
    if isinstance(tx, FeeMarketCapableTransaction):
846
        return gas_limit * tx.max_fee_per_gas
847
    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:
851
    <snip>
854
    if sender_nonce > Uint(tx.nonce):
855
        raise NonceMismatchError("nonce too low")
856
    elif sender_nonce < Uint(tx.nonce):
857
        raise NonceMismatchError("nonce too high")

chain_id

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

def chain_id(​tx: Transaction​) -> None | U64:
861
    <snip>
866
    if isinstance(tx, LegacyTransaction):
867
        if tx.v == 27 or tx.v == 28:
868
            return None
869
870
        if tx.v < U256(35):
871
            raise InvalidSignatureError("bad v")
872
873
        return U64((tx.v - U256(35)) >> U256(1))
874
    else:
875
        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:
879
    <snip>
892
    r, s = tx.r, tx.s
893
    if U256(0) >= r or r >= SECP256K1N:
894
        raise InvalidSignatureError("bad r")
895
    if U256(0) >= s or s > SECP256K1N // U256(2):
896
        raise InvalidSignatureError("bad s")
897
898
    if isinstance(tx, LegacyTransaction):
899
        v = tx.v
900
        if v == 27 or v == 28:
901
            public_key = secp256k1_recover(
902
                r, s, v - U256(27), signing_hash_pre155(tx)
903
            )
904
        else:
905
            assert v >= U256(35), "call chain_id before recover_sender"
906
            tx_chain_id = U64((v - U256(35)) >> U256(1))
907
            v = (v - U256(35)) & U256(1)
908
            public_key = secp256k1_recover(
909
                r,
910
                s,
911
                v,
912
                signing_hash_155(tx, tx_chain_id),
913
            )
914
    elif isinstance(tx, AccessListTransaction):
915
        if tx.y_parity not in (U256(0), U256(1)):
916
            raise InvalidSignatureError("bad y_parity")
917
        public_key = secp256k1_recover(
918
            r, s, tx.y_parity, signing_hash_2930(tx)
919
        )
920
    elif isinstance(tx, FeeMarketTransaction):
921
        if tx.y_parity not in (U256(0), U256(1)):
922
            raise InvalidSignatureError("bad y_parity")
923
        public_key = secp256k1_recover(
924
            r, s, tx.y_parity, signing_hash_1559(tx)
925
        )
926
    elif isinstance(tx, BlobTransaction):
927
        if tx.y_parity not in (U256(0), U256(1)):
928
            raise InvalidSignatureError("bad y_parity")
929
        public_key = secp256k1_recover(
930
            r, s, tx.y_parity, signing_hash_4844(tx)
931
        )
932
    elif isinstance(tx, SetCodeTransaction):
933
        if tx.y_parity not in (U256(0), U256(1)):
934
            raise InvalidSignatureError("bad y_parity")
935
        public_key = secp256k1_recover(
936
            r, s, tx.y_parity, signing_hash_7702(tx)
937
        )
938
939
    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:
943
    <snip>
952
    return keccak256(
953
        rlp.encode(
954
            (
955
                tx.nonce,
956
                tx.gas_price,
957
                tx.gas,
958
                tx.to,
959
                tx.value,
960
                tx.data,
961
            )
962
        )
963
    )

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:
967
    <snip>
975
    return keccak256(
976
        rlp.encode(
977
            (
978
                tx.nonce,
979
                tx.gas_price,
980
                tx.gas,
981
                tx.to,
982
                tx.value,
983
                tx.data,
984
                chain_id,
985
                Uint(0),
986
                Uint(0),
987
            )
988
        )
989
    )

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:
993
    <snip>
1001
    return keccak256(
1002
        b"\x01"
1003
        + rlp.encode(
1004
            (
1005
                tx.chain_id,
1006
                tx.nonce,
1007
                tx.gas_price,
1008
                tx.gas,
1009
                tx.to,
1010
                tx.value,
1011
                tx.data,
1012
                tx.access_list,
1013
            )
1014
        )
1015
    )

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:
1019
    <snip>
1027
    return keccak256(
1028
        b"\x02"
1029
        + rlp.encode(
1030
            (
1031
                tx.chain_id,
1032
                tx.nonce,
1033
                tx.max_priority_fee_per_gas,
1034
                tx.max_fee_per_gas,
1035
                tx.gas,
1036
                tx.to,
1037
                tx.value,
1038
                tx.data,
1039
                tx.access_list,
1040
            )
1041
        )
1042
    )

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:
1046
    <snip>
1054
    return keccak256(
1055
        b"\x03"
1056
        + rlp.encode(
1057
            (
1058
                tx.chain_id,
1059
                tx.nonce,
1060
                tx.max_priority_fee_per_gas,
1061
                tx.max_fee_per_gas,
1062
                tx.gas,
1063
                tx.to,
1064
                tx.value,
1065
                tx.data,
1066
                tx.access_list,
1067
                tx.max_fee_per_blob_gas,
1068
                tx.blob_versioned_hashes,
1069
            )
1070
        )
1071
    )

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:
1075
    <snip>
1083
    return keccak256(
1084
        b"\x04"
1085
        + rlp.encode(
1086
            (
1087
                tx.chain_id,
1088
                tx.nonce,
1089
                tx.max_priority_fee_per_gas,
1090
                tx.max_fee_per_gas,
1091
                tx.gas,
1092
                tx.to,
1093
                tx.value,
1094
                tx.data,
1095
                tx.access_list,
1096
                tx.authorizations,
1097
            )
1098
        )
1099
    )

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:
1103
    <snip>
1111
    assert isinstance(tx, (LegacyTransaction, Bytes))
1112
    if isinstance(tx, LegacyTransaction):
1113
        return keccak256(rlp.encode(tx))
1114
    else:
1115
        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]:
1121
    <snip>
1126
    return isinstance(
1127
        tx,
1128
        AccessListCapableTransaction,
1129
    )