github.com/lbryio/lbcd@v0.22.119/rpcclient/chain.go (about) 1 // Copyright (c) 2014-2017 The btcsuite developers 2 // Copyright (c) 2015-2017 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package rpcclient 7 8 import ( 9 "bytes" 10 "encoding/hex" 11 "encoding/json" 12 13 "github.com/lbryio/lbcd/btcjson" 14 "github.com/lbryio/lbcd/chaincfg/chainhash" 15 "github.com/lbryio/lbcd/wire" 16 ) 17 18 // FutureGetBestBlockHashResult is a future promise to deliver the result of a 19 // GetBestBlockAsync RPC invocation (or an applicable error). 20 type FutureGetBestBlockHashResult chan *Response 21 22 // Receive waits for the Response promised by the future and returns the hash of 23 // the best block in the longest block chain. 24 func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) { 25 res, err := ReceiveFuture(r) 26 if err != nil { 27 return nil, err 28 } 29 30 // Unmarshal result as a string. 31 var txHashStr string 32 err = json.Unmarshal(res, &txHashStr) 33 if err != nil { 34 return nil, err 35 } 36 return chainhash.NewHashFromStr(txHashStr) 37 } 38 39 // GetBestBlockHashAsync returns an instance of a type that can be used to get 40 // the result of the RPC at some future time by invoking the Receive function on 41 // the returned instance. 42 // 43 // See GetBestBlockHash for the blocking version and more details. 44 func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult { 45 cmd := btcjson.NewGetBestBlockHashCmd() 46 return c.SendCmd(cmd) 47 } 48 49 // GetBestBlockHash returns the hash of the best block in the longest block 50 // chain. 51 func (c *Client) GetBestBlockHash() (*chainhash.Hash, error) { 52 return c.GetBestBlockHashAsync().Receive() 53 } 54 55 // legacyGetBlockRequest constructs and sends a legacy getblock request which 56 // contains two separate bools to denote verbosity, in contract to a single int 57 // parameter. 58 func (c *Client) legacyGetBlockRequest(hash string, verbose, 59 verboseTx bool) ([]byte, error) { 60 61 hashJSON, err := json.Marshal(hash) 62 if err != nil { 63 return nil, err 64 } 65 verboseJSON, err := json.Marshal(btcjson.Bool(verbose)) 66 if err != nil { 67 return nil, err 68 } 69 verboseTxJSON, err := json.Marshal(btcjson.Bool(verboseTx)) 70 if err != nil { 71 return nil, err 72 } 73 return c.RawRequest("getblock", []json.RawMessage{ 74 hashJSON, verboseJSON, verboseTxJSON, 75 }) 76 } 77 78 // waitForGetBlockRes waits for the Response of a getblock request. If the 79 // Response indicates an invalid parameter was provided, a legacy style of the 80 // request is resent and its Response is returned instead. 81 func (c *Client) waitForGetBlockRes(respChan chan *Response, hash string, 82 verbose, verboseTx bool) ([]byte, error) { 83 84 res, err := ReceiveFuture(respChan) 85 86 // If we receive an invalid parameter error, then we may be 87 // communicating with a btcd node which only understands the legacy 88 // request, so we'll try that. 89 if err, ok := err.(*btcjson.RPCError); ok && 90 err.Code == btcjson.ErrRPCInvalidParams.Code { 91 return c.legacyGetBlockRequest(hash, verbose, verboseTx) 92 } 93 94 // Otherwise, we can return the Response as is. 95 return res, err 96 } 97 98 // FutureGetBlockResult is a future promise to deliver the result of a 99 // GetBlockAsync RPC invocation (or an applicable error). 100 type FutureGetBlockResult struct { 101 client *Client 102 hash string 103 Response chan *Response 104 } 105 106 // Receive waits for the Response promised by the future and returns the raw 107 // block requested from the server given its hash. 108 func (r FutureGetBlockResult) Receive() (*wire.MsgBlock, error) { 109 res, err := r.client.waitForGetBlockRes(r.Response, r.hash, false, false) 110 if err != nil { 111 return nil, err 112 } 113 114 // Unmarshal result as a string. 115 var blockHex string 116 err = json.Unmarshal(res, &blockHex) 117 if err != nil { 118 return nil, err 119 } 120 121 // Decode the serialized block hex to raw bytes. 122 serializedBlock, err := hex.DecodeString(blockHex) 123 if err != nil { 124 return nil, err 125 } 126 127 // Deserialize the block and return it. 128 var msgBlock wire.MsgBlock 129 err = msgBlock.Deserialize(bytes.NewReader(serializedBlock)) 130 if err != nil { 131 return nil, err 132 } 133 return &msgBlock, nil 134 } 135 136 // GetBlockAsync returns an instance of a type that can be used to get the 137 // result of the RPC at some future time by invoking the Receive function on the 138 // returned instance. 139 // 140 // See GetBlock for the blocking version and more details. 141 func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult { 142 hash := "" 143 if blockHash != nil { 144 hash = blockHash.String() 145 } 146 147 cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(0)) 148 return FutureGetBlockResult{ 149 client: c, 150 hash: hash, 151 Response: c.SendCmd(cmd), 152 } 153 } 154 155 // GetBlock returns a raw block from the server given its hash. 156 // 157 // See GetBlockVerbose to retrieve a data structure with information about the 158 // block instead. 159 func (c *Client) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) { 160 return c.GetBlockAsync(blockHash).Receive() 161 } 162 163 // FutureGetBlockVerboseResult is a future promise to deliver the result of a 164 // GetBlockVerboseAsync RPC invocation (or an applicable error). 165 type FutureGetBlockVerboseResult struct { 166 client *Client 167 hash string 168 Response chan *Response 169 } 170 171 // Receive waits for the Response promised by the future and returns the data 172 // structure from the server with information about the requested block. 173 func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) { 174 res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, false) 175 if err != nil { 176 return nil, err 177 } 178 179 // Unmarshal the raw result into a BlockResult. 180 var blockResult btcjson.GetBlockVerboseResult 181 err = json.Unmarshal(res, &blockResult) 182 if err != nil { 183 return nil, err 184 } 185 return &blockResult, nil 186 } 187 188 // GetBlockVerboseAsync returns an instance of a type that can be used to get 189 // the result of the RPC at some future time by invoking the Receive function on 190 // the returned instance. 191 // 192 // See GetBlockVerbose for the blocking version and more details. 193 func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult { 194 hash := "" 195 if blockHash != nil { 196 hash = blockHash.String() 197 } 198 // From the bitcoin-cli getblock documentation: 199 // "If verbosity is 1, returns an Object with information about block ." 200 cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(1)) 201 return FutureGetBlockVerboseResult{ 202 client: c, 203 hash: hash, 204 Response: c.SendCmd(cmd), 205 } 206 } 207 208 // GetBlockVerbose returns a data structure from the server with information 209 // about a block given its hash. 210 // 211 // See GetBlockVerboseTx to retrieve transaction data structures as well. 212 // See GetBlock to retrieve a raw block instead. 213 func (c *Client) GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) { 214 return c.GetBlockVerboseAsync(blockHash).Receive() 215 } 216 217 // FutureGetBlockVerboseTxResult is a future promise to deliver the result of a 218 // GetBlockVerboseTxResult RPC invocation (or an applicable error). 219 type FutureGetBlockVerboseTxResult struct { 220 client *Client 221 hash string 222 Response chan *Response 223 } 224 225 // Receive waits for the Response promised by the future and returns a verbose 226 // version of the block including detailed information about its transactions. 227 func (r FutureGetBlockVerboseTxResult) Receive() (*btcjson.GetBlockVerboseTxResult, error) { 228 res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, true) 229 if err != nil { 230 return nil, err 231 } 232 233 var blockResult btcjson.GetBlockVerboseTxResult 234 err = json.Unmarshal(res, &blockResult) 235 if err != nil { 236 return nil, err 237 } 238 239 return &blockResult, nil 240 } 241 242 // GetBlockVerboseTxAsync returns an instance of a type that can be used to get 243 // the result of the RPC at some future time by invoking the Receive function on 244 // the returned instance. 245 // 246 // See GetBlockVerboseTx or the blocking version and more details. 247 func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseTxResult { 248 hash := "" 249 if blockHash != nil { 250 hash = blockHash.String() 251 } 252 253 // From the bitcoin-cli getblock documentation: 254 // 255 // If verbosity is 2, returns an Object with information about block 256 // and information about each transaction. 257 cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(2)) 258 return FutureGetBlockVerboseTxResult{ 259 client: c, 260 hash: hash, 261 Response: c.SendCmd(cmd), 262 } 263 } 264 265 // GetBlockVerboseTx returns a data structure from the server with information 266 // about a block and its transactions given its hash. 267 // 268 // See GetBlockVerbose if only transaction hashes are preferred. 269 // See GetBlock to retrieve a raw block instead. 270 func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseTxResult, error) { 271 return c.GetBlockVerboseTxAsync(blockHash).Receive() 272 } 273 274 // FutureGetBlockCountResult is a future promise to deliver the result of a 275 // GetBlockCountAsync RPC invocation (or an applicable error). 276 type FutureGetBlockCountResult chan *Response 277 278 // Receive waits for the Response promised by the future and returns the number 279 // of blocks in the longest block chain. 280 func (r FutureGetBlockCountResult) Receive() (int64, error) { 281 res, err := ReceiveFuture(r) 282 if err != nil { 283 return 0, err 284 } 285 286 // Unmarshal the result as an int64. 287 var count int64 288 err = json.Unmarshal(res, &count) 289 if err != nil { 290 return 0, err 291 } 292 return count, nil 293 } 294 295 // GetBlockCountAsync returns an instance of a type that can be used to get the 296 // result of the RPC at some future time by invoking the Receive function on the 297 // returned instance. 298 // 299 // See GetBlockCount for the blocking version and more details. 300 func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult { 301 cmd := btcjson.NewGetBlockCountCmd() 302 return c.SendCmd(cmd) 303 } 304 305 // GetBlockCount returns the number of blocks in the longest block chain. 306 func (c *Client) GetBlockCount() (int64, error) { 307 return c.GetBlockCountAsync().Receive() 308 } 309 310 // FutureGetChainTxStatsResult is a future promise to deliver the result of a 311 // GetChainTxStatsAsync RPC invocation (or an applicable error). 312 type FutureGetChainTxStatsResult chan *Response 313 314 // Receive waits for the Response promised by the future and returns transaction statistics 315 func (r FutureGetChainTxStatsResult) Receive() (*btcjson.GetChainTxStatsResult, error) { 316 res, err := ReceiveFuture(r) 317 if err != nil { 318 return nil, err 319 } 320 321 var chainTxStats btcjson.GetChainTxStatsResult 322 err = json.Unmarshal(res, &chainTxStats) 323 if err != nil { 324 return nil, err 325 } 326 327 return &chainTxStats, nil 328 } 329 330 // GetChainTxStatsAsync returns an instance of a type that can be used to get 331 // the result of the RPC at some future time by invoking the Receive function on 332 // the returned instance. 333 // 334 // See GetChainTxStats for the blocking version and more details. 335 func (c *Client) GetChainTxStatsAsync() FutureGetChainTxStatsResult { 336 cmd := btcjson.NewGetChainTxStatsCmd(nil, nil) 337 return c.SendCmd(cmd) 338 } 339 340 // GetChainTxStatsNBlocksAsync returns an instance of a type that can be used to get 341 // the result of the RPC at some future time by invoking the Receive function on 342 // the returned instance. 343 // 344 // See GetChainTxStatsNBlocks for the blocking version and more details. 345 func (c *Client) GetChainTxStatsNBlocksAsync(nBlocks int32) FutureGetChainTxStatsResult { 346 cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, nil) 347 return c.SendCmd(cmd) 348 } 349 350 // GetChainTxStatsNBlocksBlockHashAsync returns an instance of a type that can be used to get 351 // the result of the RPC at some future time by invoking the Receive function on 352 // the returned instance. 353 // 354 // See GetChainTxStatsNBlocksBlockHash for the blocking version and more details. 355 func (c *Client) GetChainTxStatsNBlocksBlockHashAsync(nBlocks int32, blockHash chainhash.Hash) FutureGetChainTxStatsResult { 356 hash := blockHash.String() 357 cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, &hash) 358 return c.SendCmd(cmd) 359 } 360 361 // GetChainTxStats returns statistics about the total number and rate of transactions in the chain. 362 // 363 // Size of the window is one month and it ends at chain tip. 364 func (c *Client) GetChainTxStats() (*btcjson.GetChainTxStatsResult, error) { 365 return c.GetChainTxStatsAsync().Receive() 366 } 367 368 // GetChainTxStatsNBlocks returns statistics about the total number and rate of transactions in the chain. 369 // 370 // The argument specifies size of the window in number of blocks. The window ends at chain tip. 371 func (c *Client) GetChainTxStatsNBlocks(nBlocks int32) (*btcjson.GetChainTxStatsResult, error) { 372 return c.GetChainTxStatsNBlocksAsync(nBlocks).Receive() 373 } 374 375 // GetChainTxStatsNBlocksBlockHash returns statistics about the total number and rate of transactions in the chain. 376 // 377 // First argument specifies size of the window in number of blocks. 378 // Second argument is the hash of the block that ends the window. 379 func (c *Client) GetChainTxStatsNBlocksBlockHash(nBlocks int32, blockHash chainhash.Hash) (*btcjson.GetChainTxStatsResult, error) { 380 return c.GetChainTxStatsNBlocksBlockHashAsync(nBlocks, blockHash).Receive() 381 } 382 383 // FutureGetChainTipsResult is a future promise to deliver the result of a 384 // GetChainTipsAsync RPC invocation (or an applicable error). 385 type FutureGetChainTipsResult chan *Response 386 387 // Receive waits for the Response promised by the future and returns transaction statistics 388 func (r FutureGetChainTipsResult) Receive() ([]btcjson.GetChainTipsResult, error) { 389 res, err := ReceiveFuture(r) 390 if err != nil { 391 return nil, err 392 } 393 394 var chainTips []btcjson.GetChainTipsResult 395 err = json.Unmarshal(res, &chainTips) 396 if err != nil { 397 return nil, err 398 } 399 400 return chainTips, nil 401 } 402 403 // GetChainTipsAsync returns an instance of a type that can be used to get 404 // the result of the RPC at some future time by invoking the Receive function on 405 // the returned instance. 406 // 407 // See GetChainTips for the blocking version and more details. 408 func (c *Client) GetChainTipsAsync() FutureGetChainTipsResult { 409 cmd := btcjson.NewGetChainTipsCmd() 410 return c.SendCmd(cmd) 411 } 412 413 // GetChainTips returns information about all known tips in the block tree, 414 // including the main chain as well as orphaned branches. 415 // 416 // Possible values for status: 417 // "invalid" This branch contains at least one invalid block 418 // "headers-only" Not all blocks for this branch are available, but the headers are valid 419 // "valid-headers" All blocks are available for this branch, but they were never fully validated 420 // "valid-fork" This branch is not part of the active chain, but is fully validated 421 // "active" This is the tip of the active main chain, which is certainly valid 422 func (c *Client) GetChainTips() ([]btcjson.GetChainTipsResult, error) { 423 return c.GetChainTipsAsync().Receive() 424 } 425 426 // FutureGetDifficultyResult is a future promise to deliver the result of a 427 // GetDifficultyAsync RPC invocation (or an applicable error). 428 type FutureGetDifficultyResult chan *Response 429 430 // Receive waits for the Response promised by the future and returns the 431 // proof-of-work difficulty as a multiple of the minimum difficulty. 432 func (r FutureGetDifficultyResult) Receive() (float64, error) { 433 res, err := ReceiveFuture(r) 434 if err != nil { 435 return 0, err 436 } 437 438 // Unmarshal the result as a float64. 439 var difficulty float64 440 err = json.Unmarshal(res, &difficulty) 441 if err != nil { 442 return 0, err 443 } 444 return difficulty, nil 445 } 446 447 // GetDifficultyAsync returns an instance of a type that can be used to get the 448 // result of the RPC at some future time by invoking the Receive function on the 449 // returned instance. 450 // 451 // See GetDifficulty for the blocking version and more details. 452 func (c *Client) GetDifficultyAsync() FutureGetDifficultyResult { 453 cmd := btcjson.NewGetDifficultyCmd() 454 return c.SendCmd(cmd) 455 } 456 457 // GetDifficulty returns the proof-of-work difficulty as a multiple of the 458 // minimum difficulty. 459 func (c *Client) GetDifficulty() (float64, error) { 460 return c.GetDifficultyAsync().Receive() 461 } 462 463 // FutureGetBlockChainInfoResult is a promise to deliver the result of a 464 // GetBlockChainInfoAsync RPC invocation (or an applicable error). 465 type FutureGetBlockChainInfoResult struct { 466 client *Client 467 Response chan *Response 468 } 469 470 // unmarshalPartialGetBlockChainInfoResult unmarshals the response into an 471 // instance of GetBlockChainInfoResult without populating the SoftForks and 472 // UnifiedSoftForks fields. 473 func unmarshalPartialGetBlockChainInfoResult(res []byte) (*btcjson.GetBlockChainInfoResult, error) { 474 var chainInfo btcjson.GetBlockChainInfoResult 475 if err := json.Unmarshal(res, &chainInfo); err != nil { 476 return nil, err 477 } 478 return &chainInfo, nil 479 } 480 481 // unmarshalGetBlockChainInfoResultSoftForks properly unmarshals the softforks 482 // related fields into the GetBlockChainInfoResult instance. 483 func unmarshalGetBlockChainInfoResultSoftForks(chainInfo *btcjson.GetBlockChainInfoResult, 484 version BackendVersion, res []byte) error { 485 486 switch version { 487 // Versions of bitcoind on or after v0.19.0 use the unified format. 488 case BitcoindPost19: 489 var softForks btcjson.UnifiedSoftForks 490 if err := json.Unmarshal(res, &softForks); err != nil { 491 return err 492 } 493 chainInfo.UnifiedSoftForks = &softForks 494 495 // All other versions use the original format. 496 default: 497 var softForks btcjson.SoftForks 498 if err := json.Unmarshal(res, &softForks); err != nil { 499 return err 500 } 501 chainInfo.SoftForks = &softForks 502 } 503 504 return nil 505 } 506 507 // Receive waits for the Response promised by the future and returns chain info 508 // result provided by the server. 509 func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) { 510 res, err := ReceiveFuture(r.Response) 511 if err != nil { 512 return nil, err 513 } 514 chainInfo, err := unmarshalPartialGetBlockChainInfoResult(res) 515 if err != nil { 516 return nil, err 517 } 518 519 // Inspect the version to determine how we'll need to parse the 520 // softforks from the response. 521 version, err := r.client.BackendVersion() 522 if err != nil { 523 return nil, err 524 } 525 526 err = unmarshalGetBlockChainInfoResultSoftForks(chainInfo, version, res) 527 if err != nil { 528 return nil, err 529 } 530 531 return chainInfo, nil 532 } 533 534 // GetBlockChainInfoAsync returns an instance of a type that can be used to get 535 // the result of the RPC at some future time by invoking the Receive function 536 // on the returned instance. 537 // 538 // See GetBlockChainInfo for the blocking version and more details. 539 func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult { 540 cmd := btcjson.NewGetBlockChainInfoCmd() 541 return FutureGetBlockChainInfoResult{ 542 client: c, 543 Response: c.SendCmd(cmd), 544 } 545 } 546 547 // GetBlockChainInfo returns information related to the processing state of 548 // various chain-specific details such as the current difficulty from the tip 549 // of the main chain. 550 func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) { 551 return c.GetBlockChainInfoAsync().Receive() 552 } 553 554 // FutureGetBlockFilterResult is a future promise to deliver the result of a 555 // GetBlockFilterAsync RPC invocation (or an applicable error). 556 type FutureGetBlockFilterResult chan *Response 557 558 // Receive waits for the Response promised by the future and returns block filter 559 // result provided by the server. 560 func (r FutureGetBlockFilterResult) Receive() (*btcjson.GetBlockFilterResult, error) { 561 res, err := ReceiveFuture(r) 562 if err != nil { 563 return nil, err 564 } 565 566 var blockFilter btcjson.GetBlockFilterResult 567 err = json.Unmarshal(res, &blockFilter) 568 if err != nil { 569 return nil, err 570 } 571 572 return &blockFilter, nil 573 } 574 575 // GetBlockFilterAsync returns an instance of a type that can be used to get the 576 // result of the RPC at some future time by invoking the Receive function on the 577 // returned instance. 578 // 579 // See GetBlockFilter for the blocking version and more details. 580 func (c *Client) GetBlockFilterAsync(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) FutureGetBlockFilterResult { 581 hash := blockHash.String() 582 583 cmd := btcjson.NewGetBlockFilterCmd(hash, filterType) 584 return c.SendCmd(cmd) 585 } 586 587 // GetBlockFilter retrieves a BIP0157 content filter for a particular block. 588 func (c *Client) GetBlockFilter(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) (*btcjson.GetBlockFilterResult, error) { 589 return c.GetBlockFilterAsync(blockHash, filterType).Receive() 590 } 591 592 // FutureGetBlockHashResult is a future promise to deliver the result of a 593 // GetBlockHashAsync RPC invocation (or an applicable error). 594 type FutureGetBlockHashResult chan *Response 595 596 // Receive waits for the Response promised by the future and returns the hash of 597 // the block in the best block chain at the given height. 598 func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) { 599 res, err := ReceiveFuture(r) 600 if err != nil { 601 return nil, err 602 } 603 604 // Unmarshal the result as a string-encoded sha. 605 var txHashStr string 606 err = json.Unmarshal(res, &txHashStr) 607 if err != nil { 608 return nil, err 609 } 610 return chainhash.NewHashFromStr(txHashStr) 611 } 612 613 // GetBlockHashAsync returns an instance of a type that can be used to get the 614 // result of the RPC at some future time by invoking the Receive function on the 615 // returned instance. 616 // 617 // See GetBlockHash for the blocking version and more details. 618 func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult { 619 cmd := btcjson.NewGetBlockHashCmd(blockHeight) 620 return c.SendCmd(cmd) 621 } 622 623 // GetBlockHash returns the hash of the block in the best block chain at the 624 // given height. 625 func (c *Client) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) { 626 return c.GetBlockHashAsync(blockHeight).Receive() 627 } 628 629 // FutureGetBlockHeaderResult is a future promise to deliver the result of a 630 // GetBlockHeaderAsync RPC invocation (or an applicable error). 631 type FutureGetBlockHeaderResult chan *Response 632 633 // Receive waits for the Response promised by the future and returns the 634 // blockheader requested from the server given its hash. 635 func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) { 636 res, err := ReceiveFuture(r) 637 if err != nil { 638 return nil, err 639 } 640 641 // Unmarshal result as a string. 642 var bhHex string 643 err = json.Unmarshal(res, &bhHex) 644 if err != nil { 645 return nil, err 646 } 647 648 serializedBH, err := hex.DecodeString(bhHex) 649 if err != nil { 650 return nil, err 651 } 652 653 // Deserialize the blockheader and return it. 654 var bh wire.BlockHeader 655 err = bh.Deserialize(bytes.NewReader(serializedBH)) 656 if err != nil { 657 return nil, err 658 } 659 660 return &bh, err 661 } 662 663 // GetBlockHeaderAsync returns an instance of a type that can be used to get the 664 // result of the RPC at some future time by invoking the Receive function on the 665 // returned instance. 666 // 667 // See GetBlockHeader for the blocking version and more details. 668 func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult { 669 hash := "" 670 if blockHash != nil { 671 hash = blockHash.String() 672 } 673 674 cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false)) 675 return c.SendCmd(cmd) 676 } 677 678 // GetBlockHeader returns the blockheader from the server given its hash. 679 // 680 // See GetBlockHeaderVerbose to retrieve a data structure with information about the 681 // block instead. 682 func (c *Client) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) { 683 return c.GetBlockHeaderAsync(blockHash).Receive() 684 } 685 686 // FutureGetBlockHeaderVerboseResult is a future promise to deliver the result of a 687 // GetBlockAsync RPC invocation (or an applicable error). 688 type FutureGetBlockHeaderVerboseResult chan *Response 689 690 // Receive waits for the Response promised by the future and returns the 691 // data structure of the blockheader requested from the server given its hash. 692 func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) { 693 res, err := ReceiveFuture(r) 694 if err != nil { 695 return nil, err 696 } 697 698 // Unmarshal result as a string. 699 var bh btcjson.GetBlockHeaderVerboseResult 700 err = json.Unmarshal(res, &bh) 701 if err != nil { 702 return nil, err 703 } 704 705 return &bh, nil 706 } 707 708 // GetBlockHeaderVerboseAsync returns an instance of a type that can be used to get the 709 // result of the RPC at some future time by invoking the Receive function on the 710 // returned instance. 711 // 712 // See GetBlockHeader for the blocking version and more details. 713 func (c *Client) GetBlockHeaderVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderVerboseResult { 714 hash := "" 715 if blockHash != nil { 716 hash = blockHash.String() 717 } 718 719 cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(true)) 720 return c.SendCmd(cmd) 721 } 722 723 // GetBlockHeaderVerbose returns a data structure with information about the 724 // blockheader from the server given its hash. 725 // 726 // See GetBlockHeader to retrieve a blockheader instead. 727 func (c *Client) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) { 728 return c.GetBlockHeaderVerboseAsync(blockHash).Receive() 729 } 730 731 // FutureGetMempoolEntryResult is a future promise to deliver the result of a 732 // GetMempoolEntryAsync RPC invocation (or an applicable error). 733 type FutureGetMempoolEntryResult chan *Response 734 735 // Receive waits for the Response promised by the future and returns a data 736 // structure with information about the transaction in the memory pool given 737 // its hash. 738 func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) { 739 res, err := ReceiveFuture(r) 740 if err != nil { 741 return nil, err 742 } 743 744 // Unmarshal the result as an array of strings. 745 var mempoolEntryResult btcjson.GetMempoolEntryResult 746 err = json.Unmarshal(res, &mempoolEntryResult) 747 if err != nil { 748 return nil, err 749 } 750 751 return &mempoolEntryResult, nil 752 } 753 754 // GetMempoolEntryAsync returns an instance of a type that can be used to get the 755 // result of the RPC at some future time by invoking the Receive function on the 756 // returned instance. 757 // 758 // See GetMempoolEntry for the blocking version and more details. 759 func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult { 760 cmd := btcjson.NewGetMempoolEntryCmd(txHash) 761 return c.SendCmd(cmd) 762 } 763 764 // GetMempoolEntry returns a data structure with information about the 765 // transaction in the memory pool given its hash. 766 func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) { 767 return c.GetMempoolEntryAsync(txHash).Receive() 768 } 769 770 // FutureGetRawMempoolResult is a future promise to deliver the result of a 771 // GetRawMempoolAsync RPC invocation (or an applicable error). 772 type FutureGetRawMempoolResult chan *Response 773 774 // Receive waits for the Response promised by the future and returns the hashes 775 // of all transactions in the memory pool. 776 func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) { 777 res, err := ReceiveFuture(r) 778 if err != nil { 779 return nil, err 780 } 781 782 // Unmarshal the result as an array of strings. 783 var txHashStrs []string 784 err = json.Unmarshal(res, &txHashStrs) 785 if err != nil { 786 return nil, err 787 } 788 789 // Create a slice of ShaHash arrays from the string slice. 790 txHashes := make([]*chainhash.Hash, 0, len(txHashStrs)) 791 for _, hashStr := range txHashStrs { 792 txHash, err := chainhash.NewHashFromStr(hashStr) 793 if err != nil { 794 return nil, err 795 } 796 txHashes = append(txHashes, txHash) 797 } 798 799 return txHashes, nil 800 } 801 802 // GetRawMempoolAsync returns an instance of a type that can be used to get the 803 // result of the RPC at some future time by invoking the Receive function on the 804 // returned instance. 805 // 806 // See GetRawMempool for the blocking version and more details. 807 func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult { 808 cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false)) 809 return c.SendCmd(cmd) 810 } 811 812 // GetRawMempool returns the hashes of all transactions in the memory pool. 813 // 814 // See GetRawMempoolVerbose to retrieve data structures with information about 815 // the transactions instead. 816 func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) { 817 return c.GetRawMempoolAsync().Receive() 818 } 819 820 // FutureGetRawMempoolVerboseResult is a future promise to deliver the result of 821 // a GetRawMempoolVerboseAsync RPC invocation (or an applicable error). 822 type FutureGetRawMempoolVerboseResult chan *Response 823 824 // Receive waits for the Response promised by the future and returns a map of 825 // transaction hashes to an associated data structure with information about the 826 // transaction for all transactions in the memory pool. 827 func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) { 828 res, err := ReceiveFuture(r) 829 if err != nil { 830 return nil, err 831 } 832 833 // Unmarshal the result as a map of strings (tx shas) to their detailed 834 // results. 835 var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult 836 err = json.Unmarshal(res, &mempoolItems) 837 if err != nil { 838 return nil, err 839 } 840 return mempoolItems, nil 841 } 842 843 // GetRawMempoolVerboseAsync returns an instance of a type that can be used to 844 // get the result of the RPC at some future time by invoking the Receive 845 // function on the returned instance. 846 // 847 // See GetRawMempoolVerbose for the blocking version and more details. 848 func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult { 849 cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true)) 850 return c.SendCmd(cmd) 851 } 852 853 // GetRawMempoolVerbose returns a map of transaction hashes to an associated 854 // data structure with information about the transaction for all transactions in 855 // the memory pool. 856 // 857 // See GetRawMempool to retrieve only the transaction hashes instead. 858 func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) { 859 return c.GetRawMempoolVerboseAsync().Receive() 860 } 861 862 // FutureEstimateFeeResult is a future promise to deliver the result of a 863 // EstimateFeeAsync RPC invocation (or an applicable error). 864 type FutureEstimateFeeResult chan *Response 865 866 // Receive waits for the Response promised by the future and returns the info 867 // provided by the server. 868 func (r FutureEstimateFeeResult) Receive() (float64, error) { 869 res, err := ReceiveFuture(r) 870 if err != nil { 871 return -1, err 872 } 873 874 // Unmarshal result as a getinfo result object. 875 var fee float64 876 err = json.Unmarshal(res, &fee) 877 if err != nil { 878 return -1, err 879 } 880 881 return fee, nil 882 } 883 884 // EstimateFeeAsync returns an instance of a type that can be used to get the result 885 // of the RPC at some future time by invoking the Receive function on the 886 // returned instance. 887 // 888 // See EstimateFee for the blocking version and more details. 889 func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult { 890 cmd := btcjson.NewEstimateFeeCmd(numBlocks) 891 return c.SendCmd(cmd) 892 } 893 894 // EstimateFee provides an estimated fee in bitcoins per kilobyte. 895 func (c *Client) EstimateFee(numBlocks int64) (float64, error) { 896 return c.EstimateFeeAsync(numBlocks).Receive() 897 } 898 899 // FutureEstimateFeeResult is a future promise to deliver the result of a 900 // EstimateSmartFeeAsync RPC invocation (or an applicable error). 901 type FutureEstimateSmartFeeResult chan *Response 902 903 // Receive waits for the Response promised by the future and returns the 904 // estimated fee. 905 func (r FutureEstimateSmartFeeResult) Receive() (*btcjson.EstimateSmartFeeResult, error) { 906 res, err := ReceiveFuture(r) 907 if err != nil { 908 return nil, err 909 } 910 911 var verified btcjson.EstimateSmartFeeResult 912 err = json.Unmarshal(res, &verified) 913 if err != nil { 914 return nil, err 915 } 916 return &verified, nil 917 } 918 919 // EstimateSmartFeeAsync returns an instance of a type that can be used to get the 920 // result of the RPC at some future time by invoking the Receive function on the 921 // returned instance. 922 // 923 // See EstimateSmartFee for the blocking version and more details. 924 func (c *Client) EstimateSmartFeeAsync(confTarget int64, mode *btcjson.EstimateSmartFeeMode) FutureEstimateSmartFeeResult { 925 cmd := btcjson.NewEstimateSmartFeeCmd(confTarget, mode) 926 return c.SendCmd(cmd) 927 } 928 929 // EstimateSmartFee requests the server to estimate a fee level based on the given parameters. 930 func (c *Client) EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) { 931 return c.EstimateSmartFeeAsync(confTarget, mode).Receive() 932 } 933 934 // FutureVerifyChainResult is a future promise to deliver the result of a 935 // VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync 936 // invocation (or an applicable error). 937 type FutureVerifyChainResult chan *Response 938 939 // Receive waits for the Response promised by the future and returns whether 940 // or not the chain verified based on the check level and number of blocks 941 // to verify specified in the original call. 942 func (r FutureVerifyChainResult) Receive() (bool, error) { 943 res, err := ReceiveFuture(r) 944 if err != nil { 945 return false, err 946 } 947 948 // Unmarshal the result as a boolean. 949 var verified bool 950 err = json.Unmarshal(res, &verified) 951 if err != nil { 952 return false, err 953 } 954 return verified, nil 955 } 956 957 // VerifyChainAsync returns an instance of a type that can be used to get the 958 // result of the RPC at some future time by invoking the Receive function on the 959 // returned instance. 960 // 961 // See VerifyChain for the blocking version and more details. 962 func (c *Client) VerifyChainAsync() FutureVerifyChainResult { 963 cmd := btcjson.NewVerifyChainCmd(nil, nil) 964 return c.SendCmd(cmd) 965 } 966 967 // VerifyChain requests the server to verify the block chain database using 968 // the default check level and number of blocks to verify. 969 // 970 // See VerifyChainLevel and VerifyChainBlocks to override the defaults. 971 func (c *Client) VerifyChain() (bool, error) { 972 return c.VerifyChainAsync().Receive() 973 } 974 975 // VerifyChainLevelAsync returns an instance of a type that can be used to get 976 // the result of the RPC at some future time by invoking the Receive function on 977 // the returned instance. 978 // 979 // See VerifyChainLevel for the blocking version and more details. 980 func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult { 981 cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil) 982 return c.SendCmd(cmd) 983 } 984 985 // VerifyChainLevel requests the server to verify the block chain database using 986 // the passed check level and default number of blocks to verify. 987 // 988 // The check level controls how thorough the verification is with higher numbers 989 // increasing the amount of checks done as consequently how long the 990 // verification takes. 991 // 992 // See VerifyChain to use the default check level and VerifyChainBlocks to 993 // override the number of blocks to verify. 994 func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) { 995 return c.VerifyChainLevelAsync(checkLevel).Receive() 996 } 997 998 // VerifyChainBlocksAsync returns an instance of a type that can be used to get 999 // the result of the RPC at some future time by invoking the Receive function on 1000 // the returned instance. 1001 // 1002 // See VerifyChainBlocks for the blocking version and more details. 1003 func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult { 1004 cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks) 1005 return c.SendCmd(cmd) 1006 } 1007 1008 // VerifyChainBlocks requests the server to verify the block chain database 1009 // using the passed check level and number of blocks to verify. 1010 // 1011 // The check level controls how thorough the verification is with higher numbers 1012 // increasing the amount of checks done as consequently how long the 1013 // verification takes. 1014 // 1015 // The number of blocks refers to the number of blocks from the end of the 1016 // current longest chain. 1017 // 1018 // See VerifyChain and VerifyChainLevel to use defaults. 1019 func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) { 1020 return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive() 1021 } 1022 1023 // FutureGetTxOutResult is a future promise to deliver the result of a 1024 // GetTxOutAsync RPC invocation (or an applicable error). 1025 type FutureGetTxOutResult chan *Response 1026 1027 // Receive waits for the Response promised by the future and returns a 1028 // transaction given its hash. 1029 func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) { 1030 res, err := ReceiveFuture(r) 1031 if err != nil { 1032 return nil, err 1033 } 1034 1035 // take care of the special case where the output has been spent already 1036 // it should return the string "null" 1037 if string(res) == "null" { 1038 return nil, nil 1039 } 1040 1041 // Unmarshal result as an gettxout result object. 1042 var txOutInfo *btcjson.GetTxOutResult 1043 err = json.Unmarshal(res, &txOutInfo) 1044 if err != nil { 1045 return nil, err 1046 } 1047 1048 return txOutInfo, nil 1049 } 1050 1051 // GetTxOutAsync returns an instance of a type that can be used to get 1052 // the result of the RPC at some future time by invoking the Receive function on 1053 // the returned instance. 1054 // 1055 // See GetTxOut for the blocking version and more details. 1056 func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult { 1057 hash := "" 1058 if txHash != nil { 1059 hash = txHash.String() 1060 } 1061 1062 cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool) 1063 return c.SendCmd(cmd) 1064 } 1065 1066 // GetTxOut returns the transaction output info if it's unspent and 1067 // nil, otherwise. 1068 func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) { 1069 return c.GetTxOutAsync(txHash, index, mempool).Receive() 1070 } 1071 1072 // FutureGetTxOutSetInfoResult is a future promise to deliver the result of a 1073 // GetTxOutSetInfoAsync RPC invocation (or an applicable error). 1074 type FutureGetTxOutSetInfoResult chan *Response 1075 1076 // Receive waits for the Response promised by the future and returns the 1077 // results of GetTxOutSetInfoAsync RPC invocation. 1078 func (r FutureGetTxOutSetInfoResult) Receive() (*btcjson.GetTxOutSetInfoResult, error) { 1079 res, err := ReceiveFuture(r) 1080 if err != nil { 1081 return nil, err 1082 } 1083 1084 // Unmarshal result as an gettxoutsetinfo result object. 1085 var txOutSetInfo *btcjson.GetTxOutSetInfoResult 1086 err = json.Unmarshal(res, &txOutSetInfo) 1087 if err != nil { 1088 return nil, err 1089 } 1090 1091 return txOutSetInfo, nil 1092 } 1093 1094 // GetTxOutSetInfoAsync returns an instance of a type that can be used to get 1095 // the result of the RPC at some future time by invoking the Receive function on 1096 // the returned instance. 1097 // 1098 // See GetTxOutSetInfo for the blocking version and more details. 1099 func (c *Client) GetTxOutSetInfoAsync() FutureGetTxOutSetInfoResult { 1100 cmd := btcjson.NewGetTxOutSetInfoCmd() 1101 return c.SendCmd(cmd) 1102 } 1103 1104 // GetTxOutSetInfo returns the statistics about the unspent transaction output 1105 // set. 1106 func (c *Client) GetTxOutSetInfo() (*btcjson.GetTxOutSetInfoResult, error) { 1107 return c.GetTxOutSetInfoAsync().Receive() 1108 } 1109 1110 // FutureRescanBlocksResult is a future promise to deliver the result of a 1111 // RescanBlocksAsync RPC invocation (or an applicable error). 1112 // 1113 // NOTE: This is a btcsuite extension ported from 1114 // github.com/decred/dcrrpcclient. 1115 type FutureRescanBlocksResult chan *Response 1116 1117 // Receive waits for the Response promised by the future and returns the 1118 // discovered rescanblocks data. 1119 // 1120 // NOTE: This is a btcsuite extension ported from 1121 // github.com/decred/dcrrpcclient. 1122 func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) { 1123 res, err := ReceiveFuture(r) 1124 if err != nil { 1125 return nil, err 1126 } 1127 1128 var rescanBlocksResult []btcjson.RescannedBlock 1129 err = json.Unmarshal(res, &rescanBlocksResult) 1130 if err != nil { 1131 return nil, err 1132 } 1133 1134 return rescanBlocksResult, nil 1135 } 1136 1137 // RescanBlocksAsync returns an instance of a type that can be used to get the 1138 // result of the RPC at some future time by invoking the Receive function on the 1139 // returned instance. 1140 // 1141 // See RescanBlocks for the blocking version and more details. 1142 // 1143 // NOTE: This is a btcsuite extension ported from 1144 // github.com/decred/dcrrpcclient. 1145 func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult { 1146 strBlockHashes := make([]string, len(blockHashes)) 1147 for i := range blockHashes { 1148 strBlockHashes[i] = blockHashes[i].String() 1149 } 1150 1151 cmd := btcjson.NewRescanBlocksCmd(strBlockHashes) 1152 return c.SendCmd(cmd) 1153 } 1154 1155 // RescanBlocks rescans the blocks identified by blockHashes, in order, using 1156 // the client's loaded transaction filter. The blocks do not need to be on the 1157 // main chain, but they do need to be adjacent to each other. 1158 // 1159 // NOTE: This is a btcsuite extension ported from 1160 // github.com/decred/dcrrpcclient. 1161 func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) { 1162 return c.RescanBlocksAsync(blockHashes).Receive() 1163 } 1164 1165 // FutureInvalidateBlockResult is a future promise to deliver the result of a 1166 // InvalidateBlockAsync RPC invocation (or an applicable error). 1167 type FutureInvalidateBlockResult chan *Response 1168 1169 // Receive waits for the Response promised by the future and returns the raw 1170 // block requested from the server given its hash. 1171 func (r FutureInvalidateBlockResult) Receive() error { 1172 _, err := ReceiveFuture(r) 1173 1174 return err 1175 } 1176 1177 // InvalidateBlockAsync returns an instance of a type that can be used to get the 1178 // result of the RPC at some future time by invoking the Receive function on the 1179 // returned instance. 1180 // 1181 // See InvalidateBlock for the blocking version and more details. 1182 func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult { 1183 hash := "" 1184 if blockHash != nil { 1185 hash = blockHash.String() 1186 } 1187 1188 cmd := btcjson.NewInvalidateBlockCmd(hash) 1189 return c.SendCmd(cmd) 1190 } 1191 1192 // InvalidateBlock invalidates a specific block. 1193 func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) error { 1194 return c.InvalidateBlockAsync(blockHash).Receive() 1195 } 1196 1197 // FutureGetCFilterResult is a future promise to deliver the result of a 1198 // GetCFilterAsync RPC invocation (or an applicable error). 1199 type FutureGetCFilterResult chan *Response 1200 1201 // Receive waits for the Response promised by the future and returns the raw 1202 // filter requested from the server given its block hash. 1203 func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) { 1204 res, err := ReceiveFuture(r) 1205 if err != nil { 1206 return nil, err 1207 } 1208 1209 // Unmarshal result as a string. 1210 var filterHex string 1211 err = json.Unmarshal(res, &filterHex) 1212 if err != nil { 1213 return nil, err 1214 } 1215 1216 // Decode the serialized cf hex to raw bytes. 1217 serializedFilter, err := hex.DecodeString(filterHex) 1218 if err != nil { 1219 return nil, err 1220 } 1221 1222 // Assign the filter bytes to the correct field of the wire message. 1223 // We aren't going to set the block hash or extended flag, since we 1224 // don't actually get that back in the RPC response. 1225 var msgCFilter wire.MsgCFilter 1226 msgCFilter.Data = serializedFilter 1227 return &msgCFilter, nil 1228 } 1229 1230 // GetCFilterAsync returns an instance of a type that can be used to get the 1231 // result of the RPC at some future time by invoking the Receive function on the 1232 // returned instance. 1233 // 1234 // See GetCFilter for the blocking version and more details. 1235 func (c *Client) GetCFilterAsync(blockHash *chainhash.Hash, 1236 filterType wire.FilterType) FutureGetCFilterResult { 1237 hash := "" 1238 if blockHash != nil { 1239 hash = blockHash.String() 1240 } 1241 1242 cmd := btcjson.NewGetCFilterCmd(hash, filterType) 1243 return c.SendCmd(cmd) 1244 } 1245 1246 // GetCFilter returns a raw filter from the server given its block hash. 1247 func (c *Client) GetCFilter(blockHash *chainhash.Hash, 1248 filterType wire.FilterType) (*wire.MsgCFilter, error) { 1249 return c.GetCFilterAsync(blockHash, filterType).Receive() 1250 } 1251 1252 // FutureGetCFilterHeaderResult is a future promise to deliver the result of a 1253 // GetCFilterHeaderAsync RPC invocation (or an applicable error). 1254 type FutureGetCFilterHeaderResult chan *Response 1255 1256 // Receive waits for the Response promised by the future and returns the raw 1257 // filter header requested from the server given its block hash. 1258 func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) { 1259 res, err := ReceiveFuture(r) 1260 if err != nil { 1261 return nil, err 1262 } 1263 1264 // Unmarshal result as a string. 1265 var headerHex string 1266 err = json.Unmarshal(res, &headerHex) 1267 if err != nil { 1268 return nil, err 1269 } 1270 1271 // Assign the decoded header into a hash 1272 headerHash, err := chainhash.NewHashFromStr(headerHex) 1273 if err != nil { 1274 return nil, err 1275 } 1276 1277 // Assign the hash to a headers message and return it. 1278 msgCFHeaders := wire.MsgCFHeaders{PrevFilterHeader: *headerHash} 1279 return &msgCFHeaders, nil 1280 1281 } 1282 1283 // GetCFilterHeaderAsync returns an instance of a type that can be used to get 1284 // the result of the RPC at some future time by invoking the Receive function 1285 // on the returned instance. 1286 // 1287 // See GetCFilterHeader for the blocking version and more details. 1288 func (c *Client) GetCFilterHeaderAsync(blockHash *chainhash.Hash, 1289 filterType wire.FilterType) FutureGetCFilterHeaderResult { 1290 hash := "" 1291 if blockHash != nil { 1292 hash = blockHash.String() 1293 } 1294 1295 cmd := btcjson.NewGetCFilterHeaderCmd(hash, filterType) 1296 return c.SendCmd(cmd) 1297 } 1298 1299 // GetCFilterHeader returns a raw filter header from the server given its block 1300 // hash. 1301 func (c *Client) GetCFilterHeader(blockHash *chainhash.Hash, 1302 filterType wire.FilterType) (*wire.MsgCFHeaders, error) { 1303 return c.GetCFilterHeaderAsync(blockHash, filterType).Receive() 1304 } 1305 1306 // FutureGetBlockStatsResult is a future promise to deliver the result of a 1307 // GetBlockStatsAsync RPC invocation (or an applicable error). 1308 type FutureGetBlockStatsResult chan *Response 1309 1310 // Receive waits for the Response promised by the future and returns statistics 1311 // of a block at a certain height. 1312 func (r FutureGetBlockStatsResult) Receive() (*btcjson.GetBlockStatsResult, error) { 1313 res, err := ReceiveFuture(r) 1314 if err != nil { 1315 return nil, err 1316 } 1317 1318 var blockStats btcjson.GetBlockStatsResult 1319 err = json.Unmarshal(res, &blockStats) 1320 if err != nil { 1321 return nil, err 1322 } 1323 1324 return &blockStats, nil 1325 } 1326 1327 // GetBlockStatsAsync returns an instance of a type that can be used to get 1328 // the result of the RPC at some future time by invoking the Receive function on 1329 // the returned instance. 1330 // 1331 // See GetBlockStats or the blocking version and more details. 1332 func (c *Client) GetBlockStatsAsync(hashOrHeight interface{}, stats *[]string) FutureGetBlockStatsResult { 1333 if hash, ok := hashOrHeight.(*chainhash.Hash); ok { 1334 hashOrHeight = hash.String() 1335 } 1336 1337 cmd := btcjson.NewGetBlockStatsCmd(btcjson.HashOrHeight{Value: hashOrHeight}, stats) 1338 return c.SendCmd(cmd) 1339 } 1340 1341 // GetBlockStats returns block statistics. First argument specifies height or hash of the target block. 1342 // Second argument allows to select certain stats to return. 1343 func (c *Client) GetBlockStats(hashOrHeight interface{}, stats *[]string) (*btcjson.GetBlockStatsResult, error) { 1344 return c.GetBlockStatsAsync(hashOrHeight, stats).Receive() 1345 } 1346 1347 // FutureDeriveAddressesResult is a future promise to deliver the result of an 1348 // DeriveAddressesAsync RPC invocation (or an applicable error). 1349 type FutureDeriveAddressesResult chan *Response 1350 1351 // Receive waits for the Response promised by the future and derives one or more addresses 1352 // corresponding to the given output descriptor. 1353 func (r FutureDeriveAddressesResult) Receive() (*btcjson.DeriveAddressesResult, error) { 1354 res, err := ReceiveFuture(r) 1355 if err != nil { 1356 return nil, err 1357 } 1358 1359 var deriveAddressesResult btcjson.DeriveAddressesResult 1360 1361 err = json.Unmarshal(res, &deriveAddressesResult) 1362 if err != nil { 1363 return nil, err 1364 } 1365 1366 return &deriveAddressesResult, nil 1367 } 1368 1369 // DeriveAddressesAsync returns an instance of a type that can be used to get the result 1370 // of the RPC at some future time by invoking the Receive function on the 1371 // returned instance. 1372 // 1373 // See DeriveAddresses for the blocking version and more details. 1374 func (c *Client) DeriveAddressesAsync(descriptor string, descriptorRange *btcjson.DescriptorRange) FutureDeriveAddressesResult { 1375 cmd := btcjson.NewDeriveAddressesCmd(descriptor, descriptorRange) 1376 return c.SendCmd(cmd) 1377 } 1378 1379 // DeriveAddresses derives one or more addresses corresponding to an output 1380 // descriptor. If a ranged descriptor is used, the end or the range 1381 // (in [begin,end] notation) to derive must be specified. 1382 func (c *Client) DeriveAddresses(descriptor string, descriptorRange *btcjson.DescriptorRange) (*btcjson.DeriveAddressesResult, error) { 1383 return c.DeriveAddressesAsync(descriptor, descriptorRange).Receive() 1384 } 1385 1386 // FutureGetDescriptorInfoResult is a future promise to deliver the result of a 1387 // GetDescriptorInfoAsync RPC invocation (or an applicable error). 1388 type FutureGetDescriptorInfoResult chan *Response 1389 1390 // Receive waits for the Response promised by the future and returns the analysed 1391 // info of the descriptor. 1392 func (r FutureGetDescriptorInfoResult) Receive() (*btcjson.GetDescriptorInfoResult, error) { 1393 res, err := ReceiveFuture(r) 1394 if err != nil { 1395 return nil, err 1396 } 1397 1398 var descriptorInfo btcjson.GetDescriptorInfoResult 1399 1400 err = json.Unmarshal(res, &descriptorInfo) 1401 if err != nil { 1402 return nil, err 1403 } 1404 1405 return &descriptorInfo, nil 1406 } 1407 1408 // GetDescriptorInfoAsync returns an instance of a type that can be used to get 1409 // the result of the RPC at some future time by invoking the Receive function on 1410 // the returned instance. 1411 // 1412 // See GetDescriptorInfo for the blocking version and more details. 1413 func (c *Client) GetDescriptorInfoAsync(descriptor string) FutureGetDescriptorInfoResult { 1414 cmd := btcjson.NewGetDescriptorInfoCmd(descriptor) 1415 return c.SendCmd(cmd) 1416 } 1417 1418 // GetDescriptorInfo returns the analysed info of a descriptor string, by invoking the 1419 // getdescriptorinfo RPC. 1420 // 1421 // Use this function to analyse a descriptor string, or compute the checksum 1422 // for a descriptor without one. 1423 // 1424 // See btcjson.GetDescriptorInfoResult for details about the result. 1425 func (c *Client) GetDescriptorInfo(descriptor string) (*btcjson.GetDescriptorInfoResult, error) { 1426 return c.GetDescriptorInfoAsync(descriptor).Receive() 1427 }