Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
177 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。
6 i3 v( @1 ^. G* l" h5 N地址生成函数
' U2 l3 H2 u8 J, r7 y
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
      @8 U5 D2 p) S, ~1 N
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {6 Y# J2 Y) L) A5 R  V. z2 ]- L
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    " g3 l8 Z; s' L2 f* p. ~6 N
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    ) q2 U2 V0 Z$ R- M9 `2 G! ^
  5. }  i# \' N1 O4 _  o7 E
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial0 F* \8 u, q' d
  7. // contract code and a salt.8 o, I# u. Z( g4 d& i
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {$ I- e% @! f' f4 [/ C; [& i4 V7 U( K0 a
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])) r9 S( t6 d4 r) }) V# e# ~. Q; X2 u
  10. }
复制代码

. F+ D. g' K/ d0 }# P, C  d2 h用途
4 q) i2 [" {  [0 U0 n, ]$ h+ ^+ |: n. o
  1. // if the transaction created a contract, store the creation address in the receipt.
    , e" _. @! T, r9 P- d% P
  2.         if msg.To() == nil {
    4 [0 Q: f$ I6 O1 `+ S$ i
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())0 |  S+ d1 k0 M; W' e6 J# \
  4.         }* Z4 o6 C+ w8 @# R. V$ Y2 x- A+ b
  5. // Create creates a new contract using code as deployment code.
    $ h. Q  w0 Q, B' e" |/ E) B, f3 y
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {$ J( v; {) y, P; l8 Y
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))+ E1 T$ l: p( Q' q+ d
  8.         return evm.create(caller, code, gas, value, contractAddr)" D9 w; x7 L% F0 W' w
  9. }
    ! E( H) Q: A' a" [% N( S8 N
  10. // Create2 creates a new contract using code as deployment code.
    8 }, k2 Y7 K- w1 n- s9 b
  11. //
    3 b! `8 x9 T% a# S! n- X
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]3 v# k9 B3 _# i: }, r! n! ~
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
    ; h, a7 i" @1 l) R2 p* R
  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) {2 D: v- n# A8 K/ X. ~
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
    # ?% T* |, @$ M8 t' e
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    2 j& M  P( v5 N
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1