github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  )
    27  
    28  // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
    29  // a sufficient amount of computation has been carried out on a block.
    30  type Nonce struct {
    31  	nonce types.BlockNonce
    32  }
    33  
    34  // GetBytes retrieves the byte representation of the block nonce.
    35  func (n *Nonce) GetBytes() []byte {
    36  	return n.nonce[:]
    37  }
    38  
    39  // GetHex retrieves the hex string representation of the block nonce.
    40  func (n *Nonce) GetHex() string {
    41  	return fmt.Sprintf("0x%x", n.nonce[:])
    42  }
    43  
    44  // Bloom represents a 256 bit bloom filter.
    45  type Bloom struct {
    46  	bloom types.Bloom
    47  }
    48  
    49  // GetBytes retrieves the byte representation of the bloom filter.
    50  func (b *Bloom) GetBytes() []byte {
    51  	return b.bloom[:]
    52  }
    53  
    54  // GetHex retrieves the hex string representation of the bloom filter.
    55  func (b *Bloom) GetHex() string {
    56  	return fmt.Sprintf("0x%x", b.bloom[:])
    57  }
    58  
    59  // Header represents a block header in the Ethereum blockchain.
    60  type Header struct {
    61  	header *types.Header
    62  }
    63  
    64  func (h *Header) GetParentHash() *Hash   { return &Hash{h.header.ParentHash} }
    65  func (h *Header) GetUncleHash() *Hash    { return &Hash{h.header.UncleHash} }
    66  func (h *Header) GetCoinbase() *Address  { return &Address{h.header.Coinbase} }
    67  func (h *Header) GetRoot() *Hash         { return &Hash{h.header.Root} }
    68  func (h *Header) GetTxHash() *Hash       { return &Hash{h.header.TxHash} }
    69  func (h *Header) GetReceiptHash() *Hash  { return &Hash{h.header.ReceiptHash} }
    70  func (h *Header) GetBloom() *Bloom       { return &Bloom{h.header.Bloom} }
    71  func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
    72  func (h *Header) GetNumber() int64       { return h.header.Number.Int64() }
    73  func (h *Header) GetGasLimit() int64     { return h.header.GasLimit.Int64() }
    74  func (h *Header) GetGasUsed() int64      { return h.header.GasUsed.Int64() }
    75  func (h *Header) GetTime() int64         { return h.header.Time.Int64() }
    76  func (h *Header) GetExtra() []byte       { return h.header.Extra }
    77  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
    78  func (h *Header) GetNonce() *Nonce       { return &Nonce{h.header.Nonce} }
    79  
    80  func (h *Header) GetHash() *Hash        { return &Hash{h.header.Hash()} }
    81  func (h *Header) GetHashNoNonce() *Hash { return &Hash{h.header.HashNoNonce()} }
    82  
    83  // Headers represents a slice of headers.
    84  type Headers struct{ headers []*types.Header }
    85  
    86  // Size returns the number of headers in the slice.
    87  func (h *Headers) Size() int {
    88  	return len(h.headers)
    89  }
    90  
    91  // Get returns the header at the given index from the slice.
    92  func (h *Headers) Get(index int) (header *Header, _ error) {
    93  	if index < 0 || index >= len(h.headers) {
    94  		return nil, errors.New("index out of bounds")
    95  	}
    96  	return &Header{h.headers[index]}, nil
    97  }
    98  
    99  // Block represents an entire block in the Ethereum blockchain.
   100  type Block struct {
   101  	block *types.Block
   102  }
   103  
   104  func (b *Block) GetParentHash() *Hash   { return &Hash{b.block.ParentHash()} }
   105  func (b *Block) GetUncleHash() *Hash    { return &Hash{b.block.UncleHash()} }
   106  func (b *Block) GetCoinbase() *Address  { return &Address{b.block.Coinbase()} }
   107  func (b *Block) GetRoot() *Hash         { return &Hash{b.block.Root()} }
   108  func (b *Block) GetTxHash() *Hash       { return &Hash{b.block.TxHash()} }
   109  func (b *Block) GetReceiptHash() *Hash  { return &Hash{b.block.ReceiptHash()} }
   110  func (b *Block) GetBloom() *Bloom       { return &Bloom{b.block.Bloom()} }
   111  func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} }
   112  func (b *Block) GetNumber() int64       { return b.block.Number().Int64() }
   113  func (b *Block) GetGasLimit() int64     { return b.block.GasLimit().Int64() }
   114  func (b *Block) GetGasUsed() int64      { return b.block.GasUsed().Int64() }
   115  func (b *Block) GetTime() int64         { return b.block.Time().Int64() }
   116  func (b *Block) GetExtra() []byte       { return b.block.Extra() }
   117  func (b *Block) GetMixDigest() *Hash    { return &Hash{b.block.MixDigest()} }
   118  func (b *Block) GetNonce() int64        { return int64(b.block.Nonce()) }
   119  
   120  func (b *Block) GetHash() *Hash        { return &Hash{b.block.Hash()} }
   121  func (b *Block) GetHashNoNonce() *Hash { return &Hash{b.block.HashNoNonce()} }
   122  
   123  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   124  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   125  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   126  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   127  	return &Transaction{b.block.Transaction(hash.hash)}
   128  }
   129  
   130  // Transaction represents a single Ethereum transaction.
   131  type Transaction struct {
   132  	tx *types.Transaction
   133  }
   134  
   135  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   136  func (tx *Transaction) GetGas() int64        { return tx.tx.Gas().Int64() }
   137  func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
   138  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   139  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   140  
   141  func (tx *Transaction) GetHash() *Hash    { return &Hash{tx.tx.Hash()} }
   142  func (tx *Transaction) GetSigHash() *Hash { return &Hash{tx.tx.SigHash(types.HomesteadSigner{})} }
   143  func (tx *Transaction) GetCost() *BigInt  { return &BigInt{tx.tx.Cost()} }
   144  
   145  func (tx *Transaction) GetFrom() (address *Address, _ error) {
   146  	from, err := types.Sender(types.HomesteadSigner{}, tx.tx)
   147  	return &Address{from}, err
   148  }
   149  
   150  func (tx *Transaction) GetTo() *Address {
   151  	if to := tx.tx.To(); to != nil {
   152  		return &Address{*to}
   153  	}
   154  	return nil
   155  }
   156  
   157  func (tx *Transaction) WithSignature(sig []byte) (signedTx *Transaction, _ error) {
   158  	rawTx, err := tx.tx.WithSignature(types.HomesteadSigner{}, sig)
   159  	return &Transaction{rawTx}, err
   160  }
   161  
   162  // Transactions represents a slice of transactions.
   163  type Transactions struct{ txs types.Transactions }
   164  
   165  // Size returns the number of transactions in the slice.
   166  func (txs *Transactions) Size() int {
   167  	return len(txs.txs)
   168  }
   169  
   170  // Get returns the transaction at the given index from the slice.
   171  func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
   172  	if index < 0 || index >= len(txs.txs) {
   173  		return nil, errors.New("index out of bounds")
   174  	}
   175  	return &Transaction{txs.txs[index]}, nil
   176  }
   177  
   178  // Receipt represents the results of a transaction.
   179  type Receipt struct {
   180  	receipt *types.Receipt
   181  }
   182  
   183  func (r *Receipt) GetPostState() []byte          { return r.receipt.PostState }
   184  func (r *Receipt) GetCumulativeGasUsed() *BigInt { return &BigInt{r.receipt.CumulativeGasUsed} }
   185  func (r *Receipt) GetBloom() *Bloom              { return &Bloom{r.receipt.Bloom} }
   186  func (r *Receipt) GetLogs() *Logs                { return &Logs{r.receipt.Logs} }
   187  func (r *Receipt) GetTxHash() *Hash              { return &Hash{r.receipt.TxHash} }
   188  func (r *Receipt) GetContractAddress() *Address  { return &Address{r.receipt.ContractAddress} }
   189  func (r *Receipt) GetGasUsed() *BigInt           { return &BigInt{r.receipt.GasUsed} }