github.com/codingfuture/orig-energi3@v0.8.4/mobile/types.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core 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 Energi Core 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 Energi Core 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/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/rlp" 30 whisper "github.com/ethereum/go-ethereum/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) GetSignature() []byte { return h.header.Signature } 116 func (h *Header) GetMixDigest() *Hash { return &Hash{h.header.MixDigest} } 117 func (h *Header) GetNonce() *Nonce { return &Nonce{h.header.Nonce} } 118 func (h *Header) GetHash() *Hash { return &Hash{h.header.Hash()} } 119 120 // Headers represents a slice of headers. 121 type Headers struct{ headers []*types.Header } 122 123 // Size returns the number of headers in the slice. 124 func (h *Headers) Size() int { 125 return len(h.headers) 126 } 127 128 // Get returns the header at the given index from the slice. 129 func (h *Headers) Get(index int) (header *Header, _ error) { 130 if index < 0 || index >= len(h.headers) { 131 return nil, errors.New("index out of bounds") 132 } 133 return &Header{h.headers[index]}, nil 134 } 135 136 // Block represents an entire block in the Ethereum blockchain. 137 type Block struct { 138 block *types.Block 139 } 140 141 // NewBlockFromRLP parses a block from an RLP data dump. 142 func NewBlockFromRLP(data []byte) (*Block, error) { 143 b := &Block{ 144 block: new(types.Block), 145 } 146 if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil { 147 return nil, err 148 } 149 return b, nil 150 } 151 152 // EncodeRLP encodes a block into an RLP data dump. 153 func (b *Block) EncodeRLP() ([]byte, error) { 154 return rlp.EncodeToBytes(b.block) 155 } 156 157 // NewBlockFromJSON parses a block from a JSON data dump. 158 func NewBlockFromJSON(data string) (*Block, error) { 159 b := &Block{ 160 block: new(types.Block), 161 } 162 if err := json.Unmarshal([]byte(data), b.block); err != nil { 163 return nil, err 164 } 165 return b, nil 166 } 167 168 // EncodeJSON encodes a block into a JSON data dump. 169 func (b *Block) EncodeJSON() (string, error) { 170 data, err := json.Marshal(b.block) 171 return string(data), err 172 } 173 174 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 175 func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} } 176 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 177 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 178 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 179 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 180 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 181 func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} } 182 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 183 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 184 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 185 func (b *Block) GetTime() int64 { return int64(b.block.Time()) } 186 func (b *Block) GetExtra() []byte { return b.block.Extra() } 187 func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} } 188 func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) } 189 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 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) } 340 341 // Info represents a diagnostic information about the whisper node. 342 type Info struct { 343 info *whisper.Info 344 } 345 346 // NewMessage represents a new whisper message that is posted through the RPC. 347 type NewMessage struct { 348 newMessage *whisper.NewMessage 349 } 350 351 func NewNewMessage() *NewMessage { 352 nm := &NewMessage{ 353 newMessage: new(whisper.NewMessage), 354 } 355 return nm 356 } 357 358 func (nm *NewMessage) GetSymKeyID() string { return nm.newMessage.SymKeyID } 359 func (nm *NewMessage) SetSymKeyID(symKeyID string) { nm.newMessage.SymKeyID = symKeyID } 360 func (nm *NewMessage) GetPublicKey() []byte { return nm.newMessage.PublicKey } 361 func (nm *NewMessage) SetPublicKey(publicKey []byte) { 362 nm.newMessage.PublicKey = common.CopyBytes(publicKey) 363 } 364 func (nm *NewMessage) GetSig() string { return nm.newMessage.Sig } 365 func (nm *NewMessage) SetSig(sig string) { nm.newMessage.Sig = sig } 366 func (nm *NewMessage) GetTTL() int64 { return int64(nm.newMessage.TTL) } 367 func (nm *NewMessage) SetTTL(ttl int64) { nm.newMessage.TTL = uint32(ttl) } 368 func (nm *NewMessage) GetPayload() []byte { return nm.newMessage.Payload } 369 func (nm *NewMessage) SetPayload(payload []byte) { nm.newMessage.Payload = common.CopyBytes(payload) } 370 func (nm *NewMessage) GetPowTime() int64 { return int64(nm.newMessage.PowTime) } 371 func (nm *NewMessage) SetPowTime(powTime int64) { nm.newMessage.PowTime = uint32(powTime) } 372 func (nm *NewMessage) GetPowTarget() float64 { return nm.newMessage.PowTarget } 373 func (nm *NewMessage) SetPowTarget(powTarget float64) { nm.newMessage.PowTarget = powTarget } 374 func (nm *NewMessage) GetTargetPeer() string { return nm.newMessage.TargetPeer } 375 func (nm *NewMessage) SetTargetPeer(targetPeer string) { nm.newMessage.TargetPeer = targetPeer } 376 func (nm *NewMessage) GetTopic() []byte { return nm.newMessage.Topic[:] } 377 func (nm *NewMessage) SetTopic(topic []byte) { nm.newMessage.Topic = whisper.BytesToTopic(topic) } 378 379 // Message represents a whisper message. 380 type Message struct { 381 message *whisper.Message 382 } 383 384 func (m *Message) GetSig() []byte { return m.message.Sig } 385 func (m *Message) GetTTL() int64 { return int64(m.message.TTL) } 386 func (m *Message) GetTimestamp() int64 { return int64(m.message.Timestamp) } 387 func (m *Message) GetPayload() []byte { return m.message.Payload } 388 func (m *Message) GetPoW() float64 { return m.message.PoW } 389 func (m *Message) GetHash() []byte { return m.message.Hash } 390 func (m *Message) GetDst() []byte { return m.message.Dst } 391 392 // Messages represents an array of messages. 393 type Messages struct { 394 messages []*whisper.Message 395 } 396 397 // Size returns the number of messages in the slice. 398 func (m *Messages) Size() int { 399 return len(m.messages) 400 } 401 402 // Get returns the message at the given index from the slice. 403 func (m *Messages) Get(index int) (message *Message, _ error) { 404 if index < 0 || index >= len(m.messages) { 405 return nil, errors.New("index out of bounds") 406 } 407 return &Message{m.messages[index]}, nil 408 } 409 410 // Criteria holds various filter options for inbound messages. 411 type Criteria struct { 412 criteria *whisper.Criteria 413 } 414 415 func NewCriteria(topic []byte) *Criteria { 416 c := &Criteria{ 417 criteria: new(whisper.Criteria), 418 } 419 encodedTopic := whisper.BytesToTopic(topic) 420 c.criteria.Topics = []whisper.TopicType{encodedTopic} 421 return c 422 } 423 424 func (c *Criteria) GetSymKeyID() string { return c.criteria.SymKeyID } 425 func (c *Criteria) SetSymKeyID(symKeyID string) { c.criteria.SymKeyID = symKeyID } 426 func (c *Criteria) GetPrivateKeyID() string { return c.criteria.PrivateKeyID } 427 func (c *Criteria) SetPrivateKeyID(privateKeyID string) { c.criteria.PrivateKeyID = privateKeyID } 428 func (c *Criteria) GetSig() []byte { return c.criteria.Sig } 429 func (c *Criteria) SetSig(sig []byte) { c.criteria.Sig = common.CopyBytes(sig) } 430 func (c *Criteria) GetMinPow() float64 { return c.criteria.MinPow } 431 func (c *Criteria) SetMinPow(pow float64) { c.criteria.MinPow = pow }