github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/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 // NewTransaction creates a new transaction with the given properties. 136 func NewTransaction(nonce int64, to *Address, amount, gasLimit, gasPrice *BigInt, data []byte) *Transaction { 137 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, gasLimit.bigint, gasPrice.bigint, data)} 138 } 139 140 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 141 func (tx *Transaction) GetGas() int64 { return tx.tx.Gas().Int64() } 142 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 143 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 144 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 145 146 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 147 func (tx *Transaction) GetSigHash() *Hash { return &Hash{tx.tx.SigHash(types.HomesteadSigner{})} } 148 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 149 150 func (tx *Transaction) GetFrom() (address *Address, _ error) { 151 from, err := types.Sender(types.HomesteadSigner{}, tx.tx) 152 return &Address{from}, err 153 } 154 155 func (tx *Transaction) GetTo() *Address { 156 if to := tx.tx.To(); to != nil { 157 return &Address{*to} 158 } 159 return nil 160 } 161 162 func (tx *Transaction) WithSignature(sig []byte) (signedTx *Transaction, _ error) { 163 rawTx, err := tx.tx.WithSignature(types.HomesteadSigner{}, sig) 164 return &Transaction{rawTx}, err 165 } 166 167 // Transactions represents a slice of transactions. 168 type Transactions struct{ txs types.Transactions } 169 170 // Size returns the number of transactions in the slice. 171 func (txs *Transactions) Size() int { 172 return len(txs.txs) 173 } 174 175 // Get returns the transaction at the given index from the slice. 176 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 177 if index < 0 || index >= len(txs.txs) { 178 return nil, errors.New("index out of bounds") 179 } 180 return &Transaction{txs.txs[index]}, nil 181 } 182 183 // Receipt represents the results of a transaction. 184 type Receipt struct { 185 receipt *types.Receipt 186 } 187 188 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 189 func (r *Receipt) GetCumulativeGasUsed() *BigInt { return &BigInt{r.receipt.CumulativeGasUsed} } 190 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 191 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 192 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 193 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 194 func (r *Receipt) GetGasUsed() *BigInt { return &BigInt{r.receipt.GasUsed} }