github.com/phillinzzz/newBsc@v1.1.6/mobile/types.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains all the wrappers from the core/types package.
    18  
    19  package geth
    20  
    21  import (
    22  	"encoding/json"
    23  	"errors"
    24  	"fmt"
    25  
    26  	"github.com/phillinzzz/newBsc/common"
    27  	"github.com/phillinzzz/newBsc/core/types"
    28  	"github.com/phillinzzz/newBsc/rlp"
    29  )
    30  
    31  type jsonEncoder interface {
    32  	EncodeJSON() (string, error)
    33  }
    34  
    35  // encodeOrError tries to encode the object into json.
    36  // If the encoding fails the resulting error is returned.
    37  func encodeOrError(encoder jsonEncoder) string {
    38  	enc, err := encoder.EncodeJSON()
    39  	if err != nil {
    40  		return err.Error()
    41  	}
    42  	return enc
    43  }
    44  
    45  // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
    46  // a sufficient amount of computation has been carried out on a block.
    47  type Nonce struct {
    48  	nonce types.BlockNonce
    49  }
    50  
    51  // GetBytes retrieves the byte representation of the block nonce.
    52  func (n *Nonce) GetBytes() []byte {
    53  	return n.nonce[:]
    54  }
    55  
    56  // GetHex retrieves the hex string representation of the block nonce.
    57  func (n *Nonce) GetHex() string {
    58  	return fmt.Sprintf("0x%x", n.nonce[:])
    59  }
    60  
    61  // String returns a printable representation of the nonce.
    62  func (n *Nonce) String() string {
    63  	return n.GetHex()
    64  }
    65  
    66  // Bloom represents a 256 bit bloom filter.
    67  type Bloom struct {
    68  	bloom types.Bloom
    69  }
    70  
    71  // GetBytes retrieves the byte representation of the bloom filter.
    72  func (b *Bloom) GetBytes() []byte {
    73  	return b.bloom[:]
    74  }
    75  
    76  // GetHex retrieves the hex string representation of the bloom filter.
    77  func (b *Bloom) GetHex() string {
    78  	return fmt.Sprintf("0x%x", b.bloom[:])
    79  }
    80  
    81  // String returns a printable representation of the bloom filter.
    82  func (b *Bloom) String() string {
    83  	return b.GetHex()
    84  }
    85  
    86  // Header represents a block header in the Ethereum blockchain.
    87  type Header struct {
    88  	header *types.Header
    89  }
    90  
    91  // NewHeaderFromRLP parses a header from an RLP data dump.
    92  func NewHeaderFromRLP(data []byte) (*Header, error) {
    93  	h := &Header{
    94  		header: new(types.Header),
    95  	}
    96  	if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
    97  		return nil, err
    98  	}
    99  	return h, nil
   100  }
   101  
   102  // EncodeRLP encodes a header into an RLP data dump.
   103  func (h *Header) EncodeRLP() ([]byte, error) {
   104  	return rlp.EncodeToBytes(h.header)
   105  }
   106  
   107  // NewHeaderFromJSON parses a header from a JSON data dump.
   108  func NewHeaderFromJSON(data string) (*Header, error) {
   109  	h := &Header{
   110  		header: new(types.Header),
   111  	}
   112  	if err := json.Unmarshal([]byte(data), h.header); err != nil {
   113  		return nil, err
   114  	}
   115  	return h, nil
   116  }
   117  
   118  // EncodeJSON encodes a header into a JSON data dump.
   119  func (h *Header) EncodeJSON() (string, error) {
   120  	data, err := json.Marshal(h.header)
   121  	return string(data), err
   122  }
   123  
   124  // String returns a printable representation of the header.
   125  func (h *Header) String() string {
   126  	return encodeOrError(h)
   127  }
   128  
   129  func (h *Header) GetParentHash() *Hash   { return &Hash{h.header.ParentHash} }
   130  func (h *Header) GetUncleHash() *Hash    { return &Hash{h.header.UncleHash} }
   131  func (h *Header) GetCoinbase() *Address  { return &Address{h.header.Coinbase} }
   132  func (h *Header) GetRoot() *Hash         { return &Hash{h.header.Root} }
   133  func (h *Header) GetTxHash() *Hash       { return &Hash{h.header.TxHash} }
   134  func (h *Header) GetReceiptHash() *Hash  { return &Hash{h.header.ReceiptHash} }
   135  func (h *Header) GetBloom() *Bloom       { return &Bloom{h.header.Bloom} }
   136  func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
   137  func (h *Header) GetNumber() int64       { return h.header.Number.Int64() }
   138  func (h *Header) GetGasLimit() int64     { return int64(h.header.GasLimit) }
   139  func (h *Header) GetGasUsed() int64      { return int64(h.header.GasUsed) }
   140  func (h *Header) GetTime() int64         { return int64(h.header.Time) }
   141  func (h *Header) GetExtra() []byte       { return h.header.Extra }
   142  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
   143  func (h *Header) GetNonce() *Nonce       { return &Nonce{h.header.Nonce} }
   144  func (h *Header) GetHash() *Hash         { return &Hash{h.header.Hash()} }
   145  
   146  // Headers represents a slice of headers.
   147  type Headers struct{ headers []*types.Header }
   148  
   149  // Size returns the number of headers in the slice.
   150  func (h *Headers) Size() int {
   151  	return len(h.headers)
   152  }
   153  
   154  // Get returns the header at the given index from the slice.
   155  func (h *Headers) Get(index int) (header *Header, _ error) {
   156  	if index < 0 || index >= len(h.headers) {
   157  		return nil, errors.New("index out of bounds")
   158  	}
   159  	return &Header{h.headers[index]}, nil
   160  }
   161  
   162  // Block represents an entire block in the Ethereum blockchain.
   163  type Block struct {
   164  	block *types.Block
   165  }
   166  
   167  // NewBlockFromRLP parses a block from an RLP data dump.
   168  func NewBlockFromRLP(data []byte) (*Block, error) {
   169  	b := &Block{
   170  		block: new(types.Block),
   171  	}
   172  	if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
   173  		return nil, err
   174  	}
   175  	return b, nil
   176  }
   177  
   178  // EncodeRLP encodes a block into an RLP data dump.
   179  func (b *Block) EncodeRLP() ([]byte, error) {
   180  	return rlp.EncodeToBytes(b.block)
   181  }
   182  
   183  // NewBlockFromJSON parses a block from a JSON data dump.
   184  func NewBlockFromJSON(data string) (*Block, error) {
   185  	b := &Block{
   186  		block: new(types.Block),
   187  	}
   188  	if err := json.Unmarshal([]byte(data), b.block); err != nil {
   189  		return nil, err
   190  	}
   191  	return b, nil
   192  }
   193  
   194  // EncodeJSON encodes a block into a JSON data dump.
   195  func (b *Block) EncodeJSON() (string, error) {
   196  	data, err := json.Marshal(b.block)
   197  	return string(data), err
   198  }
   199  
   200  // String returns a printable representation of the block.
   201  func (b *Block) String() string {
   202  	return encodeOrError(b)
   203  }
   204  
   205  func (b *Block) GetParentHash() *Hash           { return &Hash{b.block.ParentHash()} }
   206  func (b *Block) GetUncleHash() *Hash            { return &Hash{b.block.UncleHash()} }
   207  func (b *Block) GetCoinbase() *Address          { return &Address{b.block.Coinbase()} }
   208  func (b *Block) GetRoot() *Hash                 { return &Hash{b.block.Root()} }
   209  func (b *Block) GetTxHash() *Hash               { return &Hash{b.block.TxHash()} }
   210  func (b *Block) GetReceiptHash() *Hash          { return &Hash{b.block.ReceiptHash()} }
   211  func (b *Block) GetBloom() *Bloom               { return &Bloom{b.block.Bloom()} }
   212  func (b *Block) GetDifficulty() *BigInt         { return &BigInt{b.block.Difficulty()} }
   213  func (b *Block) GetNumber() int64               { return b.block.Number().Int64() }
   214  func (b *Block) GetGasLimit() int64             { return int64(b.block.GasLimit()) }
   215  func (b *Block) GetGasUsed() int64              { return int64(b.block.GasUsed()) }
   216  func (b *Block) GetTime() int64                 { return int64(b.block.Time()) }
   217  func (b *Block) GetExtra() []byte               { return b.block.Extra() }
   218  func (b *Block) GetMixDigest() *Hash            { return &Hash{b.block.MixDigest()} }
   219  func (b *Block) GetNonce() int64                { return int64(b.block.Nonce()) }
   220  func (b *Block) GetHash() *Hash                 { return &Hash{b.block.Hash()} }
   221  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   222  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   223  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   224  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   225  	return &Transaction{b.block.Transaction(hash.hash)}
   226  }
   227  
   228  // Transaction represents a single Ethereum transaction.
   229  type Transaction struct {
   230  	tx *types.Transaction
   231  }
   232  
   233  // NewContractCreation creates a new transaction for deploying a new contract with
   234  // the given properties.
   235  func NewContractCreation(nonce int64, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
   236  	return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   237  }
   238  
   239  // NewTransaction creates a new transaction with the given properties. Contracts
   240  // can be created by transacting with a nil recipient.
   241  func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
   242  	if to == nil {
   243  		return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   244  	}
   245  	return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   246  }
   247  
   248  // NewTransactionFromRLP parses a transaction from an RLP data dump.
   249  func NewTransactionFromRLP(data []byte) (*Transaction, error) {
   250  	tx := &Transaction{
   251  		tx: new(types.Transaction),
   252  	}
   253  	if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
   254  		return nil, err
   255  	}
   256  	return tx, nil
   257  }
   258  
   259  // EncodeRLP encodes a transaction into an RLP data dump.
   260  func (tx *Transaction) EncodeRLP() ([]byte, error) {
   261  	return rlp.EncodeToBytes(tx.tx)
   262  }
   263  
   264  // NewTransactionFromJSON parses a transaction from a JSON data dump.
   265  func NewTransactionFromJSON(data string) (*Transaction, error) {
   266  	tx := &Transaction{
   267  		tx: new(types.Transaction),
   268  	}
   269  	if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
   270  		return nil, err
   271  	}
   272  	return tx, nil
   273  }
   274  
   275  // EncodeJSON encodes a transaction into a JSON data dump.
   276  func (tx *Transaction) EncodeJSON() (string, error) {
   277  	data, err := json.Marshal(tx.tx)
   278  	return string(data), err
   279  }
   280  
   281  // String returns a printable representation of the transaction.
   282  func (tx *Transaction) String() string {
   283  	return encodeOrError(tx)
   284  }
   285  
   286  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   287  func (tx *Transaction) GetGas() int64        { return int64(tx.tx.Gas()) }
   288  func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
   289  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   290  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   291  
   292  func (tx *Transaction) GetHash() *Hash   { return &Hash{tx.tx.Hash()} }
   293  func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
   294  
   295  // Deprecated: GetSigHash cannot know which signer to use.
   296  func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
   297  
   298  // Deprecated: use EthereumClient.TransactionSender
   299  func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
   300  	var signer types.Signer = types.HomesteadSigner{}
   301  	if chainID != nil {
   302  		signer = types.NewEIP155Signer(chainID.bigint)
   303  	}
   304  	from, err := types.Sender(signer, tx.tx)
   305  	return &Address{from}, err
   306  }
   307  
   308  func (tx *Transaction) GetTo() *Address {
   309  	if to := tx.tx.To(); to != nil {
   310  		return &Address{*to}
   311  	}
   312  	return nil
   313  }
   314  
   315  func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
   316  	var signer types.Signer = types.HomesteadSigner{}
   317  	if chainID != nil {
   318  		signer = types.NewEIP155Signer(chainID.bigint)
   319  	}
   320  	rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
   321  	return &Transaction{rawTx}, err
   322  }
   323  
   324  // Transactions represents a slice of transactions.
   325  type Transactions struct{ txs types.Transactions }
   326  
   327  // Size returns the number of transactions in the slice.
   328  func (txs *Transactions) Size() int {
   329  	return len(txs.txs)
   330  }
   331  
   332  // Get returns the transaction at the given index from the slice.
   333  func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
   334  	if index < 0 || index >= len(txs.txs) {
   335  		return nil, errors.New("index out of bounds")
   336  	}
   337  	return &Transaction{txs.txs[index]}, nil
   338  }
   339  
   340  // Receipt represents the results of a transaction.
   341  type Receipt struct {
   342  	receipt *types.Receipt
   343  }
   344  
   345  // NewReceiptFromRLP parses a transaction receipt from an RLP data dump.
   346  func NewReceiptFromRLP(data []byte) (*Receipt, error) {
   347  	r := &Receipt{
   348  		receipt: new(types.Receipt),
   349  	}
   350  	if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
   351  		return nil, err
   352  	}
   353  	return r, nil
   354  }
   355  
   356  // EncodeRLP encodes a transaction receipt into an RLP data dump.
   357  func (r *Receipt) EncodeRLP() ([]byte, error) {
   358  	return rlp.EncodeToBytes(r.receipt)
   359  }
   360  
   361  // NewReceiptFromJSON parses a transaction receipt from a JSON data dump.
   362  func NewReceiptFromJSON(data string) (*Receipt, error) {
   363  	r := &Receipt{
   364  		receipt: new(types.Receipt),
   365  	}
   366  	if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
   367  		return nil, err
   368  	}
   369  	return r, nil
   370  }
   371  
   372  // EncodeJSON encodes a transaction receipt into a JSON data dump.
   373  func (r *Receipt) EncodeJSON() (string, error) {
   374  	data, err := rlp.EncodeToBytes(r.receipt)
   375  	return string(data), err
   376  }
   377  
   378  // String returns a printable representation of the receipt.
   379  func (r *Receipt) String() string {
   380  	return encodeOrError(r)
   381  }
   382  
   383  func (r *Receipt) GetStatus() int               { return int(r.receipt.Status) }
   384  func (r *Receipt) GetPostState() []byte         { return r.receipt.PostState }
   385  func (r *Receipt) GetCumulativeGasUsed() int64  { return int64(r.receipt.CumulativeGasUsed) }
   386  func (r *Receipt) GetBloom() *Bloom             { return &Bloom{r.receipt.Bloom} }
   387  func (r *Receipt) GetLogs() *Logs               { return &Logs{r.receipt.Logs} }
   388  func (r *Receipt) GetTxHash() *Hash             { return &Hash{r.receipt.TxHash} }
   389  func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} }
   390  func (r *Receipt) GetGasUsed() int64            { return int64(r.receipt.GasUsed) }