地址生成函数
- // 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:])
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt.* B9 [* T; T2 i7 N [
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])4 a4 q5 s( ^& ^: `
- }
用途
- // if the transaction created a contract, store the creation address in the receipt.3 L+ P. |$ e, h/ x+ O# A
- if msg.To() == nil {( K2 Z- {0 z7 d7 ]
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())) _! P8 g8 C4 n; ^* a( H" H' m; n. [
- }
- // 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) {* L! Q$ y& h0 Z
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
- return evm.create(caller, code, gas, value, contractAddr)" Z9 d$ X+ t9 F" G- B
- }
- // 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:]' A+ _% q0 Y5 c' H# Z' e
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at., M, G, ^: L5 Z/ V! L5 ?* 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) {
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)8 A3 [" ]) ^4 w& T i
- return evm.create(caller, code, gas, endowment, contractAddr)
- }