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.

GAS_TX_BASE

Base cost of a transaction in gas units. This is the minimum amount of gas required to execute a transaction.

31
GAS_TX_BASE = Uint(21000)

GAS_TX_DATA_TOKEN_FLOOR

Minimum gas cost per byte of calldata as per EIP-7623. Used to calculate the minimum gas cost for transactions that include calldata.

37
GAS_TX_DATA_TOKEN_FLOOR = Uint(10)

GAS_TX_DATA_TOKEN_STANDARD

Gas cost per byte of calldata as per EIP-7623. Used to calculate the gas cost for transactions that include calldata.

45
GAS_TX_DATA_TOKEN_STANDARD = Uint(4)

GAS_TX_CREATE

Additional gas cost for creating a new contract.

53
GAS_TX_CREATE = Uint(32000)

GAS_TX_ACCESS_LIST_ADDRESS

Gas cost for including an address in the access list of a transaction.

58
GAS_TX_ACCESS_LIST_ADDRESS = Uint(2400)

GAS_TX_ACCESS_LIST_STORAGE_KEY

Gas cost for including a storage key in the access list of a transaction.

63
GAS_TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)

TX_MAX_GAS_LIMIT

68
TX_MAX_GAS_LIMIT = Uint(16_777_216)

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.

71
@slotted_freezable
72
@dataclass
class LegacyTransaction:

nonce

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

85
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

90
    gas_price: Uint

gas

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

95
    gas: Uint

to

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

100
    to: Bytes0 | Address

value

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

106
    value: U256

data

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

111
    data: Bytes

v

The recovery id of the signature.

117
    v: U256

r

The first part of the signature.

122
    r: U256

s

The second part of the signature.

127
    s: U256

Access

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

133
@slotted_freezable
134
@dataclass
class Access:

account

The address of the account that is accessed.

141
    account: Address

slots

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

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

152
@slotted_freezable
153
@dataclass
class AccessListTransaction:

chain_id

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

165
    chain_id: U64

nonce

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

170
    nonce: U256

gas_price

The price of gas for this transaction.

175
    gas_price: Uint

gas

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

180
    gas: Uint

to

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

185
    to: Bytes0 | Address

value

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

191
    value: U256

data

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

196
    data: Bytes

access_list

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

202
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

208
    y_parity: U256

r

The first part of the signature.

213
    r: U256

s

The second part of the signature.

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

224
@slotted_freezable
225
@dataclass
class FeeMarketTransaction:

chain_id

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

236
    chain_id: U64

nonce

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

241
    nonce: U256

max_priority_fee_per_gas

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

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

251
    max_fee_per_gas: Uint

gas

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

257
    gas: Uint

to

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

262
    to: Bytes0 | Address

value

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

268
    value: U256

data

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

273
    data: Bytes

access_list

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

279
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

285
    y_parity: U256

r

The first part of the signature.

290
    r: U256

s

The second part of the signature.

295
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

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

301
@slotted_freezable
302
@dataclass
class BlobTransaction:

chain_id

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

313
    chain_id: U64

nonce

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

318
    nonce: U256

max_priority_fee_per_gas

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

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

328
    max_fee_per_gas: Uint

gas

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

334
    gas: Uint

to

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

339
    to: Address

value

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

345
    value: U256

data

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

350
    data: Bytes

access_list

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

356
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

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

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

367
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

373
    y_parity: U256

r

The first part of the signature.

378
    r: U256

s

The second part of the signature.

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

389
@slotted_freezable
390
@dataclass
class SetCodeTransaction:

chain_id

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

401
    chain_id: U64

nonce

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

406
    nonce: U64

max_priority_fee_per_gas

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

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

416
    max_fee_per_gas: Uint

gas

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

422
    gas: Uint

to

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

427
    to: Address

value

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

433
    value: U256

data

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

438
    data: Bytes

access_list

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

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

450
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

456
    y_parity: U256

r

The first part of the signature.

461
    r: U256

s

The second part of the signature.

466
    s: U256

Transaction

Union type representing any valid transaction type.

472
Transaction = (
473
    LegacyTransaction
474
    | AccessListTransaction
475
    | FeeMarketTransaction
476
    | BlobTransaction
477
    | SetCodeTransaction
478
)

AccessListCapableTransaction

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

See has_access_list and Access for more details.

484
AccessListCapableTransaction = (
485
    AccessListTransaction
486
    | FeeMarketTransaction
487
    | BlobTransaction
488
    | SetCodeTransaction
489
)

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:
502
    """
503
    Encode a transaction into its RLP or typed transaction format.
504
    Needed because non-legacy transactions aren't RLP.
505
506
    Legacy transactions are returned as-is, while other transaction types
507
    are prefixed with their type identifier and RLP encoded.
508
    """
509
    if isinstance(tx, LegacyTransaction):
510
        return tx
511
    elif isinstance(tx, AccessListTransaction):
512
        return b"\x01" + rlp.encode(tx)
513
    elif isinstance(tx, FeeMarketTransaction):
514
        return b"\x02" + rlp.encode(tx)
515
    elif isinstance(tx, BlobTransaction):
516
        return b"\x03" + rlp.encode(tx)
517
    elif isinstance(tx, SetCodeTransaction):
518
        return b"\x04" + rlp.encode(tx)
519
    else:
520
        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:
524
    """
525
    Decode a transaction from its RLP or typed transaction format.
526
    Needed because non-legacy transactions aren't RLP.
527
528
    Accept a ``LegacyTransaction`` object (returned as-is) or raw
529
    bytes.
530
531
    EIP-2718 states that the first byte distinguishes the format:
532
    [0x00, 0x7f] is a typed transaction, [0xc0, 0xfe] is a legacy
533
    transaction (RLP list prefix).
534
    """
535
    if isinstance(tx, Bytes):
536
        if tx[0] == 1:
537
            return rlp.decode_to(AccessListTransaction, tx[1:])
538
        elif tx[0] == 2:
539
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
540
        elif tx[0] == 3:
541
            return rlp.decode_to(BlobTransaction, tx[1:])
542
        elif tx[0] == 4:
543
            return rlp.decode_to(SetCodeTransaction, tx[1:])
544
        elif tx[0] >= 0xC0:
545
            assert tx[0] <= 0xFE
546
            return rlp.decode_to(LegacyTransaction, tx)
547
        else:
548
            raise TransactionTypeError(tx[0])
549
    else:
550
        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 as a parameter and returns the intrinsic gas cost and the minimum calldata gas cost 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 is greater than 2**64 - 2. It also raises an InitCodeTooLargeError if the code size of a contract creation transaction exceeds the maximum allowed size.

def validate_transaction(tx: Transaction) -> Tuple[Uint, Uint]:
554
    """
555
    Verifies a transaction.
556
557
    The gas in a transaction gets used to pay for the intrinsic cost of
558
    operations, therefore if there is insufficient gas then it would not
559
    be possible to execute a transaction and it will be declared invalid.
560
561
    Additionally, the nonce of a transaction must not equal or exceed the
562
    limit defined in [EIP-2681].
563
    In practice, defining the limit as ``2**64-1`` has no impact because
564
    sending ``2**64-1`` transactions is improbable. It's not strictly
565
    impossible though, ``2**64-1`` transactions is the entire capacity of the
566
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
567
568
    Also, the code size of a contract creation transaction must be within
569
    limits of the protocol.
570
571
    This function takes a transaction as a parameter and returns the intrinsic
572
    gas cost and the minimum calldata gas cost for the transaction after
573
    validation. It throws an `InsufficientTransactionGasError` exception if
574
    the transaction does not provide enough gas to cover the intrinsic cost,
575
    and a `NonceOverflowError` exception if the nonce is greater than
576
    `2**64 - 2`. It also raises an `InitCodeTooLargeError` if the code size of
577
    a contract creation transaction exceeds the maximum allowed size.
578
579
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
580
    [EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
581
    """
582
    from .vm.interpreter import MAX_INIT_CODE_SIZE
583
584
    intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
585
    if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
586
        raise InsufficientTransactionGasError("Insufficient gas")
587
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
588
        raise NonceOverflowError("Nonce too high")
589
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
590
        raise InitCodeTooLargeError("Code size too large")
591
    if tx.gas > TX_MAX_GAS_LIMIT:
592
        raise TransactionGasLimitExceededError("Gas limit too high")
593
594
    return intrinsic_gas, calldata_floor_gas_cost

calculate_intrinsic_cost

Calculates the gas that is charged before execution is started.

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. Base cost (GAS_TX_BASE)

  2. Cost for data (zero and non-zero bytes)

  3. Cost for contract creation (if applicable)

  4. Cost for access list entries (if applicable)

  5. Cost for authorizations (if applicable)

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction and the minimum gas cost used by the transaction based on the calldata size.

def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
598
    """
599
    Calculates the gas that is charged before execution is started.
600
601
    The intrinsic cost of the transaction is charged before execution has
602
    begun. Functions/operations in the EVM cost money to execute so this
603
    intrinsic cost is for the operations that need to be paid for as part of
604
    the transaction. Data transfer, for example, is part of this intrinsic
605
    cost. It costs ether to send data over the wire and that ether is
606
    accounted for in the intrinsic cost calculated in this function. This
607
    intrinsic cost must be calculated and paid for before execution in order
608
    for all operations to be implemented.
609
610
    The intrinsic cost includes:
611
    1. Base cost (`GAS_TX_BASE`)
612
    2. Cost for data (zero and non-zero bytes)
613
    3. Cost for contract creation (if applicable)
614
    4. Cost for access list entries (if applicable)
615
    5. Cost for authorizations (if applicable)
616
617
618
    This function takes a transaction as a parameter and returns the intrinsic
619
    gas cost of the transaction and the minimum gas cost used by the
620
    transaction based on the calldata size.
621
    """
622
    from .vm.eoa_delegation import GAS_AUTH_PER_EMPTY_ACCOUNT
623
    from .vm.gas import init_code_cost
624
625
    num_zeros = Uint(tx.data.count(0))
626
    num_non_zeros = ulen(tx.data) - num_zeros
627
628
    tokens_in_calldata = num_zeros + num_non_zeros * Uint(4)
629
    # EIP-7623 floor price (note: no EVM costs)
630
    calldata_floor_gas_cost = (
631
        tokens_in_calldata * GAS_TX_DATA_TOKEN_FLOOR + GAS_TX_BASE
632
    )
633
634
    data_cost = tokens_in_calldata * GAS_TX_DATA_TOKEN_STANDARD
635
636
    if tx.to == Bytes0(b""):
637
        create_cost = GAS_TX_CREATE + init_code_cost(ulen(tx.data))
638
    else:
639
        create_cost = Uint(0)
640
641
    access_list_cost = Uint(0)
642
    if has_access_list(tx):
643
        for access in tx.access_list:
644
            access_list_cost += GAS_TX_ACCESS_LIST_ADDRESS
645
            access_list_cost += (
646
                ulen(access.slots) * GAS_TX_ACCESS_LIST_STORAGE_KEY
647
            )
648
649
    auth_cost = Uint(0)
650
    if isinstance(tx, SetCodeTransaction):
651
        auth_cost += Uint(GAS_AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations))
652
653
    return (
654
        Uint(
655
            GAS_TX_BASE
656
            + data_cost
657
            + create_cost
658
            + access_list_cost
659
            + auth_cost
660
        ),
661
        calldata_floor_gas_cost,
662
    )

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(chain_id: U64, ​​tx: Transaction) -> Address:
666
    """
667
    Extracts the sender address from a transaction.
668
669
    The v, r, and s values are the three parts that make up the signature
670
    of a transaction. In order to recover the sender of a transaction the two
671
    components needed are the signature (``v``, ``r``, and ``s``) and the
672
    signing hash of the transaction. The sender's public key can be obtained
673
    with these two values and therefore the sender address can be retrieved.
674
675
    This function takes chain_id and a transaction as parameters and returns
676
    the address of the sender of the transaction. It raises an
677
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
678
    """
679
    r, s = tx.r, tx.s
680
    if U256(0) >= r or r >= SECP256K1N:
681
        raise InvalidSignatureError("bad r")
682
    if U256(0) >= s or s > SECP256K1N // U256(2):
683
        raise InvalidSignatureError("bad s")
684
685
    if isinstance(tx, LegacyTransaction):
686
        v = tx.v
687
        if v == 27 or v == 28:
688
            public_key = secp256k1_recover(
689
                r, s, v - U256(27), signing_hash_pre155(tx)
690
            )
691
        else:
692
            chain_id_x2 = U256(chain_id) * U256(2)
693
            if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
694
                raise InvalidSignatureError("bad v")
695
            public_key = secp256k1_recover(
696
                r,
697
                s,
698
                v - U256(35) - chain_id_x2,
699
                signing_hash_155(tx, chain_id),
700
            )
701
    elif isinstance(tx, AccessListTransaction):
702
        if tx.y_parity not in (U256(0), U256(1)):
703
            raise InvalidSignatureError("bad y_parity")
704
        public_key = secp256k1_recover(
705
            r, s, tx.y_parity, signing_hash_2930(tx)
706
        )
707
    elif isinstance(tx, FeeMarketTransaction):
708
        if tx.y_parity not in (U256(0), U256(1)):
709
            raise InvalidSignatureError("bad y_parity")
710
        public_key = secp256k1_recover(
711
            r, s, tx.y_parity, signing_hash_1559(tx)
712
        )
713
    elif isinstance(tx, BlobTransaction):
714
        if tx.y_parity not in (U256(0), U256(1)):
715
            raise InvalidSignatureError("bad y_parity")
716
        public_key = secp256k1_recover(
717
            r, s, tx.y_parity, signing_hash_4844(tx)
718
        )
719
    elif isinstance(tx, SetCodeTransaction):
720
        if tx.y_parity not in (U256(0), U256(1)):
721
            raise InvalidSignatureError("bad y_parity")
722
        public_key = secp256k1_recover(
723
            r, s, tx.y_parity, signing_hash_7702(tx)
724
        )
725
726
    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:
730
    """
731
    Compute the hash of a transaction used in a legacy (pre [EIP-155])
732
    signature.
733
734
    This function takes a legacy transaction as a parameter and returns the
735
    signing hash of the transaction.
736
737
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
738
    """
739
    return keccak256(
740
        rlp.encode(
741
            (
742
                tx.nonce,
743
                tx.gas_price,
744
                tx.gas,
745
                tx.to,
746
                tx.value,
747
                tx.data,
748
            )
749
        )
750
    )

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:
754
    """
755
    Compute the hash of a transaction used in a [EIP-155] signature.
756
757
    This function takes a legacy transaction and a chain ID as parameters
758
    and returns the hash of the transaction used in an [EIP-155] signature.
759
760
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
761
    """
762
    return keccak256(
763
        rlp.encode(
764
            (
765
                tx.nonce,
766
                tx.gas_price,
767
                tx.gas,
768
                tx.to,
769
                tx.value,
770
                tx.data,
771
                chain_id,
772
                Uint(0),
773
                Uint(0),
774
            )
775
        )
776
    )

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:
780
    """
781
    Compute the hash of a transaction used in a [EIP-2930] signature.
782
783
    This function takes an access list transaction as a parameter
784
    and returns the hash of the transaction used in an [EIP-2930] signature.
785
786
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
787
    """
788
    return keccak256(
789
        b"\x01"
790
        + rlp.encode(
791
            (
792
                tx.chain_id,
793
                tx.nonce,
794
                tx.gas_price,
795
                tx.gas,
796
                tx.to,
797
                tx.value,
798
                tx.data,
799
                tx.access_list,
800
            )
801
        )
802
    )

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:
806
    """
807
    Compute the hash of a transaction used in an [EIP-1559] signature.
808
809
    This function takes a fee market transaction as a parameter
810
    and returns the hash of the transaction used in an [EIP-1559] signature.
811
812
    [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
813
    """
814
    return keccak256(
815
        b"\x02"
816
        + rlp.encode(
817
            (
818
                tx.chain_id,
819
                tx.nonce,
820
                tx.max_priority_fee_per_gas,
821
                tx.max_fee_per_gas,
822
                tx.gas,
823
                tx.to,
824
                tx.value,
825
                tx.data,
826
                tx.access_list,
827
            )
828
        )
829
    )

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:
833
    """
834
    Compute the hash of a transaction used in an [EIP-4844] signature.
835
836
    This function takes a transaction as a parameter and returns the
837
    signing hash of the transaction used in an [EIP-4844] signature.
838
839
    [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
840
    """
841
    return keccak256(
842
        b"\x03"
843
        + rlp.encode(
844
            (
845
                tx.chain_id,
846
                tx.nonce,
847
                tx.max_priority_fee_per_gas,
848
                tx.max_fee_per_gas,
849
                tx.gas,
850
                tx.to,
851
                tx.value,
852
                tx.data,
853
                tx.access_list,
854
                tx.max_fee_per_blob_gas,
855
                tx.blob_versioned_hashes,
856
            )
857
        )
858
    )

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:
862
    """
863
    Compute the hash of a transaction used in a [EIP-7702] signature.
864
865
    This function takes a transaction as a parameter and returns the
866
    signing hash of the transaction used in a [EIP-7702] signature.
867
868
    [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
869
    """
870
    return keccak256(
871
        b"\x04"
872
        + rlp.encode(
873
            (
874
                tx.chain_id,
875
                tx.nonce,
876
                tx.max_priority_fee_per_gas,
877
                tx.max_fee_per_gas,
878
                tx.gas,
879
                tx.to,
880
                tx.value,
881
                tx.data,
882
                tx.access_list,
883
                tx.authorizations,
884
            )
885
        )
886
    )

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:
890
    """
891
    Compute the hash of a transaction.
892
893
    This function takes a transaction as a parameter and returns the
894
    keccak256 hash of the transaction. It can handle both legacy transactions
895
    and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`,
896
    etc.).
897
    """
898
    assert isinstance(tx, (LegacyTransaction, Bytes))
899
    if isinstance(tx, LegacyTransaction):
900
        return keccak256(rlp.encode(tx))
901
    else:
902
        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]:
908
    """
909
    Return whether the transaction has an [EIP-2930]-style access list.
910
911
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
912
    """
913
    return isinstance(
914
        tx,
915
        AccessListCapableTransaction,
916
    )