github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/mobile/types.go (about)

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