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.

TX_BASE_COST

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

31
TX_BASE_COST = Uint(21000)

FLOOR_CALLDATA_COST

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

37
FLOOR_CALLDATA_COST = Uint(10)

STANDARD_CALLDATA_TOKEN_COST

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

45
STANDARD_CALLDATA_TOKEN_COST = Uint(4)

TX_CREATE_COST

Additional gas cost for creating a new contract.

53
TX_CREATE_COST = Uint(32000)

TX_ACCESS_LIST_ADDRESS_COST

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

58
TX_ACCESS_LIST_ADDRESS_COST = Uint(2400)

TX_ACCESS_LIST_STORAGE_KEY_COST

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

63
TX_ACCESS_LIST_STORAGE_KEY_COST = 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
)

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:
485
    """
486
    Encode a transaction into its RLP or typed transaction format.
487
    Needed because non-legacy transactions aren't RLP.
488
489
    Legacy transactions are returned as-is, while other transaction types
490
    are prefixed with their type identifier and RLP encoded.
491
    """
492
    if isinstance(tx, LegacyTransaction):
493
        return tx
494
    elif isinstance(tx, AccessListTransaction):
495
        return b"\x01" + rlp.encode(tx)
496
    elif isinstance(tx, FeeMarketTransaction):
497
        return b"\x02" + rlp.encode(tx)
498
    elif isinstance(tx, BlobTransaction):
499
        return b"\x03" + rlp.encode(tx)
500
    elif isinstance(tx, SetCodeTransaction):
501
        return b"\x04" + rlp.encode(tx)
502
    else:
503
        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.

Legacy transactions are returned as-is, while other transaction types are decoded based on their type identifier prefix.

def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
507
    """
508
    Decode a transaction from its RLP or typed transaction format.
509
    Needed because non-legacy transactions aren't RLP.
510
511
    Legacy transactions are returned as-is, while other transaction types
512
    are decoded based on their type identifier prefix.
513
    """
514
    if isinstance(tx, Bytes):
515
        if tx[0] == 1:
516
            return rlp.decode_to(AccessListTransaction, tx[1:])
517
        elif tx[0] == 2:
518
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
519
        elif tx[0] == 3:
520
            return rlp.decode_to(BlobTransaction, tx[1:])
521
        elif tx[0] == 4:
522
            return rlp.decode_to(SetCodeTransaction, tx[1:])
523
        else:
524
            raise TransactionTypeError(tx[0])
525
    else:
526
        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]:
530
    """
531
    Verifies a transaction.
532
533
    The gas in a transaction gets used to pay for the intrinsic cost of
534
    operations, therefore if there is insufficient gas then it would not
535
    be possible to execute a transaction and it will be declared invalid.
536
537
    Additionally, the nonce of a transaction must not equal or exceed the
538
    limit defined in [EIP-2681].
539
    In practice, defining the limit as ``2**64-1`` has no impact because
540
    sending ``2**64-1`` transactions is improbable. It's not strictly
541
    impossible though, ``2**64-1`` transactions is the entire capacity of the
542
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
543
544
    Also, the code size of a contract creation transaction must be within
545
    limits of the protocol.
546
547
    This function takes a transaction as a parameter and returns the intrinsic
548
    gas cost and the minimum calldata gas cost for the transaction after
549
    validation. It throws an `InsufficientTransactionGasError` exception if
550
    the transaction does not provide enough gas to cover the intrinsic cost,
551
    and a `NonceOverflowError` exception if the nonce is greater than
552
    `2**64 - 2`. It also raises an `InitCodeTooLargeError` if the code size of
553
    a contract creation transaction exceeds the maximum allowed size.
554
555
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
556
    [EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
557
    """
558
    from .vm.interpreter import MAX_INIT_CODE_SIZE
559
560
    intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
561
    if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
562
        raise InsufficientTransactionGasError("Insufficient gas")
563
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
564
        raise NonceOverflowError("Nonce too high")
565
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
566
        raise InitCodeTooLargeError("Code size too large")
567
    if tx.gas > TX_MAX_GAS_LIMIT:
568
        raise TransactionGasLimitExceededError("Gas limit too high")
569
570
    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 (TX_BASE_COST)

  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]:
574
    """
575
    Calculates the gas that is charged before execution is started.
576
577
    The intrinsic cost of the transaction is charged before execution has
578
    begun. Functions/operations in the EVM cost money to execute so this
579
    intrinsic cost is for the operations that need to be paid for as part of
580
    the transaction. Data transfer, for example, is part of this intrinsic
581
    cost. It costs ether to send data over the wire and that ether is
582
    accounted for in the intrinsic cost calculated in this function. This
583
    intrinsic cost must be calculated and paid for before execution in order
584
    for all operations to be implemented.
585
586
    The intrinsic cost includes:
587
    1. Base cost (`TX_BASE_COST`)
588
    2. Cost for data (zero and non-zero bytes)
589
    3. Cost for contract creation (if applicable)
590
    4. Cost for access list entries (if applicable)
591
    5. Cost for authorizations (if applicable)
592
593
594
    This function takes a transaction as a parameter and returns the intrinsic
595
    gas cost of the transaction and the minimum gas cost used by the
596
    transaction based on the calldata size.
597
    """
598
    from .vm.eoa_delegation import PER_EMPTY_ACCOUNT_COST
599
    from .vm.gas import init_code_cost
600
601
    zero_bytes = 0
602
    for byte in tx.data:
603
        if byte == 0:
604
            zero_bytes += 1
605
606
    tokens_in_calldata = Uint(zero_bytes + (len(tx.data) - zero_bytes) * 4)
607
    # EIP-7623 floor price (note: no EVM costs)
608
    calldata_floor_gas_cost = (
609
        tokens_in_calldata * FLOOR_CALLDATA_COST + TX_BASE_COST
610
    )
611
612
    data_cost = tokens_in_calldata * STANDARD_CALLDATA_TOKEN_COST
613
614
    if tx.to == Bytes0(b""):
615
        create_cost = TX_CREATE_COST + init_code_cost(ulen(tx.data))
616
    else:
617
        create_cost = Uint(0)
618
619
    access_list_cost = Uint(0)
620
    if isinstance(
621
        tx,
622
        (
623
            AccessListTransaction,
624
            FeeMarketTransaction,
625
            BlobTransaction,
626
            SetCodeTransaction,
627
        ),
628
    ):
629
        for access in tx.access_list:
630
            access_list_cost += TX_ACCESS_LIST_ADDRESS_COST
631
            access_list_cost += (
632
                ulen(access.slots) * TX_ACCESS_LIST_STORAGE_KEY_COST
633
            )
634
635
    auth_cost = Uint(0)
636
    if isinstance(tx, SetCodeTransaction):
637
        auth_cost += Uint(PER_EMPTY_ACCOUNT_COST * len(tx.authorizations))
638
639
    return (
640
        Uint(
641
            TX_BASE_COST
642
            + data_cost
643
            + create_cost
644
            + access_list_cost
645
            + auth_cost
646
        ),
647
        calldata_floor_gas_cost,
648
    )

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

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:
740
    """
741
    Compute the hash of a transaction used in a [EIP-155] signature.
742
743
    This function takes a legacy transaction and a chain ID as parameters
744
    and returns the hash of the transaction used in an [EIP-155] signature.
745
746
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
747
    """
748
    return keccak256(
749
        rlp.encode(
750
            (
751
                tx.nonce,
752
                tx.gas_price,
753
                tx.gas,
754
                tx.to,
755
                tx.value,
756
                tx.data,
757
                chain_id,
758
                Uint(0),
759
                Uint(0),
760
            )
761
        )
762
    )

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:
766
    """
767
    Compute the hash of a transaction used in a [EIP-2930] signature.
768
769
    This function takes an access list transaction as a parameter
770
    and returns the hash of the transaction used in an [EIP-2930] signature.
771
772
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
773
    """
774
    return keccak256(
775
        b"\x01"
776
        + rlp.encode(
777
            (
778
                tx.chain_id,
779
                tx.nonce,
780
                tx.gas_price,
781
                tx.gas,
782
                tx.to,
783
                tx.value,
784
                tx.data,
785
                tx.access_list,
786
            )
787
        )
788
    )

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:
792
    """
793
    Compute the hash of a transaction used in an [EIP-1559] signature.
794
795
    This function takes a fee market transaction as a parameter
796
    and returns the hash of the transaction used in an [EIP-1559] signature.
797
798
    [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
799
    """
800
    return keccak256(
801
        b"\x02"
802
        + rlp.encode(
803
            (
804
                tx.chain_id,
805
                tx.nonce,
806
                tx.max_priority_fee_per_gas,
807
                tx.max_fee_per_gas,
808
                tx.gas,
809
                tx.to,
810
                tx.value,
811
                tx.data,
812
                tx.access_list,
813
            )
814
        )
815
    )

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:
819
    """
820
    Compute the hash of a transaction used in an [EIP-4844] signature.
821
822
    This function takes a transaction as a parameter and returns the
823
    signing hash of the transaction used in an [EIP-4844] signature.
824
825
    [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
826
    """
827
    return keccak256(
828
        b"\x03"
829
        + rlp.encode(
830
            (
831
                tx.chain_id,
832
                tx.nonce,
833
                tx.max_priority_fee_per_gas,
834
                tx.max_fee_per_gas,
835
                tx.gas,
836
                tx.to,
837
                tx.value,
838
                tx.data,
839
                tx.access_list,
840
                tx.max_fee_per_blob_gas,
841
                tx.blob_versioned_hashes,
842
            )
843
        )
844
    )

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:
848
    """
849
    Compute the hash of a transaction used in a [EIP-7702] signature.
850
851
    This function takes a transaction as a parameter and returns the
852
    signing hash of the transaction used in a [EIP-7702] signature.
853
854
    [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
855
    """
856
    return keccak256(
857
        b"\x04"
858
        + rlp.encode(
859
            (
860
                tx.chain_id,
861
                tx.nonce,
862
                tx.max_priority_fee_per_gas,
863
                tx.max_fee_per_gas,
864
                tx.gas,
865
                tx.to,
866
                tx.value,
867
                tx.data,
868
                tx.access_list,
869
                tx.authorizations,
870
            )
871
        )
872
    )

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:
876
    """
877
    Compute the hash of a transaction.
878
879
    This function takes a transaction as a parameter and returns the
880
    keccak256 hash of the transaction. It can handle both legacy transactions
881
    and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`,
882
    etc.).
883
    """
884
    assert isinstance(tx, (LegacyTransaction, Bytes))
885
    if isinstance(tx, LegacyTransaction):
886
        return keccak256(rlp.encode(tx))
887
    else:
888
        return keccak256(tx)