地址生成函数0 j( `7 T! ]0 v. d9 k; E
- // CreateAddress creates an ethereum address given the bytes and the nonce
- func CreateAddress(b common.Address, nonce uint64) common.Address {
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})2 q. ~/ `( y* S
- return common.BytesToAddress(Keccak256(data)[12:])
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt." i4 C8 ^* y* V( t0 g
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {% c" o% ?3 |4 I* K
- 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.* y6 A% f# O- A2 R' j. k
- if msg.To() == nil {( `! K+ [4 v) x0 h9 B2 k
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())' l' {; m8 a2 {8 X* p2 a
- }
- // 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) {9 K; ]& l! k+ e }$ i! Y
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))% u* w9 m3 v: Q7 ]# x
- return evm.create(caller, code, gas, value, contractAddr)
- }
- // Create2 creates a new contract using code as deployment code./ g" j# H; t7 G8 c+ w; I
- //: r- I3 z# F, X' q, ]5 Z
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]) F6 B: b. S5 i8 `+ J3 _
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.! `5 N3 B! T" Z1 T3 x
- 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 _2 [7 H0 J% S7 [ Y
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 C9 m0 W* X7 X8 ~, q
- return evm.create(caller, code, gas, endowment, contractAddr)8 S/ P2 h9 a8 n6 a! i* l
- }