-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathaddress.py
More file actions
60 lines (47 loc) · 1.44 KB
/
address.py
File metadata and controls
60 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Frontier Utility Functions For Addresses
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. contents:: Table of Contents
:backlinks: none
:local:
Introduction
------------
Address specific functions used in this frontier version of specification.
"""
from typing import Union
from ethereum.base_types import U256, Uint
from ethereum.crypto import keccak256
from ... import rlp
from ..eth_types import Address
def to_address(data: Union[Uint, U256]) -> Address:
"""
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.
"""
return Address(data.to_be_bytes32()[-20:])
def compute_contract_address(address: Address, nonce: Uint) -> 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.frontier.eth_types.Address`
The computed address of the new account.
"""
computed_address = keccak256(rlp.encode([address, nonce]))
canonical_address = computed_address[-20:]
padded_address = canonical_address.rjust(20, b"\x00")
return Address(padded_address)