2016-12-17 00:59:31 +01:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2017 The Decred developers
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-07-22 04:53:54 +02:00
|
|
|
package rpcclient
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
import (
|
2016-12-17 00:59:31 +01:00
|
|
|
"bytes"
|
2014-05-07 18:14:39 +02:00
|
|
|
"encoding/base64"
|
2016-12-17 00:59:31 +01:00
|
|
|
"encoding/hex"
|
2014-06-10 02:49:41 +02:00
|
|
|
"encoding/json"
|
2014-05-07 18:14:39 +02:00
|
|
|
"fmt"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2018-05-15 05:44:11 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FutureDebugLevelResult is a future promise to deliver the result of a
|
|
|
|
// DebugLevelAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureDebugLevelResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the result
|
2014-05-07 18:14:39 +02:00
|
|
|
// of setting the debug logging level to the passed level specification or the
|
|
|
|
// list of of the available subsystems for the special keyword 'show'.
|
|
|
|
func (r FutureDebugLevelResult) Receive() (string, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmashal the result as a string.
|
|
|
|
var result string
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-06-06 06:57:05 +02:00
|
|
|
// DebugLevelAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See DebugLevel for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
|
|
|
func (c *Client) DebugLevelAsync(levelSpec string) FutureDebugLevelResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewDebugLevelCmd(levelSpec)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-06-06 06:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DebugLevel dynamically sets the debug logging level to the passed level
|
|
|
|
// specification.
|
|
|
|
//
|
2015-09-20 13:42:49 +02:00
|
|
|
// The levelspec can be either a debug level or of the form:
|
2014-06-06 06:57:05 +02:00
|
|
|
// <subsystem>=<level>,<subsystem2>=<level2>,...
|
|
|
|
//
|
|
|
|
// Additionally, the special keyword 'show' can be used to get a list of the
|
|
|
|
// available subsystems.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
|
|
|
func (c *Client) DebugLevel(levelSpec string) (string, error) {
|
|
|
|
return c.DebugLevelAsync(levelSpec).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-28 18:33:28 +02:00
|
|
|
// FutureCreateEncryptedWalletResult is a future promise to deliver the error
|
|
|
|
// result of a CreateEncryptedWalletAsync RPC invocation.
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureCreateEncryptedWalletResult chan *Response
|
2014-05-28 18:33:28 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for and returns the error Response promised by the future.
|
2014-05-28 18:33:28 +02:00
|
|
|
func (r FutureCreateEncryptedWalletResult) Receive() error {
|
2021-09-02 08:39:55 +02:00
|
|
|
_, err := ReceiveFuture(r)
|
2014-05-28 18:33:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEncryptedWalletAsync returns an instance of a type that can be used to
|
|
|
|
// get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See CreateEncryptedWallet for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcwallet extension.
|
|
|
|
func (c *Client) CreateEncryptedWalletAsync(passphrase string) FutureCreateEncryptedWalletResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewCreateEncryptedWalletCmd(passphrase)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-28 18:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEncryptedWallet requests the creation of an encrypted wallet. Wallets
|
|
|
|
// managed by btcwallet are only written to disk with encrypted private keys,
|
|
|
|
// and generating wallets on the fly is impossible as it requires user input for
|
|
|
|
// the encryption passphrase. This RPC specifies the passphrase and instructs
|
|
|
|
// the wallet creation. This may error if a wallet is already opened, or the
|
|
|
|
// new wallet cannot be written to disk.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcwallet extension.
|
|
|
|
func (c *Client) CreateEncryptedWallet(passphrase string) error {
|
|
|
|
return c.CreateEncryptedWalletAsync(passphrase).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureListAddressTransactionsResult is a future promise to deliver the result
|
|
|
|
// of a ListAddressTransactionsAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureListAddressTransactionsResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns information
|
2014-05-07 18:14:39 +02:00
|
|
|
// about all transactions associated with the provided addresses.
|
|
|
|
func (r FutureListAddressTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as an array of listtransactions objects.
|
|
|
|
var transactions []btcjson.ListTransactionsResult
|
|
|
|
err = json.Unmarshal(res, &transactions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return transactions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAddressTransactionsAsync returns an instance of a type that can be used
|
|
|
|
// to get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See ListAddressTransactions for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
|
|
|
func (c *Client) ListAddressTransactionsAsync(addresses []btcutil.Address, account string) FutureListAddressTransactionsResult {
|
|
|
|
// Convert addresses to strings.
|
|
|
|
addrs := make([]string, 0, len(addresses))
|
|
|
|
for _, addr := range addresses {
|
|
|
|
addrs = append(addrs, addr.EncodeAddress())
|
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewListAddressTransactionsCmd(addrs, &account)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAddressTransactions returns information about all transactions associated
|
|
|
|
// with the provided addresses.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcwallet extension.
|
|
|
|
func (c *Client) ListAddressTransactions(addresses []btcutil.Address, account string) ([]btcjson.ListTransactionsResult, error) {
|
|
|
|
return c.ListAddressTransactionsAsync(addresses, account).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBestBlockResult is a future promise to deliver the result of a
|
|
|
|
// GetBestBlockAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBestBlockResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the hash
|
2014-05-07 18:14:39 +02:00
|
|
|
// and height of the block in the longest (best) chain.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (r FutureGetBestBlockResult) Receive() (*chainhash.Hash, int32, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:42:49 +02:00
|
|
|
// Unmarshal result as a getbestblock result object.
|
2015-01-01 23:34:07 +01:00
|
|
|
var bestBlock btcjson.GetBestBlockResult
|
2014-06-10 02:49:41 +02:00
|
|
|
err = json.Unmarshal(res, &bestBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:42:49 +02:00
|
|
|
// Convert to hash from string.
|
2016-08-08 21:16:04 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(bestBlock.Hash)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
return hash, bestBlock.Height, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlockAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetBestBlock for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
|
|
|
func (c *Client) GetBestBlockAsync() FutureGetBestBlockResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBestBlockCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlock returns the hash and height of the block in the longest (best)
|
|
|
|
// chain.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetBestBlock() (*chainhash.Hash, int32, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBestBlockAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetCurrentNetResult is a future promise to deliver the result of a
|
|
|
|
// GetCurrentNetAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetCurrentNetResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the network
|
2014-05-07 18:14:39 +02:00
|
|
|
// the server is running on.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (r FutureGetCurrentNetResult) Receive() (wire.BitcoinNet, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as an int64.
|
|
|
|
var net int64
|
|
|
|
err = json.Unmarshal(res, &net)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 22:50:03 +01:00
|
|
|
return wire.BitcoinNet(net), nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentNetAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetCurrentNet for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
|
|
|
func (c *Client) GetCurrentNetAsync() FutureGetCurrentNetResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetCurrentNetCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentNet returns the network the server is running on.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcd extension.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetCurrentNet() (wire.BitcoinNet, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetCurrentNetAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:59:31 +01:00
|
|
|
// FutureGetHeadersResult is a future promise to deliver the result of a
|
|
|
|
// getheaders RPC invocation (or an applicable error).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetHeadersResult chan *Response
|
2016-12-17 00:59:31 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2016-12-17 00:59:31 +01:00
|
|
|
// getheaders result.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (r FutureGetHeadersResult) Receive() ([]wire.BlockHeader, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2016-12-17 00:59:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a slice of strings.
|
|
|
|
var result []string
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the []string into []wire.BlockHeader.
|
|
|
|
headers := make([]wire.BlockHeader, len(result))
|
|
|
|
for i, headerHex := range result {
|
|
|
|
serialized, err := hex.DecodeString(headerHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = headers[i].Deserialize(bytes.NewReader(serialized))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return headers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeadersAsync returns an instance of a type that can be used to get the result
|
|
|
|
// of the RPC at some future time by invoking the Receive function on the returned instance.
|
|
|
|
//
|
|
|
|
// See GetHeaders for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) GetHeadersAsync(blockLocators []chainhash.Hash, hashStop *chainhash.Hash) FutureGetHeadersResult {
|
|
|
|
locators := make([]string, len(blockLocators))
|
|
|
|
for i := range blockLocators {
|
|
|
|
locators[i] = blockLocators[i].String()
|
|
|
|
}
|
|
|
|
hash := ""
|
|
|
|
if hashStop != nil {
|
|
|
|
hash = hashStop.String()
|
|
|
|
}
|
|
|
|
cmd := btcjson.NewGetHeadersCmd(locators, hash)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2016-12-17 00:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaders mimics the wire protocol getheaders and headers messages by
|
|
|
|
// returning all headers on the main chain after the first known block in the
|
|
|
|
// locators, up until a block hash matches hashStop.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) GetHeaders(blockLocators []chainhash.Hash, hashStop *chainhash.Hash) ([]wire.BlockHeader, error) {
|
|
|
|
return c.GetHeadersAsync(blockLocators, hashStop).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureExportWatchingWalletResult is a future promise to deliver the result of
|
|
|
|
// an ExportWatchingWalletAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureExportWatchingWalletResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2014-05-07 18:14:39 +02:00
|
|
|
// exported wallet.
|
|
|
|
func (r FutureExportWatchingWalletResult) Receive() ([]byte, []byte, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a JSON object.
|
|
|
|
var obj map[string]interface{}
|
|
|
|
err = json.Unmarshal(res, &obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Check for the wallet and tx string fields in the object.
|
|
|
|
base64Wallet, ok := obj["wallet"].(string)
|
2014-05-07 18:14:39 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected response type for "+
|
|
|
|
"exportwatchingwallet 'wallet' field: %T\n",
|
2014-06-10 02:49:41 +02:00
|
|
|
obj["wallet"])
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
base64TxStore, ok := obj["tx"].(string)
|
2014-05-07 18:14:39 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected response type for "+
|
|
|
|
"exportwatchingwallet 'tx' field: %T\n",
|
2014-06-10 02:49:41 +02:00
|
|
|
obj["tx"])
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
walletBytes, err := base64.StdEncoding.DecodeString(base64Wallet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txStoreBytes, err := base64.StdEncoding.DecodeString(base64TxStore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return walletBytes, txStoreBytes, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportWatchingWalletAsync returns an instance of a type that can be used to
|
|
|
|
// get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See ExportWatchingWallet for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcwallet extension.
|
|
|
|
func (c *Client) ExportWatchingWalletAsync(account string) FutureExportWatchingWalletResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewExportWatchingWalletCmd(&account, btcjson.Bool(true))
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExportWatchingWallet returns the raw bytes for a watching-only version of
|
|
|
|
// wallet.bin and tx.bin, respectively, for the specified account that can be
|
|
|
|
// used by btcwallet to enable a wallet which does not have the private keys
|
|
|
|
// necessary to spend funds.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcwallet extension.
|
|
|
|
func (c *Client) ExportWatchingWallet(account string) ([]byte, []byte, error) {
|
|
|
|
return c.ExportWatchingWalletAsync(account).Receive()
|
|
|
|
}
|
2015-09-15 21:11:47 +02:00
|
|
|
|
|
|
|
// FutureSessionResult is a future promise to deliver the result of a
|
|
|
|
// SessionAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureSessionResult chan *Response
|
2015-09-15 21:11:47 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2015-09-15 21:11:47 +02:00
|
|
|
// session result.
|
|
|
|
func (r FutureSessionResult) Receive() (*btcjson.SessionResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2015-09-15 21:11:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:42:49 +02:00
|
|
|
// Unmarshal result as a session result object.
|
2015-09-15 21:11:47 +02:00
|
|
|
var session btcjson.SessionResult
|
|
|
|
err = json.Unmarshal(res, &session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionAsync returns an instance of a type that can be used to get the result
|
|
|
|
// of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See Session for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension.
|
|
|
|
func (c *Client) SessionAsync() FutureSessionResult {
|
|
|
|
// Not supported in HTTP POST mode.
|
|
|
|
if c.config.HTTPPostMode {
|
|
|
|
return newFutureError(ErrWebsocketsRequired)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewSessionCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2015-09-15 21:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Session returns details regarding a websocket client's current connection.
|
|
|
|
//
|
|
|
|
// This RPC requires the client to be running in websocket mode.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension.
|
|
|
|
func (c *Client) Session() (*btcjson.SessionResult, error) {
|
|
|
|
return c.SessionAsync().Receive()
|
|
|
|
}
|
2016-12-15 22:03:58 +01:00
|
|
|
|
2017-10-29 20:56:03 +01:00
|
|
|
// FutureVersionResult is a future promise to deliver the result of a version
|
2016-12-15 22:03:58 +01:00
|
|
|
// RPC invocation (or an applicable error).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureVersionResult chan *Response
|
2016-12-15 22:03:58 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the version
|
2016-12-15 22:03:58 +01:00
|
|
|
// result.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (r FutureVersionResult) Receive() (map[string]btcjson.VersionResult,
|
|
|
|
error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2016-12-15 22:03:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a version result object.
|
|
|
|
var vr map[string]btcjson.VersionResult
|
|
|
|
err = json.Unmarshal(res, &vr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return vr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionAsync returns an instance of a type that can be used to get the result
|
|
|
|
// of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See Version for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) VersionAsync() FutureVersionResult {
|
|
|
|
cmd := btcjson.NewVersionCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2016-12-15 22:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns information about the server's JSON-RPC API versions.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) Version() (map[string]btcjson.VersionResult, error) {
|
|
|
|
return c.VersionAsync().Receive()
|
|
|
|
}
|