ethereum.cancun.transactionsethereum.prague.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.

21
TX_BASE_COST = Uint(21000)

TX_DATA_COST_PER_NON_ZERO

Gas cost per non-zero byte in the transaction data.

27
TX_DATA_COST_PER_NON_ZERO = Uint(16)

TX_DATA_COST_PER_ZERO

Gas cost per zero byte in the transaction data.

32
TX_DATA_COST_PER_ZERO = Uint(4)

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.

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

35
STANDARD_CALLDATA_TOKEN_COST = Uint(4)

TX_CREATE_COST

Additional gas cost for creating a new contract.

43
TX_CREATE_COST = Uint(32000)

TX_ACCESS_LIST_ADDRESS_COST

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

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

53
TX_ACCESS_LIST_STORAGE_KEY_COST = Uint(1900)

LegacyTransaction

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

59
@slotted_freezable
60
@dataclass
class LegacyTransaction:

nonce

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

73
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

78
    gas_price: Uint

gas

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

83
    gas: Uint

to

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

88
    to: Bytes0 | Address

value

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

94
    value: U256

data

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

99
    data: Bytes

v

The recovery id of the signature.

105
    v: U256

r

The first part of the signature.

110
    r: U256

s

The second part of the signature.

115
    s: U256

Access

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

121
@slotted_freezable
122
@dataclass
class Access:

account

The address of the account that is accessed.

129
    account: Address

slots

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

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

140
@slotted_freezable
141
@dataclass
class AccessListTransaction:

chain_id

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

153
    chain_id: U64

nonce

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

158
    nonce: U256

gas_price

The price of gas for this transaction.

163
    gas_price: Uint

gas

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

168
    gas: Uint

to

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

173
    to: Bytes0 | Address

value

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

179
    value: U256

data

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

184
    data: Bytes

access_list

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

190
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

196
    y_parity: U256

r

The first part of the signature.

201
    r: U256

s

The second part of the signature.

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

212
@slotted_freezable
213
@dataclass
class FeeMarketTransaction:

chain_id

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

224
    chain_id: U64

nonce

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

229
    nonce: U256

max_priority_fee_per_gas

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

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

239
    max_fee_per_gas: Uint

gas

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

245
    gas: Uint

to

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

250
    to: Bytes0 | Address

value

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

256
    value: U256

data

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

261
    data: Bytes

access_list

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

267
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

273
    y_parity: U256

r

The first part of the signature.

278
    r: U256

s

The second part of the signature.

283
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

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

289
@slotted_freezable
290
@dataclass
class BlobTransaction:

chain_id

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

301
    chain_id: U64

nonce

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

306
    nonce: U256

max_priority_fee_per_gas

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

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

316
    max_fee_per_gas: Uint

gas

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

322
    gas: Uint

to

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

327
    to: Address

value

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

333
    value: U256

data

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

338
    data: Bytes

access_list

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

344
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

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

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

355
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

361
    y_parity: U256

r

The first part of the signature.

366
    r: U256

s

The second part of the signature.

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

377
@slotted_freezable
378
@dataclass
class SetCodeTransaction:

chain_id

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

389
    chain_id: U64

nonce

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

394
    nonce: U64

max_priority_fee_per_gas

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

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

404
    max_fee_per_gas: Uint

gas

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

410
    gas: Uint

to

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

415
    to: Address

value

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

421
    value: U256

data

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

426
    data: Bytes

access_list

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

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

438
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

444
    y_parity: U256

r

The first part of the signature.

449
    r: U256

s

The second part of the signature.

454
    s: U256

Transaction

Union type representing any valid transaction type.

460
Transaction = (
371
    LegacyTransaction
372
    | AccessListTransaction
373
    | FeeMarketTransaction
374
    | BlobTransaction
461
    LegacyTransaction
462
    | AccessListTransaction
463
    | FeeMarketTransaction
464
    | BlobTransaction
465
    | SetCodeTransaction
466
)

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:
473
    """
474
    Encode a transaction into its RLP or typed transaction format.
475
    Needed because non-legacy transactions aren't RLP.
476
477
    Legacy transactions are returned as-is, while other transaction types
478
    are prefixed with their type identifier and RLP encoded.
479
    """
480
    if isinstance(tx, LegacyTransaction):
481
        return tx
482
    elif isinstance(tx, AccessListTransaction):
483
        return b"\x01" + rlp.encode(tx)
484
    elif isinstance(tx, FeeMarketTransaction):
485
        return b"\x02" + rlp.encode(tx)
486
    elif isinstance(tx, BlobTransaction):
487
        return b"\x03" + rlp.encode(tx)
397
    else:
398
        raise Exception(f"Unable to encode transaction of type {type(tx)}")
488
    elif isinstance(tx, SetCodeTransaction):
489
        return b"\x04" + rlp.encode(tx)
490
    else:
491
        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:
495
    """
496
    Decode a transaction from its RLP or typed transaction format.
497
    Needed because non-legacy transactions aren't RLP.
498
499
    Legacy transactions are returned as-is, while other transaction types
500
    are decoded based on their type identifier prefix.
501
    """
502
    if isinstance(tx, Bytes):
503
        if tx[0] == 1:
504
            return rlp.decode_to(AccessListTransaction, tx[1:])
505
        elif tx[0] == 2:
506
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
507
        elif tx[0] == 3:
508
            return rlp.decode_to(BlobTransaction, tx[1:])
416
        else:
417
            raise TransactionTypeError(tx[0])
509
        elif tx[0] == 4:
510
            return rlp.decode_to(SetCodeTransaction, tx[1:])
511
        else:
512
            raise TransactionTypeError(tx[0])
513
    else:
514
        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 of the transaction after validation. It throws angas cost and the minimum calldata gas cost for the transaction after validation. It throws an InvalidTransaction exception if the transaction is invalid. exception if the transaction is invalid.

def validate_transaction(tx: Transaction) -> UintTuple[Uint, Uint]:
518
    """
519
    Verifies a transaction.
520
521
    The gas in a transaction gets used to pay for the intrinsic cost of
522
    operations, therefore if there is insufficient gas then it would not
523
    be possible to execute a transaction and it will be declared invalid.
524
525
    Additionally, the nonce of a transaction must not equal or exceed the
526
    limit defined in [EIP-2681].
527
    In practice, defining the limit as ``2**64-1`` has no impact because
528
    sending ``2**64-1`` transactions is improbable. It's not strictly
529
    impossible though, ``2**64-1`` transactions is the entire capacity of the
530
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
531
532
    Also, the code size of a contract creation transaction must be within
533
    limits of the protocol.
534
535
    This function takes a transaction as a parameter and returns the intrinsic
441
    gas cost of the transaction after validation. It throws an
442
    `InvalidTransaction` exception if the transaction is invalid.
536
    gas cost and the minimum calldata gas cost for the transaction after
537
    validation. It throws an `InvalidTransaction` exception
538
    if the transaction is invalid.
539
540
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
541
    [EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
542
    """
543
    from .vm.interpreter import MAX_INIT_CODE_SIZE
544
448
    intrinsic_gas = calculate_intrinsic_cost(tx)
449
    if intrinsic_gas > tx.gas:
545
    intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
546
    if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
547
        raise InvalidTransaction("Insufficient gas")
548
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
549
        raise InvalidTransaction("Nonce too high")
550
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
551
        raise InvalidTransaction("Code size too large")
552
456
    return intrinsic_gas
553
    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.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) -> UintTuple[Uint, Uint]:
557
    """
558
    Calculates the gas that is charged before execution is started.
559
560
    The intrinsic cost of the transaction is charged before execution has
561
    begun. Functions/operations in the EVM cost money to execute so this
562
    intrinsic cost is for the operations that need to be paid for as part of
563
    the transaction. Data transfer, for example, is part of this intrinsic
564
    cost. It costs ether to send data over the wire and that ether is
565
    accounted for in the intrinsic cost calculated in this function. This
566
    intrinsic cost must be calculated and paid for before execution in order
567
    for all operations to be implemented.
568
569
    The intrinsic cost includes:
570
    1. Base cost (`TX_BASE_COST`)
571
    2. Cost for data (zero and non-zero bytes)
572
    3. Cost for contract creation (if applicable)
573
    4. Cost for access list entries (if applicable)
574
    5. Cost for authorizations (if applicable)
575
576
577
    This function takes a transaction as a parameter and returns the intrinsic
479
    gas cost of the transaction.
578
    gas cost of the transaction and the minimum gas cost used by the
579
    transaction based on the calldata size.
580
    """
581
    from .vm.eoa_delegation import PER_EMPTY_ACCOUNT_COST
582
    from .vm.gas import init_code_cost
583
483
    data_cost = Uint(0)
484
584
    zero_bytes = 0
585
    for byte in tx.data:
586
        if byte == 0:
487
            data_cost += TX_DATA_COST_PER_ZERO
488
        else:
489
            data_cost += TX_DATA_COST_PER_NON_ZERO
587
            zero_bytes += 1
588
589
    tokens_in_calldata = Uint(zero_bytes + (len(tx.data) - zero_bytes) * 4)
590
    # EIP-7623 floor price (note: no EVM costs)
591
    calldata_floor_gas_cost = (
592
        tokens_in_calldata * FLOOR_CALLDATA_COST + TX_BASE_COST
593
    )
594
595
    data_cost = tokens_in_calldata * STANDARD_CALLDATA_TOKEN_COST
596
597
    if tx.to == Bytes0(b""):
598
        create_cost = TX_CREATE_COST + init_code_cost(ulen(tx.data))
599
    else:
600
        create_cost = Uint(0)
601
602
    access_list_cost = Uint(0)
603
    if isinstance(
498
        tx, (AccessListTransaction, FeeMarketTransaction, BlobTransaction)
604
        tx,
605
        (
606
            AccessListTransaction,
607
            FeeMarketTransaction,
608
            BlobTransaction,
609
            SetCodeTransaction,
610
        ),
611
    ):
612
        for access in tx.access_list:
613
            access_list_cost += TX_ACCESS_LIST_ADDRESS_COST
614
            access_list_cost += (
615
                ulen(access.slots) * TX_ACCESS_LIST_STORAGE_KEY_COST
616
            )
617
506
    return TX_BASE_COST + data_cost + create_cost + access_list_cost
618
    auth_cost = Uint(0)
619
    if isinstance(tx, SetCodeTransaction):
620
        auth_cost += Uint(PER_EMPTY_ACCOUNT_COST * len(tx.authorizations))
621
622
    return (
623
        Uint(
624
            TX_BASE_COST
625
            + data_cost
626
            + create_cost
627
            + access_list_cost
628
            + auth_cost
629
        ),
630
        calldata_floor_gas_cost,
631
    )

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

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:
723
    """
724
    Compute the hash of a transaction used in a [EIP-155] signature.
725
726
    This function takes a legacy transaction and a chain ID as parameters
727
    and returns the hash of the transaction used in an [EIP-155] signature.
728
729
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
730
    """
731
    return keccak256(
732
        rlp.encode(
733
            (
734
                tx.nonce,
735
                tx.gas_price,
736
                tx.gas,
737
                tx.to,
738
                tx.value,
739
                tx.data,
740
                chain_id,
741
                Uint(0),
742
                Uint(0),
743
            )
744
        )
745
    )

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:
749
    """
750
    Compute the hash of a transaction used in a [EIP-2930] signature.
751
752
    This function takes an access list transaction as a parameter
753
    and returns the hash of the transaction used in an [EIP-2930] signature.
754
755
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
756
    """
757
    return keccak256(
758
        b"\x01"
759
        + rlp.encode(
760
            (
761
                tx.chain_id,
762
                tx.nonce,
763
                tx.gas_price,
764
                tx.gas,
765
                tx.to,
766
                tx.value,
767
                tx.data,
768
                tx.access_list,
769
            )
770
        )
771
    )

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:
775
    """
776
    Compute the hash of a transaction used in an [EIP-1559] signature.
777
778
    This function takes a fee market transaction as a parameter
779
    and returns the hash of the transaction used in an [EIP-1559] signature.
780
781
    [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
782
    """
783
    return keccak256(
784
        b"\x02"
785
        + rlp.encode(
786
            (
787
                tx.chain_id,
788
                tx.nonce,
789
                tx.max_priority_fee_per_gas,
790
                tx.max_fee_per_gas,
791
                tx.gas,
792
                tx.to,
793
                tx.value,
794
                tx.data,
795
                tx.access_list,
796
            )
797
        )
798
    )

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

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

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:
859
    """
860
    Compute the hash of a transaction.
861
862
    This function takes a transaction as a parameter and returns the
863
    keccak256 hash of the transaction. It can handle both legacy transactions
864
    and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`,
865
    etc.).
866
    """
867
    assert isinstance(tx, (LegacyTransaction, Bytes))
868
    if isinstance(tx, LegacyTransaction):
869
        return keccak256(rlp.encode(tx))
870
    else:
871
        return keccak256(tx)