Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
244 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。7 D6 I4 a) R! e0 e
地址生成函数
9 k' g# I" X. x- c. g# q
  1. // CreateAddress creates an ethereum address given the bytes and the nonce. B) z* p, ]9 \! q6 Z
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {1 C/ Z2 x( _9 L$ I* Z$ r& q
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    ( `$ @& `6 r3 u2 B5 D' K
  4.         return common.BytesToAddress(Keccak256(data)[12:])* P1 `; a0 H# e, d: q
  5. }
    9 o8 Q: S. Y# |: |3 ]& `8 S
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial4 T' L0 h( S0 H4 X9 S3 M  i
  7. // contract code and a salt.% g5 v9 v/ q7 L
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {$ I# S& U# F: {2 Z! Q9 _
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])6 y' `4 k4 z$ R# p6 z
  10. }
复制代码

  l, S4 [/ I6 {& \3 X4 }! X) T: o用途1 Q! N5 n8 L3 m: Q
  1. // if the transaction created a contract, store the creation address in the receipt.
    + ]3 A) R9 ?7 A& P
  2.         if msg.To() == nil {& ^3 r" M* q  `! o8 r+ @3 }
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())6 u1 s( m' ]' ?3 v" ^3 d" a7 t
  4.         }
    & |5 l8 ]' O( x! C& ?; j+ ~" W
  5. // Create creates a new contract using code as deployment code.
    ! k" u6 V- G+ A9 I
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
    / U& I5 T. A" L3 V6 r+ J8 q
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    : J/ w4 c3 a' U: ]8 M
  8.         return evm.create(caller, code, gas, value, contractAddr)/ Z3 b: ?* Y/ D
  9. }6 x* q( _0 t% K/ c
  10. // Create2 creates a new contract using code as deployment code.) B; O, m" J3 Q) S* b
  11. //
    # `+ p4 A5 l( E1 R) w0 Z' o
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]7 U; E1 W) T3 Y" ^! Q+ X# P" d4 r
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.: v' X5 B8 R) e5 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) {6 }1 l; S; m1 x( o
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
    $ y- x/ V& ^  X8 f7 Y( g# {9 h
  16.         return evm.create(caller, code, gas, endowment, contractAddr)+ t7 ~8 w# A; F7 n2 y" I
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1