Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
443 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。
, c3 W- o  M8 F& x$ A  A( c地址生成函数
" O$ A. C% ]; ^
  1. // CreateAddress creates an ethereum address given the bytes and the nonce; T% P2 u& G; ~  }
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    # B$ D- ^/ j$ G
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})6 e9 _  K0 r6 b  p
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    " L6 ]3 \" u) U7 \& j$ M9 [0 r
  5. }8 D, n+ U6 Z  ]0 |, z; i9 ^
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    + d. a( c; a2 \# i& D) S
  7. // contract code and a salt./ R2 M# Q: ?6 X# J3 ^" R
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {4 G; Y! w) |9 T4 d+ k. a8 @- N
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])' {( }  D4 m% P
  10. }
复制代码
  y9 J/ y7 P6 T6 H/ ?* u
用途
' f5 M! h6 j' @- c3 ^4 |* T
  1. // if the transaction created a contract, store the creation address in the receipt.
    % t( X& \4 P1 X
  2.         if msg.To() == nil {4 B6 O* N, k( R5 o- \. P
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())/ X, \4 d) r& s' |& ]+ M
  4.         }
    4 j( Y  p( o/ f) a
  5. // Create creates a new contract using code as deployment code.
    $ ?$ q/ l; t8 n  O% L/ p# J
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {" k! l: U2 p# V: C$ D1 Y; s9 p! f
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address())), W! N+ g  D5 Q1 ~
  8.         return evm.create(caller, code, gas, value, contractAddr)) u% z/ X; z. i6 n; l( n
  9. }) ~, ^; Z7 _# x' G( x
  10. // Create2 creates a new contract using code as deployment code.
    ) b  ]" g* r$ e6 c* ~6 @" W
  11. //
    7 }& Y3 t& P' ~. a) h
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
    5 c9 y) M6 i% _6 A
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at., }8 r5 l! k+ T$ I' Z6 _
  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 I1 f! t# p6 N' C4 P
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
    + P" q6 n2 i& c7 [/ ^! [3 ^
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    % z2 \) d2 {  P* [/ L8 h0 l8 i* q
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1