地址生成函数
- // CreateAddress creates an ethereum address given the bytes and the nonce& Q# y Z0 s3 _( n. h7 q: U t3 b9 Q/ h
- func CreateAddress(b common.Address, nonce uint64) common.Address {; ~. x3 ^$ m* C& Y
- data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})6 l8 C0 B2 a) a. d7 w5 s9 T
- return common.BytesToAddress(Keccak256(data)[12:])+ ?0 d# d: }. n) [# a, [, |7 b
- }
- // CreateAddress2 creates an ethereum address given the address bytes, initial
- // contract code and a salt.4 V: K& `: ~( E+ L
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {' F3 a9 a0 T" Q Y+ b3 M
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
- }
用途; [) ?# c i+ I2 T1 I' e2 K S8 j
- // 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())0 t& |- P. P0 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) {4 H, D% n# V* T8 ?! j
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))! s1 v8 _+ V# P4 n4 B% J6 h
- return evm.create(caller, code, gas, value, contractAddr)
- }
- // Create2 creates a new contract using code as deployment code.
- //, p2 y& c# z7 y0 P: W* r2 }- s
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]7 ^0 t" i6 `6 q% k" T& j I
- // 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) { c0 c4 H4 O6 C5 G# J2 i7 N4 |
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 ]+ n7 J6 p7 P+ X- s) x$ t
- return evm.create(caller, code, gas, endowment, contractAddr)" j3 I" }% \. E' A
- }