地址生成函数
- // CreateAddress creates an ethereum address given the bytes and the nonce. B) z* p, ]9 \! q6 Z
- func CreateAddress(b common.Address, nonce uint64) common.Address {1 C/ Z2 x( _9 L$ I* Z$ r& q
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
- return common.BytesToAddress(Keccak256(data)[12:])* P1 `; a0 H# e, d: q
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial4 T' L0 h( S0 H4 X9 S3 M i
- // contract code and a salt.% g5 v9 v/ q7 L
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {$ I# S& U# F: {2 Z! Q9 _
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])6 y' `4 k4 z$ R# p6 z
- }
用途1 Q! N5 n8 L3 m: Q
- // if the transaction created a contract, store the creation address in the receipt.
- if msg.To() == nil {& ^3 r" M* q `! o8 r+ @3 }
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())6 u1 s( m' ]' ?3 v" ^3 d" a7 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) {
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
- return evm.create(caller, code, gas, value, contractAddr)/ Z3 b: ?* Y/ D
- }6 x* q( _0 t% K/ c
- // Create2 creates a new contract using code as deployment code.) B; O, m" J3 Q) S* b
- //
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]7 U; E1 W) T3 Y" ^! Q+ X# P" d4 r
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.: v' X5 B8 R) e5 G
- 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) {6 }1 l; S; m1 x( o
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
- return evm.create(caller, code, gas, endowment, contractAddr)+ t7 ~8 w# A; F7 n2 y" I
- }