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
The sidebar is recommended
- To learn blockchain technology, let's build a group school district blockchain technology
- Review of dimitra and Morpheus AMA, global blockchain agricultural technology platform
- Bitcoin public key principle
- Blockchain application series - did
- Bitcoin series - using docker to build BTC private chain
- Bitcoin series - BTC synchronous public chain, test chain and private chain
- Bitcoin series - BTC compilation and installation
- Ethereum series - Web3 js
- Experience - deploy Ethereum private chain (POA)
- Experience part - deploying Ethereum private chain (POW)
guess what you like
-
Ethereum series - Smart contract development and commissioning
-
Ethereum series - DAPP development three swordsman
-
IPFs series - combination of IPFs and blockchain
-
How can the Internet of things, big data, cloud computing, blockchain and artificial intelligence be combined to promote the development of digital economy?
-
The form of story telling tells you what is server, cloud deployment, virtualization, artificial intelligence, blockchain, cloud computing, big data and privacy computing
-
Open source Hongmeng, metauniverse, blockchain, open source collection | alot open source science and technology festival and openharmony technology forum are in hot registration
-
Bankless year-end summary: encrypted network will subvert the old power system
-
The other side of Dao: bribery in the chain and the rise of dark Dao
-
The first 3D chain game (CSC) of metauniverse concept seeks token in encrypted world and blockchain game
-
Metauniverse 3D [NFT] arrg mobile game of metauniverse blockchain [Curtis sword]
Random recommended
- It is said on the Internet that three arrows bought "Adidas and gear", which is actually a fraud
- How will the global network and metauniverse affect international politics?
- GIS + blockchain, a new application scenario of geospatial data, is expected in the future
- The demand for NFT, a clothing luxury brand, has soared, and metauniverse may provide opportunities for transformation
- Curtis sword CSC coin star "Q coin mode" will be launched in January
- Adaoracle ecology and its decentralized Oracle network promote the development of blockchain
- a16z:Web3. 0 ecological panorama
- Development and application of blockchain in food industry
- Chat blockchain (III)
- Ethereum smart contract - building the basic environment
- Founder of ENS: not only Eth, to be the domain name service provider of every digital resource in the world
- A piece of land is 32 million. Why do real estate tycoons buy land in the virtual world?
- Three minutes to understand why arbitrum TVL can lead in layer2
- 13th anniversary of bitcoin white paper, 13 key knowledge points
- From didi to bitcoin, towards a new era of zero friction economy
- Randomness in bitcoin
- Blockchain private Chain NEW
- Blockchain private chain
- Blockchain game: unsustainable or is the future coming?
- Chat blockchain (IV)
- What else does the meta universe lack? Immersive sound hasn't been customized for avatars yet
- Ethereum web3js calls smart contract and does not return transaction hash
- How is Block 0 confirmation implemented
- What are the testing tools for Ethereum smart contract?
- Why can't I display pictures in my wallet when I bind the tokenid with the hash returned from uploading to IPFs?
- TransferHelper: TRANSFER_ FROM_ FAILED
- How to solve the problem of solidness version and package?
- How can a token in Tron wave field and TRX Group LP, pair contract obtain the quantity of a token through LP?
- Using openzepplin library contract
- What was the initial POS consensus agreement?
- Introduction to blockchain
- What progress has filecoin made so far?
- What are the impacts of Libra compared with DCEP?
- What is a token in the blockchain?
- Is there any material that can make Xiaobai learn blockchain quickly?
- How to validate smart contracts?
- Can Ethereum's official wallet password be restored if you forget it?
- What is BiP
- How to convert byte type to integer uint in solidity
- Can this be used in the smart contract constructor?