github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/mobile/types.go (about)

     1  // Copyright 2016 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/common"
    27  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    28  	"github.com/SmartMeshFoundation/Spectrum/rlp"
    29  )
    30  
    31  // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
    32  // a sufficient amount of computation has been carried out on a block.
    33  type Nonce struct {
    34  	nonce types.BlockNonce
    35  }
    36  
    37  // GetBytes retrieves the byte representation of the block nonce.
    38  func (n *Nonce) GetBytes() []byte {
    39  	return n.nonce[:]
    40  }
    41  
    42  // GetHex retrieves the hex string representation of the block nonce.
    43  func (n *Nonce) GetHex() string {
    44  	return fmt.Sprintf("0x%x", n.nonce[:])
    45  }
    46  
    47  // Bloom represents a 256 bit bloom filter.
    48  type Bloom struct {
    49  	bloom types.Bloom
    50  }
    51  
    52  // GetBytes retrieves the byte representation of the bloom filter.
    53  func (b *Bloom) GetBytes() []byte {
    54  	return b.bloom[:]
    55  }
    56  
    57  // GetHex retrieves the hex string representation of the bloom filter.
    58  func (b *Bloom) GetHex() string {
    59  	return fmt.Sprintf("0x%x", b.bloom[:])
    60  }
    61  
    62  // Header represents a block header in the Ethereum blockchain.
    63  type Header struct {
    64  	header *types.Header
    65  }
    66  
    67  // NewHeaderFromRLP parses a header from an RLP data dump.
    68  func NewHeaderFromRLP(data []byte) (*Header, error) {
    69  	h := &Header{
    70  		header: new(types.Header),
    71  	}
    72  	if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
    73  		return nil, err
    74  	}
    75  	return h, nil
    76  }
    77  
    78  // EncodeRLP encodes a header into an RLP data dump.
    79  func (h *Header) EncodeRLP() ([]byte, error) {
    80  	return rlp.EncodeToBytes(h.header)
    81  }
    82  
    83  // NewHeaderFromJSON parses a header from an JSON data dump.
    84  func NewHeaderFromJSON(data string) (*Header, error) {
    85  	h := &Header{
    86  		header: new(types.Header),
    87  	}
    88  	if err := json.Unmarshal([]byte(data), h.header); err != nil {
    89  		return nil, err
    90  	}
    91  	return h, nil
    92  }
    93  
    94  // EncodeJSON encodes a header into an JSON data dump.
    95  func (h *Header) EncodeJSON() (string, error) {
    96  	data, err := json.Marshal(h.header)
    97  	return string(data), err
    98  }
    99  
   100  // String implements the fmt.Stringer interface to print some semi-meaningful
   101  // data dump of the header for debugging purposes.
   102  func (h *Header) String() string {
   103  	return h.header.String()
   104  }
   105  
   106  func (h *Header) GetParentHash() *Hash   { return &Hash{h.header.ParentHash} }
   107  func (h *Header) GetUncleHash() *Hash    { return &Hash{h.header.UncleHash} }
   108  func (h *Header) GetCoinbase() *Address  { return &Address{h.header.Coinbase} }
   109  func (h *Header) GetRoot() *Hash         { return &Hash{h.header.Root} }
   110  func (h *Header) GetTxHash() *Hash       { return &Hash{h.header.TxHash} }
   111  func (h *Header) GetReceiptHash() *Hash  { return &Hash{h.header.ReceiptHash} }
   112  func (h *Header) GetBloom() *Bloom       { return &Bloom{h.header.Bloom} }
   113  func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
   114  func (h *Header) GetNumber() int64       { return h.header.Number.Int64() }
   115  func (h *Header) GetGasLimit() int64     { return h.header.GasLimit.Int64() }
   116  func (h *Header) GetGasUsed() int64      { return h.header.GasUsed.Int64() }
   117  func (h *Header) GetTime() int64         { return h.header.Time.Int64() }
   118  func (h *Header) GetExtra() []byte       { return h.header.Extra }
   119  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
   120  func (h *Header) GetNonce() *Nonce       { return &Nonce{h.header.Nonce} }
   121  func (h *Header) GetHash() *Hash         { return &Hash{h.header.Hash()} }
   122  
   123  // Headers represents a slice of headers.
   124  type Headers struct{ headers []*types.Header }
   125  
   126  // Size returns the number of headers in the slice.
   127  func (h *Headers) Size() int {
   128  	return len(h.headers)
   129  }
   130  
   131  // Get returns the header at the given index from the slice.
   132  func (h *Headers) Get(index int) (header *Header, _ error) {
   133  	if index < 0 || index >= len(h.headers) {
   134  		return nil, errors.New("index out of bounds")
   135  	}
   136  	return &Header{h.headers[index]}, nil
   137  }
   138  
   139  // Block represents an entire block in the Ethereum blockchain.
   140  type Block struct {
   141  	block *types.Block
   142  }
   143  
   144  // NewBlockFromRLP parses a block from an RLP data dump.
   145  func NewBlockFromRLP(data []byte) (*Block, error) {
   146  	b := &Block{
   147  		block: new(types.Block),
   148  	}
   149  	if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
   150  		return nil, err
   151  	}
   152  	return b, nil
   153  }
   154  
   155  // EncodeRLP encodes a block into an RLP data dump.
   156  func (b *Block) EncodeRLP() ([]byte, error) {
   157  	return rlp.EncodeToBytes(b.block)
   158  }
   159  
   160  // NewBlockFromJSON parses a block from an JSON data dump.
   161  func NewBlockFromJSON(data string) (*Block, error) {
   162  	b := &Block{
   163  		block: new(types.Block),
   164  	}
   165  	if err := json.Unmarshal([]byte(data), b.block); err != nil {
   166  		return nil, err
   167  	}
   168  	return b, nil
   169  }
   170  
   171  // EncodeJSON encodes a block into an JSON data dump.
   172  func (b *Block) EncodeJSON() (string, error) {
   173  	data, err := json.Marshal(b.block)
   174  	return string(data), err
   175  }
   176  
   177  // String implements the fmt.Stringer interface to print some semi-meaningful
   178  // data dump of the block for debugging purposes.
   179  func (b *Block) String() string {
   180  	return b.block.String()
   181  }
   182  
   183  func (b *Block) GetParentHash() *Hash   { return &Hash{b.block.ParentHash()} }
   184  func (b *Block) GetUncleHash() *Hash    { return &Hash{b.block.UncleHash()} }
   185  func (b *Block) GetCoinbase() *Address  { return &Address{b.block.Coinbase()} }
   186  func (b *Block) GetRoot() *Hash         { return &Hash{b.block.Root()} }
   187  func (b *Block) GetTxHash() *Hash       { return &Hash{b.block.TxHash()} }
   188  func (b *Block) GetReceiptHash() *Hash  { return &Hash{b.block.ReceiptHash()} }
   189  func (b *Block) GetBloom() *Bloom       { return &Bloom{b.block.Bloom()} }
   190  func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} }
   191  func (b *Block) GetNumber() int64       { return b.block.Number().Int64() }
   192  func (b *Block) GetGasLimit() int64     { return b.block.GasLimit().Int64() }
   193  func (b *Block) GetGasUsed() int64      { return b.block.GasUsed().Int64() }
   194  func (b *Block) GetTime() int64         { return b.block.Time().Int64() }
   195  func (b *Block) GetExtra() []byte       { return b.block.Extra() }
   196  func (b *Block) GetMixDigest() *Hash    { return &Hash{b.block.MixDigest()} }
   197  func (b *Block) GetNonce() int64        { return int64(b.block.Nonce()) }
   198  
   199  func (b *Block) GetHash() *Hash        { return &Hash{b.block.Hash()} }
   200  func (b *Block) GetHashNoNonce() *Hash { return &Hash{b.block.HashNoNonce()} }
   201  
   202  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   203  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   204  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   205  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   206  	return &Transaction{b.block.Transaction(hash.hash)}
   207  }
   208  
   209  // Transaction represents a single Ethereum transaction.
   210  type Transaction struct {
   211  	tx *types.Transaction
   212  }
   213  
   214  // NewTransaction creates a new transaction with the given properties.
   215  func NewTransaction(nonce int64, to *Address, amount, gasLimit, gasPrice *BigInt, data []byte) *Transaction {
   216  	return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, gasLimit.bigint, gasPrice.bigint, common.CopyBytes(data))}
   217  }
   218  
   219  // NewTransactionFromRLP parses a transaction from an RLP data dump.
   220  func NewTransactionFromRLP(data []byte) (*Transaction, error) {
   221  	tx := &Transaction{
   222  		tx: new(types.Transaction),
   223  	}
   224  	if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
   225  		return nil, err
   226  	}
   227  	return tx, nil
   228  }
   229  
   230  // EncodeRLP encodes a transaction into an RLP data dump.
   231  func (tx *Transaction) EncodeRLP() ([]byte, error) {
   232  	return rlp.EncodeToBytes(tx.tx)
   233  }
   234  
   235  // NewTransactionFromJSON parses a transaction from an JSON data dump.
   236  func NewTransactionFromJSON(data string) (*Transaction, error) {
   237  	tx := &Transaction{
   238  		tx: new(types.Transaction),
   239  	}
   240  	if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
   241  		return nil, err
   242  	}
   243  	return tx, nil
   244  }
   245  
   246  // EncodeJSON encodes a transaction into an JSON data dump.
   247  func (tx *Transaction) EncodeJSON() (string, error) {
   248  	data, err := json.Marshal(tx.tx)
   249  	return string(data), err
   250  }
   251  
   252  // String implements the fmt.Stringer interface to print some semi-meaningful
   253  // data dump of the transaction for debugging purposes.
   254  func (tx *Transaction) String() string {
   255  	return tx.tx.String()
   256  }
   257  
   258  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   259  func (tx *Transaction) GetGas() int64        { return tx.tx.Gas().Int64() }
   260  func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
   261  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   262  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   263  
   264  func (tx *Transaction) GetHash() *Hash   { return &Hash{tx.tx.Hash()} }
   265  func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
   266  
   267  // Deprecated: GetSigHash cannot know which signer to use.
   268  func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
   269  
   270  // Deprecated: use EthereumClient.TransactionSender
   271  func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
   272  	var signer types.Signer = types.HomesteadSigner{}
   273  	if chainID != nil {
   274  		signer = types.NewEIP155Signer(chainID.bigint)
   275  	}
   276  	from, err := types.Sender(signer, tx.tx)
   277  	return &Address{from}, err
   278  }
   279  
   280  func (tx *Transaction) GetTo() *Address {
   281  	if to := tx.tx.To(); to != nil {
   282  		return &Address{*to}
   283  	}
   284  	return nil
   285  }
   286  
   287  func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
   288  	var signer types.Signer = types.HomesteadSigner{}
   289  	if chainID != nil {
   290  		signer = types.NewEIP155Signer(chainID.bigint)
   291  	}
   292  	rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
   293  	return &Transaction{rawTx}, err
   294  }
   295  
   296  // Transactions represents a slice of transactions.
   297  type Transactions struct{ txs types.Transactions }
   298  
   299  // Size returns the number of transactions in the slice.
   300  func (txs *Transactions) Size() int {
   301  	return len(txs.txs)
   302  }
   303  
   304  // Get returns the transaction at the given index from the slice.
   305  func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
   306  	if index < 0 || index >= len(txs.txs) {
   307  		return nil, errors.New("index out of bounds")
   308  	}
   309  	return &Transaction{txs.txs[index]}, nil
   310  }
   311  
   312  // Receipt represents the results of a transaction.
   313  type Receipt struct {
   314  	receipt *types.Receipt
   315  }
   316  
   317  // NewReceiptFromRLP parses a transaction receipt from an RLP data dump.
   318  func NewReceiptFromRLP(data []byte) (*Receipt, error) {
   319  	r := &Receipt{
   320  		receipt: new(types.Receipt),
   321  	}
   322  	if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
   323  		return nil, err
   324  	}
   325  	return r, nil
   326  }
   327  
   328  // EncodeRLP encodes a transaction receipt into an RLP data dump.
   329  func (r *Receipt) EncodeRLP() ([]byte, error) {
   330  	return rlp.EncodeToBytes(r.receipt)
   331  }
   332  
   333  // NewReceiptFromJSON parses a transaction receipt from an JSON data dump.
   334  func NewReceiptFromJSON(data string) (*Receipt, error) {
   335  	r := &Receipt{
   336  		receipt: new(types.Receipt),
   337  	}
   338  	if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
   339  		return nil, err
   340  	}
   341  	return r, nil
   342  }
   343  
   344  // EncodeJSON encodes a transaction receipt into an JSON data dump.
   345  func (r *Receipt) EncodeJSON() (string, error) {
   346  	data, err := rlp.EncodeToBytes(r.receipt)
   347  	return string(data), err
   348  }
   349  
   350  // String implements the fmt.Stringer interface to print some semi-meaningful
   351  // data dump of the transaction receipt for debugging purposes.
   352  func (r *Receipt) String() string {
   353  	return r.receipt.String()
   354  }
   355  
   356  func (r *Receipt) GetPostState() []byte          { return r.receipt.PostState }
   357  func (r *Receipt) GetCumulativeGasUsed() *BigInt { return &BigInt{r.receipt.CumulativeGasUsed} }
   358  func (r *Receipt) GetBloom() *Bloom              { return &Bloom{r.receipt.Bloom} }
   359  func (r *Receipt) GetLogs() *Logs                { return &Logs{r.receipt.Logs} }
   360  func (r *Receipt) GetTxHash() *Hash              { return &Hash{r.receipt.TxHash} }
   361  func (r *Receipt) GetContractAddress() *Address  { return &Address{r.receipt.ContractAddress} }
   362  func (r *Receipt) GetGasUsed() *BigInt           { return &BigInt{r.receipt.GasUsed} }