地址生成函数. x' u- ~ p" b1 U) p
- // 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}), R" e$ y5 S6 A' X6 ^# a4 V1 h) d
- return common.BytesToAddress(Keccak256(data)[12:])5 z6 \, @/ k4 F h+ i8 t
- }! z! |- S* K& h1 ^- ]( y
- // CreateAddress2 creates an ethereum address given the address bytes, initial: }6 `5 [8 N/ D6 E* i& e) p
- // contract code and a salt.$ _8 k& T& N7 V j0 H3 O' x
- func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
- return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])) T0 d) n9 R; v! d: W
- }
用途; f y1 {2 }/ L7 t2 m5 p
- // if the transaction created a contract, store the creation address in the receipt.9 q! z2 k+ _2 b- q7 ~
- if msg.To() == nil {: k8 S3 C% l+ A6 e7 b% ^& u; F
- receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
- }# e+ H J9 w) Y6 u
- // Create creates a new contract using code as deployment code.% g5 x0 T/ J8 H) q6 p
- func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {( q- }" z7 A5 P7 H+ n
- contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
- return evm.create(caller, code, gas, value, contractAddr)* |2 M7 s' V( M8 ]
- }
- // Create2 creates a new contract using code as deployment code.
- //, {: p$ E: P+ L, O, R7 D) r( N
- // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]* L- b2 |8 l/ O1 i8 S% d0 {
- // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.! ?. ]8 h5 C) I( v: f& @
- 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) {$ B/ k9 [6 s1 B, W$ {* Y+ C; v
- contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
- return evm.create(caller, code, gas, endowment, contractAddr); i, l) q1 k j+ l2 ~
- }