美文网首页
ETH源码阅读(contract地址生成规则)

ETH源码阅读(contract地址生成规则)

作者: 坠叶飘香 | 来源:发表于2018-09-27 20:29 被阅读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:])
}

相关文章

网友评论

      本文标题:ETH源码阅读(contract地址生成规则)

      本文链接:https://www.haomeiwen.com/subject/tqsyoftx.html