github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/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/CommerciumBlockchain/go-commercium/common"
    27  	"github.com/CommerciumBlockchain/go-commercium/core/types"
    28  	"github.com/CommerciumBlockchain/go-commercium/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) GetTime() int64         { return int64(h.header.Time) }
   139  func (h *Header) GetExtra() []byte       { return h.header.Extra }
   140  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
   141  func (h *Header) GetNonce() *Nonce       { return &Nonce{h.header.Nonce} }
   142  func (h *Header) GetHash() *Hash         { return &Hash{h.header.Hash()} }
   143  
   144  // Headers represents a slice of headers.
   145  type Headers struct{ headers []*types.Header }
   146  
   147  // Size returns the number of headers in the slice.
   148  func (h *Headers) Size() int {
   149  	return len(h.headers)
   150  }
   151  
   152  // Get returns the header at the given index from the slice.
   153  func (h *Headers) Get(index int) (header *Header, _ error) {
   154  	if index < 0 || index >= len(h.headers) {
   155  		return nil, errors.New("index out of bounds")
   156  	}
   157  	return &Header{h.headers[index]}, nil
   158  }
   159  
   160  // Block represents an entire block in the Ethereum blockchain.
   161  type Block struct {
   162  	block *types.Block
   163  }
   164  
   165  // NewBlockFromRLP parses a block from an RLP data dump.
   166  func NewBlockFromRLP(data []byte) (*Block, error) {
   167  	b := &Block{
   168  		block: new(types.Block),
   169  	}
   170  	if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
   171  		return nil, err
   172  	}
   173  	return b, nil
   174  }
   175  
   176  // EncodeRLP encodes a block into an RLP data dump.
   177  func (b *Block) EncodeRLP() ([]byte, error) {
   178  	return rlp.EncodeToBytes(b.block)
   179  }
   180  
   181  // NewBlockFromJSON parses a block from a JSON data dump.
   182  func NewBlockFromJSON(data string) (*Block, error) {
   183  	b := &Block{
   184  		block: new(types.Block),
   185  	}
   186  	if err := json.Unmarshal([]byte(data), b.block); err != nil {
   187  		return nil, err
   188  	}
   189  	return b, nil
   190  }
   191  
   192  // EncodeJSON encodes a block into a JSON data dump.
   193  func (b *Block) EncodeJSON() (string, error) {
   194  	data, err := json.Marshal(b.block)
   195  	return string(data), err
   196  }
   197  
   198  // String returns a printable representation of the block.
   199  func (b *Block) String() string {
   200  	return encodeOrError(b)
   201  }
   202  
   203  func (b *Block) GetParentHash() *Hash           { return &Hash{b.block.ParentHash()} }
   204  func (b *Block) GetUncleHash() *Hash            { return &Hash{b.block.UncleHash()} }
   205  func (b *Block) GetCoinbase() *Address          { return &Address{b.block.Coinbase()} }
   206  func (b *Block) GetRoot() *Hash                 { return &Hash{b.block.Root()} }
   207  func (b *Block) GetTxHash() *Hash               { return &Hash{b.block.TxHash()} }
   208  func (b *Block) GetReceiptHash() *Hash          { return &Hash{b.block.ReceiptHash()} }
   209  func (b *Block) GetBloom() *Bloom               { return &Bloom{b.block.Bloom()} }
   210  func (b *Block) GetDifficulty() *BigInt         { return &BigInt{b.block.Difficulty()} }
   211  func (b *Block) GetNumber() int64               { return b.block.Number().Int64() }
   212  func (b *Block) GetTime() int64                 { return int64(b.block.Time()) }
   213  func (b *Block) GetExtra() []byte               { return b.block.Extra() }
   214  func (b *Block) GetMixDigest() *Hash            { return &Hash{b.block.MixDigest()} }
   215  func (b *Block) GetNonce() int64                { return int64(b.block.Nonce()) }
   216  func (b *Block) GetHash() *Hash                 { return &Hash{b.block.Hash()} }
   217  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   218  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   219  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   220  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   221  	return &Transaction{b.block.Transaction(hash.hash)}
   222  }
   223  
   224  // Transaction represents a single Ethereum transaction.
   225  type Transaction struct {
   226  	tx *types.Transaction
   227  }
   228  
   229  // NewContractCreation creates a new transaction for deploying a new contract with
   230  // the given properties.
   231  func NewContractCreation(nonce int64, amount *BigInt, data []byte) *Transaction {
   232  	return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, common.CopyBytes(data))}
   233  }
   234  
   235  // NewTransaction creates a new transaction with the given properties. Contracts
   236  // can be created by transacting with a nil recipient.
   237  func NewTransaction(nonce int64, to *Address, amount *BigInt, data []byte) *Transaction {
   238  	if to == nil {
   239  		return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, common.CopyBytes(data))}
   240  	}
   241  	return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, common.CopyBytes(data))}
   242  }
   243  
   244  // NewTransactionFromRLP parses a transaction from an RLP data dump.
   245  func NewTransactionFromRLP(data []byte) (*Transaction, error) {
   246  	tx := &Transaction{
   247  		tx: new(types.Transaction),
   248  	}
   249  	if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
   250  		return nil, err
   251  	}
   252  	return tx, nil
   253  }
   254  
   255  // EncodeRLP encodes a transaction into an RLP data dump.
   256  func (tx *Transaction) EncodeRLP() ([]byte, error) {
   257  	return rlp.EncodeToBytes(tx.tx)
   258  }
   259  
   260  // NewTransactionFromJSON parses a transaction from a JSON data dump.
   261  func NewTransactionFromJSON(data string) (*Transaction, error) {
   262  	tx := &Transaction{
   263  		tx: new(types.Transaction),
   264  	}
   265  	if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
   266  		return nil, err
   267  	}
   268  	return tx, nil
   269  }
   270  
   271  // EncodeJSON encodes a transaction into a JSON data dump.
   272  func (tx *Transaction) EncodeJSON() (string, error) {
   273  	data, err := json.Marshal(tx.tx)
   274  	return string(data), err
   275  }
   276  
   277  // String returns a printable representation of the transaction.
   278  func (tx *Transaction) String() string {
   279  	return encodeOrError(tx)
   280  }
   281  
   282  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   283  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   284  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   285  
   286  func (tx *Transaction) GetHash() *Hash   { return &Hash{tx.tx.Hash()} }
   287  func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
   288  
   289  // Deprecated: GetSigHash cannot know which signer to use.
   290  func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
   291  
   292  // Deprecated: use EthereumClient.TransactionSender
   293  func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
   294  	var signer types.Signer = types.HomesteadSigner{}
   295  	if chainID != nil {
   296  		signer = types.NewEIP155Signer(chainID.bigint)
   297  	}
   298  	from, err := types.Sender(signer, tx.tx)
   299  	return &Address{from}, err
   300  }
   301  
   302  func (tx *Transaction) GetTo() *Address {
   303  	if to := tx.tx.To(); to != nil {
   304  		return &Address{*to}
   305  	}
   306  	return nil
   307  }
   308  
   309  func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
   310  	var signer types.Signer = types.HomesteadSigner{}
   311  	if chainID != nil {
   312  		signer = types.NewEIP155Signer(chainID.bigint)
   313  	}
   314  	rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
   315  	return &Transaction{rawTx}, err
   316  }
   317  
   318  // Transactions represents a slice of transactions.
   319  type Transactions struct{ txs types.Transactions }
   320  
   321  // Size returns the number of transactions in the slice.
   322  func (txs *Transactions) Size() int {
   323  	return len(txs.txs)
   324  }
   325  
   326  // Get returns the transaction at the given index from the slice.
   327  func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
   328  	if index < 0 || index >= len(txs.txs) {
   329  		return nil, errors.New("index out of bounds")
   330  	}
   331  	return &Transaction{txs.txs[index]}, nil
   332  }
   333  
   334  // Receipt represents the results of a transaction.
   335  type Receipt struct {
   336  	receipt *types.Receipt
   337  }
   338  
   339  // NewReceiptFromRLP parses a transaction receipt from an RLP data dump.
   340  func NewReceiptFromRLP(data []byte) (*Receipt, error) {
   341  	r := &Receipt{
   342  		receipt: new(types.Receipt),
   343  	}
   344  	if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
   345  		return nil, err
   346  	}
   347  	return r, nil
   348  }
   349  
   350  // EncodeRLP encodes a transaction receipt into an RLP data dump.
   351  func (r *Receipt) EncodeRLP() ([]byte, error) {
   352  	return rlp.EncodeToBytes(r.receipt)
   353  }
   354  
   355  // NewReceiptFromJSON parses a transaction receipt from a JSON data dump.
   356  func NewReceiptFromJSON(data string) (*Receipt, error) {
   357  	r := &Receipt{
   358  		receipt: new(types.Receipt),
   359  	}
   360  	if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
   361  		return nil, err
   362  	}
   363  	return r, nil
   364  }
   365  
   366  // EncodeJSON encodes a transaction receipt into a JSON data dump.
   367  func (r *Receipt) EncodeJSON() (string, error) {
   368  	data, err := rlp.EncodeToBytes(r.receipt)
   369  	return string(data), err
   370  }
   371  
   372  // String returns a printable representation of the receipt.
   373  func (r *Receipt) String() string {
   374  	return encodeOrError(r)
   375  }
   376  
   377  func (r *Receipt) GetStatus() int               { return int(r.receipt.Status) }
   378  func (r *Receipt) GetPostState() []byte         { return r.receipt.PostState }
   379  func (r *Receipt) GetBloom() *Bloom             { return &Bloom{r.receipt.Bloom} }
   380  func (r *Receipt) GetLogs() *Logs               { return &Logs{r.receipt.Logs} }
   381  func (r *Receipt) GetTxHash() *Hash             { return &Hash{r.receipt.TxHash} }
   382  func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} }