ethereum.forks.bpo5.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_MAX_GAS_LIMIT

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

34
@slotted_freezable
35
@dataclass
class LegacyTransaction:

nonce

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

48
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

53
    gas_price: Uint

gas

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

58
    gas: Uint

to

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

63
    to: Bytes0 | Address

value

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

69
    value: U256

data

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

74
    data: Bytes

v

The recovery id of the signature.

80
    v: U256

r

The first part of the signature.

85
    r: U256

s

The second part of the signature.

90
    s: U256

Access

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

96
@slotted_freezable
97
@dataclass
class Access:

account

The address of the account that is accessed.

104
    account: Address

slots

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

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

115
@slotted_freezable
116
@dataclass
class AccessListTransaction:

chain_id

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

128
    chain_id: U64

nonce

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

133
    nonce: U256

gas_price

The price of gas for this transaction.

138
    gas_price: Uint

gas

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

143
    gas: Uint

to

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

148
    to: Bytes0 | Address

value

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

154
    value: U256

data

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

159
    data: Bytes

access_list

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

165
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

171
    y_parity: U256

r

The first part of the signature.

176
    r: U256

s

The second part of the signature.

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

187
@slotted_freezable
188
@dataclass
class FeeMarketTransaction:

chain_id

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

199
    chain_id: U64

nonce

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

204
    nonce: U256

max_priority_fee_per_gas

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

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

214
    max_fee_per_gas: Uint

gas

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

220
    gas: Uint

to

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

225
    to: Bytes0 | Address

value

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

231
    value: U256

data

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

236
    data: Bytes

access_list

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

242
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

248
    y_parity: U256

r

The first part of the signature.

253
    r: U256

s

The second part of the signature.

258
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

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

264
@slotted_freezable
265
@dataclass
class BlobTransaction:

chain_id

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

276
    chain_id: U64

nonce

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

281
    nonce: U256

max_priority_fee_per_gas

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

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

291
    max_fee_per_gas: Uint

gas

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

297
    gas: Uint

to

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

302
    to: Address

value

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

308
    value: U256

data

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

313
    data: Bytes

access_list

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

319
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

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

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

330
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

336
    y_parity: U256

r

The first part of the signature.

341
    r: U256

s

The second part of the signature.

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

352
@slotted_freezable
353
@dataclass
class SetCodeTransaction:

chain_id

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

364
    chain_id: U64

nonce

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

369
    nonce: U64

max_priority_fee_per_gas

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

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

379
    max_fee_per_gas: Uint

gas

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

385
    gas: Uint

to

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

390
    to: Address

value

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

396
    value: U256

data

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

401
    data: Bytes

access_list

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

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

413
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

419
    y_parity: U256

r

The first part of the signature.

424
    r: U256

s

The second part of the signature.

429
    s: U256

Transaction

Union type representing any valid transaction type.

435
Transaction = (
436
    LegacyTransaction
437
    | AccessListTransaction
438
    | FeeMarketTransaction
439
    | BlobTransaction
440
    | SetCodeTransaction
441
)

AccessListCapableTransaction

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

See has_access_list and Access for more details.

447
AccessListCapableTransaction = (
448
    AccessListTransaction
449
    | FeeMarketTransaction
450
    | BlobTransaction
451
    | SetCodeTransaction
452
)

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:
465
    """
466
    Encode a transaction into its RLP or typed transaction format.
467
    Needed because non-legacy transactions aren't RLP.
468
469
    Legacy transactions are returned as-is, while other transaction types
470
    are prefixed with their type identifier and RLP encoded.
471
    """
472
    if isinstance(tx, LegacyTransaction):
473
        return tx
474
    elif isinstance(tx, AccessListTransaction):
475
        return b"\x01" + rlp.encode(tx)
476
    elif isinstance(tx, FeeMarketTransaction):
477
        return b"\x02" + rlp.encode(tx)
478
    elif isinstance(tx, BlobTransaction):
479
        return b"\x03" + rlp.encode(tx)
480
    elif isinstance(tx, SetCodeTransaction):
481
        return b"\x04" + rlp.encode(tx)
482
    else:
483
        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:
487
    """
488
    Decode a transaction from its RLP or typed transaction format.
489
    Needed because non-legacy transactions aren't RLP.
490
491
    Legacy transactions are returned as-is, while other transaction types
492
    are decoded based on their type identifier prefix.
493
    """
494
    if isinstance(tx, Bytes):
495
        if tx[0] == 1:
496
            return rlp.decode_to(AccessListTransaction, tx[1:])
497
        elif tx[0] == 2:
498
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
499
        elif tx[0] == 3:
500
            return rlp.decode_to(BlobTransaction, tx[1:])
501
        elif tx[0] == 4:
502
            return rlp.decode_to(SetCodeTransaction, tx[1:])
503
        else:
504
            raise TransactionTypeError(tx[0])
505
    else:
506
        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]:
510
    """
511
    Verifies a transaction.
512
513
    The gas in a transaction gets used to pay for the intrinsic cost of
514
    operations, therefore if there is insufficient gas then it would not
515
    be possible to execute a transaction and it will be declared invalid.
516
517
    Additionally, the nonce of a transaction must not equal or exceed the
518
    limit defined in [EIP-2681].
519
    In practice, defining the limit as ``2**64-1`` has no impact because
520
    sending ``2**64-1`` transactions is improbable. It's not strictly
521
    impossible though, ``2**64-1`` transactions is the entire capacity of the
522
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
523
524
    Also, the code size of a contract creation transaction must be within
525
    limits of the protocol.
526
527
    This function takes a transaction as a parameter and returns the intrinsic
528
    gas cost and the minimum calldata gas cost for the transaction after
529
    validation. It throws an `InsufficientTransactionGasError` exception if
530
    the transaction does not provide enough gas to cover the intrinsic cost,
531
    and a `NonceOverflowError` exception if the nonce is greater than
532
    `2**64 - 2`. It also raises an `InitCodeTooLargeError` if the code size of
533
    a contract creation transaction exceeds the maximum allowed size.
534
535
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
536
    [EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
537
    """
538
    from .vm.interpreter import MAX_INIT_CODE_SIZE
539
540
    intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
541
    if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
542
        raise InsufficientTransactionGasError("Insufficient gas")
543
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
544
        raise NonceOverflowError("Nonce too high")
545
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
546
        raise InitCodeTooLargeError("Code size too large")
547
    if tx.gas > TX_MAX_GAS_LIMIT:
548
        raise TransactionGasLimitExceededError("Gas limit too high")
549
550
    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)

  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]:
554
    """
555
    Calculates the gas that is charged before execution is started.
556
557
    The intrinsic cost of the transaction is charged before execution has
558
    begun. Functions/operations in the EVM cost money to execute so this
559
    intrinsic cost is for the operations that need to be paid for as part of
560
    the transaction. Data transfer, for example, is part of this intrinsic
561
    cost. It costs ether to send data over the wire and that ether is
562
    accounted for in the intrinsic cost calculated in this function. This
563
    intrinsic cost must be calculated and paid for before execution in order
564
    for all operations to be implemented.
565
566
    The intrinsic cost includes:
567
    1. Base cost (`TX_BASE`)
568
    2. Cost for data (zero and non-zero bytes)
569
    3. Cost for contract creation (if applicable)
570
    4. Cost for access list entries (if applicable)
571
    5. Cost for authorizations (if applicable)
572
573
574
    This function takes a transaction as a parameter and returns the intrinsic
575
    gas cost of the transaction and the minimum gas cost used by the
576
    transaction based on the calldata size.
577
    """
578
    from .vm.gas import GasCosts, init_code_cost
579
580
    num_zeros = Uint(tx.data.count(0))
581
    num_non_zeros = ulen(tx.data) - num_zeros
582
583
    tokens_in_calldata = num_zeros + num_non_zeros * Uint(4)
584
    # EIP-7623 floor price (note: no EVM costs)
585
    calldata_floor_gas_cost = (
586
        tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR + GasCosts.TX_BASE
587
    )
588
589
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
590
591
    if tx.to == Bytes0(b""):
592
        create_cost = GasCosts.TX_CREATE + init_code_cost(ulen(tx.data))
593
    else:
594
        create_cost = Uint(0)
595
596
    access_list_cost = Uint(0)
597
    if has_access_list(tx):
598
        for access in tx.access_list:
599
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
600
            access_list_cost += (
601
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
602
            )
603
604
    auth_cost = Uint(0)
605
    if isinstance(tx, SetCodeTransaction):
606
        auth_cost += Uint(
607
            GasCosts.AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations)
608
        )
609
610
    return (
611
        Uint(
612
            GasCosts.TX_BASE
613
            + data_cost
614
            + create_cost
615
            + access_list_cost
616
            + auth_cost
617
        ),
618
        calldata_floor_gas_cost,
619
    )

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

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:
711
    """
712
    Compute the hash of a transaction used in a [EIP-155] signature.
713
714
    This function takes a legacy transaction and a chain ID as parameters
715
    and returns the hash of the transaction used in an [EIP-155] signature.
716
717
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
718
    """
719
    return keccak256(
720
        rlp.encode(
721
            (
722
                tx.nonce,
723
                tx.gas_price,
724
                tx.gas,
725
                tx.to,
726
                tx.value,
727
                tx.data,
728
                chain_id,
729
                Uint(0),
730
                Uint(0),
731
            )
732
        )
733
    )

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:
737
    """
738
    Compute the hash of a transaction used in a [EIP-2930] signature.
739
740
    This function takes an access list transaction as a parameter
741
    and returns the hash of the transaction used in an [EIP-2930] signature.
742
743
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
744
    """
745
    return keccak256(
746
        b"\x01"
747
        + rlp.encode(
748
            (
749
                tx.chain_id,
750
                tx.nonce,
751
                tx.gas_price,
752
                tx.gas,
753
                tx.to,
754
                tx.value,
755
                tx.data,
756
                tx.access_list,
757
            )
758
        )
759
    )

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

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

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:
819
    """
820
    Compute the hash of a transaction used in a [EIP-7702] signature.
821
822
    This function takes a transaction as a parameter and returns the
823
    signing hash of the transaction used in a [EIP-7702] signature.
824
825
    [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
826
    """
827
    return keccak256(
828
        b"\x04"
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.authorizations,
841
            )
842
        )
843
    )

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:
847
    """
848
    Compute the hash of a transaction.
849
850
    This function takes a transaction as a parameter and returns the
851
    keccak256 hash of the transaction. It can handle both legacy transactions
852
    and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`,
853
    etc.).
854
    """
855
    assert isinstance(tx, (LegacyTransaction, Bytes))
856
    if isinstance(tx, LegacyTransaction):
857
        return keccak256(rlp.encode(tx))
858
    else:
859
        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]:
865
    """
866
    Return whether the transaction has an [EIP-2930]-style access list.
867
868
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
869
    """
870
    return isinstance(
871
        tx,
872
        AccessListCapableTransaction,
873
    )