ethereum.shanghai.utils.address

Hardfork Utility Functions For Addresses ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Address specific functions used in this shanghai version of specification.

 1"""
 2Hardfork Utility Functions For Addresses
 3^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 4
 5.. contents:: Table of Contents
 6    :backlinks: none
 7    :local:
 8
 9Introduction
10------------
11
12Address specific functions used in this shanghai version of
13specification.
14"""
15from typing import Union
16
17from ethereum.base_types import U256, Bytes32, Uint
18from ethereum.crypto.hash import keccak256
19from ethereum.utils.byte import left_pad_zero_bytes
20
21from ... import rlp
22from ..fork_types import Address
23
24
25def to_address(data: Union[Uint, U256]) -> Address:
26    """
27    Convert a Uint or U256 value to a valid address (20 bytes).
28
29    Parameters
30    ----------
31    data :
32        The string to be converted to bytes.
33
34    Returns
35    -------
36    address : `Address`
37        The obtained address.
38    """
39    return Address(data.to_be_bytes32()[-20:])
40
41
42def compute_contract_address(address: Address, nonce: Uint) -> Address:
43    """
44    Computes address of the new account that needs to be created.
45
46    Parameters
47    ----------
48    address :
49        The address of the account that wants to create the new account.
50    nonce :
51        The transaction count of the account that wants to create the new
52        account.
53
54    Returns
55    -------
56    address: `Address`
57        The computed address of the new account.
58    """
59    computed_address = keccak256(rlp.encode([address, nonce]))
60    canonical_address = computed_address[-20:]
61    padded_address = left_pad_zero_bytes(canonical_address, 20)
62    return Address(padded_address)
63
64
65def compute_create2_contract_address(
66    address: Address, salt: Bytes32, call_data: bytearray
67) -> Address:
68    """
69    Computes address of the new account that needs to be created, which is
70    based on the sender address, salt and the call data as well.
71
72    Parameters
73    ----------
74    address :
75        The address of the account that wants to create the new account.
76    salt :
77        Address generation salt.
78    call_data :
79        The code of the new account which is to be created.
80
81    Returns
82    -------
83    address: `ethereum.shanghai.fork_types.Address`
84        The computed address of the new account.
85    """
86    preimage = b"\xff" + address + salt + keccak256(call_data)
87    computed_address = keccak256(preimage)
88    canonical_address = computed_address[-20:]
89    padded_address = left_pad_zero_bytes(canonical_address, 20)
90
91    return Address(padded_address)
def to_address( data: Union[ethereum.base_types.Uint, ethereum.base_types.U256]) -> ethereum.base_types.Bytes20:
26def to_address(data: Union[Uint, U256]) -> Address:
27    """
28    Convert a Uint or U256 value to a valid address (20 bytes).
29
30    Parameters
31    ----------
32    data :
33        The string to be converted to bytes.
34
35    Returns
36    -------
37    address : `Address`
38        The obtained address.
39    """
40    return Address(data.to_be_bytes32()[-20:])

Convert a Uint or U256 value to a valid address (20 bytes).

Parameters

data : The string to be converted to bytes.

Returns

address : Address The obtained address.

def compute_contract_address( address: ethereum.base_types.Bytes20, nonce: ethereum.base_types.Uint) -> ethereum.base_types.Bytes20:
43def compute_contract_address(address: Address, nonce: Uint) -> Address:
44    """
45    Computes address of the new account that needs to be created.
46
47    Parameters
48    ----------
49    address :
50        The address of the account that wants to create the new account.
51    nonce :
52        The transaction count of the account that wants to create the new
53        account.
54
55    Returns
56    -------
57    address: `Address`
58        The computed address of the new account.
59    """
60    computed_address = keccak256(rlp.encode([address, nonce]))
61    canonical_address = computed_address[-20:]
62    padded_address = left_pad_zero_bytes(canonical_address, 20)
63    return Address(padded_address)

Computes address of the new account that needs to be created.

Parameters

address : The address of the account that wants to create the new account. nonce : The transaction count of the account that wants to create the new account.

Returns

address: Address The computed address of the new account.

def compute_create2_contract_address( address: ethereum.base_types.Bytes20, salt: ethereum.base_types.Bytes32, call_data: bytearray) -> ethereum.base_types.Bytes20:
66def compute_create2_contract_address(
67    address: Address, salt: Bytes32, call_data: bytearray
68) -> Address:
69    """
70    Computes address of the new account that needs to be created, which is
71    based on the sender address, salt and the call data as well.
72
73    Parameters
74    ----------
75    address :
76        The address of the account that wants to create the new account.
77    salt :
78        Address generation salt.
79    call_data :
80        The code of the new account which is to be created.
81
82    Returns
83    -------
84    address: `ethereum.shanghai.fork_types.Address`
85        The computed address of the new account.
86    """
87    preimage = b"\xff" + address + salt + keccak256(call_data)
88    computed_address = keccak256(preimage)
89    canonical_address = computed_address[-20:]
90    padded_address = left_pad_zero_bytes(canonical_address, 20)
91
92    return Address(padded_address)

Computes address of the new account that needs to be created, which is based on the sender address, salt and the call data as well.

Parameters

address : The address of the account that wants to create the new account. salt : Address generation salt. call_data : The code of the new account which is to be created.

Returns

address: ethereum.shanghai.fork_types.Address The computed address of the new account.