github.com/abschain-develop/go-abs@v2.0.3+incompatible/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/abschain-develop/go-abs/common" 27 "github.com/abschain-develop/go-abs/core/types" 28 "github.com/abschain-develop/go-abs/rlp" 29 whisper "github.com/abschain-develop/go-abs/whisper/whisperv6" 30 ) 31 32 // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that 33 // a sufficient amount of computation has been carried out on a block. 34 type Nonce struct { 35 nonce types.BlockNonce 36 } 37 38 // GetBytes retrieves the byte representation of the block nonce. 39 func (n *Nonce) GetBytes() []byte { 40 return n.nonce[:] 41 } 42 43 // GetHex retrieves the hex string representation of the block nonce. 44 func (n *Nonce) GetHex() string { 45 return fmt.Sprintf("0x%x", n.nonce[:]) 46 } 47 48 // Bloom represents a 256 bit bloom filter. 49 type Bloom struct { 50 bloom types.Bloom 51 } 52 53 // GetBytes retrieves the byte representation of the bloom filter. 54 func (b *Bloom) GetBytes() []byte { 55 return b.bloom[:] 56 } 57 58 // GetHex retrieves the hex string representation of the bloom filter. 59 func (b *Bloom) GetHex() string { 60 return fmt.Sprintf("0x%x", b.bloom[:]) 61 } 62 63 // Header represents a block header in the Ethereum blockchain. 64 type Header struct { 65 header *types.Header 66 } 67 68 // NewHeaderFromRLP parses a header from an RLP data dump. 69 func NewHeaderFromRLP(data []byte) (*Header, error) { 70 h := &Header{ 71 header: new(types.Header), 72 } 73 if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil { 74 return nil, err 75 } 76 return h, nil 77 } 78 79 // EncodeRLP encodes a header into an RLP data dump. 80 func (h *Header) EncodeRLP() ([]byte, error) { 81 return rlp.EncodeToBytes(h.header) 82 } 83 84 // NewHeaderFromJSON parses a header from a JSON data dump. 85 func NewHeaderFromJSON(data string) (*Header, error) { 86 h := &Header{ 87 header: new(types.Header), 88 } 89 if err := json.Unmarshal([]byte(data), h.header); err != nil { 90 return nil, err 91 } 92 return h, nil 93 } 94 95 // EncodeJSON encodes a header into a JSON data dump. 96 func (h *Header) EncodeJSON() (string, error) { 97 data, err := json.Marshal(h.header) 98 return string(data), err 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 int64(h.header.Time) } 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 Ethereum 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 a 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 a 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 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 173 func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} } 174 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 175 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 176 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 177 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 178 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 179 func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} } 180 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 181 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 182 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 183 func (b *Block) GetTime() int64 { return int64(b.block.Time()) } 184 func (b *Block) GetExtra() []byte { return b.block.Extra() } 185 func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} } 186 func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) } 187 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 188 func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} } 189 func (b *Block) GetUncles() *Headers { return &Headers{b.block.Uncles()} } 190 func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} } 191 func (b *Block) GetTransaction(hash *Hash) *Transaction { 192 return &Transaction{b.block.Transaction(hash.hash)} 193 } 194 195 // Transaction represents a single Ethereum transaction. 196 type Transaction struct { 197 tx *types.Transaction 198 } 199 200 // NewTransaction creates a new transaction with the given properties. 201 func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 202 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 203 } 204 205 // NewTransactionFromRLP parses a transaction from an RLP data dump. 206 func NewTransactionFromRLP(data []byte) (*Transaction, error) { 207 tx := &Transaction{ 208 tx: new(types.Transaction), 209 } 210 if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil { 211 return nil, err 212 } 213 return tx, nil 214 } 215 216 // EncodeRLP encodes a transaction into an RLP data dump. 217 func (tx *Transaction) EncodeRLP() ([]byte, error) { 218 return rlp.EncodeToBytes(tx.tx) 219 } 220 221 // NewTransactionFromJSON parses a transaction from a JSON data dump. 222 func NewTransactionFromJSON(data string) (*Transaction, error) { 223 tx := &Transaction{ 224 tx: new(types.Transaction), 225 } 226 if err := json.Unmarshal([]byte(data), tx.tx); err != nil { 227 return nil, err 228 } 229 return tx, nil 230 } 231 232 // EncodeJSON encodes a transaction into a JSON data dump. 233 func (tx *Transaction) EncodeJSON() (string, error) { 234 data, err := json.Marshal(tx.tx) 235 return string(data), err 236 } 237 238 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 239 func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) } 240 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 241 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 242 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 243 244 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 245 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 246 247 // Deprecated: GetSigHash cannot know which signer to use. 248 func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} } 249 250 // Deprecated: use EthereumClient.TransactionSender 251 func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) { 252 var signer types.Signer = types.HomesteadSigner{} 253 if chainID != nil { 254 signer = types.NewEIP155Signer(chainID.bigint) 255 } 256 from, err := types.Sender(signer, tx.tx) 257 return &Address{from}, err 258 } 259 260 func (tx *Transaction) GetTo() *Address { 261 if to := tx.tx.To(); to != nil { 262 return &Address{*to} 263 } 264 return nil 265 } 266 267 func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) { 268 var signer types.Signer = types.HomesteadSigner{} 269 if chainID != nil { 270 signer = types.NewEIP155Signer(chainID.bigint) 271 } 272 rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig)) 273 return &Transaction{rawTx}, err 274 } 275 276 // Transactions represents a slice of transactions. 277 type Transactions struct{ txs types.Transactions } 278 279 // Size returns the number of transactions in the slice. 280 func (txs *Transactions) Size() int { 281 return len(txs.txs) 282 } 283 284 // Get returns the transaction at the given index from the slice. 285 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 286 if index < 0 || index >= len(txs.txs) { 287 return nil, errors.New("index out of bounds") 288 } 289 return &Transaction{txs.txs[index]}, nil 290 } 291 292 // Receipt represents the results of a transaction. 293 type Receipt struct { 294 receipt *types.Receipt 295 } 296 297 // NewReceiptFromRLP parses a transaction receipt from an RLP data dump. 298 func NewReceiptFromRLP(data []byte) (*Receipt, error) { 299 r := &Receipt{ 300 receipt: new(types.Receipt), 301 } 302 if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil { 303 return nil, err 304 } 305 return r, nil 306 } 307 308 // EncodeRLP encodes a transaction receipt into an RLP data dump. 309 func (r *Receipt) EncodeRLP() ([]byte, error) { 310 return rlp.EncodeToBytes(r.receipt) 311 } 312 313 // NewReceiptFromJSON parses a transaction receipt from a JSON data dump. 314 func NewReceiptFromJSON(data string) (*Receipt, error) { 315 r := &Receipt{ 316 receipt: new(types.Receipt), 317 } 318 if err := json.Unmarshal([]byte(data), r.receipt); err != nil { 319 return nil, err 320 } 321 return r, nil 322 } 323 324 // EncodeJSON encodes a transaction receipt into a JSON data dump. 325 func (r *Receipt) EncodeJSON() (string, error) { 326 data, err := rlp.EncodeToBytes(r.receipt) 327 return string(data), err 328 } 329 330 func (r *Receipt) GetStatus() int { return int(r.receipt.Status) } 331 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 332 func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) } 333 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 334 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 335 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 336 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 337 func (r *Receipt) GetGasUsed() int64 { return int64(r.receipt.GasUsed) } 338 339 // Info represents a diagnostic information about the whisper node. 340 type Info struct { 341 info *whisper.Info 342 } 343 344 // NewMessage represents a new whisper message that is posted through the RPC. 345 type NewMessage struct { 346 newMessage *whisper.NewMessage 347 } 348 349 func NewNewMessage() *NewMessage { 350 nm := &NewMessage{ 351 newMessage: new(whisper.NewMessage), 352 } 353 return nm 354 } 355 356 func (nm *NewMessage) GetSymKeyID() string { return nm.newMessage.SymKeyID } 357 func (nm *NewMessage) SetSymKeyID(symKeyID string) { nm.newMessage.SymKeyID = symKeyID } 358 func (nm *NewMessage) GetPublicKey() []byte { return nm.newMessage.PublicKey } 359 func (nm *NewMessage) SetPublicKey(publicKey []byte) { 360 nm.newMessage.PublicKey = common.CopyBytes(publicKey) 361 } 362 func (nm *NewMessage) GetSig() string { return nm.newMessage.Sig } 363 func (nm *NewMessage) SetSig(sig string) { nm.newMessage.Sig = sig } 364 func (nm *NewMessage) GetTTL() int64 { return int64(nm.newMessage.TTL) } 365 func (nm *NewMessage) SetTTL(ttl int64) { nm.newMessage.TTL = uint32(ttl) } 366 func (nm *NewMessage) GetPayload() []byte { return nm.newMessage.Payload } 367 func (nm *NewMessage) SetPayload(payload []byte) { nm.newMessage.Payload = common.CopyBytes(payload) } 368 func (nm *NewMessage) GetPowTime() int64 { return int64(nm.newMessage.PowTime) } 369 func (nm *NewMessage) SetPowTime(powTime int64) { nm.newMessage.PowTime = uint32(powTime) } 370 func (nm *NewMessage) GetPowTarget() float64 { return nm.newMessage.PowTarget } 371 func (nm *NewMessage) SetPowTarget(powTarget float64) { nm.newMessage.PowTarget = powTarget } 372 func (nm *NewMessage) GetTargetPeer() string { return nm.newMessage.TargetPeer } 373 func (nm *NewMessage) SetTargetPeer(targetPeer string) { nm.newMessage.TargetPeer = targetPeer } 374 func (nm *NewMessage) GetTopic() []byte { return nm.newMessage.Topic[:] } 375 func (nm *NewMessage) SetTopic(topic []byte) { nm.newMessage.Topic = whisper.BytesToTopic(topic) } 376 377 // Message represents a whisper message. 378 type Message struct { 379 message *whisper.Message 380 } 381 382 func (m *Message) GetSig() []byte { return m.message.Sig } 383 func (m *Message) GetTTL() int64 { return int64(m.message.TTL) } 384 func (m *Message) GetTimestamp() int64 { return int64(m.message.Timestamp) } 385 func (m *Message) GetPayload() []byte { return m.message.Payload } 386 func (m *Message) GetPoW() float64 { return m.message.PoW } 387 func (m *Message) GetHash() []byte { return m.message.Hash } 388 func (m *Message) GetDst() []byte { return m.message.Dst } 389 390 // Messages represents an array of messages. 391 type Messages struct { 392 messages []*whisper.Message 393 } 394 395 // Size returns the number of messages in the slice. 396 func (m *Messages) Size() int { 397 return len(m.messages) 398 } 399 400 // Get returns the message at the given index from the slice. 401 func (m *Messages) Get(index int) (message *Message, _ error) { 402 if index < 0 || index >= len(m.messages) { 403 return nil, errors.New("index out of bounds") 404 } 405 return &Message{m.messages[index]}, nil 406 } 407 408 // Criteria holds various filter options for inbound messages. 409 type Criteria struct { 410 criteria *whisper.Criteria 411 } 412 413 func NewCriteria(topic []byte) *Criteria { 414 c := &Criteria{ 415 criteria: new(whisper.Criteria), 416 } 417 encodedTopic := whisper.BytesToTopic(topic) 418 c.criteria.Topics = []whisper.TopicType{encodedTopic} 419 return c 420 } 421 422 func (c *Criteria) GetSymKeyID() string { return c.criteria.SymKeyID } 423 func (c *Criteria) SetSymKeyID(symKeyID string) { c.criteria.SymKeyID = symKeyID } 424 func (c *Criteria) GetPrivateKeyID() string { return c.criteria.PrivateKeyID } 425 func (c *Criteria) SetPrivateKeyID(privateKeyID string) { c.criteria.PrivateKeyID = privateKeyID } 426 func (c *Criteria) GetSig() []byte { return c.criteria.Sig } 427 func (c *Criteria) SetSig(sig []byte) { c.criteria.Sig = common.CopyBytes(sig) } 428 func (c *Criteria) GetMinPow() float64 { return c.criteria.MinPow } 429 func (c *Criteria) SetMinPow(pow float64) { c.criteria.MinPow = pow }