ethereum.homestead.utils.address

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

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

Introduction

Address specific functions used in this homestead 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 homestead version of specification.
13"""
14from typing import Union
15
16from ethereum.base_types import U256, Uint
17from ethereum.crypto.hash import keccak256
18from ethereum.utils.byte import left_pad_zero_bytes
19
20from ... import rlp
21from ..fork_types import Address
22
23
24def to_address(data: Union[Uint, U256]) -> Address:
25    """
26    Convert a Uint or U256 value to a valid address (20 bytes).
27
28    Parameters
29    ----------
30    data :
31        The string to be converted to bytes.
32
33    Returns
34    -------
35    address : `Address`
36        The obtained address.
37    """
38    return Address(data.to_be_bytes32()[-20:])
39
40
41def compute_contract_address(address: Address, nonce: Uint) -> Address:
42    """
43    Computes address of the new account that needs to be created.
44
45    Parameters
46    ----------
47    address :
48        The address of the account that wants to create the new account.
49    nonce :
50        The transaction count of the account that wants to create the new
51        account.
52
53    Returns
54    -------
55    address: `ethereum.homestead.fork_types.Address`
56        The computed address of the new account.
57    """
58    computed_address = keccak256(rlp.encode([address, nonce]))
59    canonical_address = computed_address[-20:]
60    padded_address = left_pad_zero_bytes(canonical_address, 20)
61    return Address(padded_address)
def to_address( data: Union[ethereum.base_types.Uint, ethereum.base_types.U256]) -> ethereum.base_types.Bytes20:
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:])

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:
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: `ethereum.homestead.fork_types.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)

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: ethereum.homestead.fork_types.Address The computed address of the new account.