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