ETH源码阅读(contract地址生成规则)
2018-09-27 本文已影响0人
坠叶飘香
通过合约创建者和这笔交易的nonce,生成address
go-ethereum/internal/ethapi/api.go
signer := types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Number())
from, err := types.Sender(signer, tx) //得到合约创建者地址
if err != nil {
return common.Hash{}, err
}
addr := crypto.CreateAddress(from, tx.Nonce()) //通过合约创建者和这笔交易的nonce,生成address
go-ethereum/crypto/crypto.go
// 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:])
}