Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
438 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。$ e3 L+ G2 T; r% `5 d# h8 W+ L5 A
地址生成函数0 j( `7 T! ]0 v. d9 k; E
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    , x. F! V# T% o. m0 W
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    + z( g% {- {0 z3 [+ b0 O
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})2 q. ~/ `( y* S
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    . J; W* a( a$ V) M+ ~$ A
  5. }
    ) Q, |; X, |3 S3 v$ C$ J0 K3 d
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    5 [4 h+ E2 W  y* m9 Q/ m
  7. // contract code and a salt." i4 C8 ^* y* V( t0 g
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {% c" o% ?3 |4 I* K
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    & D6 W- ?$ x2 x+ ]& \4 \
  10. }
复制代码
/ K: B, a/ b4 X2 W
用途
1 X# C" g4 {. H. H0 N
  1. // if the transaction created a contract, store the creation address in the receipt.* y6 A% f# O- A2 R' j. k
  2.         if msg.To() == nil {( `! K+ [4 v) x0 h9 B2 k
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())' l' {; m8 a2 {8 X* p2 a
  4.         }
    ! R  d# w0 t; ]0 b
  5. // Create creates a new contract using code as deployment code.
    / J, g( O& a/ ?) `
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {9 K; ]& l! k+ e  }$ i! Y
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))% u* w9 m3 v: Q7 ]# x
  8.         return evm.create(caller, code, gas, value, contractAddr)
    ) h( n; n' K# ?% p' w3 d( m  c5 O
  9. }
    : F9 Z: K1 m; y
  10. // Create2 creates a new contract using code as deployment code./ g" j# H; t7 G8 c+ w; I
  11. //: r- I3 z# F, X' q, ]5 Z
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]) F6 B: b. S5 i8 `+ J3 _
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.! `5 N3 B! T" Z1 T3 x
  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 _2 [7 H0 J% S7 [  Y
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)0 C9 m0 W* X7 X8 ~, q
  16.         return evm.create(caller, code, gas, endowment, contractAddr)8 S/ P2 h9 a8 n6 a! i* l
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1