地址生成函数
- // 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})
- return common.BytesToAddress(Keccak256(data)[12:])
- }5 u/ ] Z4 Q3 ^# m Y5 y# y
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt.+ o4 g8 L" d: Q3 M" L( ?2 _7 g4 _
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
- }
用途* w- q: a3 j' g+ k
- // 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())
- }5 K) U6 t- H/ ?# P
- // 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) {$ M% Q% x9 m% ]
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))& q. p( x1 D3 s% \- ]0 `8 }2 J
- return evm.create(caller, code, gas, value, contractAddr)
- }7 D; L' q* s! T) h" e9 W$ X
- // Create2 creates a new contract using code as deployment code.. Q% x6 K1 P/ T" r+ s6 P
- //
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]2 T% A" G m1 K
- // 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) {5 q. w0 C3 Y, s/ G7 U& Q& f" }0 R
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
- return evm.create(caller, code, gas, endowment, contractAddr)
- }