Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
237 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。( w8 `' v: D2 x0 y
地址生成函数
  F5 J2 R( B' J0 M
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    4 G# g" {! W0 b0 z  X8 S4 H' V
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    . Z% }* E$ ~" U: L1 e; O7 q
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    5 e4 @9 c: z+ V6 e6 X# \1 |3 o
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    ' E  {# n+ O9 N' M' C1 i  T
  5. }5 u/ ]  Z4 Q3 ^# m  Y5 y# y
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    3 o3 A( o1 D' F+ Z, V
  7. // contract code and a salt.+ o4 g8 L" d: Q3 M" L( ?2 _7 g4 _
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
    $ m9 |) o  P8 q8 R, ~  L
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])
    2 {  Y) x+ j" b& H6 r
  10. }
复制代码
$ ~1 x6 f4 l. h$ h! l
用途* w- q: a3 j' g+ k
  1. // if the transaction created a contract, store the creation address in the receipt.
    2 b! s, }% w( v) A. g
  2.         if msg.To() == nil {
    6 r. n0 a. U) G- L% b7 y
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
    1 ^, H+ Z: B  v
  4.         }5 K) U6 t- H/ ?# P
  5. // Create creates a new contract using code as deployment code.
    : g% a- o; j. X! {" O8 ]' {
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {$ M% Q% x9 m% ]
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))& q. p( x1 D3 s% \- ]0 `8 }2 J
  8.         return evm.create(caller, code, gas, value, contractAddr)
    % ~) z* t$ _( D: K" L
  9. }7 D; L' q* s! T) h" e9 W$ X
  10. // Create2 creates a new contract using code as deployment code.. Q% x6 K1 P/ T" r+ s6 P
  11. //
    , r4 L) T- T' p9 F
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]2 T% A" G  m1 K
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
    1 O4 h1 H( p9 C! p3 j3 A3 ^
  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) {5 q. w0 C3 Y, s/ G7 U& Q& f" }0 R
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
    1 \, }2 s& m! o" C5 o
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    0 U# ?. h- U: Z' N' M# b$ [  D6 ^
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1