地址生成函数6 E3 E4 M% w8 J& \' u
- // CreateAddress creates an ethereum address given the bytes and the nonce0 H- Q7 e% K" W c
- func CreateAddress(b common.Address, nonce uint64) common.Address {
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
- return common.BytesToAddress(Keccak256(data)[12:])
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial" \. {8 i2 ?! F( o/ p
- // contract code and a salt.
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {" m! h4 }& {: J; ^2 u
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
- }
用途
- // if the transaction created a contract, store the creation address in the receipt.
- if msg.To() == nil {/ }# C9 K( W3 x. L0 M+ x
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())/ ? H6 \& }; V; |" t
- }( m$ [* m; E0 Y# k5 ?1 T
- // Create creates a new contract using code as deployment code.
- func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {7 U9 T" Y" \5 Q+ q" l; |
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
- return evm.create(caller, code, gas, value, contractAddr)
- }
- // Create2 creates a new contract using code as deployment code.6 @# R' \6 y; E1 u
- //
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.. d v' }% L( o4 @9 X% A
- func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)/ ]+ Y, U+ F9 R2 P4 f( x
- return evm.create(caller, code, gas, endowment, contractAddr)
- }