core, crypto: use option2 to generate new address

This commit is contained in:
rjl493456442 2018-07-20 16:46:51 +08:00
parent aa54c6d181
commit fddfd4fb1c
2 changed files with 3 additions and 5 deletions

View file

@ -408,7 +408,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
// The different between Create2 with Create is Create2 uses sha3(msg.sender ++ salt ++ init_code)[12:] // The different between Create2 with Create is Create2 uses sha3(msg.sender ++ salt ++ init_code)[12:]
// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at. // 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) { 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(), salt.Bytes(), code) contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
return evm.create(caller, code, gas, endowment, contractAddr) return evm.create(caller, code, gas, endowment, contractAddr)
} }

View file

@ -78,10 +78,8 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
// CreateAddress2 creates an ethereum address given the address bytes, initial // CreateAddress2 creates an ethereum address given the address bytes, initial
// contract code and a salt. // contract code and a salt.
// TODO(rjl493456442) considering address collision, should we add the 0xff as the input prefix? func CreateAddress2(b common.Address, salt common.Hash, code []byte) common.Address {
func CreateAddress2(b common.Address, salt []byte, code []byte) common.Address { return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt.Bytes(), code)[12:])
data, _ := rlp.EncodeToBytes([]interface{}{b, salt, code})
return common.BytesToAddress(Keccak256(data)[12:])
} }
// ToECDSA creates a private key with the given D value. // ToECDSA creates a private key with the given D value.