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.
15 "github.com/btcsuite/btcd/btcjson"
16 "github.com/btcsuite/btcd/chaincfg/chainhash"
17 "github.com/btcsuite/btcd/wire"
18 "github.com/btcsuite/btcutil"
21 // FutureDebugLevelResult is a future promise to deliver the result of a
22 // DebugLevelAsync RPC invocation (or an applicable error).
23 type FutureDebugLevelResult chan *response
25 // Receive waits for the response promised by the future and returns the result
26 // of setting the debug logging level to the passed level specification or the
27 // list of of the available subsystems for the special keyword 'show'.
28 func (r FutureDebugLevelResult) Receive() (string, error) {
29 res, err := receiveFuture(r)
34 // Unmashal the result as a string.
36 err = json.Unmarshal(res, &result)
43 // DebugLevelAsync returns an instance of a type that can be used to get the
44 // result of the RPC at some future time by invoking the Receive function on
45 // the returned instance.
47 // See DebugLevel for the blocking version and more details.
49 // NOTE: This is a btcd extension.
50 func (c *Client) DebugLevelAsync(levelSpec string) FutureDebugLevelResult {
51 cmd := btcjson.NewDebugLevelCmd(levelSpec)
55 // DebugLevel dynamically sets the debug logging level to the passed level
58 // The levelspec can be either a debug level or of the form:
59 // <subsystem>=<level>,<subsystem2>=<level2>,...
61 // Additionally, the special keyword 'show' can be used to get a list of the
62 // available subsystems.
64 // NOTE: This is a btcd extension.
65 func (c *Client) DebugLevel(levelSpec string) (string, error) {
66 return c.DebugLevelAsync(levelSpec).Receive()
69 // FutureCreateEncryptedWalletResult is a future promise to deliver the error
70 // result of a CreateEncryptedWalletAsync RPC invocation.
71 type FutureCreateEncryptedWalletResult chan *response
73 // Receive waits for and returns the error response promised by the future.
74 func (r FutureCreateEncryptedWalletResult) Receive() error {
75 _, err := receiveFuture(r)
79 // CreateEncryptedWalletAsync returns an instance of a type that can be used to
80 // get the result of the RPC at some future time by invoking the Receive
81 // function on the returned instance.
83 // See CreateEncryptedWallet for the blocking version and more details.
85 // NOTE: This is a btcwallet extension.
86 func (c *Client) CreateEncryptedWalletAsync(passphrase string) FutureCreateEncryptedWalletResult {
87 cmd := btcjson.NewCreateEncryptedWalletCmd(passphrase)
91 // CreateEncryptedWallet requests the creation of an encrypted wallet. Wallets
92 // managed by btcwallet are only written to disk with encrypted private keys,
93 // and generating wallets on the fly is impossible as it requires user input for
94 // the encryption passphrase. This RPC specifies the passphrase and instructs
95 // the wallet creation. This may error if a wallet is already opened, or the
96 // new wallet cannot be written to disk.
98 // NOTE: This is a btcwallet extension.
99 func (c *Client) CreateEncryptedWallet(passphrase string) error {
100 return c.CreateEncryptedWalletAsync(passphrase).Receive()
103 // FutureListAddressTransactionsResult is a future promise to deliver the result
104 // of a ListAddressTransactionsAsync RPC invocation (or an applicable error).
105 type FutureListAddressTransactionsResult chan *response
107 // Receive waits for the response promised by the future and returns information
108 // about all transactions associated with the provided addresses.
109 func (r FutureListAddressTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) {
110 res, err := receiveFuture(r)
115 // Unmarshal the result as an array of listtransactions objects.
116 var transactions []btcjson.ListTransactionsResult
117 err = json.Unmarshal(res, &transactions)
121 return transactions, nil
124 // ListAddressTransactionsAsync returns an instance of a type that can be used
125 // to get the result of the RPC at some future time by invoking the Receive
126 // function on the returned instance.
128 // See ListAddressTransactions for the blocking version and more details.
130 // NOTE: This is a btcd extension.
131 func (c *Client) ListAddressTransactionsAsync(addresses []btcutil.Address, account string) FutureListAddressTransactionsResult {
132 // Convert addresses to strings.
133 addrs := make([]string, 0, len(addresses))
134 for _, addr := range addresses {
135 addrs = append(addrs, addr.EncodeAddress())
137 cmd := btcjson.NewListAddressTransactionsCmd(addrs, &account)
138 return c.sendCmd(cmd)
141 // ListAddressTransactions returns information about all transactions associated
142 // with the provided addresses.
144 // NOTE: This is a btcwallet extension.
145 func (c *Client) ListAddressTransactions(addresses []btcutil.Address, account string) ([]btcjson.ListTransactionsResult, error) {
146 return c.ListAddressTransactionsAsync(addresses, account).Receive()
149 // FutureGetBestBlockResult is a future promise to deliver the result of a
150 // GetBestBlockAsync RPC invocation (or an applicable error).
151 type FutureGetBestBlockResult chan *response
153 // Receive waits for the response promised by the future and returns the hash
154 // and height of the block in the longest (best) chain.
155 func (r FutureGetBestBlockResult) Receive() (*chainhash.Hash, int32, error) {
156 res, err := receiveFuture(r)
161 // Unmarshal result as a getbestblock result object.
162 var bestBlock btcjson.GetBestBlockResult
163 err = json.Unmarshal(res, &bestBlock)
168 // Convert to hash from string.
169 hash, err := chainhash.NewHashFromStr(bestBlock.Hash)
174 return hash, bestBlock.Height, nil
177 // GetBestBlockAsync returns an instance of a type that can be used to get the
178 // result of the RPC at some future time by invoking the Receive function on the
179 // returned instance.
181 // See GetBestBlock for the blocking version and more details.
183 // NOTE: This is a btcd extension.
184 func (c *Client) GetBestBlockAsync() FutureGetBestBlockResult {
185 cmd := btcjson.NewGetBestBlockCmd()
186 return c.sendCmd(cmd)
189 // GetBestBlock returns the hash and height of the block in the longest (best)
192 // NOTE: This is a btcd extension.
193 func (c *Client) GetBestBlock() (*chainhash.Hash, int32, error) {
194 return c.GetBestBlockAsync().Receive()
197 // FutureGetCurrentNetResult is a future promise to deliver the result of a
198 // GetCurrentNetAsync RPC invocation (or an applicable error).
199 type FutureGetCurrentNetResult chan *response
201 // Receive waits for the response promised by the future and returns the network
202 // the server is running on.
203 func (r FutureGetCurrentNetResult) Receive() (wire.BitcoinNet, error) {
204 res, err := receiveFuture(r)
209 // Unmarshal result as an int64.
211 err = json.Unmarshal(res, &net)
216 return wire.BitcoinNet(net), nil
219 // GetCurrentNetAsync returns an instance of a type that can be used to get the
220 // result of the RPC at some future time by invoking the Receive function on the
221 // returned instance.
223 // See GetCurrentNet for the blocking version and more details.
225 // NOTE: This is a btcd extension.
226 func (c *Client) GetCurrentNetAsync() FutureGetCurrentNetResult {
227 cmd := btcjson.NewGetCurrentNetCmd()
228 return c.sendCmd(cmd)
231 // GetCurrentNet returns the network the server is running on.
233 // NOTE: This is a btcd extension.
234 func (c *Client) GetCurrentNet() (wire.BitcoinNet, error) {
235 return c.GetCurrentNetAsync().Receive()
238 // FutureGetHeadersResult is a future promise to deliver the result of a
239 // getheaders RPC invocation (or an applicable error).
241 // NOTE: This is a btcsuite extension ported from
242 // github.com/decred/dcrrpcclient.
243 type FutureGetHeadersResult chan *response
245 // Receive waits for the response promised by the future and returns the
246 // getheaders result.
248 // NOTE: This is a btcsuite extension ported from
249 // github.com/decred/dcrrpcclient.
250 func (r FutureGetHeadersResult) Receive() ([]wire.BlockHeader, error) {
251 res, err := receiveFuture(r)
256 // Unmarshal result as a slice of strings.
258 err = json.Unmarshal(res, &result)
263 // Deserialize the []string into []wire.BlockHeader.
264 headers := make([]wire.BlockHeader, len(result))
265 for i, headerHex := range result {
266 serialized, err := hex.DecodeString(headerHex)
270 err = headers[i].Deserialize(bytes.NewReader(serialized))
278 // GetHeadersAsync returns an instance of a type that can be used to get the result
279 // of the RPC at some future time by invoking the Receive function on the returned instance.
281 // See GetHeaders for the blocking version and more details.
283 // NOTE: This is a btcsuite extension ported from
284 // github.com/decred/dcrrpcclient.
285 func (c *Client) GetHeadersAsync(blockLocators []chainhash.Hash, hashStop *chainhash.Hash) FutureGetHeadersResult {
286 locators := make([]string, len(blockLocators))
287 for i := range blockLocators {
288 locators[i] = blockLocators[i].String()
292 hash = hashStop.String()
294 cmd := btcjson.NewGetHeadersCmd(locators, hash)
295 return c.sendCmd(cmd)
298 // GetHeaders mimics the wire protocol getheaders and headers messages by
299 // returning all headers on the main chain after the first known block in the
300 // locators, up until a block hash matches hashStop.
302 // NOTE: This is a btcsuite extension ported from
303 // github.com/decred/dcrrpcclient.
304 func (c *Client) GetHeaders(blockLocators []chainhash.Hash, hashStop *chainhash.Hash) ([]wire.BlockHeader, error) {
305 return c.GetHeadersAsync(blockLocators, hashStop).Receive()
308 // FutureExportWatchingWalletResult is a future promise to deliver the result of
309 // an ExportWatchingWalletAsync RPC invocation (or an applicable error).
310 type FutureExportWatchingWalletResult chan *response
312 // Receive waits for the response promised by the future and returns the
314 func (r FutureExportWatchingWalletResult) Receive() ([]byte, []byte, error) {
315 res, err := receiveFuture(r)
320 // Unmarshal result as a JSON object.
321 var obj map[string]interface{}
322 err = json.Unmarshal(res, &obj)
327 // Check for the wallet and tx string fields in the object.
328 base64Wallet, ok := obj["wallet"].(string)
330 return nil, nil, fmt.Errorf("unexpected response type for "+
331 "exportwatchingwallet 'wallet' field: %T\n",
334 base64TxStore, ok := obj["tx"].(string)
336 return nil, nil, fmt.Errorf("unexpected response type for "+
337 "exportwatchingwallet 'tx' field: %T\n",
341 walletBytes, err := base64.StdEncoding.DecodeString(base64Wallet)
346 txStoreBytes, err := base64.StdEncoding.DecodeString(base64TxStore)
351 return walletBytes, txStoreBytes, nil
355 // ExportWatchingWalletAsync returns an instance of a type that can be used to
356 // get the result of the RPC at some future time by invoking the Receive
357 // function on the returned instance.
359 // See ExportWatchingWallet for the blocking version and more details.
361 // NOTE: This is a btcwallet extension.
362 func (c *Client) ExportWatchingWalletAsync(account string) FutureExportWatchingWalletResult {
363 cmd := btcjson.NewExportWatchingWalletCmd(&account, btcjson.Bool(true))
364 return c.sendCmd(cmd)
367 // ExportWatchingWallet returns the raw bytes for a watching-only version of
368 // wallet.bin and tx.bin, respectively, for the specified account that can be
369 // used by btcwallet to enable a wallet which does not have the private keys
370 // necessary to spend funds.
372 // NOTE: This is a btcwallet extension.
373 func (c *Client) ExportWatchingWallet(account string) ([]byte, []byte, error) {
374 return c.ExportWatchingWalletAsync(account).Receive()
377 // FutureSessionResult is a future promise to deliver the result of a
378 // SessionAsync RPC invocation (or an applicable error).
379 type FutureSessionResult chan *response
381 // Receive waits for the response promised by the future and returns the
383 func (r FutureSessionResult) Receive() (*btcjson.SessionResult, error) {
384 res, err := receiveFuture(r)
389 // Unmarshal result as a session result object.
390 var session btcjson.SessionResult
391 err = json.Unmarshal(res, &session)
399 // SessionAsync returns an instance of a type that can be used to get the result
400 // of the RPC at some future time by invoking the Receive function on the
401 // returned instance.
403 // See Session for the blocking version and more details.
405 // NOTE: This is a btcsuite extension.
406 func (c *Client) SessionAsync() FutureSessionResult {
407 // Not supported in HTTP POST mode.
408 if c.config.HTTPPostMode {
409 return newFutureError(ErrWebsocketsRequired)
412 cmd := btcjson.NewSessionCmd()
413 return c.sendCmd(cmd)
416 // Session returns details regarding a websocket client's current connection.
418 // This RPC requires the client to be running in websocket mode.
420 // NOTE: This is a btcsuite extension.
421 func (c *Client) Session() (*btcjson.SessionResult, error) {
422 return c.SessionAsync().Receive()
425 // FutureVersionResult is a future promise to delivere the result of a version
426 // RPC invocation (or an applicable error).
428 // NOTE: This is a btcsuite extension ported from
429 // github.com/decred/dcrrpcclient.
430 type FutureVersionResult chan *response
432 // Receive waits for the response promised by the future and returns the version
435 // NOTE: This is a btcsuite extension ported from
436 // github.com/decred/dcrrpcclient.
437 func (r FutureVersionResult) Receive() (map[string]btcjson.VersionResult,
439 res, err := receiveFuture(r)
444 // Unmarshal result as a version result object.
445 var vr map[string]btcjson.VersionResult
446 err = json.Unmarshal(res, &vr)
454 // VersionAsync returns an instance of a type that can be used to get the result
455 // of the RPC at some future time by invoking the Receive function on the
456 // returned instance.
458 // See Version for the blocking version and more details.
460 // NOTE: This is a btcsuite extension ported from
461 // github.com/decred/dcrrpcclient.
462 func (c *Client) VersionAsync() FutureVersionResult {
463 cmd := btcjson.NewVersionCmd()
464 return c.sendCmd(cmd)
467 // Version returns information about the server's JSON-RPC API versions.
469 // NOTE: This is a btcsuite extension ported from
470 // github.com/decred/dcrrpcclient.
471 func (c *Client) Version() (map[string]btcjson.VersionResult, error) {
472 return c.VersionAsync().Receive()