Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
441 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。! b# F( g' m( U* S8 l
地址生成函数9 x8 ?5 F6 g6 B. b8 p
  1. // CreateAddress creates an ethereum address given the bytes and the nonce$ k7 l: O  h0 t+ G2 q6 Q
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {- S% _( m) ]( G) G" P, P- K' u$ K
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})' f; S! Y; v: O3 l
  4.         return common.BytesToAddress(Keccak256(data)[12:])0 W' w% V8 V9 ~6 @
  5. }# [3 n1 A& N8 Z
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial; m' s! H# F3 x8 c" g8 [
  7. // contract code and a salt.
    0 h! Q9 C$ w8 L6 |. \8 Y2 F7 s9 V/ Z
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {4 k2 }# o2 s/ t) H4 o$ }( V8 s
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    + }' x# W  P6 ]. ^2 g6 a$ x
  10. }
复制代码

& h7 F* k, V6 J用途
" t7 e( G7 I( z) B
  1. // if the transaction created a contract, store the creation address in the receipt.
    / G6 s( F+ M4 J* o% ~
  2.         if msg.To() == nil {
    1 w9 j. I- K0 ~6 h/ V
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
    ) s' _2 L0 }/ J' b
  4.         }% A: G+ j! D. f  W# E3 D: o
  5. // Create creates a new contract using code as deployment code.
    ) j4 t  `3 I! M) q/ g* h) O; J
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {& |' v# C/ m% j) ?& r# x
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))! ~9 m' N9 T+ Z0 k' R9 Q0 r
  8.         return evm.create(caller, code, gas, value, contractAddr)
    , L) Z' r4 g: j# V" `# v
  9. }
    ) m+ b1 ?  e, V& r8 O$ T
  10. // Create2 creates a new contract using code as deployment code.3 F4 K9 _. h* ]+ Y# s
  11. //
    5 J3 p/ s) |7 L8 `9 E/ r4 n! q. _
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
    6 k! n- q; `; n7 v
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.) M: o9 @1 w8 j5 ?& [% _
  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) {: B9 R: P- n, H5 e% Q# [: j9 D
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)+ S, k1 p3 H6 K
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    4 A0 N, Z, t$ ]: A+ `' [  p
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1