Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
442 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。: |" }0 P% M$ k4 b
地址生成函数6 E3 E4 M% w8 J& \' u
  1. // CreateAddress creates an ethereum address given the bytes and the nonce0 H- Q7 e% K" W  c
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    2 u7 N! q+ C- Z8 s* C7 Q
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    # D2 b: H1 O9 ?. u: f4 |4 y' L5 w
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    6 a4 j0 [) t5 f- n) [$ Y2 e9 m/ U
  5. }
      d1 A' D- I, Y( Z5 M
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial" \. {8 i2 ?! F( o/ p
  7. // contract code and a salt.
    * Q0 u: m: Q. t+ j: i
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {" m! h4 }& {: J; ^2 u
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    8 O) f! P% f, `- ?& e( s
  10. }
复制代码

3 m& k! I" t6 o3 \用途
. |+ h  Q( p8 I& N2 ^
  1. // if the transaction created a contract, store the creation address in the receipt.
    0 {' ^# H4 K9 k. W; i& z1 O
  2.         if msg.To() == nil {/ }# C9 K( W3 x. L0 M+ x
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())/ ?  H6 \& }; V; |" t
  4.         }( m$ [* m; E0 Y# k5 ?1 T
  5. // Create creates a new contract using code as deployment code.
    - I! Y1 J/ K4 I( Y8 o
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {7 U9 T" Y" \5 Q+ q" l; |
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    " q% W7 |6 O. f7 P. O1 b6 c
  8.         return evm.create(caller, code, gas, value, contractAddr)
    ; V3 R; t! J5 g& Q
  9. }
    : ~4 s, `& A2 |
  10. // Create2 creates a new contract using code as deployment code.6 @# R' \6 y; E1 u
  11. //
    0 I. C( M( f1 {7 h
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
    8 A4 w! y: N% h1 n. q, b
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.. d  v' }% L( o4 @9 X% A
  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) {
    : p$ c4 W% g# V0 i, R% B2 i
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)/ ]+ Y, U+ F9 R2 P4 f( x
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    4 m* w. Z9 p# A$ j* @- F& S9 d
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1