地址生成函数
- // CreateAddress creates an ethereum address given the bytes and the nonce
- func CreateAddress(b common.Address, nonce uint64) common.Address {6 Y# J2 Y) L) A5 R V. z2 ]- L
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
- return common.BytesToAddress(Keccak256(data)[12:])
- } i# \' N1 O4 _ o7 E
- // CreateAddress2 creates an ethereum address given the address bytes, initial0 F* \8 u, q' d
- // contract code and a salt.8 o, I# u. Z( g4 d& i
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {$ I- e% @! f' f4 [/ C; [& i4 V7 U( K0 a
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])) r9 S( t6 d4 r) }) V# e# ~. Q; X2 u
- }
用途
- // if the transaction created a contract, store the creation address in the receipt.
- if msg.To() == nil {
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())0 | S+ d1 k0 M; W' e6 J# \
- }* Z4 o6 C+ w8 @# R. V$ Y2 x- A+ b
- // 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) {$ J( v; {) y, P; l8 Y
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))+ E1 T$ l: p( Q' q+ d
- return evm.create(caller, code, gas, value, contractAddr)" D9 w; x7 L% F0 W' w
- }
- // Create2 creates a new contract using code as deployment code.
- //
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]3 v# k9 B3 _# i: }, r! n! ~
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
- 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) {2 D: v- n# A8 K/ X. ~
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
- return evm.create(caller, code, gas, endowment, contractAddr)
- }