Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
253 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。
3 M: g. J" r- A$ l+ F0 ^地址生成函数
" O! S- X) K/ L  p. }
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    $ n9 ?. E4 d1 K' K
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {4 i$ _: V/ a% q" s, p9 e8 b
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})7 a! n/ \! I1 S" d
  4.         return common.BytesToAddress(Keccak256(data)[12:])  r: j! _6 O& q) U5 ?; U# O
  5. }
    , |* T3 V1 q/ q6 m6 H, A* K# \0 E
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial6 g& J# ^1 _& f5 b. i
  7. // contract code and a salt./ p/ ^* L  y  g! s
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
    0 s/ c5 |. G9 u2 s" G7 v% x5 D( |
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])0 _$ N3 K5 @# |. M3 k' A# v9 R
  10. }
复制代码

! a3 u: ^+ {  n5 H* \. _: V用途6 A, e! @* z8 p1 x' O8 R2 I
  1. // if the transaction created a contract, store the creation address in the receipt.
    1 i6 s! W4 ?/ g$ j0 R3 r. ?2 u9 r
  2.         if msg.To() == nil {  g. E* {1 ]: d6 B* M+ j
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())) g5 M& e! `& g  i- f. B) Y# g
  4.         }
    4 u4 d1 T: Q/ Q, d
  5. // Create creates a new contract using code as deployment code./ y% l! w1 R: |3 u" T5 T
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
    $ L* Y4 ~& [0 z3 h& k/ W! F
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    : A6 N8 q, F$ [8 Q: _
  8.         return evm.create(caller, code, gas, value, contractAddr)& X; P+ W* k4 Z% Z0 y
  9. }
    / l/ _( l! x/ J8 h
  10. // Create2 creates a new contract using code as deployment code.' `& t8 ~* C1 |/ v
  11. //
    ! E% p6 [9 M+ g2 @3 f. f! ?
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
    . T# g8 q0 C# t$ l4 x4 Z2 `
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
    : V! N2 a: }8 O' H
  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) {
    - J# ^3 A, w2 @
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)4 w( e! S9 a2 X
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    , V: c" |. r0 h' Z
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1