github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/mobile/types.go (about) 1 // Copyright 2019 The ebakus/go-ebakus Authors 2 // This file is part of the ebakus/go-ebakus library. 3 // 4 // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Contains all the wrappers from the core/types package. 18 19 package ebakus 20 21 import ( 22 "encoding/json" 23 "errors" 24 "fmt" 25 26 "github.com/ebakus/go-ebakus/common" 27 "github.com/ebakus/go-ebakus/core/types" 28 "github.com/ebakus/go-ebakus/rlp" 29 whisper "github.com/ebakus/go-ebakus/whisper/whisperv6" 30 ) 31 32 // Bloom represents a 256 bit bloom filter. 33 type Bloom struct { 34 bloom types.Bloom 35 } 36 37 // GetBytes retrieves the byte representation of the bloom filter. 38 func (b *Bloom) GetBytes() []byte { 39 return b.bloom[:] 40 } 41 42 // GetHex retrieves the hex string representation of the bloom filter. 43 func (b *Bloom) GetHex() string { 44 return fmt.Sprintf("0x%x", b.bloom[:]) 45 } 46 47 // Header represents a block header in the Ebakus blockchain. 48 type Header struct { 49 header *types.Header 50 } 51 52 // NewHeaderFromRLP parses a header from an RLP data dump. 53 func NewHeaderFromRLP(data []byte) (*Header, error) { 54 h := &Header{ 55 header: new(types.Header), 56 } 57 if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil { 58 return nil, err 59 } 60 return h, nil 61 } 62 63 // EncodeRLP encodes a header into an RLP data dump. 64 func (h *Header) EncodeRLP() ([]byte, error) { 65 return rlp.EncodeToBytes(h.header) 66 } 67 68 // NewHeaderFromJSON parses a header from a JSON data dump. 69 func NewHeaderFromJSON(data string) (*Header, error) { 70 h := &Header{ 71 header: new(types.Header), 72 } 73 if err := json.Unmarshal([]byte(data), h.header); err != nil { 74 return nil, err 75 } 76 return h, nil 77 } 78 79 // EncodeJSON encodes a header into a JSON data dump. 80 func (h *Header) EncodeJSON() (string, error) { 81 data, err := json.Marshal(h.header) 82 return string(data), err 83 } 84 85 func (h *Header) GetParentHash() *Hash { return &Hash{h.header.ParentHash} } 86 func (h *Header) GetCoinbase() *Address { return &Address{h.header.Coinbase} } 87 func (h *Header) GetRoot() *Hash { return &Hash{h.header.Root} } 88 func (h *Header) GetTxHash() *Hash { return &Hash{h.header.TxHash} } 89 func (h *Header) GetReceiptHash() *Hash { return &Hash{h.header.ReceiptHash} } 90 func (h *Header) GetBloom() *Bloom { return &Bloom{h.header.Bloom} } 91 func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} } 92 func (h *Header) GetNumber() int64 { return h.header.Number.Int64() } 93 func (h *Header) GetGasLimit() int64 { return int64(h.header.GasLimit) } 94 func (h *Header) GetGasUsed() int64 { return int64(h.header.GasUsed) } 95 func (h *Header) GetTime() int64 { return int64(h.header.Time) } 96 func (h *Header) GetHash() *Hash { return &Hash{h.header.Hash()} } 97 98 // Headers represents a slice of headers. 99 type Headers struct{ headers []*types.Header } 100 101 // Size returns the number of headers in the slice. 102 func (h *Headers) Size() int { 103 return len(h.headers) 104 } 105 106 // Get returns the header at the given index from the slice. 107 func (h *Headers) Get(index int) (header *Header, _ error) { 108 if index < 0 || index >= len(h.headers) { 109 return nil, errors.New("index out of bounds") 110 } 111 return &Header{h.headers[index]}, nil 112 } 113 114 // Block represents an entire block in the Ebakus blockchain. 115 type Block struct { 116 block *types.Block 117 } 118 119 // NewBlockFromRLP parses a block from an RLP data dump. 120 func NewBlockFromRLP(data []byte) (*Block, error) { 121 b := &Block{ 122 block: new(types.Block), 123 } 124 if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil { 125 return nil, err 126 } 127 return b, nil 128 } 129 130 // EncodeRLP encodes a block into an RLP data dump. 131 func (b *Block) EncodeRLP() ([]byte, error) { 132 return rlp.EncodeToBytes(b.block) 133 } 134 135 // NewBlockFromJSON parses a block from a JSON data dump. 136 func NewBlockFromJSON(data string) (*Block, error) { 137 b := &Block{ 138 block: new(types.Block), 139 } 140 if err := json.Unmarshal([]byte(data), b.block); err != nil { 141 return nil, err 142 } 143 return b, nil 144 } 145 146 // EncodeJSON encodes a block into a JSON data dump. 147 func (b *Block) EncodeJSON() (string, error) { 148 data, err := json.Marshal(b.block) 149 return string(data), err 150 } 151 152 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 153 func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} } 154 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 155 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 156 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 157 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 158 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 159 func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} } 160 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 161 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 162 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 163 func (b *Block) GetTime() int64 { return int64(b.block.Time()) } 164 func (b *Block) GetExtra() []byte { return b.block.Extra() } 165 func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} } 166 func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) } 167 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 168 func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} } 169 func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} } 170 func (b *Block) GetTransaction(hash *Hash) *Transaction { 171 return &Transaction{b.block.Transaction(hash.hash)} 172 } 173 174 // Transaction represents a single Ebakus transaction. 175 type Transaction struct { 176 tx *types.Transaction 177 } 178 179 // NewContractCreation creates a new transaction for deploying a new contract with 180 // the given properties. 181 func NewContractCreation(nonce int64, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 182 return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 183 } 184 185 // NewTransaction creates a new transaction with the given properties. Contracts 186 // can be created by transacting with a nil recipient. 187 func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 188 if to == nil { 189 return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 190 } 191 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 192 } 193 194 // NewTransactionFromRLP parses a transaction from an RLP data dump. 195 func NewTransactionFromRLP(data []byte) (*Transaction, error) { 196 tx := &Transaction{ 197 tx: new(types.Transaction), 198 } 199 if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil { 200 return nil, err 201 } 202 return tx, nil 203 } 204 205 // EncodeRLP encodes a transaction into an RLP data dump. 206 func (tx *Transaction) EncodeRLP() ([]byte, error) { 207 return rlp.EncodeToBytes(tx.tx) 208 } 209 210 // NewTransactionFromJSON parses a transaction from a JSON data dump. 211 func NewTransactionFromJSON(data string) (*Transaction, error) { 212 tx := &Transaction{ 213 tx: new(types.Transaction), 214 } 215 if err := json.Unmarshal([]byte(data), tx.tx); err != nil { 216 return nil, err 217 } 218 return tx, nil 219 } 220 221 // EncodeJSON encodes a transaction into a JSON data dump. 222 func (tx *Transaction) EncodeJSON() (string, error) { 223 data, err := json.Marshal(tx.tx) 224 return string(data), err 225 } 226 227 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 228 func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) } 229 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 230 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 231 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 232 233 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 234 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 235 236 // Deprecated: GetSigHash cannot know which signer to use. 237 func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} } 238 239 // Deprecated: use EbakusClient.TransactionSender 240 func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) { 241 var signer types.Signer = types.HomesteadSigner{} 242 if chainID != nil { 243 signer = types.NewEIP155Signer(chainID.bigint) 244 } 245 from, err := types.Sender(signer, tx.tx) 246 return &Address{from}, err 247 } 248 249 func (tx *Transaction) GetTo() *Address { 250 if to := tx.tx.To(); to != nil { 251 return &Address{*to} 252 } 253 return nil 254 } 255 256 func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) { 257 var signer types.Signer = types.HomesteadSigner{} 258 if chainID != nil { 259 signer = types.NewEIP155Signer(chainID.bigint) 260 } 261 rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig)) 262 return &Transaction{rawTx}, err 263 } 264 265 // Transactions represents a slice of transactions. 266 type Transactions struct{ txs types.Transactions } 267 268 // Size returns the number of transactions in the slice. 269 func (txs *Transactions) Size() int { 270 return len(txs.txs) 271 } 272 273 // Get returns the transaction at the given index from the slice. 274 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 275 if index < 0 || index >= len(txs.txs) { 276 return nil, errors.New("index out of bounds") 277 } 278 return &Transaction{txs.txs[index]}, nil 279 } 280 281 // Receipt represents the results of a transaction. 282 type Receipt struct { 283 receipt *types.Receipt 284 } 285 286 // NewReceiptFromRLP parses a transaction receipt from an RLP data dump. 287 func NewReceiptFromRLP(data []byte) (*Receipt, error) { 288 r := &Receipt{ 289 receipt: new(types.Receipt), 290 } 291 if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil { 292 return nil, err 293 } 294 return r, nil 295 } 296 297 // EncodeRLP encodes a transaction receipt into an RLP data dump. 298 func (r *Receipt) EncodeRLP() ([]byte, error) { 299 return rlp.EncodeToBytes(r.receipt) 300 } 301 302 // NewReceiptFromJSON parses a transaction receipt from a JSON data dump. 303 func NewReceiptFromJSON(data string) (*Receipt, error) { 304 r := &Receipt{ 305 receipt: new(types.Receipt), 306 } 307 if err := json.Unmarshal([]byte(data), r.receipt); err != nil { 308 return nil, err 309 } 310 return r, nil 311 } 312 313 // EncodeJSON encodes a transaction receipt into a JSON data dump. 314 func (r *Receipt) EncodeJSON() (string, error) { 315 data, err := rlp.EncodeToBytes(r.receipt) 316 return string(data), err 317 } 318 319 func (r *Receipt) GetStatus() int { return int(r.receipt.Status) } 320 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 321 func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) } 322 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 323 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 324 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 325 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 326 func (r *Receipt) GetGasUsed() int64 { return int64(r.receipt.GasUsed) } 327 328 // Info represents a diagnostic information about the whisper node. 329 type Info struct { 330 info *whisper.Info 331 } 332 333 // NewMessage represents a new whisper message that is posted through the RPC. 334 type NewMessage struct { 335 newMessage *whisper.NewMessage 336 } 337 338 func NewNewMessage() *NewMessage { 339 nm := &NewMessage{ 340 newMessage: new(whisper.NewMessage), 341 } 342 return nm 343 } 344 345 func (nm *NewMessage) GetSymKeyID() string { return nm.newMessage.SymKeyID } 346 func (nm *NewMessage) SetSymKeyID(symKeyID string) { nm.newMessage.SymKeyID = symKeyID } 347 func (nm *NewMessage) GetPublicKey() []byte { return nm.newMessage.PublicKey } 348 func (nm *NewMessage) SetPublicKey(publicKey []byte) { 349 nm.newMessage.PublicKey = common.CopyBytes(publicKey) 350 } 351 func (nm *NewMessage) GetSig() string { return nm.newMessage.Sig } 352 func (nm *NewMessage) SetSig(sig string) { nm.newMessage.Sig = sig } 353 func (nm *NewMessage) GetTTL() int64 { return int64(nm.newMessage.TTL) } 354 func (nm *NewMessage) SetTTL(ttl int64) { nm.newMessage.TTL = uint32(ttl) } 355 func (nm *NewMessage) GetPayload() []byte { return nm.newMessage.Payload } 356 func (nm *NewMessage) SetPayload(payload []byte) { nm.newMessage.Payload = common.CopyBytes(payload) } 357 func (nm *NewMessage) GetPowTime() int64 { return int64(nm.newMessage.PowTime) } 358 func (nm *NewMessage) SetPowTime(powTime int64) { nm.newMessage.PowTime = uint32(powTime) } 359 func (nm *NewMessage) GetPowTarget() float64 { return nm.newMessage.PowTarget } 360 func (nm *NewMessage) SetPowTarget(powTarget float64) { nm.newMessage.PowTarget = powTarget } 361 func (nm *NewMessage) GetTargetPeer() string { return nm.newMessage.TargetPeer } 362 func (nm *NewMessage) SetTargetPeer(targetPeer string) { nm.newMessage.TargetPeer = targetPeer } 363 func (nm *NewMessage) GetTopic() []byte { return nm.newMessage.Topic[:] } 364 func (nm *NewMessage) SetTopic(topic []byte) { nm.newMessage.Topic = whisper.BytesToTopic(topic) } 365 366 // Message represents a whisper message. 367 type Message struct { 368 message *whisper.Message 369 } 370 371 func (m *Message) GetSig() []byte { return m.message.Sig } 372 func (m *Message) GetTTL() int64 { return int64(m.message.TTL) } 373 func (m *Message) GetTimestamp() int64 { return int64(m.message.Timestamp) } 374 func (m *Message) GetPayload() []byte { return m.message.Payload } 375 func (m *Message) GetPoW() float64 { return m.message.PoW } 376 func (m *Message) GetHash() []byte { return m.message.Hash } 377 func (m *Message) GetDst() []byte { return m.message.Dst } 378 379 // Messages represents an array of messages. 380 type Messages struct { 381 messages []*whisper.Message 382 } 383 384 // Size returns the number of messages in the slice. 385 func (m *Messages) Size() int { 386 return len(m.messages) 387 } 388 389 // Get returns the message at the given index from the slice. 390 func (m *Messages) Get(index int) (message *Message, _ error) { 391 if index < 0 || index >= len(m.messages) { 392 return nil, errors.New("index out of bounds") 393 } 394 return &Message{m.messages[index]}, nil 395 } 396 397 // Criteria holds various filter options for inbound messages. 398 type Criteria struct { 399 criteria *whisper.Criteria 400 } 401 402 func NewCriteria(topic []byte) *Criteria { 403 c := &Criteria{ 404 criteria: new(whisper.Criteria), 405 } 406 encodedTopic := whisper.BytesToTopic(topic) 407 c.criteria.Topics = []whisper.TopicType{encodedTopic} 408 return c 409 } 410 411 func (c *Criteria) GetSymKeyID() string { return c.criteria.SymKeyID } 412 func (c *Criteria) SetSymKeyID(symKeyID string) { c.criteria.SymKeyID = symKeyID } 413 func (c *Criteria) GetPrivateKeyID() string { return c.criteria.PrivateKeyID } 414 func (c *Criteria) SetPrivateKeyID(privateKeyID string) { c.criteria.PrivateKeyID = privateKeyID } 415 func (c *Criteria) GetSig() []byte { return c.criteria.Sig } 416 func (c *Criteria) SetSig(sig []byte) { c.criteria.Sig = common.CopyBytes(sig) } 417 func (c *Criteria) GetMinPow() float64 { return c.criteria.MinPow } 418 func (c *Criteria) SetMinPow(pow float64) { c.criteria.MinPow = pow }