地址生成函数
- // CreateAddress creates an ethereum address given the bytes and the nonce; T% P2 u& G; ~ }
- func CreateAddress(b common.Address, nonce uint64) common.Address {
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})6 e9 _ K0 r6 b p
- return common.BytesToAddress(Keccak256(data)[12:])
- }8 D, n+ U6 Z ]0 |, z; i9 ^
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt./ R2 M# Q: ?6 X# J3 ^" R
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {4 G; Y! w) |9 T4 d+ k. a8 @- N
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])' {( } D4 m% P
- }
用途
- // if the transaction created a contract, store the creation address in the receipt.
- if msg.To() == nil {4 B6 O* N, k( R5 o- \. P
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())/ X, \4 d) r& s' |& ]+ M
- }
- // 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) {" k! l: U2 p# V: C$ D1 Y; s9 p! f
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address())), W! N+ g D5 Q1 ~
- return evm.create(caller, code, gas, value, contractAddr)) u% z/ X; z. i6 n; l( n
- }) ~, ^; Z7 _# x' G( x
- // 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:]
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at., }8 r5 l! k+ T$ I' Z6 _
- 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)
- return evm.create(caller, code, gas, endowment, contractAddr)
- }