Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

以太坊合约地址的生成方式

高原789
243 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。, S3 \  ]2 W% I9 H* a
地址生成函数. x' u- ~  p" b1 U) p
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    9 W2 Y: M7 `! ~9 }. l
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    4 u" P3 I2 q" X8 z2 j0 F9 q
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}), R" e$ y5 S6 A' X6 ^# a4 V1 h) d
  4.         return common.BytesToAddress(Keccak256(data)[12:])5 z6 \, @/ k4 F  h+ i8 t
  5. }! z! |- S* K& h1 ^- ]( y
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial: }6 `5 [8 N/ D6 E* i& e) p
  7. // contract code and a salt.$ _8 k& T& N7 V  j0 H3 O' x
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
    9 V6 V& u# R7 `) J8 X2 O
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])) T0 d) n9 R; v! d: W
  10. }
复制代码

' d( S. H$ c! A% V用途; f  y1 {2 }/ L7 t2 m5 p
  1. // if the transaction created a contract, store the creation address in the receipt.9 q! z2 k+ _2 b- q7 ~
  2.         if msg.To() == nil {: k8 S3 C% l+ A6 e7 b% ^& u; F
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
    2 s6 [; P* _& c4 d, r) m
  4.         }# e+ H  J9 w) Y6 u
  5. // Create creates a new contract using code as deployment code.% g5 x0 T/ J8 H) q6 p
  6. 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
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    3 o3 k1 A. g6 Y$ b2 u4 R
  8.         return evm.create(caller, code, gas, value, contractAddr)* |2 M7 s' V( M8 ]
  9. }
    1 p6 [' m  S3 ?+ a
  10. // Create2 creates a new contract using code as deployment code.
    % S+ g, G$ s/ l& G# `' A9 N7 c
  11. //, {: p$ E: P+ L, O, R7 D) r( N
  12. // 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 {
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.! ?. ]8 h5 C) I( v: f& @
  14. 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
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
    7 g- ^; m9 A4 h0 S* u! k6 M; F: a
  16.         return evm.create(caller, code, gas, endowment, contractAddr); i, l) q1 k  j+ l2 ~
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1