地址生成函数9 x8 ?5 F6 g6 B. b8 p
- // CreateAddress creates an ethereum address given the bytes and the nonce$ k7 l: O h0 t+ G2 q6 Q
- func CreateAddress(b common.Address, nonce uint64) common.Address {- S% _( m) ]( G) G" P, P- K' u$ K
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})' f; S! Y; v: O3 l
- return common.BytesToAddress(Keccak256(data)[12:])0 W' w% V8 V9 ~6 @
- }# [3 n1 A& N8 Z
- // CreateAddress2 creates an ethereum address given the address bytes, initial; m' s! H# F3 x8 c" g8 [
- // contract code and a salt.
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {4 k2 }# o2 s/ t) H4 o$ }( V8 s
- 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 {
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
- }% A: G+ j! D. f W# E3 D: o
- // 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) {& |' v# C/ m% j) ?& r# x
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))! ~9 m' N9 T+ Z0 k' R9 Q0 r
- return evm.create(caller, code, gas, value, contractAddr)
- }
- // Create2 creates a new contract using code as deployment code.3 F4 K9 _. h* ]+ Y# s
- //
- // 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.) M: o9 @1 w8 j5 ?& [% _
- 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) {: B9 R: P- n, H5 e% Q# [: j9 D
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)+ S, k1 p3 H6 K
- return evm.create(caller, code, gas, endowment, contractAddr)
- }