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