OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / rpcclient / wallet.go
1 // Copyright (c) 2014-2017 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package rpcclient
6
7 import (
8         "encoding/json"
9         "strconv"
10
11         "github.com/btcsuite/btcd/btcjson"
12         "github.com/btcsuite/btcd/chaincfg"
13         "github.com/btcsuite/btcd/chaincfg/chainhash"
14         "github.com/btcsuite/btcd/wire"
15         "github.com/btcsuite/btcutil"
16 )
17
18 // *****************************
19 // Transaction Listing Functions
20 // *****************************
21
22 // FutureGetTransactionResult is a future promise to deliver the result
23 // of a GetTransactionAsync RPC invocation (or an applicable error).
24 type FutureGetTransactionResult chan *response
25
26 // Receive waits for the response promised by the future and returns detailed
27 // information about a wallet transaction.
28 func (r FutureGetTransactionResult) Receive() (*btcjson.GetTransactionResult, error) {
29         res, err := receiveFuture(r)
30         if err != nil {
31                 return nil, err
32         }
33
34         // Unmarshal result as a gettransaction result object
35         var getTx btcjson.GetTransactionResult
36         err = json.Unmarshal(res, &getTx)
37         if err != nil {
38                 return nil, err
39         }
40
41         return &getTx, nil
42 }
43
44 // GetTransactionAsync returns an instance of a type that can be used to get the
45 // result of the RPC at some future time by invoking the Receive function on
46 // the returned instance.
47 //
48 // See GetTransaction for the blocking version and more details.
49 func (c *Client) GetTransactionAsync(txHash *chainhash.Hash) FutureGetTransactionResult {
50         hash := ""
51         if txHash != nil {
52                 hash = txHash.String()
53         }
54
55         cmd := btcjson.NewGetTransactionCmd(hash, nil)
56         return c.sendCmd(cmd)
57 }
58
59 // GetTransaction returns detailed information about a wallet transaction.
60 //
61 // See GetRawTransaction to return the raw transaction instead.
62 func (c *Client) GetTransaction(txHash *chainhash.Hash) (*btcjson.GetTransactionResult, error) {
63         return c.GetTransactionAsync(txHash).Receive()
64 }
65
66 // FutureListTransactionsResult is a future promise to deliver the result of a
67 // ListTransactionsAsync, ListTransactionsCountAsync, or
68 // ListTransactionsCountFromAsync RPC invocation (or an applicable error).
69 type FutureListTransactionsResult chan *response
70
71 // Receive waits for the response promised by the future and returns a list of
72 // the most recent transactions.
73 func (r FutureListTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) {
74         res, err := receiveFuture(r)
75         if err != nil {
76                 return nil, err
77         }
78
79         // Unmarshal result as an array of listtransaction result objects.
80         var transactions []btcjson.ListTransactionsResult
81         err = json.Unmarshal(res, &transactions)
82         if err != nil {
83                 return nil, err
84         }
85
86         return transactions, nil
87 }
88
89 // ListTransactionsAsync returns an instance of a type that can be used to get
90 // the result of the RPC at some future time by invoking the Receive function on
91 // the returned instance.
92 //
93 // See ListTransactions for the blocking version and more details.
94 func (c *Client) ListTransactionsAsync(account string) FutureListTransactionsResult {
95         cmd := btcjson.NewListTransactionsCmd(&account, nil, nil, nil)
96         return c.sendCmd(cmd)
97 }
98
99 // ListTransactions returns a list of the most recent transactions.
100 //
101 // See the ListTransactionsCount and ListTransactionsCountFrom to control the
102 // number of transactions returned and starting point, respectively.
103 func (c *Client) ListTransactions(account string) ([]btcjson.ListTransactionsResult, error) {
104         return c.ListTransactionsAsync(account).Receive()
105 }
106
107 // ListTransactionsCountAsync returns an instance of a type that can be used to
108 // get the result of the RPC at some future time by invoking the Receive
109 // function on the returned instance.
110 //
111 // See ListTransactionsCount for the blocking version and more details.
112 func (c *Client) ListTransactionsCountAsync(account string, count int) FutureListTransactionsResult {
113         cmd := btcjson.NewListTransactionsCmd(&account, &count, nil, nil)
114         return c.sendCmd(cmd)
115 }
116
117 // ListTransactionsCount returns a list of the most recent transactions up
118 // to the passed count.
119 //
120 // See the ListTransactions and ListTransactionsCountFrom functions for
121 // different options.
122 func (c *Client) ListTransactionsCount(account string, count int) ([]btcjson.ListTransactionsResult, error) {
123         return c.ListTransactionsCountAsync(account, count).Receive()
124 }
125
126 // ListTransactionsCountFromAsync returns an instance of a type that can be used
127 // to get the result of the RPC at some future time by invoking the Receive
128 // function on the returned instance.
129 //
130 // See ListTransactionsCountFrom for the blocking version and more details.
131 func (c *Client) ListTransactionsCountFromAsync(account string, count, from int) FutureListTransactionsResult {
132         cmd := btcjson.NewListTransactionsCmd(&account, &count, &from, nil)
133         return c.sendCmd(cmd)
134 }
135
136 // ListTransactionsCountFrom returns a list of the most recent transactions up
137 // to the passed count while skipping the first 'from' transactions.
138 //
139 // See the ListTransactions and ListTransactionsCount functions to use defaults.
140 func (c *Client) ListTransactionsCountFrom(account string, count, from int) ([]btcjson.ListTransactionsResult, error) {
141         return c.ListTransactionsCountFromAsync(account, count, from).Receive()
142 }
143
144 // FutureListUnspentResult is a future promise to deliver the result of a
145 // ListUnspentAsync, ListUnspentMinAsync, ListUnspentMinMaxAsync, or
146 // ListUnspentMinMaxAddressesAsync RPC invocation (or an applicable error).
147 type FutureListUnspentResult chan *response
148
149 // Receive waits for the response promised by the future and returns all
150 // unspent wallet transaction outputs returned by the RPC call.  If the
151 // future wac returnd by a call to ListUnspentMinAsync, ListUnspentMinMaxAsync,
152 // or ListUnspentMinMaxAddressesAsync, the range may be limited by the
153 // parameters of the RPC invocation.
154 func (r FutureListUnspentResult) Receive() ([]btcjson.ListUnspentResult, error) {
155         res, err := receiveFuture(r)
156         if err != nil {
157                 return nil, err
158         }
159
160         // Unmarshal result as an array of listunspent results.
161         var unspent []btcjson.ListUnspentResult
162         err = json.Unmarshal(res, &unspent)
163         if err != nil {
164                 return nil, err
165         }
166
167         return unspent, nil
168 }
169
170 // ListUnspentAsync returns an instance of a type that can be used to get
171 // the result of the RPC at some future time by invoking the Receive function
172 // on the returned instance.
173 //
174 // See ListUnspent for the blocking version and more details.
175 func (c *Client) ListUnspentAsync() FutureListUnspentResult {
176         cmd := btcjson.NewListUnspentCmd(nil, nil, nil)
177         return c.sendCmd(cmd)
178 }
179
180 // ListUnspentMinAsync returns an instance of a type that can be used to get
181 // the result of the RPC at some future time by invoking the Receive function
182 // on the returned instance.
183 //
184 // See ListUnspentMin for the blocking version and more details.
185 func (c *Client) ListUnspentMinAsync(minConf int) FutureListUnspentResult {
186         cmd := btcjson.NewListUnspentCmd(&minConf, nil, nil)
187         return c.sendCmd(cmd)
188 }
189
190 // ListUnspentMinMaxAsync returns an instance of a type that can be used to get
191 // the result of the RPC at some future time by invoking the Receive function
192 // on the returned instance.
193 //
194 // See ListUnspentMinMax for the blocking version and more details.
195 func (c *Client) ListUnspentMinMaxAsync(minConf, maxConf int) FutureListUnspentResult {
196         cmd := btcjson.NewListUnspentCmd(&minConf, &maxConf, nil)
197         return c.sendCmd(cmd)
198 }
199
200 // ListUnspentMinMaxAddressesAsync returns an instance of a type that can be
201 // used to get the result of the RPC at some future time by invoking the Receive
202 // function on the returned instance.
203 //
204 // See ListUnspentMinMaxAddresses for the blocking version and more details.
205 func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, addrs []btcutil.Address) FutureListUnspentResult {
206         addrStrs := make([]string, 0, len(addrs))
207         for _, a := range addrs {
208                 addrStrs = append(addrStrs, a.EncodeAddress())
209         }
210
211         cmd := btcjson.NewListUnspentCmd(&minConf, &maxConf, &addrStrs)
212         return c.sendCmd(cmd)
213 }
214
215 // ListUnspent returns all unspent transaction outputs known to a wallet, using
216 // the default number of minimum and maximum number of confirmations as a
217 // filter (1 and 999999, respectively).
218 func (c *Client) ListUnspent() ([]btcjson.ListUnspentResult, error) {
219         return c.ListUnspentAsync().Receive()
220 }
221
222 // ListUnspentMin returns all unspent transaction outputs known to a wallet,
223 // using the specified number of minimum conformations and default number of
224 // maximum confiramtions (999999) as a filter.
225 func (c *Client) ListUnspentMin(minConf int) ([]btcjson.ListUnspentResult, error) {
226         return c.ListUnspentMinAsync(minConf).Receive()
227 }
228
229 // ListUnspentMinMax returns all unspent transaction outputs known to a wallet,
230 // using the specified number of minimum and maximum number of confirmations as
231 // a filter.
232 func (c *Client) ListUnspentMinMax(minConf, maxConf int) ([]btcjson.ListUnspentResult, error) {
233         return c.ListUnspentMinMaxAsync(minConf, maxConf).Receive()
234 }
235
236 // ListUnspentMinMaxAddresses returns all unspent transaction outputs that pay
237 // to any of specified addresses in a wallet using the specified number of
238 // minimum and maximum number of confirmations as a filter.
239 func (c *Client) ListUnspentMinMaxAddresses(minConf, maxConf int, addrs []btcutil.Address) ([]btcjson.ListUnspentResult, error) {
240         return c.ListUnspentMinMaxAddressesAsync(minConf, maxConf, addrs).Receive()
241 }
242
243 // FutureListSinceBlockResult is a future promise to deliver the result of a
244 // ListSinceBlockAsync or ListSinceBlockMinConfAsync RPC invocation (or an
245 // applicable error).
246 type FutureListSinceBlockResult chan *response
247
248 // Receive waits for the response promised by the future and returns all
249 // transactions added in blocks since the specified block hash, or all
250 // transactions if it is nil.
251 func (r FutureListSinceBlockResult) Receive() (*btcjson.ListSinceBlockResult, error) {
252         res, err := receiveFuture(r)
253         if err != nil {
254                 return nil, err
255         }
256
257         // Unmarshal result as a listsinceblock result object.
258         var listResult btcjson.ListSinceBlockResult
259         err = json.Unmarshal(res, &listResult)
260         if err != nil {
261                 return nil, err
262         }
263
264         return &listResult, nil
265 }
266
267 // ListSinceBlockAsync returns an instance of a type that can be used to get
268 // the result of the RPC at some future time by invoking the Receive function on
269 // the returned instance.
270 //
271 // See ListSinceBlock for the blocking version and more details.
272 func (c *Client) ListSinceBlockAsync(blockHash *chainhash.Hash) FutureListSinceBlockResult {
273         var hash *string
274         if blockHash != nil {
275                 hash = btcjson.String(blockHash.String())
276         }
277
278         cmd := btcjson.NewListSinceBlockCmd(hash, nil, nil)
279         return c.sendCmd(cmd)
280 }
281
282 // ListSinceBlock returns all transactions added in blocks since the specified
283 // block hash, or all transactions if it is nil, using the default number of
284 // minimum confirmations as a filter.
285 //
286 // See ListSinceBlockMinConf to override the minimum number of confirmations.
287 func (c *Client) ListSinceBlock(blockHash *chainhash.Hash) (*btcjson.ListSinceBlockResult, error) {
288         return c.ListSinceBlockAsync(blockHash).Receive()
289 }
290
291 // ListSinceBlockMinConfAsync returns an instance of a type that can be used to
292 // get the result of the RPC at some future time by invoking the Receive
293 // function on the returned instance.
294 //
295 // See ListSinceBlockMinConf for the blocking version and more details.
296 func (c *Client) ListSinceBlockMinConfAsync(blockHash *chainhash.Hash, minConfirms int) FutureListSinceBlockResult {
297         var hash *string
298         if blockHash != nil {
299                 hash = btcjson.String(blockHash.String())
300         }
301
302         cmd := btcjson.NewListSinceBlockCmd(hash, &minConfirms, nil)
303         return c.sendCmd(cmd)
304 }
305
306 // ListSinceBlockMinConf returns all transactions added in blocks since the
307 // specified block hash, or all transactions if it is nil, using the specified
308 // number of minimum confirmations as a filter.
309 //
310 // See ListSinceBlock to use the default minimum number of confirmations.
311 func (c *Client) ListSinceBlockMinConf(blockHash *chainhash.Hash, minConfirms int) (*btcjson.ListSinceBlockResult, error) {
312         return c.ListSinceBlockMinConfAsync(blockHash, minConfirms).Receive()
313 }
314
315 // **************************
316 // Transaction Send Functions
317 // **************************
318
319 // FutureLockUnspentResult is a future promise to deliver the error result of a
320 // LockUnspentAsync RPC invocation.
321 type FutureLockUnspentResult chan *response
322
323 // Receive waits for the response promised by the future and returns the result
324 // of locking or unlocking the unspent output(s).
325 func (r FutureLockUnspentResult) Receive() error {
326         _, err := receiveFuture(r)
327         return err
328 }
329
330 // LockUnspentAsync returns an instance of a type that can be used to get the
331 // result of the RPC at some future time by invoking the Receive function on the
332 // returned instance.
333 //
334 // See LockUnspent for the blocking version and more details.
335 func (c *Client) LockUnspentAsync(unlock bool, ops []*wire.OutPoint) FutureLockUnspentResult {
336         outputs := make([]btcjson.TransactionInput, len(ops))
337         for i, op := range ops {
338                 outputs[i] = btcjson.TransactionInput{
339                         Txid: op.Hash.String(),
340                         Vout: op.Index,
341                 }
342         }
343         cmd := btcjson.NewLockUnspentCmd(unlock, outputs)
344         return c.sendCmd(cmd)
345 }
346
347 // LockUnspent marks outputs as locked or unlocked, depending on the value of
348 // the unlock bool.  When locked, the unspent output will not be selected as
349 // input for newly created, non-raw transactions, and will not be returned in
350 // future ListUnspent results, until the output is marked unlocked again.
351 //
352 // If unlock is false, each outpoint in ops will be marked locked.  If unlocked
353 // is true and specific outputs are specified in ops (len != 0), exactly those
354 // outputs will be marked unlocked.  If unlocked is true and no outpoints are
355 // specified, all previous locked outputs are marked unlocked.
356 //
357 // The locked or unlocked state of outputs are not written to disk and after
358 // restarting a wallet process, this data will be reset (every output unlocked).
359 //
360 // NOTE: While this method would be a bit more readable if the unlock bool was
361 // reversed (that is, LockUnspent(true, ...) locked the outputs), it has been
362 // left as unlock to keep compatibility with the reference client API and to
363 // avoid confusion for those who are already familiar with the lockunspent RPC.
364 func (c *Client) LockUnspent(unlock bool, ops []*wire.OutPoint) error {
365         return c.LockUnspentAsync(unlock, ops).Receive()
366 }
367
368 // FutureListLockUnspentResult is a future promise to deliver the result of a
369 // ListLockUnspentAsync RPC invocation (or an applicable error).
370 type FutureListLockUnspentResult chan *response
371
372 // Receive waits for the response promised by the future and returns the result
373 // of all currently locked unspent outputs.
374 func (r FutureListLockUnspentResult) Receive() ([]*wire.OutPoint, error) {
375         res, err := receiveFuture(r)
376         if err != nil {
377                 return nil, err
378         }
379
380         // Unmarshal as an array of transaction inputs.
381         var inputs []btcjson.TransactionInput
382         err = json.Unmarshal(res, &inputs)
383         if err != nil {
384                 return nil, err
385         }
386
387         // Create a slice of outpoints from the transaction input structs.
388         ops := make([]*wire.OutPoint, len(inputs))
389         for i, input := range inputs {
390                 sha, err := chainhash.NewHashFromStr(input.Txid)
391                 if err != nil {
392                         return nil, err
393                 }
394                 ops[i] = wire.NewOutPoint(sha, input.Vout)
395         }
396
397         return ops, nil
398 }
399
400 // ListLockUnspentAsync returns an instance of a type that can be used to get
401 // the result of the RPC at some future time by invoking the Receive function on
402 // the returned instance.
403 //
404 // See ListLockUnspent for the blocking version and more details.
405 func (c *Client) ListLockUnspentAsync() FutureListLockUnspentResult {
406         cmd := btcjson.NewListLockUnspentCmd()
407         return c.sendCmd(cmd)
408 }
409
410 // ListLockUnspent returns a slice of outpoints for all unspent outputs marked
411 // as locked by a wallet.  Unspent outputs may be marked locked using
412 // LockOutput.
413 func (c *Client) ListLockUnspent() ([]*wire.OutPoint, error) {
414         return c.ListLockUnspentAsync().Receive()
415 }
416
417 // FutureSetTxFeeResult is a future promise to deliver the result of a
418 // SetTxFeeAsync RPC invocation (or an applicable error).
419 type FutureSetTxFeeResult chan *response
420
421 // Receive waits for the response promised by the future and returns the result
422 // of setting an optional transaction fee per KB that helps ensure transactions
423 // are processed quickly.  Most transaction are 1KB.
424 func (r FutureSetTxFeeResult) Receive() error {
425         _, err := receiveFuture(r)
426         return err
427 }
428
429 // SetTxFeeAsync returns an instance of a type that can be used to get the
430 // result of the RPC at some future time by invoking the Receive function on the
431 // returned instance.
432 //
433 // See SetTxFee for the blocking version and more details.
434 func (c *Client) SetTxFeeAsync(fee btcutil.Amount) FutureSetTxFeeResult {
435         cmd := btcjson.NewSetTxFeeCmd(fee.ToBTC())
436         return c.sendCmd(cmd)
437 }
438
439 // SetTxFee sets an optional transaction fee per KB that helps ensure
440 // transactions are processed quickly.  Most transaction are 1KB.
441 func (c *Client) SetTxFee(fee btcutil.Amount) error {
442         return c.SetTxFeeAsync(fee).Receive()
443 }
444
445 // FutureSendToAddressResult is a future promise to deliver the result of a
446 // SendToAddressAsync RPC invocation (or an applicable error).
447 type FutureSendToAddressResult chan *response
448
449 // Receive waits for the response promised by the future and returns the hash
450 // of the transaction sending the passed amount to the given address.
451 func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) {
452         res, err := receiveFuture(r)
453         if err != nil {
454                 return nil, err
455         }
456
457         // Unmarshal result as a string.
458         var txHash string
459         err = json.Unmarshal(res, &txHash)
460         if err != nil {
461                 return nil, err
462         }
463
464         return chainhash.NewHashFromStr(txHash)
465 }
466
467 // SendToAddressAsync returns an instance of a type that can be used to get the
468 // result of the RPC at some future time by invoking the Receive function on the
469 // returned instance.
470 //
471 // See SendToAddress for the blocking version and more details.
472 func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount) FutureSendToAddressResult {
473         addr := address.EncodeAddress()
474         cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), nil, nil)
475         return c.sendCmd(cmd)
476 }
477
478 // SendToAddress sends the passed amount to the given address.
479 //
480 // See SendToAddressComment to associate comments with the transaction in the
481 // wallet.  The comments are not part of the transaction and are only internal
482 // to the wallet.
483 //
484 // NOTE: This function requires to the wallet to be unlocked.  See the
485 // WalletPassphrase function for more details.
486 func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) {
487         return c.SendToAddressAsync(address, amount).Receive()
488 }
489
490 // SendToAddressCommentAsync returns an instance of a type that can be used to
491 // get the result of the RPC at some future time by invoking the Receive
492 // function on the returned instance.
493 //
494 // See SendToAddressComment for the blocking version and more details.
495 func (c *Client) SendToAddressCommentAsync(address btcutil.Address,
496         amount btcutil.Amount, comment,
497         commentTo string) FutureSendToAddressResult {
498
499         addr := address.EncodeAddress()
500         cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), &comment,
501                 &commentTo)
502         return c.sendCmd(cmd)
503 }
504
505 // SendToAddressComment sends the passed amount to the given address and stores
506 // the provided comment and comment to in the wallet.  The comment parameter is
507 // intended to be used for the purpose of the transaction while the commentTo
508 // parameter is indended to be used for who the transaction is being sent to.
509 //
510 // The comments are not part of the transaction and are only internal
511 // to the wallet.
512 //
513 // See SendToAddress to avoid using comments.
514 //
515 // NOTE: This function requires to the wallet to be unlocked.  See the
516 // WalletPassphrase function for more details.
517 func (c *Client) SendToAddressComment(address btcutil.Address, amount btcutil.Amount, comment, commentTo string) (*chainhash.Hash, error) {
518         return c.SendToAddressCommentAsync(address, amount, comment,
519                 commentTo).Receive()
520 }
521
522 // FutureSendFromResult is a future promise to deliver the result of a
523 // SendFromAsync, SendFromMinConfAsync, or SendFromCommentAsync RPC invocation
524 // (or an applicable error).
525 type FutureSendFromResult chan *response
526
527 // Receive waits for the response promised by the future and returns the hash
528 // of the transaction sending amount to the given address using the provided
529 // account as a source of funds.
530 func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) {
531         res, err := receiveFuture(r)
532         if err != nil {
533                 return nil, err
534         }
535
536         // Unmarshal result as a string.
537         var txHash string
538         err = json.Unmarshal(res, &txHash)
539         if err != nil {
540                 return nil, err
541         }
542
543         return chainhash.NewHashFromStr(txHash)
544 }
545
546 // SendFromAsync returns an instance of a type that can be used to get the
547 // result of the RPC at some future time by invoking the Receive function on the
548 // returned instance.
549 //
550 // See SendFrom for the blocking version and more details.
551 func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) FutureSendFromResult {
552         addr := toAddress.EncodeAddress()
553         cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil,
554                 nil, nil)
555         return c.sendCmd(cmd)
556 }
557
558 // SendFrom sends the passed amount to the given address using the provided
559 // account as a source of funds.  Only funds with the default number of minimum
560 // confirmations will be used.
561 //
562 // See SendFromMinConf and SendFromComment for different options.
563 //
564 // NOTE: This function requires to the wallet to be unlocked.  See the
565 // WalletPassphrase function for more details.
566 func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) {
567         return c.SendFromAsync(fromAccount, toAddress, amount).Receive()
568 }
569
570 // SendFromMinConfAsync returns an instance of a type that can be used to get
571 // the result of the RPC at some future time by invoking the Receive function on
572 // the returned instance.
573 //
574 // See SendFromMinConf for the blocking version and more details.
575 func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) FutureSendFromResult {
576         addr := toAddress.EncodeAddress()
577         cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
578                 &minConfirms, nil, nil)
579         return c.sendCmd(cmd)
580 }
581
582 // SendFromMinConf sends the passed amount to the given address using the
583 // provided account as a source of funds.  Only funds with the passed number of
584 // minimum confirmations will be used.
585 //
586 // See SendFrom to use the default number of minimum confirmations and
587 // SendFromComment for additional options.
588 //
589 // NOTE: This function requires to the wallet to be unlocked.  See the
590 // WalletPassphrase function for more details.
591 func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) (*chainhash.Hash, error) {
592         return c.SendFromMinConfAsync(fromAccount, toAddress, amount,
593                 minConfirms).Receive()
594 }
595
596 // SendFromCommentAsync returns an instance of a type that can be used to get
597 // the result of the RPC at some future time by invoking the Receive function on
598 // the returned instance.
599 //
600 // See SendFromComment for the blocking version and more details.
601 func (c *Client) SendFromCommentAsync(fromAccount string,
602         toAddress btcutil.Address, amount btcutil.Amount, minConfirms int,
603         comment, commentTo string) FutureSendFromResult {
604
605         addr := toAddress.EncodeAddress()
606         cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
607                 &minConfirms, &comment, &commentTo)
608         return c.sendCmd(cmd)
609 }
610
611 // SendFromComment sends the passed amount to the given address using the
612 // provided account as a source of funds and stores the provided comment and
613 // comment to in the wallet.  The comment parameter is intended to be used for
614 // the purpose of the transaction while the commentTo parameter is indended to
615 // be used for who the transaction is being sent to.  Only funds with the passed
616 // number of minimum confirmations will be used.
617 //
618 // See SendFrom and SendFromMinConf to use defaults.
619 //
620 // NOTE: This function requires to the wallet to be unlocked.  See the
621 // WalletPassphrase function for more details.
622 func (c *Client) SendFromComment(fromAccount string, toAddress btcutil.Address,
623         amount btcutil.Amount, minConfirms int,
624         comment, commentTo string) (*chainhash.Hash, error) {
625
626         return c.SendFromCommentAsync(fromAccount, toAddress, amount,
627                 minConfirms, comment, commentTo).Receive()
628 }
629
630 // FutureSendManyResult is a future promise to deliver the result of a
631 // SendManyAsync, SendManyMinConfAsync, or SendManyCommentAsync RPC invocation
632 // (or an applicable error).
633 type FutureSendManyResult chan *response
634
635 // Receive waits for the response promised by the future and returns the hash
636 // of the transaction sending multiple amounts to multiple addresses using the
637 // provided account as a source of funds.
638 func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) {
639         res, err := receiveFuture(r)
640         if err != nil {
641                 return nil, err
642         }
643
644         // Unmashal result as a string.
645         var txHash string
646         err = json.Unmarshal(res, &txHash)
647         if err != nil {
648                 return nil, err
649         }
650
651         return chainhash.NewHashFromStr(txHash)
652 }
653
654 // SendManyAsync returns an instance of a type that can be used to get the
655 // result of the RPC at some future time by invoking the Receive function on the
656 // returned instance.
657 //
658 // See SendMany for the blocking version and more details.
659 func (c *Client) SendManyAsync(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) FutureSendManyResult {
660         convertedAmounts := make(map[string]float64, len(amounts))
661         for addr, amount := range amounts {
662                 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
663         }
664         cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil)
665         return c.sendCmd(cmd)
666 }
667
668 // SendMany sends multiple amounts to multiple addresses using the provided
669 // account as a source of funds in a single transaction.  Only funds with the
670 // default number of minimum confirmations will be used.
671 //
672 // See SendManyMinConf and SendManyComment for different options.
673 //
674 // NOTE: This function requires to the wallet to be unlocked.  See the
675 // WalletPassphrase function for more details.
676 func (c *Client) SendMany(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) (*chainhash.Hash, error) {
677         return c.SendManyAsync(fromAccount, amounts).Receive()
678 }
679
680 // SendManyMinConfAsync returns an instance of a type that can be used to get
681 // the result of the RPC at some future time by invoking the Receive function on
682 // the returned instance.
683 //
684 // See SendManyMinConf for the blocking version and more details.
685 func (c *Client) SendManyMinConfAsync(fromAccount string,
686         amounts map[btcutil.Address]btcutil.Amount,
687         minConfirms int) FutureSendManyResult {
688
689         convertedAmounts := make(map[string]float64, len(amounts))
690         for addr, amount := range amounts {
691                 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
692         }
693         cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
694                 &minConfirms, nil)
695         return c.sendCmd(cmd)
696 }
697
698 // SendManyMinConf sends multiple amounts to multiple addresses using the
699 // provided account as a source of funds in a single transaction.  Only funds
700 // with the passed number of minimum confirmations will be used.
701 //
702 // See SendMany to use the default number of minimum confirmations and
703 // SendManyComment for additional options.
704 //
705 // NOTE: This function requires to the wallet to be unlocked.  See the
706 // WalletPassphrase function for more details.
707 func (c *Client) SendManyMinConf(fromAccount string,
708         amounts map[btcutil.Address]btcutil.Amount,
709         minConfirms int) (*chainhash.Hash, error) {
710
711         return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive()
712 }
713
714 // SendManyCommentAsync returns an instance of a type that can be used to get
715 // the result of the RPC at some future time by invoking the Receive function on
716 // the returned instance.
717 //
718 // See SendManyComment for the blocking version and more details.
719 func (c *Client) SendManyCommentAsync(fromAccount string,
720         amounts map[btcutil.Address]btcutil.Amount, minConfirms int,
721         comment string) FutureSendManyResult {
722
723         convertedAmounts := make(map[string]float64, len(amounts))
724         for addr, amount := range amounts {
725                 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
726         }
727         cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
728                 &minConfirms, &comment)
729         return c.sendCmd(cmd)
730 }
731
732 // SendManyComment sends multiple amounts to multiple addresses using the
733 // provided account as a source of funds in a single transaction and stores the
734 // provided comment in the wallet.  The comment parameter is intended to be used
735 // for the purpose of the transaction   Only funds with the passed number of
736 // minimum confirmations will be used.
737 //
738 // See SendMany and SendManyMinConf to use defaults.
739 //
740 // NOTE: This function requires to the wallet to be unlocked.  See the
741 // WalletPassphrase function for more details.
742 func (c *Client) SendManyComment(fromAccount string,
743         amounts map[btcutil.Address]btcutil.Amount, minConfirms int,
744         comment string) (*chainhash.Hash, error) {
745
746         return c.SendManyCommentAsync(fromAccount, amounts, minConfirms,
747                 comment).Receive()
748 }
749
750 // *************************
751 // Address/Account Functions
752 // *************************
753
754 // FutureAddMultisigAddressResult is a future promise to deliver the result of a
755 // AddMultisigAddressAsync RPC invocation (or an applicable error).
756 type FutureAddMultisigAddressResult chan *response
757
758 // Receive waits for the response promised by the future and returns the
759 // multisignature address that requires the specified number of signatures for
760 // the provided addresses.
761 func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) {
762         res, err := receiveFuture(r)
763         if err != nil {
764                 return nil, err
765         }
766
767         // Unmarshal result as a string.
768         var addr string
769         err = json.Unmarshal(res, &addr)
770         if err != nil {
771                 return nil, err
772         }
773
774         return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
775 }
776
777 // AddMultisigAddressAsync returns an instance of a type that can be used to get
778 // the result of the RPC at some future time by invoking the Receive function on
779 // the returned instance.
780 //
781 // See AddMultisigAddress for the blocking version and more details.
782 func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []btcutil.Address, account string) FutureAddMultisigAddressResult {
783         addrs := make([]string, 0, len(addresses))
784         for _, addr := range addresses {
785                 addrs = append(addrs, addr.String())
786         }
787
788         cmd := btcjson.NewAddMultisigAddressCmd(requiredSigs, addrs, &account)
789         return c.sendCmd(cmd)
790 }
791
792 // AddMultisigAddress adds a multisignature address that requires the specified
793 // number of signatures for the provided addresses to the wallet.
794 func (c *Client) AddMultisigAddress(requiredSigs int, addresses []btcutil.Address, account string) (btcutil.Address, error) {
795         return c.AddMultisigAddressAsync(requiredSigs, addresses,
796                 account).Receive()
797 }
798
799 // FutureCreateMultisigResult is a future promise to deliver the result of a
800 // CreateMultisigAsync RPC invocation (or an applicable error).
801 type FutureCreateMultisigResult chan *response
802
803 // Receive waits for the response promised by the future and returns the
804 // multisignature address and script needed to redeem it.
805 func (r FutureCreateMultisigResult) Receive() (*btcjson.CreateMultiSigResult, error) {
806         res, err := receiveFuture(r)
807         if err != nil {
808                 return nil, err
809         }
810
811         // Unmarshal result as a createmultisig result object.
812         var multisigRes btcjson.CreateMultiSigResult
813         err = json.Unmarshal(res, &multisigRes)
814         if err != nil {
815                 return nil, err
816         }
817
818         return &multisigRes, nil
819 }
820
821 // CreateMultisigAsync returns an instance of a type that can be used to get
822 // the result of the RPC at some future time by invoking the Receive function on
823 // the returned instance.
824 //
825 // See CreateMultisig for the blocking version and more details.
826 func (c *Client) CreateMultisigAsync(requiredSigs int, addresses []btcutil.Address) FutureCreateMultisigResult {
827         addrs := make([]string, 0, len(addresses))
828         for _, addr := range addresses {
829                 addrs = append(addrs, addr.String())
830         }
831
832         cmd := btcjson.NewCreateMultisigCmd(requiredSigs, addrs)
833         return c.sendCmd(cmd)
834 }
835
836 // CreateMultisig creates a multisignature address that requires the specified
837 // number of signatures for the provided addresses and returns the
838 // multisignature address and script needed to redeem it.
839 func (c *Client) CreateMultisig(requiredSigs int, addresses []btcutil.Address) (*btcjson.CreateMultiSigResult, error) {
840         return c.CreateMultisigAsync(requiredSigs, addresses).Receive()
841 }
842
843 // FutureCreateNewAccountResult is a future promise to deliver the result of a
844 // CreateNewAccountAsync RPC invocation (or an applicable error).
845 type FutureCreateNewAccountResult chan *response
846
847 // Receive waits for the response promised by the future and returns the
848 // result of creating new account.
849 func (r FutureCreateNewAccountResult) Receive() error {
850         _, err := receiveFuture(r)
851         return err
852 }
853
854 // CreateNewAccountAsync returns an instance of a type that can be used to get the
855 // result of the RPC at some future time by invoking the Receive function on the
856 // returned instance.
857 //
858 // See CreateNewAccount for the blocking version and more details.
859 func (c *Client) CreateNewAccountAsync(account string) FutureCreateNewAccountResult {
860         cmd := btcjson.NewCreateNewAccountCmd(account)
861         return c.sendCmd(cmd)
862 }
863
864 // CreateNewAccount creates a new wallet account.
865 func (c *Client) CreateNewAccount(account string) error {
866         return c.CreateNewAccountAsync(account).Receive()
867 }
868
869 // FutureGetNewAddressResult is a future promise to deliver the result of a
870 // GetNewAddressAsync RPC invocation (or an applicable error).
871 type FutureGetNewAddressResult chan *response
872
873 // Receive waits for the response promised by the future and returns a new
874 // address.
875 func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) {
876         res, err := receiveFuture(r)
877         if err != nil {
878                 return nil, err
879         }
880
881         // Unmarshal result as a string.
882         var addr string
883         err = json.Unmarshal(res, &addr)
884         if err != nil {
885                 return nil, err
886         }
887
888         return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
889 }
890
891 // GetNewAddressAsync returns an instance of a type that can be used to get the
892 // result of the RPC at some future time by invoking the Receive function on the
893 // returned instance.
894 //
895 // See GetNewAddress for the blocking version and more details.
896 func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult {
897         cmd := btcjson.NewGetNewAddressCmd(&account)
898         return c.sendCmd(cmd)
899 }
900
901 // GetNewAddress returns a new address.
902 func (c *Client) GetNewAddress(account string) (btcutil.Address, error) {
903         return c.GetNewAddressAsync(account).Receive()
904 }
905
906 // FutureGetRawChangeAddressResult is a future promise to deliver the result of
907 // a GetRawChangeAddressAsync RPC invocation (or an applicable error).
908 type FutureGetRawChangeAddressResult chan *response
909
910 // Receive waits for the response promised by the future and returns a new
911 // address for receiving change that will be associated with the provided
912 // account.  Note that this is only for raw transactions and NOT for normal use.
913 func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) {
914         res, err := receiveFuture(r)
915         if err != nil {
916                 return nil, err
917         }
918
919         // Unmarshal result as a string.
920         var addr string
921         err = json.Unmarshal(res, &addr)
922         if err != nil {
923                 return nil, err
924         }
925
926         return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
927 }
928
929 // GetRawChangeAddressAsync returns an instance of a type that can be used to
930 // get the result of the RPC at some future time by invoking the Receive
931 // function on the returned instance.
932 //
933 // See GetRawChangeAddress for the blocking version and more details.
934 func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddressResult {
935         cmd := btcjson.NewGetRawChangeAddressCmd(&account)
936         return c.sendCmd(cmd)
937 }
938
939 // GetRawChangeAddress returns a new address for receiving change that will be
940 // associated with the provided account.  Note that this is only for raw
941 // transactions and NOT for normal use.
942 func (c *Client) GetRawChangeAddress(account string) (btcutil.Address, error) {
943         return c.GetRawChangeAddressAsync(account).Receive()
944 }
945
946 // FutureGetAccountAddressResult is a future promise to deliver the result of a
947 // GetAccountAddressAsync RPC invocation (or an applicable error).
948 type FutureGetAccountAddressResult chan *response
949
950 // Receive waits for the response promised by the future and returns the current
951 // Bitcoin address for receiving payments to the specified account.
952 func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) {
953         res, err := receiveFuture(r)
954         if err != nil {
955                 return nil, err
956         }
957
958         // Unmarshal result as a string.
959         var addr string
960         err = json.Unmarshal(res, &addr)
961         if err != nil {
962                 return nil, err
963         }
964
965         return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
966 }
967
968 // GetAccountAddressAsync returns an instance of a type that can be used to get
969 // the result of the RPC at some future time by invoking the Receive function on
970 // the returned instance.
971 //
972 // See GetAccountAddress for the blocking version and more details.
973 func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressResult {
974         cmd := btcjson.NewGetAccountAddressCmd(account)
975         return c.sendCmd(cmd)
976 }
977
978 // GetAccountAddress returns the current Bitcoin address for receiving payments
979 // to the specified account.
980 func (c *Client) GetAccountAddress(account string) (btcutil.Address, error) {
981         return c.GetAccountAddressAsync(account).Receive()
982 }
983
984 // FutureGetAccountResult is a future promise to deliver the result of a
985 // GetAccountAsync RPC invocation (or an applicable error).
986 type FutureGetAccountResult chan *response
987
988 // Receive waits for the response promised by the future and returns the account
989 // associated with the passed address.
990 func (r FutureGetAccountResult) Receive() (string, error) {
991         res, err := receiveFuture(r)
992         if err != nil {
993                 return "", err
994         }
995
996         // Unmarshal result as a string.
997         var account string
998         err = json.Unmarshal(res, &account)
999         if err != nil {
1000                 return "", err
1001         }
1002
1003         return account, nil
1004 }
1005
1006 // GetAccountAsync returns an instance of a type that can be used to get the
1007 // result of the RPC at some future time by invoking the Receive function on the
1008 // returned instance.
1009 //
1010 // See GetAccount for the blocking version and more details.
1011 func (c *Client) GetAccountAsync(address btcutil.Address) FutureGetAccountResult {
1012         addr := address.EncodeAddress()
1013         cmd := btcjson.NewGetAccountCmd(addr)
1014         return c.sendCmd(cmd)
1015 }
1016
1017 // GetAccount returns the account associated with the passed address.
1018 func (c *Client) GetAccount(address btcutil.Address) (string, error) {
1019         return c.GetAccountAsync(address).Receive()
1020 }
1021
1022 // FutureSetAccountResult is a future promise to deliver the result of a
1023 // SetAccountAsync RPC invocation (or an applicable error).
1024 type FutureSetAccountResult chan *response
1025
1026 // Receive waits for the response promised by the future and returns the result
1027 // of setting the account to be associated with the passed address.
1028 func (r FutureSetAccountResult) Receive() error {
1029         _, err := receiveFuture(r)
1030         return err
1031 }
1032
1033 // SetAccountAsync returns an instance of a type that can be used to get the
1034 // result of the RPC at some future time by invoking the Receive function on the
1035 // returned instance.
1036 //
1037 // See SetAccount for the blocking version and more details.
1038 func (c *Client) SetAccountAsync(address btcutil.Address, account string) FutureSetAccountResult {
1039         addr := address.EncodeAddress()
1040         cmd := btcjson.NewSetAccountCmd(addr, account)
1041         return c.sendCmd(cmd)
1042 }
1043
1044 // SetAccount sets the account associated with the passed address.
1045 func (c *Client) SetAccount(address btcutil.Address, account string) error {
1046         return c.SetAccountAsync(address, account).Receive()
1047 }
1048
1049 // FutureGetAddressesByAccountResult is a future promise to deliver the result
1050 // of a GetAddressesByAccountAsync RPC invocation (or an applicable error).
1051 type FutureGetAddressesByAccountResult chan *response
1052
1053 // Receive waits for the response promised by the future and returns the list of
1054 // addresses associated with the passed account.
1055 func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error) {
1056         res, err := receiveFuture(r)
1057         if err != nil {
1058                 return nil, err
1059         }
1060
1061         // Unmashal result as an array of string.
1062         var addrStrings []string
1063         err = json.Unmarshal(res, &addrStrings)
1064         if err != nil {
1065                 return nil, err
1066         }
1067
1068         addrs := make([]btcutil.Address, 0, len(addrStrings))
1069         for _, addrStr := range addrStrings {
1070                 addr, err := btcutil.DecodeAddress(addrStr,
1071                         &chaincfg.MainNetParams)
1072                 if err != nil {
1073                         return nil, err
1074                 }
1075                 addrs = append(addrs, addr)
1076         }
1077
1078         return addrs, nil
1079 }
1080
1081 // GetAddressesByAccountAsync returns an instance of a type that can be used to
1082 // get the result of the RPC at some future time by invoking the Receive
1083 // function on the returned instance.
1084 //
1085 // See GetAddressesByAccount for the blocking version and more details.
1086 func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesByAccountResult {
1087         cmd := btcjson.NewGetAddressesByAccountCmd(account)
1088         return c.sendCmd(cmd)
1089 }
1090
1091 // GetAddressesByAccount returns the list of addresses associated with the
1092 // passed account.
1093 func (c *Client) GetAddressesByAccount(account string) ([]btcutil.Address, error) {
1094         return c.GetAddressesByAccountAsync(account).Receive()
1095 }
1096
1097 // FutureMoveResult is a future promise to deliver the result of a MoveAsync,
1098 // MoveMinConfAsync, or MoveCommentAsync RPC invocation (or an applicable
1099 // error).
1100 type FutureMoveResult chan *response
1101
1102 // Receive waits for the response promised by the future and returns the result
1103 // of the move operation.
1104 func (r FutureMoveResult) Receive() (bool, error) {
1105         res, err := receiveFuture(r)
1106         if err != nil {
1107                 return false, err
1108         }
1109
1110         // Unmarshal result as a boolean.
1111         var moveResult bool
1112         err = json.Unmarshal(res, &moveResult)
1113         if err != nil {
1114                 return false, err
1115         }
1116
1117         return moveResult, nil
1118 }
1119
1120 // MoveAsync returns an instance of a type that can be used to get the result of
1121 // the RPC at some future time by invoking the Receive function on the returned
1122 // instance.
1123 //
1124 // See Move for the blocking version and more details.
1125 func (c *Client) MoveAsync(fromAccount, toAccount string, amount btcutil.Amount) FutureMoveResult {
1126         cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(), nil,
1127                 nil)
1128         return c.sendCmd(cmd)
1129 }
1130
1131 // Move moves specified amount from one account in your wallet to another.  Only
1132 // funds with the default number of minimum confirmations will be used.
1133 //
1134 // See MoveMinConf and MoveComment for different options.
1135 func (c *Client) Move(fromAccount, toAccount string, amount btcutil.Amount) (bool, error) {
1136         return c.MoveAsync(fromAccount, toAccount, amount).Receive()
1137 }
1138
1139 // MoveMinConfAsync returns an instance of a type that can be used to get the
1140 // result of the RPC at some future time by invoking the Receive function on the
1141 // returned instance.
1142 //
1143 // See MoveMinConf for the blocking version and more details.
1144 func (c *Client) MoveMinConfAsync(fromAccount, toAccount string,
1145         amount btcutil.Amount, minConfirms int) FutureMoveResult {
1146
1147         cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(),
1148                 &minConfirms, nil)
1149         return c.sendCmd(cmd)
1150 }
1151
1152 // MoveMinConf moves specified amount from one account in your wallet to
1153 // another.  Only funds with the passed number of minimum confirmations will be
1154 // used.
1155 //
1156 // See Move to use the default number of minimum confirmations and MoveComment
1157 // for additional options.
1158 func (c *Client) MoveMinConf(fromAccount, toAccount string, amount btcutil.Amount, minConf int) (bool, error) {
1159         return c.MoveMinConfAsync(fromAccount, toAccount, amount, minConf).Receive()
1160 }
1161
1162 // MoveCommentAsync returns an instance of a type that can be used to get the
1163 // result of the RPC at some future time by invoking the Receive function on the
1164 // returned instance.
1165 //
1166 // See MoveComment for the blocking version and more details.
1167 func (c *Client) MoveCommentAsync(fromAccount, toAccount string,
1168         amount btcutil.Amount, minConfirms int, comment string) FutureMoveResult {
1169
1170         cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(),
1171                 &minConfirms, &comment)
1172         return c.sendCmd(cmd)
1173 }
1174
1175 // MoveComment moves specified amount from one account in your wallet to
1176 // another and stores the provided comment in the wallet.  The comment
1177 // parameter is only available in the wallet.  Only funds with the passed number
1178 // of minimum confirmations will be used.
1179 //
1180 // See Move and MoveMinConf to use defaults.
1181 func (c *Client) MoveComment(fromAccount, toAccount string, amount btcutil.Amount,
1182         minConf int, comment string) (bool, error) {
1183
1184         return c.MoveCommentAsync(fromAccount, toAccount, amount, minConf,
1185                 comment).Receive()
1186 }
1187
1188 // FutureRenameAccountResult is a future promise to deliver the result of a
1189 // RenameAccountAsync RPC invocation (or an applicable error).
1190 type FutureRenameAccountResult chan *response
1191
1192 // Receive waits for the response promised by the future and returns the
1193 // result of creating new account.
1194 func (r FutureRenameAccountResult) Receive() error {
1195         _, err := receiveFuture(r)
1196         return err
1197 }
1198
1199 // RenameAccountAsync returns an instance of a type that can be used to get the
1200 // result of the RPC at some future time by invoking the Receive function on the
1201 // returned instance.
1202 //
1203 // See RenameAccount for the blocking version and more details.
1204 func (c *Client) RenameAccountAsync(oldAccount, newAccount string) FutureRenameAccountResult {
1205         cmd := btcjson.NewRenameAccountCmd(oldAccount, newAccount)
1206         return c.sendCmd(cmd)
1207 }
1208
1209 // RenameAccount creates a new wallet account.
1210 func (c *Client) RenameAccount(oldAccount, newAccount string) error {
1211         return c.RenameAccountAsync(oldAccount, newAccount).Receive()
1212 }
1213
1214 // FutureValidateAddressResult is a future promise to deliver the result of a
1215 // ValidateAddressAsync RPC invocation (or an applicable error).
1216 type FutureValidateAddressResult chan *response
1217
1218 // Receive waits for the response promised by the future and returns information
1219 // about the given bitcoin address.
1220 func (r FutureValidateAddressResult) Receive() (*btcjson.ValidateAddressWalletResult, error) {
1221         res, err := receiveFuture(r)
1222         if err != nil {
1223                 return nil, err
1224         }
1225
1226         // Unmarshal result as a validateaddress result object.
1227         var addrResult btcjson.ValidateAddressWalletResult
1228         err = json.Unmarshal(res, &addrResult)
1229         if err != nil {
1230                 return nil, err
1231         }
1232
1233         return &addrResult, nil
1234 }
1235
1236 // ValidateAddressAsync returns an instance of a type that can be used to get
1237 // the result of the RPC at some future time by invoking the Receive function on
1238 // the returned instance.
1239 //
1240 // See ValidateAddress for the blocking version and more details.
1241 func (c *Client) ValidateAddressAsync(address btcutil.Address) FutureValidateAddressResult {
1242         addr := address.EncodeAddress()
1243         cmd := btcjson.NewValidateAddressCmd(addr)
1244         return c.sendCmd(cmd)
1245 }
1246
1247 // ValidateAddress returns information about the given bitcoin address.
1248 func (c *Client) ValidateAddress(address btcutil.Address) (*btcjson.ValidateAddressWalletResult, error) {
1249         return c.ValidateAddressAsync(address).Receive()
1250 }
1251
1252 // FutureKeyPoolRefillResult is a future promise to deliver the result of a
1253 // KeyPoolRefillAsync RPC invocation (or an applicable error).
1254 type FutureKeyPoolRefillResult chan *response
1255
1256 // Receive waits for the response promised by the future and returns the result
1257 // of refilling the key pool.
1258 func (r FutureKeyPoolRefillResult) Receive() error {
1259         _, err := receiveFuture(r)
1260         return err
1261 }
1262
1263 // KeyPoolRefillAsync returns an instance of a type that can be used to get the
1264 // result of the RPC at some future time by invoking the Receive function on the
1265 // returned instance.
1266 //
1267 // See KeyPoolRefill for the blocking version and more details.
1268 func (c *Client) KeyPoolRefillAsync() FutureKeyPoolRefillResult {
1269         cmd := btcjson.NewKeyPoolRefillCmd(nil)
1270         return c.sendCmd(cmd)
1271 }
1272
1273 // KeyPoolRefill fills the key pool as necessary to reach the default size.
1274 //
1275 // See KeyPoolRefillSize to override the size of the key pool.
1276 func (c *Client) KeyPoolRefill() error {
1277         return c.KeyPoolRefillAsync().Receive()
1278 }
1279
1280 // KeyPoolRefillSizeAsync returns an instance of a type that can be used to get
1281 // the result of the RPC at some future time by invoking the Receive function on
1282 // the returned instance.
1283 //
1284 // See KeyPoolRefillSize for the blocking version and more details.
1285 func (c *Client) KeyPoolRefillSizeAsync(newSize uint) FutureKeyPoolRefillResult {
1286         cmd := btcjson.NewKeyPoolRefillCmd(&newSize)
1287         return c.sendCmd(cmd)
1288 }
1289
1290 // KeyPoolRefillSize fills the key pool as necessary to reach the specified
1291 // size.
1292 func (c *Client) KeyPoolRefillSize(newSize uint) error {
1293         return c.KeyPoolRefillSizeAsync(newSize).Receive()
1294 }
1295
1296 // ************************
1297 // Amount/Balance Functions
1298 // ************************
1299
1300 // FutureListAccountsResult is a future promise to deliver the result of a
1301 // ListAccountsAsync or ListAccountsMinConfAsync RPC invocation (or an
1302 // applicable error).
1303 type FutureListAccountsResult chan *response
1304
1305 // Receive waits for the response promised by the future and returns returns a
1306 // map of account names and their associated balances.
1307 func (r FutureListAccountsResult) Receive() (map[string]btcutil.Amount, error) {
1308         res, err := receiveFuture(r)
1309         if err != nil {
1310                 return nil, err
1311         }
1312
1313         // Unmarshal result as a json object.
1314         var accounts map[string]float64
1315         err = json.Unmarshal(res, &accounts)
1316         if err != nil {
1317                 return nil, err
1318         }
1319
1320         accountsMap := make(map[string]btcutil.Amount)
1321         for k, v := range accounts {
1322                 amount, err := btcutil.NewAmount(v)
1323                 if err != nil {
1324                         return nil, err
1325                 }
1326
1327                 accountsMap[k] = amount
1328         }
1329
1330         return accountsMap, nil
1331 }
1332
1333 // ListAccountsAsync returns an instance of a type that can be used to get the
1334 // result of the RPC at some future time by invoking the Receive function on the
1335 // returned instance.
1336 //
1337 // See ListAccounts for the blocking version and more details.
1338 func (c *Client) ListAccountsAsync() FutureListAccountsResult {
1339         cmd := btcjson.NewListAccountsCmd(nil)
1340         return c.sendCmd(cmd)
1341 }
1342
1343 // ListAccounts returns a map of account names and their associated balances
1344 // using the default number of minimum confirmations.
1345 //
1346 // See ListAccountsMinConf to override the minimum number of confirmations.
1347 func (c *Client) ListAccounts() (map[string]btcutil.Amount, error) {
1348         return c.ListAccountsAsync().Receive()
1349 }
1350
1351 // ListAccountsMinConfAsync returns an instance of a type that can be used to
1352 // get the result of the RPC at some future time by invoking the Receive
1353 // function on the returned instance.
1354 //
1355 // See ListAccountsMinConf for the blocking version and more details.
1356 func (c *Client) ListAccountsMinConfAsync(minConfirms int) FutureListAccountsResult {
1357         cmd := btcjson.NewListAccountsCmd(&minConfirms)
1358         return c.sendCmd(cmd)
1359 }
1360
1361 // ListAccountsMinConf returns a map of account names and their associated
1362 // balances using the specified number of minimum confirmations.
1363 //
1364 // See ListAccounts to use the default minimum number of confirmations.
1365 func (c *Client) ListAccountsMinConf(minConfirms int) (map[string]btcutil.Amount, error) {
1366         return c.ListAccountsMinConfAsync(minConfirms).Receive()
1367 }
1368
1369 // FutureGetBalanceResult is a future promise to deliver the result of a
1370 // GetBalanceAsync or GetBalanceMinConfAsync RPC invocation (or an applicable
1371 // error).
1372 type FutureGetBalanceResult chan *response
1373
1374 // Receive waits for the response promised by the future and returns the
1375 // available balance from the server for the specified account.
1376 func (r FutureGetBalanceResult) Receive() (btcutil.Amount, error) {
1377         res, err := receiveFuture(r)
1378         if err != nil {
1379                 return 0, err
1380         }
1381
1382         // Unmarshal result as a floating point number.
1383         var balance float64
1384         err = json.Unmarshal(res, &balance)
1385         if err != nil {
1386                 return 0, err
1387         }
1388
1389         amount, err := btcutil.NewAmount(balance)
1390         if err != nil {
1391                 return 0, err
1392         }
1393
1394         return amount, nil
1395 }
1396
1397 // FutureGetBalanceParseResult is same as FutureGetBalanceResult except
1398 // that the result is expected to be a string which is then parsed into
1399 // a float64 value
1400 // This is required for compatiblity with servers like blockchain.info
1401 type FutureGetBalanceParseResult chan *response
1402
1403 // Receive waits for the response promised by the future and returns the
1404 // available balance from the server for the specified account.
1405 func (r FutureGetBalanceParseResult) Receive() (btcutil.Amount, error) {
1406         res, err := receiveFuture(r)
1407         if err != nil {
1408                 return 0, err
1409         }
1410
1411         // Unmarshal result as a string
1412         var balanceString string
1413         err = json.Unmarshal(res, &balanceString)
1414         if err != nil {
1415                 return 0, err
1416         }
1417
1418         balance, err := strconv.ParseFloat(balanceString, 64)
1419         if err != nil {
1420                 return 0, err
1421         }
1422         amount, err := btcutil.NewAmount(balance)
1423         if err != nil {
1424                 return 0, err
1425         }
1426
1427         return amount, nil
1428 }
1429
1430 // GetBalanceAsync returns an instance of a type that can be used to get the
1431 // result of the RPC at some future time by invoking the Receive function on the
1432 // returned instance.
1433 //
1434 // See GetBalance for the blocking version and more details.
1435 func (c *Client) GetBalanceAsync(account string) FutureGetBalanceResult {
1436         cmd := btcjson.NewGetBalanceCmd(&account, nil)
1437         return c.sendCmd(cmd)
1438 }
1439
1440 // GetBalance returns the available balance from the server for the specified
1441 // account using the default number of minimum confirmations.  The account may
1442 // be "*" for all accounts.
1443 //
1444 // See GetBalanceMinConf to override the minimum number of confirmations.
1445 func (c *Client) GetBalance(account string) (btcutil.Amount, error) {
1446         return c.GetBalanceAsync(account).Receive()
1447 }
1448
1449 // GetBalanceMinConfAsync returns an instance of a type that can be used to get
1450 // the result of the RPC at some future time by invoking the Receive function on
1451 // the returned instance.
1452 //
1453 // See GetBalanceMinConf for the blocking version and more details.
1454 func (c *Client) GetBalanceMinConfAsync(account string, minConfirms int) FutureGetBalanceResult {
1455         cmd := btcjson.NewGetBalanceCmd(&account, &minConfirms)
1456         return c.sendCmd(cmd)
1457 }
1458
1459 // GetBalanceMinConf returns the available balance from the server for the
1460 // specified account using the specified number of minimum confirmations.  The
1461 // account may be "*" for all accounts.
1462 //
1463 // See GetBalance to use the default minimum number of confirmations.
1464 func (c *Client) GetBalanceMinConf(account string, minConfirms int) (btcutil.Amount, error) {
1465         if c.config.EnableBCInfoHacks {
1466                 response := c.GetBalanceMinConfAsync(account, minConfirms)
1467                 return FutureGetBalanceParseResult(response).Receive()
1468         }
1469         return c.GetBalanceMinConfAsync(account, minConfirms).Receive()
1470 }
1471
1472 // FutureGetReceivedByAccountResult is a future promise to deliver the result of
1473 // a GetReceivedByAccountAsync or GetReceivedByAccountMinConfAsync RPC
1474 // invocation (or an applicable error).
1475 type FutureGetReceivedByAccountResult chan *response
1476
1477 // Receive waits for the response promised by the future and returns the total
1478 // amount received with the specified account.
1479 func (r FutureGetReceivedByAccountResult) Receive() (btcutil.Amount, error) {
1480         res, err := receiveFuture(r)
1481         if err != nil {
1482                 return 0, err
1483         }
1484
1485         // Unmarshal result as a floating point number.
1486         var balance float64
1487         err = json.Unmarshal(res, &balance)
1488         if err != nil {
1489                 return 0, err
1490         }
1491
1492         amount, err := btcutil.NewAmount(balance)
1493         if err != nil {
1494                 return 0, err
1495         }
1496
1497         return amount, nil
1498 }
1499
1500 // GetReceivedByAccountAsync returns an instance of a type that can be used to
1501 // get the result of the RPC at some future time by invoking the Receive
1502 // function on the returned instance.
1503 //
1504 // See GetReceivedByAccount for the blocking version and more details.
1505 func (c *Client) GetReceivedByAccountAsync(account string) FutureGetReceivedByAccountResult {
1506         cmd := btcjson.NewGetReceivedByAccountCmd(account, nil)
1507         return c.sendCmd(cmd)
1508 }
1509
1510 // GetReceivedByAccount returns the total amount received with the specified
1511 // account with at least the default number of minimum confirmations.
1512 //
1513 // See GetReceivedByAccountMinConf to override the minimum number of
1514 // confirmations.
1515 func (c *Client) GetReceivedByAccount(account string) (btcutil.Amount, error) {
1516         return c.GetReceivedByAccountAsync(account).Receive()
1517 }
1518
1519 // GetReceivedByAccountMinConfAsync returns an instance of a type that can be
1520 // used to get the result of the RPC at some future time by invoking the Receive
1521 // function on the returned instance.
1522 //
1523 // See GetReceivedByAccountMinConf for the blocking version and more details.
1524 func (c *Client) GetReceivedByAccountMinConfAsync(account string, minConfirms int) FutureGetReceivedByAccountResult {
1525         cmd := btcjson.NewGetReceivedByAccountCmd(account, &minConfirms)
1526         return c.sendCmd(cmd)
1527 }
1528
1529 // GetReceivedByAccountMinConf returns the total amount received with the
1530 // specified account with at least the specified number of minimum
1531 // confirmations.
1532 //
1533 // See GetReceivedByAccount to use the default minimum number of confirmations.
1534 func (c *Client) GetReceivedByAccountMinConf(account string, minConfirms int) (btcutil.Amount, error) {
1535         return c.GetReceivedByAccountMinConfAsync(account, minConfirms).Receive()
1536 }
1537
1538 // FutureGetUnconfirmedBalanceResult is a future promise to deliver the result
1539 // of a GetUnconfirmedBalanceAsync RPC invocation (or an applicable error).
1540 type FutureGetUnconfirmedBalanceResult chan *response
1541
1542 // Receive waits for the response promised by the future and returns returns the
1543 // unconfirmed balance from the server for the specified account.
1544 func (r FutureGetUnconfirmedBalanceResult) Receive() (btcutil.Amount, error) {
1545         res, err := receiveFuture(r)
1546         if err != nil {
1547                 return 0, err
1548         }
1549
1550         // Unmarshal result as a floating point number.
1551         var balance float64
1552         err = json.Unmarshal(res, &balance)
1553         if err != nil {
1554                 return 0, err
1555         }
1556
1557         amount, err := btcutil.NewAmount(balance)
1558         if err != nil {
1559                 return 0, err
1560         }
1561
1562         return amount, nil
1563 }
1564
1565 // GetUnconfirmedBalanceAsync returns an instance of a type that can be used to
1566 // get the result of the RPC at some future time by invoking the Receive
1567 // function on the returned instance.
1568 //
1569 // See GetUnconfirmedBalance for the blocking version and more details.
1570 func (c *Client) GetUnconfirmedBalanceAsync(account string) FutureGetUnconfirmedBalanceResult {
1571         cmd := btcjson.NewGetUnconfirmedBalanceCmd(&account)
1572         return c.sendCmd(cmd)
1573 }
1574
1575 // GetUnconfirmedBalance returns the unconfirmed balance from the server for
1576 // the specified account.
1577 func (c *Client) GetUnconfirmedBalance(account string) (btcutil.Amount, error) {
1578         return c.GetUnconfirmedBalanceAsync(account).Receive()
1579 }
1580
1581 // FutureGetReceivedByAddressResult is a future promise to deliver the result of
1582 // a GetReceivedByAddressAsync or GetReceivedByAddressMinConfAsync RPC
1583 // invocation (or an applicable error).
1584 type FutureGetReceivedByAddressResult chan *response
1585
1586 // Receive waits for the response promised by the future and returns the total
1587 // amount received by the specified address.
1588 func (r FutureGetReceivedByAddressResult) Receive() (btcutil.Amount, error) {
1589         res, err := receiveFuture(r)
1590         if err != nil {
1591                 return 0, err
1592         }
1593
1594         // Unmarshal result as a floating point number.
1595         var balance float64
1596         err = json.Unmarshal(res, &balance)
1597         if err != nil {
1598                 return 0, err
1599         }
1600
1601         amount, err := btcutil.NewAmount(balance)
1602         if err != nil {
1603                 return 0, err
1604         }
1605
1606         return amount, nil
1607 }
1608
1609 // GetReceivedByAddressAsync returns an instance of a type that can be used to
1610 // get the result of the RPC at some future time by invoking the Receive
1611 // function on the returned instance.
1612 //
1613 // See GetReceivedByAddress for the blocking version and more details.
1614 func (c *Client) GetReceivedByAddressAsync(address btcutil.Address) FutureGetReceivedByAddressResult {
1615         addr := address.EncodeAddress()
1616         cmd := btcjson.NewGetReceivedByAddressCmd(addr, nil)
1617         return c.sendCmd(cmd)
1618
1619 }
1620
1621 // GetReceivedByAddress returns the total amount received by the specified
1622 // address with at least the default number of minimum confirmations.
1623 //
1624 // See GetReceivedByAddressMinConf to override the minimum number of
1625 // confirmations.
1626 func (c *Client) GetReceivedByAddress(address btcutil.Address) (btcutil.Amount, error) {
1627         return c.GetReceivedByAddressAsync(address).Receive()
1628 }
1629
1630 // GetReceivedByAddressMinConfAsync returns an instance of a type that can be
1631 // used to get the result of the RPC at some future time by invoking the Receive
1632 // function on the returned instance.
1633 //
1634 // See GetReceivedByAddressMinConf for the blocking version and more details.
1635 func (c *Client) GetReceivedByAddressMinConfAsync(address btcutil.Address, minConfirms int) FutureGetReceivedByAddressResult {
1636         addr := address.EncodeAddress()
1637         cmd := btcjson.NewGetReceivedByAddressCmd(addr, &minConfirms)
1638         return c.sendCmd(cmd)
1639 }
1640
1641 // GetReceivedByAddressMinConf returns the total amount received by the specified
1642 // address with at least the specified number of minimum confirmations.
1643 //
1644 // See GetReceivedByAddress to use the default minimum number of confirmations.
1645 func (c *Client) GetReceivedByAddressMinConf(address btcutil.Address, minConfirms int) (btcutil.Amount, error) {
1646         return c.GetReceivedByAddressMinConfAsync(address, minConfirms).Receive()
1647 }
1648
1649 // FutureListReceivedByAccountResult is a future promise to deliver the result
1650 // of a ListReceivedByAccountAsync, ListReceivedByAccountMinConfAsync, or
1651 // ListReceivedByAccountIncludeEmptyAsync RPC invocation (or an applicable
1652 // error).
1653 type FutureListReceivedByAccountResult chan *response
1654
1655 // Receive waits for the response promised by the future and returns a list of
1656 // balances by account.
1657 func (r FutureListReceivedByAccountResult) Receive() ([]btcjson.ListReceivedByAccountResult, error) {
1658         res, err := receiveFuture(r)
1659         if err != nil {
1660                 return nil, err
1661         }
1662
1663         // Unmarshal as an array of listreceivedbyaccount result objects.
1664         var received []btcjson.ListReceivedByAccountResult
1665         err = json.Unmarshal(res, &received)
1666         if err != nil {
1667                 return nil, err
1668         }
1669
1670         return received, nil
1671 }
1672
1673 // ListReceivedByAccountAsync returns an instance of a type that can be used to
1674 // get the result of the RPC at some future time by invoking the Receive
1675 // function on the returned instance.
1676 //
1677 // See ListReceivedByAccount for the blocking version and more details.
1678 func (c *Client) ListReceivedByAccountAsync() FutureListReceivedByAccountResult {
1679         cmd := btcjson.NewListReceivedByAccountCmd(nil, nil, nil)
1680         return c.sendCmd(cmd)
1681 }
1682
1683 // ListReceivedByAccount lists balances by account using the default number
1684 // of minimum confirmations and including accounts that haven't received any
1685 // payments.
1686 //
1687 // See ListReceivedByAccountMinConf to override the minimum number of
1688 // confirmations and ListReceivedByAccountIncludeEmpty to filter accounts that
1689 // haven't received any payments from the results.
1690 func (c *Client) ListReceivedByAccount() ([]btcjson.ListReceivedByAccountResult, error) {
1691         return c.ListReceivedByAccountAsync().Receive()
1692 }
1693
1694 // ListReceivedByAccountMinConfAsync returns an instance of a type that can be
1695 // used to get the result of the RPC at some future time by invoking the Receive
1696 // function on the returned instance.
1697 //
1698 // See ListReceivedByAccountMinConf for the blocking version and more details.
1699 func (c *Client) ListReceivedByAccountMinConfAsync(minConfirms int) FutureListReceivedByAccountResult {
1700         cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, nil, nil)
1701         return c.sendCmd(cmd)
1702 }
1703
1704 // ListReceivedByAccountMinConf lists balances by account using the specified
1705 // number of minimum confirmations not including accounts that haven't received
1706 // any payments.
1707 //
1708 // See ListReceivedByAccount to use the default minimum number of confirmations
1709 // and ListReceivedByAccountIncludeEmpty to also include accounts that haven't
1710 // received any payments in the results.
1711 func (c *Client) ListReceivedByAccountMinConf(minConfirms int) ([]btcjson.ListReceivedByAccountResult, error) {
1712         return c.ListReceivedByAccountMinConfAsync(minConfirms).Receive()
1713 }
1714
1715 // ListReceivedByAccountIncludeEmptyAsync returns an instance of a type that can
1716 // be used to get the result of the RPC at some future time by invoking the
1717 // Receive function on the returned instance.
1718 //
1719 // See ListReceivedByAccountIncludeEmpty for the blocking version and more details.
1720 func (c *Client) ListReceivedByAccountIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAccountResult {
1721         cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, &includeEmpty,
1722                 nil)
1723         return c.sendCmd(cmd)
1724 }
1725
1726 // ListReceivedByAccountIncludeEmpty lists balances by account using the
1727 // specified number of minimum confirmations and including accounts that
1728 // haven't received any payments depending on specified flag.
1729 //
1730 // See ListReceivedByAccount and ListReceivedByAccountMinConf to use defaults.
1731 func (c *Client) ListReceivedByAccountIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAccountResult, error) {
1732         return c.ListReceivedByAccountIncludeEmptyAsync(minConfirms,
1733                 includeEmpty).Receive()
1734 }
1735
1736 // FutureListReceivedByAddressResult is a future promise to deliver the result
1737 // of a ListReceivedByAddressAsync, ListReceivedByAddressMinConfAsync, or
1738 // ListReceivedByAddressIncludeEmptyAsync RPC invocation (or an applicable
1739 // error).
1740 type FutureListReceivedByAddressResult chan *response
1741
1742 // Receive waits for the response promised by the future and returns a list of
1743 // balances by address.
1744 func (r FutureListReceivedByAddressResult) Receive() ([]btcjson.ListReceivedByAddressResult, error) {
1745         res, err := receiveFuture(r)
1746         if err != nil {
1747                 return nil, err
1748         }
1749
1750         // Unmarshal as an array of listreceivedbyaddress result objects.
1751         var received []btcjson.ListReceivedByAddressResult
1752         err = json.Unmarshal(res, &received)
1753         if err != nil {
1754                 return nil, err
1755         }
1756
1757         return received, nil
1758 }
1759
1760 // ListReceivedByAddressAsync returns an instance of a type that can be used to
1761 // get the result of the RPC at some future time by invoking the Receive
1762 // function on the returned instance.
1763 //
1764 // See ListReceivedByAddress for the blocking version and more details.
1765 func (c *Client) ListReceivedByAddressAsync() FutureListReceivedByAddressResult {
1766         cmd := btcjson.NewListReceivedByAddressCmd(nil, nil, nil)
1767         return c.sendCmd(cmd)
1768 }
1769
1770 // ListReceivedByAddress lists balances by address using the default number
1771 // of minimum confirmations not including addresses that haven't received any
1772 // payments or watching only addresses.
1773 //
1774 // See ListReceivedByAddressMinConf to override the minimum number of
1775 // confirmations and ListReceivedByAddressIncludeEmpty to also include addresses
1776 // that haven't received any payments in the results.
1777 func (c *Client) ListReceivedByAddress() ([]btcjson.ListReceivedByAddressResult, error) {
1778         return c.ListReceivedByAddressAsync().Receive()
1779 }
1780
1781 // ListReceivedByAddressMinConfAsync returns an instance of a type that can be
1782 // used to get the result of the RPC at some future time by invoking the Receive
1783 // function on the returned instance.
1784 //
1785 // See ListReceivedByAddressMinConf for the blocking version and more details.
1786 func (c *Client) ListReceivedByAddressMinConfAsync(minConfirms int) FutureListReceivedByAddressResult {
1787         cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, nil, nil)
1788         return c.sendCmd(cmd)
1789 }
1790
1791 // ListReceivedByAddressMinConf lists balances by address using the specified
1792 // number of minimum confirmations not including addresses that haven't received
1793 // any payments.
1794 //
1795 // See ListReceivedByAddress to use the default minimum number of confirmations
1796 // and ListReceivedByAddressIncludeEmpty to also include addresses that haven't
1797 // received any payments in the results.
1798 func (c *Client) ListReceivedByAddressMinConf(minConfirms int) ([]btcjson.ListReceivedByAddressResult, error) {
1799         return c.ListReceivedByAddressMinConfAsync(minConfirms).Receive()
1800 }
1801
1802 // ListReceivedByAddressIncludeEmptyAsync returns an instance of a type that can
1803 // be used to get the result of the RPC at some future time by invoking the
1804 // Receive function on the returned instance.
1805 //
1806 // See ListReceivedByAccountIncludeEmpty for the blocking version and more details.
1807 func (c *Client) ListReceivedByAddressIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAddressResult {
1808         cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, &includeEmpty,
1809                 nil)
1810         return c.sendCmd(cmd)
1811 }
1812
1813 // ListReceivedByAddressIncludeEmpty lists balances by address using the
1814 // specified number of minimum confirmations and including addresses that
1815 // haven't received any payments depending on specified flag.
1816 //
1817 // See ListReceivedByAddress and ListReceivedByAddressMinConf to use defaults.
1818 func (c *Client) ListReceivedByAddressIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAddressResult, error) {
1819         return c.ListReceivedByAddressIncludeEmptyAsync(minConfirms,
1820                 includeEmpty).Receive()
1821 }
1822
1823 // ************************
1824 // Wallet Locking Functions
1825 // ************************
1826
1827 // FutureWalletLockResult is a future promise to deliver the result of a
1828 // WalletLockAsync RPC invocation (or an applicable error).
1829 type FutureWalletLockResult chan *response
1830
1831 // Receive waits for the response promised by the future and returns the result
1832 // of locking the wallet.
1833 func (r FutureWalletLockResult) Receive() error {
1834         _, err := receiveFuture(r)
1835         return err
1836 }
1837
1838 // WalletLockAsync returns an instance of a type that can be used to get the
1839 // result of the RPC at some future time by invoking the Receive function on the
1840 // returned instance.
1841 //
1842 // See WalletLock for the blocking version and more details.
1843 func (c *Client) WalletLockAsync() FutureWalletLockResult {
1844         cmd := btcjson.NewWalletLockCmd()
1845         return c.sendCmd(cmd)
1846 }
1847
1848 // WalletLock locks the wallet by removing the encryption key from memory.
1849 //
1850 // After calling this function, the WalletPassphrase function must be used to
1851 // unlock the wallet prior to calling any other function which requires the
1852 // wallet to be unlocked.
1853 func (c *Client) WalletLock() error {
1854         return c.WalletLockAsync().Receive()
1855 }
1856
1857 // WalletPassphrase unlocks the wallet by using the passphrase to derive the
1858 // decryption key which is then stored in memory for the specified timeout
1859 // (in seconds).
1860 func (c *Client) WalletPassphrase(passphrase string, timeoutSecs int64) error {
1861         cmd := btcjson.NewWalletPassphraseCmd(passphrase, timeoutSecs)
1862         _, err := c.sendCmdAndWait(cmd)
1863         return err
1864 }
1865
1866 // FutureWalletPassphraseChangeResult is a future promise to deliver the result
1867 // of a WalletPassphraseChangeAsync RPC invocation (or an applicable error).
1868 type FutureWalletPassphraseChangeResult chan *response
1869
1870 // Receive waits for the response promised by the future and returns the result
1871 // of changing the wallet passphrase.
1872 func (r FutureWalletPassphraseChangeResult) Receive() error {
1873         _, err := receiveFuture(r)
1874         return err
1875 }
1876
1877 // WalletPassphraseChangeAsync returns an instance of a type that can be used to
1878 // get the result of the RPC at some future time by invoking the Receive
1879 // function on the returned instance.
1880 //
1881 // See WalletPassphraseChange for the blocking version and more details.
1882 func (c *Client) WalletPassphraseChangeAsync(old, new string) FutureWalletPassphraseChangeResult {
1883         cmd := btcjson.NewWalletPassphraseChangeCmd(old, new)
1884         return c.sendCmd(cmd)
1885 }
1886
1887 // WalletPassphraseChange changes the wallet passphrase from the specified old
1888 // to new passphrase.
1889 func (c *Client) WalletPassphraseChange(old, new string) error {
1890         return c.WalletPassphraseChangeAsync(old, new).Receive()
1891 }
1892
1893 // *************************
1894 // Message Signing Functions
1895 // *************************
1896
1897 // FutureSignMessageResult is a future promise to deliver the result of a
1898 // SignMessageAsync RPC invocation (or an applicable error).
1899 type FutureSignMessageResult chan *response
1900
1901 // Receive waits for the response promised by the future and returns the message
1902 // signed with the private key of the specified address.
1903 func (r FutureSignMessageResult) Receive() (string, error) {
1904         res, err := receiveFuture(r)
1905         if err != nil {
1906                 return "", err
1907         }
1908
1909         // Unmarshal result as a string.
1910         var b64 string
1911         err = json.Unmarshal(res, &b64)
1912         if err != nil {
1913                 return "", err
1914         }
1915
1916         return b64, nil
1917 }
1918
1919 // SignMessageAsync returns an instance of a type that can be used to get the
1920 // result of the RPC at some future time by invoking the Receive function on the
1921 // returned instance.
1922 //
1923 // See SignMessage for the blocking version and more details.
1924 func (c *Client) SignMessageAsync(address btcutil.Address, message string) FutureSignMessageResult {
1925         addr := address.EncodeAddress()
1926         cmd := btcjson.NewSignMessageCmd(addr, message)
1927         return c.sendCmd(cmd)
1928 }
1929
1930 // SignMessage signs a message with the private key of the specified address.
1931 //
1932 // NOTE: This function requires to the wallet to be unlocked.  See the
1933 // WalletPassphrase function for more details.
1934 func (c *Client) SignMessage(address btcutil.Address, message string) (string, error) {
1935         return c.SignMessageAsync(address, message).Receive()
1936 }
1937
1938 // FutureVerifyMessageResult is a future promise to deliver the result of a
1939 // VerifyMessageAsync RPC invocation (or an applicable error).
1940 type FutureVerifyMessageResult chan *response
1941
1942 // Receive waits for the response promised by the future and returns whether or
1943 // not the message was successfully verified.
1944 func (r FutureVerifyMessageResult) Receive() (bool, error) {
1945         res, err := receiveFuture(r)
1946         if err != nil {
1947                 return false, err
1948         }
1949
1950         // Unmarshal result as a boolean.
1951         var verified bool
1952         err = json.Unmarshal(res, &verified)
1953         if err != nil {
1954                 return false, err
1955         }
1956
1957         return verified, nil
1958 }
1959
1960 // VerifyMessageAsync returns an instance of a type that can be used to get the
1961 // result of the RPC at some future time by invoking the Receive function on the
1962 // returned instance.
1963 //
1964 // See VerifyMessage for the blocking version and more details.
1965 func (c *Client) VerifyMessageAsync(address btcutil.Address, signature, message string) FutureVerifyMessageResult {
1966         addr := address.EncodeAddress()
1967         cmd := btcjson.NewVerifyMessageCmd(addr, signature, message)
1968         return c.sendCmd(cmd)
1969 }
1970
1971 // VerifyMessage verifies a signed message.
1972 //
1973 // NOTE: This function requires to the wallet to be unlocked.  See the
1974 // WalletPassphrase function for more details.
1975 func (c *Client) VerifyMessage(address btcutil.Address, signature, message string) (bool, error) {
1976         return c.VerifyMessageAsync(address, signature, message).Receive()
1977 }
1978
1979 // *********************
1980 // Dump/Import Functions
1981 // *********************
1982
1983 // FutureDumpPrivKeyResult is a future promise to deliver the result of a
1984 // DumpPrivKeyAsync RPC invocation (or an applicable error).
1985 type FutureDumpPrivKeyResult chan *response
1986
1987 // Receive waits for the response promised by the future and returns the private
1988 // key corresponding to the passed address encoded in the wallet import format
1989 // (WIF)
1990 func (r FutureDumpPrivKeyResult) Receive() (*btcutil.WIF, error) {
1991         res, err := receiveFuture(r)
1992         if err != nil {
1993                 return nil, err
1994         }
1995
1996         // Unmarshal result as a string.
1997         var privKeyWIF string
1998         err = json.Unmarshal(res, &privKeyWIF)
1999         if err != nil {
2000                 return nil, err
2001         }
2002
2003         return btcutil.DecodeWIF(privKeyWIF)
2004 }
2005
2006 // DumpPrivKeyAsync returns an instance of a type that can be used to get the
2007 // result of the RPC at some future time by invoking the Receive function on the
2008 // returned instance.
2009 //
2010 // See DumpPrivKey for the blocking version and more details.
2011 func (c *Client) DumpPrivKeyAsync(address btcutil.Address) FutureDumpPrivKeyResult {
2012         addr := address.EncodeAddress()
2013         cmd := btcjson.NewDumpPrivKeyCmd(addr)
2014         return c.sendCmd(cmd)
2015 }
2016
2017 // DumpPrivKey gets the private key corresponding to the passed address encoded
2018 // in the wallet import format (WIF).
2019 //
2020 // NOTE: This function requires to the wallet to be unlocked.  See the
2021 // WalletPassphrase function for more details.
2022 func (c *Client) DumpPrivKey(address btcutil.Address) (*btcutil.WIF, error) {
2023         return c.DumpPrivKeyAsync(address).Receive()
2024 }
2025
2026 // FutureImportAddressResult is a future promise to deliver the result of an
2027 // ImportAddressAsync RPC invocation (or an applicable error).
2028 type FutureImportAddressResult chan *response
2029
2030 // Receive waits for the response promised by the future and returns the result
2031 // of importing the passed public address.
2032 func (r FutureImportAddressResult) Receive() error {
2033         _, err := receiveFuture(r)
2034         return err
2035 }
2036
2037 // ImportAddressAsync returns an instance of a type that can be used to get the
2038 // result of the RPC at some future time by invoking the Receive function on the
2039 // returned instance.
2040 //
2041 // See ImportAddress for the blocking version and more details.
2042 func (c *Client) ImportAddressAsync(address string) FutureImportAddressResult {
2043         cmd := btcjson.NewImportAddressCmd(address, nil)
2044         return c.sendCmd(cmd)
2045 }
2046
2047 // ImportAddress imports the passed public address.
2048 func (c *Client) ImportAddress(address string) error {
2049         return c.ImportAddressAsync(address).Receive()
2050 }
2051
2052 // ImportAddressRescanAsync returns an instance of a type that can be used to get the
2053 // result of the RPC at some future time by invoking the Receive function on the
2054 // returned instance.
2055 //
2056 // See ImportAddress for the blocking version and more details.
2057 func (c *Client) ImportAddressRescanAsync(address string, rescan bool) FutureImportAddressResult {
2058         cmd := btcjson.NewImportAddressCmd(address, &rescan)
2059         return c.sendCmd(cmd)
2060 }
2061
2062 // ImportAddressRescan imports the passed public address. When rescan is true,
2063 // the block history is scanned for transactions addressed to provided address.
2064 func (c *Client) ImportAddressRescan(address string, rescan bool) error {
2065         return c.ImportAddressRescanAsync(address, rescan).Receive()
2066 }
2067
2068 // FutureImportPrivKeyResult is a future promise to deliver the result of an
2069 // ImportPrivKeyAsync RPC invocation (or an applicable error).
2070 type FutureImportPrivKeyResult chan *response
2071
2072 // Receive waits for the response promised by the future and returns the result
2073 // of importing the passed private key which must be the wallet import format
2074 // (WIF).
2075 func (r FutureImportPrivKeyResult) Receive() error {
2076         _, err := receiveFuture(r)
2077         return err
2078 }
2079
2080 // ImportPrivKeyAsync returns an instance of a type that can be used to get the
2081 // result of the RPC at some future time by invoking the Receive function on the
2082 // returned instance.
2083 //
2084 // See ImportPrivKey for the blocking version and more details.
2085 func (c *Client) ImportPrivKeyAsync(privKeyWIF *btcutil.WIF) FutureImportPrivKeyResult {
2086         wif := ""
2087         if privKeyWIF != nil {
2088                 wif = privKeyWIF.String()
2089         }
2090
2091         cmd := btcjson.NewImportPrivKeyCmd(wif, nil, nil)
2092         return c.sendCmd(cmd)
2093 }
2094
2095 // ImportPrivKey imports the passed private key which must be the wallet import
2096 // format (WIF).
2097 func (c *Client) ImportPrivKey(privKeyWIF *btcutil.WIF) error {
2098         return c.ImportPrivKeyAsync(privKeyWIF).Receive()
2099 }
2100
2101 // ImportPrivKeyLabelAsync returns an instance of a type that can be used to get the
2102 // result of the RPC at some future time by invoking the Receive function on the
2103 // returned instance.
2104 //
2105 // See ImportPrivKey for the blocking version and more details.
2106 func (c *Client) ImportPrivKeyLabelAsync(privKeyWIF *btcutil.WIF, label string) FutureImportPrivKeyResult {
2107         wif := ""
2108         if privKeyWIF != nil {
2109                 wif = privKeyWIF.String()
2110         }
2111
2112         cmd := btcjson.NewImportPrivKeyCmd(wif, &label, nil)
2113         return c.sendCmd(cmd)
2114 }
2115
2116 // ImportPrivKeyLabel imports the passed private key which must be the wallet import
2117 // format (WIF). It sets the account label to the one provided.
2118 func (c *Client) ImportPrivKeyLabel(privKeyWIF *btcutil.WIF, label string) error {
2119         return c.ImportPrivKeyLabelAsync(privKeyWIF, label).Receive()
2120 }
2121
2122 // ImportPrivKeyRescanAsync returns an instance of a type that can be used to get the
2123 // result of the RPC at some future time by invoking the Receive function on the
2124 // returned instance.
2125 //
2126 // See ImportPrivKey for the blocking version and more details.
2127 func (c *Client) ImportPrivKeyRescanAsync(privKeyWIF *btcutil.WIF, label string, rescan bool) FutureImportPrivKeyResult {
2128         wif := ""
2129         if privKeyWIF != nil {
2130                 wif = privKeyWIF.String()
2131         }
2132
2133         cmd := btcjson.NewImportPrivKeyCmd(wif, &label, &rescan)
2134         return c.sendCmd(cmd)
2135 }
2136
2137 // ImportPrivKeyRescan imports the passed private key which must be the wallet import
2138 // format (WIF). It sets the account label to the one provided. When rescan is true,
2139 // the block history is scanned for transactions addressed to provided privKey.
2140 func (c *Client) ImportPrivKeyRescan(privKeyWIF *btcutil.WIF, label string, rescan bool) error {
2141         return c.ImportPrivKeyRescanAsync(privKeyWIF, label, rescan).Receive()
2142 }
2143
2144 // FutureImportPubKeyResult is a future promise to deliver the result of an
2145 // ImportPubKeyAsync RPC invocation (or an applicable error).
2146 type FutureImportPubKeyResult chan *response
2147
2148 // Receive waits for the response promised by the future and returns the result
2149 // of importing the passed public key.
2150 func (r FutureImportPubKeyResult) Receive() error {
2151         _, err := receiveFuture(r)
2152         return err
2153 }
2154
2155 // ImportPubKeyAsync returns an instance of a type that can be used to get the
2156 // result of the RPC at some future time by invoking the Receive function on the
2157 // returned instance.
2158 //
2159 // See ImportPubKey for the blocking version and more details.
2160 func (c *Client) ImportPubKeyAsync(pubKey string) FutureImportPubKeyResult {
2161         cmd := btcjson.NewImportPubKeyCmd(pubKey, nil)
2162         return c.sendCmd(cmd)
2163 }
2164
2165 // ImportPubKey imports the passed public key.
2166 func (c *Client) ImportPubKey(pubKey string) error {
2167         return c.ImportPubKeyAsync(pubKey).Receive()
2168 }
2169
2170 // ImportPubKeyRescanAsync returns an instance of a type that can be used to get the
2171 // result of the RPC at some future time by invoking the Receive function on the
2172 // returned instance.
2173 //
2174 // See ImportPubKey for the blocking version and more details.
2175 func (c *Client) ImportPubKeyRescanAsync(pubKey string, rescan bool) FutureImportPubKeyResult {
2176         cmd := btcjson.NewImportPubKeyCmd(pubKey, &rescan)
2177         return c.sendCmd(cmd)
2178 }
2179
2180 // ImportPubKeyRescan imports the passed public key. When rescan is true, the
2181 // block history is scanned for transactions addressed to provided pubkey.
2182 func (c *Client) ImportPubKeyRescan(pubKey string, rescan bool) error {
2183         return c.ImportPubKeyRescanAsync(pubKey, rescan).Receive()
2184 }
2185
2186 // ***********************
2187 // Miscellaneous Functions
2188 // ***********************
2189
2190 // NOTE: While getinfo is implemented here (in wallet.go), a btcd chain server
2191 // will respond to getinfo requests as well, excluding any wallet information.
2192
2193 // FutureGetInfoResult is a future promise to deliver the result of a
2194 // GetInfoAsync RPC invocation (or an applicable error).
2195 type FutureGetInfoResult chan *response
2196
2197 // Receive waits for the response promised by the future and returns the info
2198 // provided by the server.
2199 func (r FutureGetInfoResult) Receive() (*btcjson.InfoWalletResult, error) {
2200         res, err := receiveFuture(r)
2201         if err != nil {
2202                 return nil, err
2203         }
2204
2205         // Unmarshal result as a getinfo result object.
2206         var infoRes btcjson.InfoWalletResult
2207         err = json.Unmarshal(res, &infoRes)
2208         if err != nil {
2209                 return nil, err
2210         }
2211
2212         return &infoRes, nil
2213 }
2214
2215 // GetInfoAsync returns an instance of a type that can be used to get the result
2216 // of the RPC at some future time by invoking the Receive function on the
2217 // returned instance.
2218 //
2219 // See GetInfo for the blocking version and more details.
2220 func (c *Client) GetInfoAsync() FutureGetInfoResult {
2221         cmd := btcjson.NewGetInfoCmd()
2222         return c.sendCmd(cmd)
2223 }
2224
2225 // GetInfo returns miscellaneous info regarding the RPC server.  The returned
2226 // info object may be void of wallet information if the remote server does
2227 // not include wallet functionality.
2228 func (c *Client) GetInfo() (*btcjson.InfoWalletResult, error) {
2229         return c.GetInfoAsync().Receive()
2230 }
2231
2232 // TODO(davec): Implement
2233 // backupwallet (NYI in btcwallet)
2234 // encryptwallet (Won't be supported by btcwallet since it's always encrypted)
2235 // getwalletinfo (NYI in btcwallet or btcjson)
2236 // listaddressgroupings (NYI in btcwallet)
2237 // listreceivedbyaccount (NYI in btcwallet)
2238
2239 // DUMP
2240 // importwallet (NYI in btcwallet)
2241 // dumpwallet (NYI in btcwallet)