github.com/p202io/bor@v0.1.4/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/maticnetwork/bor/common"
    27  	"github.com/maticnetwork/bor/core/types"
    28  	"github.com/maticnetwork/bor/rlp"
    29  	whisper "github.com/maticnetwork/bor/whisper/whisperv6"
    30  )
    31  
    32  // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
    33  // a sufficient amount of computation has been carried out on a block.
    34  type Nonce struct {
    35  	nonce types.BlockNonce
    36  }
    37  
    38  // GetBytes retrieves the byte representation of the block nonce.
    39  func (n *Nonce) GetBytes() []byte {
    40  	return n.nonce[:]
    41  }
    42  
    43  // GetHex retrieves the hex string representation of the block nonce.
    44  func (n *Nonce) GetHex() string {
    45  	return fmt.Sprintf("0x%x", n.nonce[:])
    46  }
    47  
    48  // Bloom represents a 256 bit bloom filter.
    49  type Bloom struct {
    50  	bloom types.Bloom
    51  }
    52  
    53  // GetBytes retrieves the byte representation of the bloom filter.
    54  func (b *Bloom) GetBytes() []byte {
    55  	return b.bloom[:]
    56  }
    57  
    58  // GetHex retrieves the hex string representation of the bloom filter.
    59  func (b *Bloom) GetHex() string {
    60  	return fmt.Sprintf("0x%x", b.bloom[:])
    61  }
    62  
    63  // Header represents a block header in the Ethereum blockchain.
    64  type Header struct {
    65  	header *types.Header
    66  }
    67  
    68  // NewHeaderFromRLP parses a header from an RLP data dump.
    69  func NewHeaderFromRLP(data []byte) (*Header, error) {
    70  	h := &Header{
    71  		header: new(types.Header),
    72  	}
    73  	if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
    74  		return nil, err
    75  	}
    76  	return h, nil
    77  }
    78  
    79  // EncodeRLP encodes a header into an RLP data dump.
    80  func (h *Header) EncodeRLP() ([]byte, error) {
    81  	return rlp.EncodeToBytes(h.header)
    82  }
    83  
    84  // NewHeaderFromJSON parses a header from a JSON data dump.
    85  func NewHeaderFromJSON(data string) (*Header, error) {
    86  	h := &Header{
    87  		header: new(types.Header),
    88  	}
    89  	if err := json.Unmarshal([]byte(data), h.header); err != nil {
    90  		return nil, err
    91  	}
    92  	return h, nil
    93  }
    94  
    95  // EncodeJSON encodes a header into a JSON data dump.
    96  func (h *Header) EncodeJSON() (string, error) {
    97  	data, err := json.Marshal(h.header)
    98  	return string(data), err
    99  }
   100  
   101  func (h *Header) GetParentHash() *Hash   { return &Hash{h.header.ParentHash} }
   102  func (h *Header) GetUncleHash() *Hash    { return &Hash{h.header.UncleHash} }
   103  func (h *Header) GetCoinbase() *Address  { return &Address{h.header.Coinbase} }
   104  func (h *Header) GetRoot() *Hash         { return &Hash{h.header.Root} }
   105  func (h *Header) GetTxHash() *Hash       { return &Hash{h.header.TxHash} }
   106  func (h *Header) GetReceiptHash() *Hash  { return &Hash{h.header.ReceiptHash} }
   107  func (h *Header) GetBloom() *Bloom       { return &Bloom{h.header.Bloom} }
   108  func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
   109  func (h *Header) GetNumber() int64       { return h.header.Number.Int64() }
   110  func (h *Header) GetGasLimit() int64     { return int64(h.header.GasLimit) }
   111  func (h *Header) GetGasUsed() int64      { return int64(h.header.GasUsed) }
   112  func (h *Header) GetTime() int64         { return int64(h.header.Time) }
   113  func (h *Header) GetExtra() []byte       { return h.header.Extra }
   114  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
   115  func (h *Header) GetNonce() *Nonce       { return &Nonce{h.header.Nonce} }
   116  func (h *Header) GetHash() *Hash         { return &Hash{h.header.Hash()} }
   117  
   118  // Headers represents a slice of headers.
   119  type Headers struct{ headers []*types.Header }
   120  
   121  // Size returns the number of headers in the slice.
   122  func (h *Headers) Size() int {
   123  	return len(h.headers)
   124  }
   125  
   126  // Get returns the header at the given index from the slice.
   127  func (h *Headers) Get(index int) (header *Header, _ error) {
   128  	if index < 0 || index >= len(h.headers) {
   129  		return nil, errors.New("index out of bounds")
   130  	}
   131  	return &Header{h.headers[index]}, nil
   132  }
   133  
   134  // Block represents an entire block in the Ethereum blockchain.
   135  type Block struct {
   136  	block *types.Block
   137  }
   138  
   139  // NewBlockFromRLP parses a block from an RLP data dump.
   140  func NewBlockFromRLP(data []byte) (*Block, error) {
   141  	b := &Block{
   142  		block: new(types.Block),
   143  	}
   144  	if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
   145  		return nil, err
   146  	}
   147  	return b, nil
   148  }
   149  
   150  // EncodeRLP encodes a block into an RLP data dump.
   151  func (b *Block) EncodeRLP() ([]byte, error) {
   152  	return rlp.EncodeToBytes(b.block)
   153  }
   154  
   155  // NewBlockFromJSON parses a block from a JSON data dump.
   156  func NewBlockFromJSON(data string) (*Block, error) {
   157  	b := &Block{
   158  		block: new(types.Block),
   159  	}
   160  	if err := json.Unmarshal([]byte(data), b.block); err != nil {
   161  		return nil, err
   162  	}
   163  	return b, nil
   164  }
   165  
   166  // EncodeJSON encodes a block into a JSON data dump.
   167  func (b *Block) EncodeJSON() (string, error) {
   168  	data, err := json.Marshal(b.block)
   169  	return string(data), err
   170  }
   171  
   172  func (b *Block) GetParentHash() *Hash           { return &Hash{b.block.ParentHash()} }
   173  func (b *Block) GetUncleHash() *Hash            { return &Hash{b.block.UncleHash()} }
   174  func (b *Block) GetCoinbase() *Address          { return &Address{b.block.Coinbase()} }
   175  func (b *Block) GetRoot() *Hash                 { return &Hash{b.block.Root()} }
   176  func (b *Block) GetTxHash() *Hash               { return &Hash{b.block.TxHash()} }
   177  func (b *Block) GetReceiptHash() *Hash          { return &Hash{b.block.ReceiptHash()} }
   178  func (b *Block) GetBloom() *Bloom               { return &Bloom{b.block.Bloom()} }
   179  func (b *Block) GetDifficulty() *BigInt         { return &BigInt{b.block.Difficulty()} }
   180  func (b *Block) GetNumber() int64               { return b.block.Number().Int64() }
   181  func (b *Block) GetGasLimit() int64             { return int64(b.block.GasLimit()) }
   182  func (b *Block) GetGasUsed() int64              { return int64(b.block.GasUsed()) }
   183  func (b *Block) GetTime() int64                 { return int64(b.block.Time()) }
   184  func (b *Block) GetExtra() []byte               { return b.block.Extra() }
   185  func (b *Block) GetMixDigest() *Hash            { return &Hash{b.block.MixDigest()} }
   186  func (b *Block) GetNonce() int64                { return int64(b.block.Nonce()) }
   187  func (b *Block) GetHash() *Hash                 { return &Hash{b.block.Hash()} }
   188  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   189  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   190  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   191  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   192  	return &Transaction{b.block.Transaction(hash.hash)}
   193  }
   194  
   195  // Transaction represents a single Ethereum transaction.
   196  type Transaction struct {
   197  	tx *types.Transaction
   198  }
   199  
   200  // NewContractCreation creates a new transaction for deploying a new contract with
   201  // the given properties.
   202  func NewContractCreation(nonce int64, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
   203  	return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   204  }
   205  
   206  // NewTransaction creates a new transaction with the given properties. Contracts
   207  // can be created by transacting with a nil recipient.
   208  func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
   209  	if to == nil {
   210  		return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   211  	}
   212  	return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   213  }
   214  
   215  // NewTransactionFromRLP parses a transaction from an RLP data dump.
   216  func NewTransactionFromRLP(data []byte) (*Transaction, error) {
   217  	tx := &Transaction{
   218  		tx: new(types.Transaction),
   219  	}
   220  	if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
   221  		return nil, err
   222  	}
   223  	return tx, nil
   224  }
   225  
   226  // EncodeRLP encodes a transaction into an RLP data dump.
   227  func (tx *Transaction) EncodeRLP() ([]byte, error) {
   228  	return rlp.EncodeToBytes(tx.tx)
   229  }
   230  
   231  // NewTransactionFromJSON parses a transaction from a JSON data dump.
   232  func NewTransactionFromJSON(data string) (*Transaction, error) {
   233  	tx := &Transaction{
   234  		tx: new(types.Transaction),
   235  	}
   236  	if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
   237  		return nil, err
   238  	}
   239  	return tx, nil
   240  }
   241  
   242  // EncodeJSON encodes a transaction into a JSON data dump.
   243  func (tx *Transaction) EncodeJSON() (string, error) {
   244  	data, err := json.Marshal(tx.tx)
   245  	return string(data), err
   246  }
   247  
   248  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   249  func (tx *Transaction) GetGas() int64        { return int64(tx.tx.Gas()) }
   250  func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
   251  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   252  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   253  
   254  func (tx *Transaction) GetHash() *Hash   { return &Hash{tx.tx.Hash()} }
   255  func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
   256  
   257  // Deprecated: GetSigHash cannot know which signer to use.
   258  func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
   259  
   260  // Deprecated: use EthereumClient.TransactionSender
   261  func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
   262  	var signer types.Signer = types.HomesteadSigner{}
   263  	if chainID != nil {
   264  		signer = types.NewEIP155Signer(chainID.bigint)
   265  	}
   266  	from, err := types.Sender(signer, tx.tx)
   267  	return &Address{from}, err
   268  }
   269  
   270  func (tx *Transaction) GetTo() *Address {
   271  	if to := tx.tx.To(); to != nil {
   272  		return &Address{*to}
   273  	}
   274  	return nil
   275  }
   276  
   277  func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
   278  	var signer types.Signer = types.HomesteadSigner{}
   279  	if chainID != nil {
   280  		signer = types.NewEIP155Signer(chainID.bigint)
   281  	}
   282  	rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
   283  	return &Transaction{rawTx}, err
   284  }
   285  
   286  // Transactions represents a slice of transactions.
   287  type Transactions struct{ txs types.Transactions }
   288  
   289  // Size returns the number of transactions in the slice.
   290  func (txs *Transactions) Size() int {
   291  	return len(txs.txs)
   292  }
   293  
   294  // Get returns the transaction at the given index from the slice.
   295  func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
   296  	if index < 0 || index >= len(txs.txs) {
   297  		return nil, errors.New("index out of bounds")
   298  	}
   299  	return &Transaction{txs.txs[index]}, nil
   300  }
   301  
   302  // Receipt represents the results of a transaction.
   303  type Receipt struct {
   304  	receipt *types.Receipt
   305  }
   306  
   307  // NewReceiptFromRLP parses a transaction receipt from an RLP data dump.
   308  func NewReceiptFromRLP(data []byte) (*Receipt, error) {
   309  	r := &Receipt{
   310  		receipt: new(types.Receipt),
   311  	}
   312  	if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
   313  		return nil, err
   314  	}
   315  	return r, nil
   316  }
   317  
   318  // EncodeRLP encodes a transaction receipt into an RLP data dump.
   319  func (r *Receipt) EncodeRLP() ([]byte, error) {
   320  	return rlp.EncodeToBytes(r.receipt)
   321  }
   322  
   323  // NewReceiptFromJSON parses a transaction receipt from a JSON data dump.
   324  func NewReceiptFromJSON(data string) (*Receipt, error) {
   325  	r := &Receipt{
   326  		receipt: new(types.Receipt),
   327  	}
   328  	if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
   329  		return nil, err
   330  	}
   331  	return r, nil
   332  }
   333  
   334  // EncodeJSON encodes a transaction receipt into a JSON data dump.
   335  func (r *Receipt) EncodeJSON() (string, error) {
   336  	data, err := rlp.EncodeToBytes(r.receipt)
   337  	return string(data), err
   338  }
   339  
   340  func (r *Receipt) GetStatus() int               { return int(r.receipt.Status) }
   341  func (r *Receipt) GetPostState() []byte         { return r.receipt.PostState }
   342  func (r *Receipt) GetCumulativeGasUsed() int64  { return int64(r.receipt.CumulativeGasUsed) }
   343  func (r *Receipt) GetBloom() *Bloom             { return &Bloom{r.receipt.Bloom} }
   344  func (r *Receipt) GetLogs() *Logs               { return &Logs{r.receipt.Logs} }
   345  func (r *Receipt) GetTxHash() *Hash             { return &Hash{r.receipt.TxHash} }
   346  func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} }
   347  func (r *Receipt) GetGasUsed() int64            { return int64(r.receipt.GasUsed) }
   348  
   349  // Info represents a diagnostic information about the whisper node.
   350  type Info struct {
   351  	info *whisper.Info
   352  }
   353  
   354  // NewMessage represents a new whisper message that is posted through the RPC.
   355  type NewMessage struct {
   356  	newMessage *whisper.NewMessage
   357  }
   358  
   359  func NewNewMessage() *NewMessage {
   360  	nm := &NewMessage{
   361  		newMessage: new(whisper.NewMessage),
   362  	}
   363  	return nm
   364  }
   365  
   366  func (nm *NewMessage) GetSymKeyID() string         { return nm.newMessage.SymKeyID }
   367  func (nm *NewMessage) SetSymKeyID(symKeyID string) { nm.newMessage.SymKeyID = symKeyID }
   368  func (nm *NewMessage) GetPublicKey() []byte        { return nm.newMessage.PublicKey }
   369  func (nm *NewMessage) SetPublicKey(publicKey []byte) {
   370  	nm.newMessage.PublicKey = common.CopyBytes(publicKey)
   371  }
   372  func (nm *NewMessage) GetSig() string                  { return nm.newMessage.Sig }
   373  func (nm *NewMessage) SetSig(sig string)               { nm.newMessage.Sig = sig }
   374  func (nm *NewMessage) GetTTL() int64                   { return int64(nm.newMessage.TTL) }
   375  func (nm *NewMessage) SetTTL(ttl int64)                { nm.newMessage.TTL = uint32(ttl) }
   376  func (nm *NewMessage) GetPayload() []byte              { return nm.newMessage.Payload }
   377  func (nm *NewMessage) SetPayload(payload []byte)       { nm.newMessage.Payload = common.CopyBytes(payload) }
   378  func (nm *NewMessage) GetPowTime() int64               { return int64(nm.newMessage.PowTime) }
   379  func (nm *NewMessage) SetPowTime(powTime int64)        { nm.newMessage.PowTime = uint32(powTime) }
   380  func (nm *NewMessage) GetPowTarget() float64           { return nm.newMessage.PowTarget }
   381  func (nm *NewMessage) SetPowTarget(powTarget float64)  { nm.newMessage.PowTarget = powTarget }
   382  func (nm *NewMessage) GetTargetPeer() string           { return nm.newMessage.TargetPeer }
   383  func (nm *NewMessage) SetTargetPeer(targetPeer string) { nm.newMessage.TargetPeer = targetPeer }
   384  func (nm *NewMessage) GetTopic() []byte                { return nm.newMessage.Topic[:] }
   385  func (nm *NewMessage) SetTopic(topic []byte)           { nm.newMessage.Topic = whisper.BytesToTopic(topic) }
   386  
   387  // Message represents a whisper message.
   388  type Message struct {
   389  	message *whisper.Message
   390  }
   391  
   392  func (m *Message) GetSig() []byte      { return m.message.Sig }
   393  func (m *Message) GetTTL() int64       { return int64(m.message.TTL) }
   394  func (m *Message) GetTimestamp() int64 { return int64(m.message.Timestamp) }
   395  func (m *Message) GetPayload() []byte  { return m.message.Payload }
   396  func (m *Message) GetPoW() float64     { return m.message.PoW }
   397  func (m *Message) GetHash() []byte     { return m.message.Hash }
   398  func (m *Message) GetDst() []byte      { return m.message.Dst }
   399  
   400  // Messages represents an array of messages.
   401  type Messages struct {
   402  	messages []*whisper.Message
   403  }
   404  
   405  // Size returns the number of messages in the slice.
   406  func (m *Messages) Size() int {
   407  	return len(m.messages)
   408  }
   409  
   410  // Get returns the message at the given index from the slice.
   411  func (m *Messages) Get(index int) (message *Message, _ error) {
   412  	if index < 0 || index >= len(m.messages) {
   413  		return nil, errors.New("index out of bounds")
   414  	}
   415  	return &Message{m.messages[index]}, nil
   416  }
   417  
   418  // Criteria holds various filter options for inbound messages.
   419  type Criteria struct {
   420  	criteria *whisper.Criteria
   421  }
   422  
   423  func NewCriteria(topic []byte) *Criteria {
   424  	c := &Criteria{
   425  		criteria: new(whisper.Criteria),
   426  	}
   427  	encodedTopic := whisper.BytesToTopic(topic)
   428  	c.criteria.Topics = []whisper.TopicType{encodedTopic}
   429  	return c
   430  }
   431  
   432  func (c *Criteria) GetSymKeyID() string                 { return c.criteria.SymKeyID }
   433  func (c *Criteria) SetSymKeyID(symKeyID string)         { c.criteria.SymKeyID = symKeyID }
   434  func (c *Criteria) GetPrivateKeyID() string             { return c.criteria.PrivateKeyID }
   435  func (c *Criteria) SetPrivateKeyID(privateKeyID string) { c.criteria.PrivateKeyID = privateKeyID }
   436  func (c *Criteria) GetSig() []byte                      { return c.criteria.Sig }
   437  func (c *Criteria) SetSig(sig []byte)                   { c.criteria.Sig = common.CopyBytes(sig) }
   438  func (c *Criteria) GetMinPow() float64                  { return c.criteria.MinPow }
   439  func (c *Criteria) SetMinPow(pow float64)               { c.criteria.MinPow = pow }