Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

高原789
380 0 0
目前CreateAddress,以太坊的地址取决于部署合约的人的地址,当前区块链上部署者的nonce(=该部署交易的nonce)。
1 z- R( s9 ^; j! b' P地址生成函数
) t* {1 w# A  U
  1. // CreateAddress creates an ethereum address given the bytes and the nonce
    ( M" B4 |0 o; A8 b9 R
  2. func CreateAddress(b common.Address, nonce uint64) common.Address {
    1 J5 p0 `5 e5 [  d6 u
  3.         data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    & b, s/ P  }( f! g. o% ~
  4.         return common.BytesToAddress(Keccak256(data)[12:])
    ! w; s8 @- k. A* B8 v. Z& e; j
  5. }
    " g9 y% C9 p8 a5 g2 o
  6. // CreateAddress2 creates an ethereum address given the address bytes, initial
    9 l* n1 |7 e! V, D3 p4 i# `; w
  7. // contract code and a salt.* B9 [* T; T2 i7 N  [
  8. func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address {
    : V% d  c! c! G( d
  9.         return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:])4 a4 q5 s( ^& ^: `
  10. }
复制代码

6 ]  Y! x; x# X1 C用途
4 z- F; o+ H  {
  1. // if the transaction created a contract, store the creation address in the receipt.3 L+ P. |$ e, h/ x+ O# A
  2.         if msg.To() == nil {( K2 Z- {0 z7 d7 ]
  3.                 receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())) _! P8 g8 C4 n; ^* a( H" H' m; n. [
  4.         }
    & D2 a! C# Q) v* r1 P4 u6 i4 c
  5. // Create creates a new contract using code as deployment code.
    ( {" {3 r0 R" }6 v5 P: i
  6. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {* L! Q$ y& h0 Z
  7.         contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
    . w4 H: Z% r0 a) k6 U: i5 {5 S  w
  8.         return evm.create(caller, code, gas, value, contractAddr)" Z9 d$ X+ t9 F" G- B
  9. }
    # {* B, e; H5 A6 L2 q! m: G2 o
  10. // Create2 creates a new contract using code as deployment code.
    ! f3 w; Q3 c, I: x" }
  11. //
    $ t6 U1 u  C& o' m" \
  12. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]' A+ _% q0 Y5 c' H# Z' e
  13. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at., M, G, ^: L5 Z/ V! L5 ?* 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) {
    $ C7 v2 s7 f3 o$ [9 A+ a9 Z5 S  m" ~
  15.         contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)8 A3 [" ]) ^4 w& T  i
  16.         return evm.create(caller, code, gas, endowment, contractAddr)
    # ]3 p  {5 w2 k$ R7 ~: B
  17. }
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

高原789 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1