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