github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/types.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // Contains all the wrappers from the core/types package. 13 14 package geth 15 16 import ( 17 "encoding/json" 18 "errors" 19 "fmt" 20 21 "github.com/Sberex/go-sberex/common" 22 "github.com/Sberex/go-sberex/core/types" 23 "github.com/Sberex/go-sberex/rlp" 24 ) 25 26 // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that 27 // a sufficient amount of computation has been carried out on a block. 28 type Nonce struct { 29 nonce types.BlockNonce 30 } 31 32 // GetBytes retrieves the byte representation of the block nonce. 33 func (n *Nonce) GetBytes() []byte { 34 return n.nonce[:] 35 } 36 37 // GetHex retrieves the hex string representation of the block nonce. 38 func (n *Nonce) GetHex() string { 39 return fmt.Sprintf("0x%x", n.nonce[:]) 40 } 41 42 // Bloom represents a 256 bit bloom filter. 43 type Bloom struct { 44 bloom types.Bloom 45 } 46 47 // GetBytes retrieves the byte representation of the bloom filter. 48 func (b *Bloom) GetBytes() []byte { 49 return b.bloom[:] 50 } 51 52 // GetHex retrieves the hex string representation of the bloom filter. 53 func (b *Bloom) GetHex() string { 54 return fmt.Sprintf("0x%x", b.bloom[:]) 55 } 56 57 // Header represents a block header in the Sberex blockchain. 58 type Header struct { 59 header *types.Header 60 } 61 62 // NewHeaderFromRLP parses a header from an RLP data dump. 63 func NewHeaderFromRLP(data []byte) (*Header, error) { 64 h := &Header{ 65 header: new(types.Header), 66 } 67 if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil { 68 return nil, err 69 } 70 return h, nil 71 } 72 73 // EncodeRLP encodes a header into an RLP data dump. 74 func (h *Header) EncodeRLP() ([]byte, error) { 75 return rlp.EncodeToBytes(h.header) 76 } 77 78 // NewHeaderFromJSON parses a header from an JSON data dump. 79 func NewHeaderFromJSON(data string) (*Header, error) { 80 h := &Header{ 81 header: new(types.Header), 82 } 83 if err := json.Unmarshal([]byte(data), h.header); err != nil { 84 return nil, err 85 } 86 return h, nil 87 } 88 89 // EncodeJSON encodes a header into an JSON data dump. 90 func (h *Header) EncodeJSON() (string, error) { 91 data, err := json.Marshal(h.header) 92 return string(data), err 93 } 94 95 // String implements the fmt.Stringer interface to print some semi-meaningful 96 // data dump of the header for debugging purposes. 97 func (h *Header) String() string { 98 return h.header.String() 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 h.header.Time.Int64() } 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 Sberex 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 an 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 an 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 // String implements the fmt.Stringer interface to print some semi-meaningful 173 // data dump of the block for debugging purposes. 174 func (b *Block) String() string { 175 return b.block.String() 176 } 177 178 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 179 func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} } 180 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 181 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 182 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 183 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 184 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 185 func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} } 186 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 187 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 188 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 189 func (b *Block) GetTime() int64 { return b.block.Time().Int64() } 190 func (b *Block) GetExtra() []byte { return b.block.Extra() } 191 func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} } 192 func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) } 193 194 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 195 func (b *Block) GetHashNoNonce() *Hash { return &Hash{b.block.HashNoNonce()} } 196 197 func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} } 198 func (b *Block) GetUncles() *Headers { return &Headers{b.block.Uncles()} } 199 func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} } 200 func (b *Block) GetTransaction(hash *Hash) *Transaction { 201 return &Transaction{b.block.Transaction(hash.hash)} 202 } 203 204 // Transaction represents a single Sberex transaction. 205 type Transaction struct { 206 tx *types.Transaction 207 } 208 209 // NewTransaction creates a new transaction with the given properties. 210 func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 211 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 212 } 213 214 // NewTransactionFromRLP parses a transaction from an RLP data dump. 215 func NewTransactionFromRLP(data []byte) (*Transaction, error) { 216 tx := &Transaction{ 217 tx: new(types.Transaction), 218 } 219 if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil { 220 return nil, err 221 } 222 return tx, nil 223 } 224 225 // EncodeRLP encodes a transaction into an RLP data dump. 226 func (tx *Transaction) EncodeRLP() ([]byte, error) { 227 return rlp.EncodeToBytes(tx.tx) 228 } 229 230 // NewTransactionFromJSON parses a transaction from an JSON data dump. 231 func NewTransactionFromJSON(data string) (*Transaction, error) { 232 tx := &Transaction{ 233 tx: new(types.Transaction), 234 } 235 if err := json.Unmarshal([]byte(data), tx.tx); err != nil { 236 return nil, err 237 } 238 return tx, nil 239 } 240 241 // EncodeJSON encodes a transaction into an JSON data dump. 242 func (tx *Transaction) EncodeJSON() (string, error) { 243 data, err := json.Marshal(tx.tx) 244 return string(data), err 245 } 246 247 // String implements the fmt.Stringer interface to print some semi-meaningful 248 // data dump of the transaction for debugging purposes. 249 func (tx *Transaction) String() string { 250 return tx.tx.String() 251 } 252 253 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 254 func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) } 255 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 256 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 257 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 258 259 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 260 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 261 262 // Deprecated: GetSigHash cannot know which signer to use. 263 func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} } 264 265 // Deprecated: use SberexClient.TransactionSender 266 func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) { 267 var signer types.Signer = types.HomesteadSigner{} 268 if chainID != nil { 269 signer = types.NewEIP155Signer(chainID.bigint) 270 } 271 from, err := types.Sender(signer, tx.tx) 272 return &Address{from}, err 273 } 274 275 func (tx *Transaction) GetTo() *Address { 276 if to := tx.tx.To(); to != nil { 277 return &Address{*to} 278 } 279 return nil 280 } 281 282 func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) { 283 var signer types.Signer = types.HomesteadSigner{} 284 if chainID != nil { 285 signer = types.NewEIP155Signer(chainID.bigint) 286 } 287 rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig)) 288 return &Transaction{rawTx}, err 289 } 290 291 // Transactions represents a slice of transactions. 292 type Transactions struct{ txs types.Transactions } 293 294 // Size returns the number of transactions in the slice. 295 func (txs *Transactions) Size() int { 296 return len(txs.txs) 297 } 298 299 // Get returns the transaction at the given index from the slice. 300 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 301 if index < 0 || index >= len(txs.txs) { 302 return nil, errors.New("index out of bounds") 303 } 304 return &Transaction{txs.txs[index]}, nil 305 } 306 307 // Receipt represents the results of a transaction. 308 type Receipt struct { 309 receipt *types.Receipt 310 } 311 312 // NewReceiptFromRLP parses a transaction receipt from an RLP data dump. 313 func NewReceiptFromRLP(data []byte) (*Receipt, error) { 314 r := &Receipt{ 315 receipt: new(types.Receipt), 316 } 317 if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil { 318 return nil, err 319 } 320 return r, nil 321 } 322 323 // EncodeRLP encodes a transaction receipt into an RLP data dump. 324 func (r *Receipt) EncodeRLP() ([]byte, error) { 325 return rlp.EncodeToBytes(r.receipt) 326 } 327 328 // NewReceiptFromJSON parses a transaction receipt from an JSON data dump. 329 func NewReceiptFromJSON(data string) (*Receipt, error) { 330 r := &Receipt{ 331 receipt: new(types.Receipt), 332 } 333 if err := json.Unmarshal([]byte(data), r.receipt); err != nil { 334 return nil, err 335 } 336 return r, nil 337 } 338 339 // EncodeJSON encodes a transaction receipt into an JSON data dump. 340 func (r *Receipt) EncodeJSON() (string, error) { 341 data, err := rlp.EncodeToBytes(r.receipt) 342 return string(data), err 343 } 344 345 // String implements the fmt.Stringer interface to print some semi-meaningful 346 // data dump of the transaction receipt for debugging purposes. 347 func (r *Receipt) String() string { 348 return r.receipt.String() 349 } 350 351 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 352 func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) } 353 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 354 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 355 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 356 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 357 func (r *Receipt) GetGasUsed() int64 { return int64(r.receipt.GasUsed) }