地址生成函数 l8 A m2 ]0 T) |7 C' M' [: q
- // CreateAddress creates an ethereum address given the bytes and the nonce
- func CreateAddress(b common.Address, nonce uint64) common.Address {6 b% c7 r2 A0 I& W* O( C
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}): Q& s' e- p; R1 P3 Q" y; R
- return common.BytesToAddress(Keccak256(data)[12:])
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt.4 h$ t* k9 a, t* K* C
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
- }
用途) N ]4 `# s% a9 Q% ~
- // 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())3 f6 H6 F, D( U3 \
- }
- // 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)/ X" y3 W7 a n" V; x) k6 c0 N
- }
- // 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:]) o y' ?) B; r# @ Q! H0 W+ N0 S# |
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.& x" c4 M' q+ R, 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) {$ f3 F4 B6 l6 X+ d* K% D% \' E
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 a( k0 [' ^7 M$ j" ^3 X. X& L5 B
- return evm.create(caller, code, gas, endowment, contractAddr)2 k$ e0 e) p' m, L' z4 M
- }