current position:Home>How is the Ethereum contract address calculated?

How is the Ethereum contract address calculated?

2022-02-03 20:47:04 Q & A of Denglian community

How is the contract address calculated ? Is there any way to know the address of the contract in advance ?




Take the answer 1:

Contract address generation

The address of the Ethereum contract is based on the Creator (sender) The address of the and the number of transactions sent by the Creator (nonce) To calculate the determined . sender and nonce Conduct RLP code , And then use Keccak-256 Conduct hash Calculation .

Reference resources pyethereum Code (Python):

def mk_contract_address(sender, nonce):    return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]

Use Solidity Code

//  nonce  by  0  Address generated when nonce0 = address(keccak256(0xd6, 0x94, address, 0x80))nonce1 = address(keccak256(0xd6, 0x94, address, 0x01))

Here are some Discuss

If sender by 0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0 , The address of the contract created is as follows , This process is completely certain :

nonce0= "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d"  nonce1= "0x343c43a37d37dff08ae8c4a11544c718abb4fcf8"nonce2= "0xf778b86fa74e846c4f0a1fbd1335fe81c00a0c91"nonce3= "0xfffd933a0bc612844eaf0c6fe3e5b8e9b6c1d19c"

Use Web3j Of Java Code

private String calculateContractAddress(String address, long nonce){    byte[] addressAsBytes = Numeric.hexStringToByteArray(address);    byte[] calculatedAddressAsBytes =            Hash.sha3(RlpEncoder.encode(                    new RlpList(                            RlpString.create(addressAsBytes),                            RlpString.create((nonce)))));    calculatedAddressAsBytes = Arrays.copyOfRange(calculatedAddressAsBytes,            12, calculatedAddressAsBytes.length);    String calculatedAddressAsHex = Numeric.toHexString(calculatedAddressAsBytes);    return calculatedAddressAsHex;}

notes : according to EIP 161 standard Contract account use nonce=1 initial ( On the primary network ). therefore , The first contract address created by a contract will use non-zero nonce Calculate .

CREATE2

stay EIP-1014 Added a new opcode CREATE2 ( stay 19 year 1 Of the month Constantinople hard fork Opcode introduced in ), It's another way to create contracts .

For the CREATE2 Contract created , Its address will be :

keccak256(0xff ++ senderAddress ++ salt ++ keccak256(init_code))[12:]

{% note info %}

CREATE2 It is very useful in the second floor expansion, especially in the state channel , Here is an example to Understand simple state channels , Even if the state channel contract doesn't exist yet , Just make sure the contract is created salt,init_code, You can use the status channel to pay .{% endnote %}

For more information CREATE2, Refer to the EIP-1014.

Original question and answer link

Block chain in depth - Build a high-quality blockchain technology blog , All the school district chains come here , Focus on You know Microblogging .




copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032047017508.html

Random recommended