Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
440 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。
9 c2 w+ U' [! G. X( [地址生成函数
1 M$ k' k: z- S
  1. // CreateAddress creates an ethereum address given the bytes and the nonce& Q# y  Z0 s3 _( n. h7 q: U  t3 b9 Q/ h
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {; ~. x3 ^$ m* C& Y
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})6 l8 C0 B2 a) a. d7 w5 s9 T
  4.         return common.BytesToAddress(Keccak256(data)[12:])+ ?0 d# d: }. n) [# a, [, |7 b
  5. }
    * z( z+ v! ^( p: j$ r' r9 P0 f
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    # _4 a8 F) H0 h2 _
  7. // contract code and a salt.4 V: K& `: ~( E+ L
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {' F3 a9 a0 T" Q  Y+ b3 M
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    % a" O( P8 E* g. C8 x
  10. }
复制代码

* L. S# F5 Q0 ?3 m1 S# x) x用途; [) ?# c  i+ I2 T1 I' e2 K  S8 j
  1. // if the transaction created a contract, store the creation address in the receipt.
    " Z: g6 V- d/ R0 i% d6 @5 O
  2.         if msg.To() == nil {
    . W! A" P) B. s; w9 Z
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())0 t& |- P. P0 m) @$ ?
  4.         }
    * L7 ?" g* P2 R9 ]
  5. // Create creates a new contract using code as deployment code.
    1 |7 J; o9 U9 _
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {4 H, D% n# V* T8 ?! j
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))! s1 v8 _+ V# P4 n4 B% J6 h
  8.         return evm.create(caller, code, gas, value, contractAddr)
    2 i# s1 q6 D4 F& W2 {$ Q* `: A! X* N
  9. }
    . R) T. e( ?; K. z4 Q1 W  S. n4 T
  10. // Create2 creates a new contract using code as deployment code.
    8 l: C. x& {  s: J
  11. //, p2 y& c# z7 y0 P: W* r2 }- s
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]7 ^0 t" i6 `6 q% k" T& j  I
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
      ]  X& G, e& q- g
  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) {  c0 c4 H4 O6 C5 G# J2 i7 N4 |
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 ]+ n7 J6 p7 P+ X- s) x$ t
  16.         return evm.create(caller, code, gas, endowment, contractAddr)" j3 I" }% \. E' A
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1