Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
322 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。5 M( X# D! w" d) T/ H  u0 D
地址生成函数  l8 A  m2 ]0 T) |7 C' M' [: q
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    - g& P8 R1 D# Z( w& M/ K
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {6 b% c7 r2 A0 I& W* O( C
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}): Q& s' e- p; R1 P3 Q" y; R
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    # ~* K: p' i: `2 a+ ~; C
  5. }
    - w3 ]  E1 r" N! {, _( G
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    - u: q: ~+ \" Z) h9 V! N
  7. // contract code and a salt.4 h$ t* k9 a, t* K* C
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
    . H  q, C' ~. C* W$ e
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    ' m! Q2 W9 Z% t9 W! K
  10. }
复制代码
) P: e  \1 A. Z, d# l
用途) N  ]4 `# s% a9 Q% ~
  1. // if the transaction created a contract, store the creation address in the receipt.
    * f- H; ^/ k3 M  q
  2.         if msg.To() == nil {
    ) U4 f: }8 n" R5 `9 ]' J9 i
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())3 f6 H6 F, D( U3 \
  4.         }
    6 v* v% x# w: c+ Y
  5. // Create creates a new contract using code as deployment code.
    & u& p+ i& J# ?1 s$ }' |
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
    9 Z3 `5 Q/ S# J) Q# k+ n+ [5 R1 ~
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    - Q! c% E& |) [2 i) ?
  8.         return evm.create(caller, code, gas, value, contractAddr)/ X" y3 W7 a  n" V; x) k6 c0 N
  9. }
    ) m" J- r( r+ @- b/ j6 C4 _! E
  10. // Create2 creates a new contract using code as deployment code.
    0 J/ A( P# F+ N3 i6 g
  11. //
    : l, C7 a- T7 o9 ^9 A
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]) o  y' ?) B; r# @  Q! H0 W+ N0 S# |
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.& x" c4 M' q+ R, 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) {$ f3 F4 B6 l6 X+ d* K% D% \' E
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 a( k0 [' ^7 M$ j" ^3 X. X& L5 B
  16.         return evm.create(caller, code, gas, endowment, contractAddr)2 k$ e0 e) p' m, L' z4 M
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1