github.com/lbryio/lbcd@v0.22.119/rpcclient/wallet.go (about) 1 // Copyright (c) 2014-2020 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/lbryio/lbcd/btcjson" 12 "github.com/lbryio/lbcd/chaincfg" 13 "github.com/lbryio/lbcd/chaincfg/chainhash" 14 "github.com/lbryio/lbcd/wire" 15 btcutil "github.com/lbryio/lbcutil" 16 ) 17 18 // ***************************** 19 // Transaction Listing Functions 20 // ***************************** 21 22 // FutureGetTransactionResult is a future promise to deliver the result 23 // of a GetTransactionAsync or GetTransactionWatchOnlyAsync RPC invocation 24 // (or an applicable error). 25 type FutureGetTransactionResult chan *Response 26 27 // Receive waits for the Response promised by the future and returns detailed 28 // information about a wallet transaction. 29 func (r FutureGetTransactionResult) Receive() (*btcjson.GetTransactionResult, error) { 30 res, err := ReceiveFuture(r) 31 if err != nil { 32 return nil, err 33 } 34 35 // Unmarshal result as a gettransaction result object 36 var getTx btcjson.GetTransactionResult 37 err = json.Unmarshal(res, &getTx) 38 if err != nil { 39 return nil, err 40 } 41 42 return &getTx, nil 43 } 44 45 // GetTransactionAsync returns an instance of a type that can be used to get the 46 // result of the RPC at some future time by invoking the Receive function on 47 // the returned instance. 48 // 49 // See GetTransaction for the blocking version and more details. 50 func (c *Client) GetTransactionAsync(txHash *chainhash.Hash) FutureGetTransactionResult { 51 hash := "" 52 if txHash != nil { 53 hash = txHash.String() 54 } 55 56 cmd := btcjson.NewGetTransactionCmd(hash, nil) 57 return c.SendCmd(cmd) 58 } 59 60 // GetTransaction returns detailed information about a wallet transaction. 61 // 62 // See GetRawTransaction to return the raw transaction instead. 63 func (c *Client) GetTransaction(txHash *chainhash.Hash) (*btcjson.GetTransactionResult, error) { 64 return c.GetTransactionAsync(txHash).Receive() 65 } 66 67 // GetTransactionWatchOnlyAsync returns an instance of a type that can be used 68 // to get the result of the RPC at some future time by invoking the Receive function on 69 // the returned instance. 70 // 71 // See GetTransactionWatchOnly for the blocking version and more details. 72 func (c *Client) GetTransactionWatchOnlyAsync(txHash *chainhash.Hash, watchOnly bool) FutureGetTransactionResult { 73 hash := "" 74 if txHash != nil { 75 hash = txHash.String() 76 } 77 78 cmd := btcjson.NewGetTransactionCmd(hash, &watchOnly) 79 return c.SendCmd(cmd) 80 } 81 82 // GetTransactionWatchOnly returns detailed information about a wallet 83 // transaction, and allow including watch-only addresses in balance 84 // calculation and details. 85 func (c *Client) GetTransactionWatchOnly(txHash *chainhash.Hash, watchOnly bool) (*btcjson.GetTransactionResult, error) { 86 return c.GetTransactionWatchOnlyAsync(txHash, watchOnly).Receive() 87 } 88 89 // FutureListTransactionsResult is a future promise to deliver the result of a 90 // ListTransactionsAsync, ListTransactionsCountAsync, or 91 // ListTransactionsCountFromAsync RPC invocation (or an applicable error). 92 type FutureListTransactionsResult chan *Response 93 94 // Receive waits for the Response promised by the future and returns a list of 95 // the most recent transactions. 96 func (r FutureListTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) { 97 res, err := ReceiveFuture(r) 98 if err != nil { 99 return nil, err 100 } 101 102 // Unmarshal result as an array of listtransaction result objects. 103 var transactions []btcjson.ListTransactionsResult 104 err = json.Unmarshal(res, &transactions) 105 if err != nil { 106 return nil, err 107 } 108 109 return transactions, nil 110 } 111 112 // ListTransactionsAsync returns an instance of a type that can be used to get 113 // the result of the RPC at some future time by invoking the Receive function on 114 // the returned instance. 115 // 116 // See ListTransactions for the blocking version and more details. 117 func (c *Client) ListTransactionsAsync(account string) FutureListTransactionsResult { 118 cmd := btcjson.NewListTransactionsCmd(&account, nil, nil, nil) 119 return c.SendCmd(cmd) 120 } 121 122 // ListTransactions returns a list of the most recent transactions. 123 // 124 // See the ListTransactionsCount and ListTransactionsCountFrom to control the 125 // number of transactions returned and starting point, respectively. 126 func (c *Client) ListTransactions(account string) ([]btcjson.ListTransactionsResult, error) { 127 return c.ListTransactionsAsync(account).Receive() 128 } 129 130 // ListTransactionsCountAsync returns an instance of a type that can be used to 131 // get the result of the RPC at some future time by invoking the Receive 132 // function on the returned instance. 133 // 134 // See ListTransactionsCount for the blocking version and more details. 135 func (c *Client) ListTransactionsCountAsync(account string, count int) FutureListTransactionsResult { 136 cmd := btcjson.NewListTransactionsCmd(&account, &count, nil, nil) 137 return c.SendCmd(cmd) 138 } 139 140 // ListTransactionsCount returns a list of the most recent transactions up 141 // to the passed count. 142 // 143 // See the ListTransactions and ListTransactionsCountFrom functions for 144 // different options. 145 func (c *Client) ListTransactionsCount(account string, count int) ([]btcjson.ListTransactionsResult, error) { 146 return c.ListTransactionsCountAsync(account, count).Receive() 147 } 148 149 // ListTransactionsCountFromAsync returns an instance of a type that can be used 150 // to get the result of the RPC at some future time by invoking the Receive 151 // function on the returned instance. 152 // 153 // See ListTransactionsCountFrom for the blocking version and more details. 154 func (c *Client) ListTransactionsCountFromAsync(account string, count, from int) FutureListTransactionsResult { 155 cmd := btcjson.NewListTransactionsCmd(&account, &count, &from, nil) 156 return c.SendCmd(cmd) 157 } 158 159 // ListTransactionsCountFrom returns a list of the most recent transactions up 160 // to the passed count while skipping the first 'from' transactions. 161 // 162 // See the ListTransactions and ListTransactionsCount functions to use defaults. 163 func (c *Client) ListTransactionsCountFrom(account string, count, from int) ([]btcjson.ListTransactionsResult, error) { 164 return c.ListTransactionsCountFromAsync(account, count, from).Receive() 165 } 166 167 // ListTransactionsCountFromWatchOnlyAsync returns an instance of a type that can be used 168 // to get the result of the RPC at some future time by invoking the Receive 169 // function on the returned instance. 170 // 171 // See ListTransactionsCountFromWatchOnly for the blocking version and more details. 172 func (c *Client) ListTransactionsCountFromWatchOnlyAsync(account string, count, from int, watchOnly bool) FutureListTransactionsResult { 173 cmd := btcjson.NewListTransactionsCmd(&account, &count, &from, &watchOnly) 174 return c.SendCmd(cmd) 175 } 176 177 // ListTransactionsCountFromWatchOnly returns a list of the most recent transactions up 178 // to the passed count while skipping the first 'from' transactions. It will include or 179 // exclude transactions from watch-only addresses based on the passed value for the watchOnly parameter 180 // 181 // See the ListTransactions and ListTransactionsCount functions to use defaults. 182 func (c *Client) ListTransactionsCountFromWatchOnly(account string, count, from int, watchOnly bool) ([]btcjson.ListTransactionsResult, error) { 183 return c.ListTransactionsCountFromWatchOnlyAsync(account, count, from, watchOnly).Receive() 184 } 185 186 // FutureListUnspentResult is a future promise to deliver the result of a 187 // ListUnspentAsync, ListUnspentMinAsync, ListUnspentMinMaxAsync, or 188 // ListUnspentMinMaxAddressesAsync RPC invocation (or an applicable error). 189 type FutureListUnspentResult chan *Response 190 191 // Receive waits for the Response promised by the future and returns all 192 // unspent wallet transaction outputs returned by the RPC call. If the 193 // future wac returned by a call to ListUnspentMinAsync, ListUnspentMinMaxAsync, 194 // or ListUnspentMinMaxAddressesAsync, the range may be limited by the 195 // parameters of the RPC invocation. 196 func (r FutureListUnspentResult) Receive() ([]btcjson.ListUnspentResult, error) { 197 res, err := ReceiveFuture(r) 198 if err != nil { 199 return nil, err 200 } 201 202 // Unmarshal result as an array of listunspent results. 203 var unspent []btcjson.ListUnspentResult 204 err = json.Unmarshal(res, &unspent) 205 if err != nil { 206 return nil, err 207 } 208 209 return unspent, nil 210 } 211 212 // ListUnspentAsync returns an instance of a type that can be used to get 213 // the result of the RPC at some future time by invoking the Receive function 214 // on the returned instance. 215 // 216 // See ListUnspent for the blocking version and more details. 217 func (c *Client) ListUnspentAsync() FutureListUnspentResult { 218 cmd := btcjson.NewListUnspentCmd(nil, nil, nil) 219 return c.SendCmd(cmd) 220 } 221 222 // ListUnspentMinAsync returns an instance of a type that can be used to get 223 // the result of the RPC at some future time by invoking the Receive function 224 // on the returned instance. 225 // 226 // See ListUnspentMin for the blocking version and more details. 227 func (c *Client) ListUnspentMinAsync(minConf int) FutureListUnspentResult { 228 cmd := btcjson.NewListUnspentCmd(&minConf, nil, nil) 229 return c.SendCmd(cmd) 230 } 231 232 // ListUnspentMinMaxAsync returns an instance of a type that can be used to get 233 // the result of the RPC at some future time by invoking the Receive function 234 // on the returned instance. 235 // 236 // See ListUnspentMinMax for the blocking version and more details. 237 func (c *Client) ListUnspentMinMaxAsync(minConf, maxConf int) FutureListUnspentResult { 238 cmd := btcjson.NewListUnspentCmd(&minConf, &maxConf, nil) 239 return c.SendCmd(cmd) 240 } 241 242 // ListUnspentMinMaxAddressesAsync returns an instance of a type that can be 243 // used to get the result of the RPC at some future time by invoking the Receive 244 // function on the returned instance. 245 // 246 // See ListUnspentMinMaxAddresses for the blocking version and more details. 247 func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, addrs []btcutil.Address) FutureListUnspentResult { 248 addrStrs := make([]string, 0, len(addrs)) 249 for _, a := range addrs { 250 addrStrs = append(addrStrs, a.EncodeAddress()) 251 } 252 253 cmd := btcjson.NewListUnspentCmd(&minConf, &maxConf, &addrStrs) 254 return c.SendCmd(cmd) 255 } 256 257 // ListUnspent returns all unspent transaction outputs known to a wallet, using 258 // the default number of minimum and maximum number of confirmations as a 259 // filter (1 and 999999, respectively). 260 func (c *Client) ListUnspent() ([]btcjson.ListUnspentResult, error) { 261 return c.ListUnspentAsync().Receive() 262 } 263 264 // ListUnspentMin returns all unspent transaction outputs known to a wallet, 265 // using the specified number of minimum confirmations and default number of 266 // maximum confirmations (999999) as a filter. 267 func (c *Client) ListUnspentMin(minConf int) ([]btcjson.ListUnspentResult, error) { 268 return c.ListUnspentMinAsync(minConf).Receive() 269 } 270 271 // ListUnspentMinMax returns all unspent transaction outputs known to a wallet, 272 // using the specified number of minimum and maximum number of confirmations as 273 // a filter. 274 func (c *Client) ListUnspentMinMax(minConf, maxConf int) ([]btcjson.ListUnspentResult, error) { 275 return c.ListUnspentMinMaxAsync(minConf, maxConf).Receive() 276 } 277 278 // ListUnspentMinMaxAddresses returns all unspent transaction outputs that pay 279 // to any of specified addresses in a wallet using the specified number of 280 // minimum and maximum number of confirmations as a filter. 281 func (c *Client) ListUnspentMinMaxAddresses(minConf, maxConf int, addrs []btcutil.Address) ([]btcjson.ListUnspentResult, error) { 282 return c.ListUnspentMinMaxAddressesAsync(minConf, maxConf, addrs).Receive() 283 } 284 285 // FutureListSinceBlockResult is a future promise to deliver the result of a 286 // ListSinceBlockAsync or ListSinceBlockMinConfAsync RPC invocation (or an 287 // applicable error). 288 type FutureListSinceBlockResult chan *Response 289 290 // Receive waits for the Response promised by the future and returns all 291 // transactions added in blocks since the specified block hash, or all 292 // transactions if it is nil. 293 func (r FutureListSinceBlockResult) Receive() (*btcjson.ListSinceBlockResult, error) { 294 res, err := ReceiveFuture(r) 295 if err != nil { 296 return nil, err 297 } 298 299 // Unmarshal result as a listsinceblock result object. 300 var listResult btcjson.ListSinceBlockResult 301 err = json.Unmarshal(res, &listResult) 302 if err != nil { 303 return nil, err 304 } 305 306 return &listResult, nil 307 } 308 309 // ListSinceBlockAsync returns an instance of a type that can be used to get 310 // the result of the RPC at some future time by invoking the Receive function on 311 // the returned instance. 312 // 313 // See ListSinceBlock for the blocking version and more details. 314 func (c *Client) ListSinceBlockAsync(blockHash *chainhash.Hash) FutureListSinceBlockResult { 315 var hash *string 316 if blockHash != nil { 317 hash = btcjson.String(blockHash.String()) 318 } 319 320 cmd := btcjson.NewListSinceBlockCmd(hash, nil, nil) 321 return c.SendCmd(cmd) 322 } 323 324 // ListSinceBlock returns all transactions added in blocks since the specified 325 // block hash, or all transactions if it is nil, using the default number of 326 // minimum confirmations as a filter. 327 // 328 // See ListSinceBlockMinConf to override the minimum number of confirmations. 329 // See ListSinceBlockMinConfWatchOnly to override the minimum number of confirmations and watch only parameter. 330 func (c *Client) ListSinceBlock(blockHash *chainhash.Hash) (*btcjson.ListSinceBlockResult, error) { 331 return c.ListSinceBlockAsync(blockHash).Receive() 332 } 333 334 // ListSinceBlockMinConfAsync returns an instance of a type that can be used to 335 // get the result of the RPC at some future time by invoking the Receive 336 // function on the returned instance. 337 // 338 // See ListSinceBlockMinConf for the blocking version and more details. 339 func (c *Client) ListSinceBlockMinConfAsync(blockHash *chainhash.Hash, minConfirms int) FutureListSinceBlockResult { 340 var hash *string 341 if blockHash != nil { 342 hash = btcjson.String(blockHash.String()) 343 } 344 345 cmd := btcjson.NewListSinceBlockCmd(hash, &minConfirms, nil) 346 return c.SendCmd(cmd) 347 } 348 349 // ListSinceBlockMinConf returns all transactions added in blocks since the 350 // specified block hash, or all transactions if it is nil, using the specified 351 // number of minimum confirmations as a filter. 352 // 353 // See ListSinceBlock to use the default minimum number of confirmations. 354 func (c *Client) ListSinceBlockMinConf(blockHash *chainhash.Hash, minConfirms int) (*btcjson.ListSinceBlockResult, error) { 355 return c.ListSinceBlockMinConfAsync(blockHash, minConfirms).Receive() 356 } 357 358 // ListSinceBlockMinConfWatchOnlyAsync returns an instance of a type that can be used to 359 // get the result of the RPC at some future time by invoking the Receive 360 // function on the returned instance. 361 // 362 // See ListSinceBlockMinConfWatchOnly for the blocking version and more details. 363 func (c *Client) ListSinceBlockMinConfWatchOnlyAsync(blockHash *chainhash.Hash, minConfirms int, watchOnly bool) FutureListSinceBlockResult { 364 var hash *string 365 if blockHash != nil { 366 hash = btcjson.String(blockHash.String()) 367 } 368 369 cmd := btcjson.NewListSinceBlockCmd(hash, &minConfirms, &watchOnly) 370 return c.SendCmd(cmd) 371 } 372 373 // ListSinceBlockMinConfWatchOnly returns all transactions added in blocks since the 374 // specified block hash, or all transactions if it is nil, using the specified 375 // number of minimum confirmations as a filter. 376 // 377 // See ListSinceBlock to use the default minimum number of confirmations and default watch only parameter. 378 func (c *Client) ListSinceBlockMinConfWatchOnly(blockHash *chainhash.Hash, minConfirms int, watchOnly bool) (*btcjson.ListSinceBlockResult, error) { 379 return c.ListSinceBlockMinConfWatchOnlyAsync(blockHash, minConfirms, watchOnly).Receive() 380 } 381 382 // ************************** 383 // Transaction Send Functions 384 // ************************** 385 386 // FutureLockUnspentResult is a future promise to deliver the error result of a 387 // LockUnspentAsync RPC invocation. 388 type FutureLockUnspentResult chan *Response 389 390 // Receive waits for the Response promised by the future and returns the result 391 // of locking or unlocking the unspent output(s). 392 func (r FutureLockUnspentResult) Receive() error { 393 _, err := ReceiveFuture(r) 394 return err 395 } 396 397 // LockUnspentAsync returns an instance of a type that can be used to get the 398 // result of the RPC at some future time by invoking the Receive function on the 399 // returned instance. 400 // 401 // See LockUnspent for the blocking version and more details. 402 func (c *Client) LockUnspentAsync(unlock bool, ops []*wire.OutPoint) FutureLockUnspentResult { 403 outputs := make([]btcjson.TransactionInput, len(ops)) 404 for i, op := range ops { 405 outputs[i] = btcjson.TransactionInput{ 406 Txid: op.Hash.String(), 407 Vout: op.Index, 408 } 409 } 410 cmd := btcjson.NewLockUnspentCmd(unlock, outputs) 411 return c.SendCmd(cmd) 412 } 413 414 // LockUnspent marks outputs as locked or unlocked, depending on the value of 415 // the unlock bool. When locked, the unspent output will not be selected as 416 // input for newly created, non-raw transactions, and will not be returned in 417 // future ListUnspent results, until the output is marked unlocked again. 418 // 419 // If unlock is false, each outpoint in ops will be marked locked. If unlocked 420 // is true and specific outputs are specified in ops (len != 0), exactly those 421 // outputs will be marked unlocked. If unlocked is true and no outpoints are 422 // specified, all previous locked outputs are marked unlocked. 423 // 424 // The locked or unlocked state of outputs are not written to disk and after 425 // restarting a wallet process, this data will be reset (every output unlocked). 426 // 427 // NOTE: While this method would be a bit more readable if the unlock bool was 428 // reversed (that is, LockUnspent(true, ...) locked the outputs), it has been 429 // left as unlock to keep compatibility with the reference client API and to 430 // avoid confusion for those who are already familiar with the lockunspent RPC. 431 func (c *Client) LockUnspent(unlock bool, ops []*wire.OutPoint) error { 432 return c.LockUnspentAsync(unlock, ops).Receive() 433 } 434 435 // FutureListLockUnspentResult is a future promise to deliver the result of a 436 // ListLockUnspentAsync RPC invocation (or an applicable error). 437 type FutureListLockUnspentResult chan *Response 438 439 // Receive waits for the Response promised by the future and returns the result 440 // of all currently locked unspent outputs. 441 func (r FutureListLockUnspentResult) Receive() ([]*wire.OutPoint, error) { 442 res, err := ReceiveFuture(r) 443 if err != nil { 444 return nil, err 445 } 446 447 // Unmarshal as an array of transaction inputs. 448 var inputs []btcjson.TransactionInput 449 err = json.Unmarshal(res, &inputs) 450 if err != nil { 451 return nil, err 452 } 453 454 // Create a slice of outpoints from the transaction input structs. 455 ops := make([]*wire.OutPoint, len(inputs)) 456 for i, input := range inputs { 457 sha, err := chainhash.NewHashFromStr(input.Txid) 458 if err != nil { 459 return nil, err 460 } 461 ops[i] = wire.NewOutPoint(sha, input.Vout) 462 } 463 464 return ops, nil 465 } 466 467 // ListLockUnspentAsync returns an instance of a type that can be used to get 468 // the result of the RPC at some future time by invoking the Receive function on 469 // the returned instance. 470 // 471 // See ListLockUnspent for the blocking version and more details. 472 func (c *Client) ListLockUnspentAsync() FutureListLockUnspentResult { 473 cmd := btcjson.NewListLockUnspentCmd() 474 return c.SendCmd(cmd) 475 } 476 477 // ListLockUnspent returns a slice of outpoints for all unspent outputs marked 478 // as locked by a wallet. Unspent outputs may be marked locked using 479 // LockOutput. 480 func (c *Client) ListLockUnspent() ([]*wire.OutPoint, error) { 481 return c.ListLockUnspentAsync().Receive() 482 } 483 484 // FutureSetTxFeeResult is a future promise to deliver the result of a 485 // SetTxFeeAsync RPC invocation (or an applicable error). 486 type FutureSetTxFeeResult chan *Response 487 488 // Receive waits for the Response promised by the future and returns the result 489 // of setting an optional transaction fee per KB that helps ensure transactions 490 // are processed quickly. Most transaction are 1KB. 491 func (r FutureSetTxFeeResult) Receive() error { 492 _, err := ReceiveFuture(r) 493 return err 494 } 495 496 // SetTxFeeAsync returns an instance of a type that can be used to get the 497 // result of the RPC at some future time by invoking the Receive function on the 498 // returned instance. 499 // 500 // See SetTxFee for the blocking version and more details. 501 func (c *Client) SetTxFeeAsync(fee btcutil.Amount) FutureSetTxFeeResult { 502 cmd := btcjson.NewSetTxFeeCmd(fee.ToBTC()) 503 return c.SendCmd(cmd) 504 } 505 506 // SetTxFee sets an optional transaction fee per KB that helps ensure 507 // transactions are processed quickly. Most transaction are 1KB. 508 func (c *Client) SetTxFee(fee btcutil.Amount) error { 509 return c.SetTxFeeAsync(fee).Receive() 510 } 511 512 // FutureSendToAddressResult is a future promise to deliver the result of a 513 // SendToAddressAsync RPC invocation (or an applicable error). 514 type FutureSendToAddressResult chan *Response 515 516 // Receive waits for the Response promised by the future and returns the hash 517 // of the transaction sending the passed amount to the given address. 518 func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) { 519 res, err := ReceiveFuture(r) 520 if err != nil { 521 return nil, err 522 } 523 524 // Unmarshal result as a string. 525 var txHash string 526 err = json.Unmarshal(res, &txHash) 527 if err != nil { 528 return nil, err 529 } 530 531 return chainhash.NewHashFromStr(txHash) 532 } 533 534 // SendToAddressAsync returns an instance of a type that can be used to get the 535 // result of the RPC at some future time by invoking the Receive function on the 536 // returned instance. 537 // 538 // See SendToAddress for the blocking version and more details. 539 func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount, 540 addrType *string) FutureSendToAddressResult { 541 addr := address.EncodeAddress() 542 cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType, nil, nil) 543 return c.SendCmd(cmd) 544 } 545 546 // SendToAddress sends the passed amount to the given address. 547 // 548 // See SendToAddressComment to associate comments with the transaction in the 549 // wallet. The comments are not part of the transaction and are only internal 550 // to the wallet. 551 // 552 // NOTE: This function requires to the wallet to be unlocked. See the 553 // WalletPassphrase function for more details. 554 func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount, 555 addrType *string) (*chainhash.Hash, error) { 556 return c.SendToAddressAsync(address, amount, addrType).Receive() 557 } 558 559 // SendToAddressCommentAsync returns an instance of a type that can be used to 560 // get the result of the RPC at some future time by invoking the Receive 561 // function on the returned instance. 562 // 563 // See SendToAddressComment for the blocking version and more details. 564 func (c *Client) SendToAddressCommentAsync(address btcutil.Address, 565 amount btcutil.Amount, addrType *string, comment string, 566 commentTo string) FutureSendToAddressResult { 567 568 addr := address.EncodeAddress() 569 cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType, 570 &comment, &commentTo) 571 return c.SendCmd(cmd) 572 } 573 574 // SendToAddressComment sends the passed amount to the given address and stores 575 // the provided comment and comment to in the wallet. The comment parameter is 576 // intended to be used for the purpose of the transaction while the commentTo 577 // parameter is intended to be used for who the transaction is being sent to. 578 // 579 // The comments are not part of the transaction and are only internal 580 // to the wallet. 581 // 582 // See SendToAddress to avoid using comments. 583 // 584 // NOTE: This function requires to the wallet to be unlocked. See the 585 // WalletPassphrase function for more details. 586 func (c *Client) SendToAddressComment(address btcutil.Address, amount btcutil.Amount, 587 addrType *string, comment, commentTo string) (*chainhash.Hash, error) { 588 return c.SendToAddressCommentAsync(address, amount, addrType, 589 comment, commentTo).Receive() 590 } 591 592 // FutureSendFromResult is a future promise to deliver the result of a 593 // SendFromAsync, SendFromMinConfAsync, or SendFromCommentAsync RPC invocation 594 // (or an applicable error). 595 type FutureSendFromResult chan *Response 596 597 // Receive waits for the Response promised by the future and returns the hash 598 // of the transaction sending amount to the given address using the provided 599 // account as a source of funds. 600 func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) { 601 res, err := ReceiveFuture(r) 602 if err != nil { 603 return nil, err 604 } 605 606 // Unmarshal result as a string. 607 var txHash string 608 err = json.Unmarshal(res, &txHash) 609 if err != nil { 610 return nil, err 611 } 612 613 return chainhash.NewHashFromStr(txHash) 614 } 615 616 // SendFromAsync returns an instance of a type that can be used to get the 617 // result of the RPC at some future time by invoking the Receive function on the 618 // returned instance. 619 // 620 // See SendFrom for the blocking version and more details. 621 func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, 622 amount btcutil.Amount, addrType *string) FutureSendFromResult { 623 addr := toAddress.EncodeAddress() 624 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil, 625 addrType, nil, nil) 626 return c.SendCmd(cmd) 627 } 628 629 // SendFrom sends the passed amount to the given address using the provided 630 // account as a source of funds. Only funds with the default number of minimum 631 // confirmations will be used. 632 // 633 // See SendFromMinConf and SendFromComment for different options. 634 // 635 // NOTE: This function requires to the wallet to be unlocked. See the 636 // WalletPassphrase function for more details. 637 func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, addrType *string) (*chainhash.Hash, error) { 638 return c.SendFromAsync(fromAccount, toAddress, amount, addrType).Receive() 639 } 640 641 // SendFromMinConfAsync returns an instance of a type that can be used to get 642 // the result of the RPC at some future time by invoking the Receive function on 643 // the returned instance. 644 // 645 // See SendFromMinConf for the blocking version and more details. 646 func (c *Client) SendFromMinConfAsync(fromAccount string, 647 toAddress btcutil.Address, amount btcutil.Amount, 648 minConfirms int, addrType *string) FutureSendFromResult { 649 addr := toAddress.EncodeAddress() 650 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), 651 &minConfirms, addrType, nil, nil) 652 return c.SendCmd(cmd) 653 } 654 655 // SendFromMinConf sends the passed amount to the given address using the 656 // provided account as a source of funds. Only funds with the passed number of 657 // minimum confirmations will be used. 658 // 659 // See SendFrom to use the default number of minimum confirmations and 660 // SendFromComment for additional options. 661 // 662 // NOTE: This function requires to the wallet to be unlocked. See the 663 // WalletPassphrase function for more details. 664 func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address, 665 amount btcutil.Amount, minConfirms int, addrType *string) (*chainhash.Hash, error) { 666 return c.SendFromMinConfAsync(fromAccount, toAddress, amount, 667 minConfirms, addrType).Receive() 668 } 669 670 // SendFromCommentAsync returns an instance of a type that can be used to get 671 // the result of the RPC at some future time by invoking the Receive function on 672 // the returned instance. 673 // 674 // See SendFromComment for the blocking version and more details. 675 func (c *Client) SendFromCommentAsync(fromAccount string, 676 toAddress btcutil.Address, amount btcutil.Amount, minConfirms int, 677 addrType *string, comment, commentTo string) FutureSendFromResult { 678 679 addr := toAddress.EncodeAddress() 680 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), 681 &minConfirms, addrType, &comment, &commentTo) 682 return c.SendCmd(cmd) 683 } 684 685 // SendFromComment sends the passed amount to the given address using the 686 // provided account as a source of funds and stores the provided comment and 687 // comment to in the wallet. The comment parameter is intended to be used for 688 // the purpose of the transaction while the commentTo parameter is intended to 689 // be used for who the transaction is being sent to. Only funds with the passed 690 // number of minimum confirmations will be used. 691 // 692 // See SendFrom and SendFromMinConf to use defaults. 693 // 694 // NOTE: This function requires to the wallet to be unlocked. See the 695 // WalletPassphrase function for more details. 696 func (c *Client) SendFromComment(fromAccount string, toAddress btcutil.Address, 697 amount btcutil.Amount, minConfirms int, addrType *string, 698 comment, commentTo string) (*chainhash.Hash, error) { 699 700 return c.SendFromCommentAsync(fromAccount, toAddress, amount, 701 minConfirms, addrType, comment, commentTo).Receive() 702 } 703 704 // FutureSendManyResult is a future promise to deliver the result of a 705 // SendManyAsync, SendManyMinConfAsync, or SendManyCommentAsync RPC invocation 706 // (or an applicable error). 707 type FutureSendManyResult chan *Response 708 709 // Receive waits for the Response promised by the future and returns the hash 710 // of the transaction sending multiple amounts to multiple addresses using the 711 // provided account as a source of funds. 712 func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) { 713 res, err := ReceiveFuture(r) 714 if err != nil { 715 return nil, err 716 } 717 718 // Unmashal result as a string. 719 var txHash string 720 err = json.Unmarshal(res, &txHash) 721 if err != nil { 722 return nil, err 723 } 724 725 return chainhash.NewHashFromStr(txHash) 726 } 727 728 // SendManyAsync returns an instance of a type that can be used to get the 729 // result of the RPC at some future time by invoking the Receive function on the 730 // returned instance. 731 // 732 // See SendMany for the blocking version and more details. 733 func (c *Client) SendManyAsync(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) FutureSendManyResult { 734 convertedAmounts := make(map[string]float64, len(amounts)) 735 for addr, amount := range amounts { 736 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 737 } 738 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil, nil) 739 return c.SendCmd(cmd) 740 } 741 742 // SendMany sends multiple amounts to multiple addresses using the provided 743 // account as a source of funds in a single transaction. Only funds with the 744 // default number of minimum confirmations will be used. 745 // 746 // See SendManyMinConf and SendManyComment for different options. 747 // 748 // NOTE: This function requires to the wallet to be unlocked. See the 749 // WalletPassphrase function for more details. 750 func (c *Client) SendMany(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) (*chainhash.Hash, error) { 751 return c.SendManyAsync(fromAccount, amounts).Receive() 752 } 753 754 // SendManyMinConfAsync returns an instance of a type that can be used to get 755 // the result of the RPC at some future time by invoking the Receive function on 756 // the returned instance. 757 // 758 // See SendManyMinConf for the blocking version and more details. 759 func (c *Client) SendManyMinConfAsync(fromAccount string, 760 amounts map[btcutil.Address]btcutil.Amount, 761 minConfirms int, addrType *string) FutureSendManyResult { 762 763 convertedAmounts := make(map[string]float64, len(amounts)) 764 for addr, amount := range amounts { 765 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 766 } 767 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, 768 &minConfirms, nil, addrType) 769 return c.SendCmd(cmd) 770 } 771 772 // SendManyMinConf sends multiple amounts to multiple addresses using the 773 // provided account as a source of funds in a single transaction. Only funds 774 // with the passed number of minimum confirmations will be used. 775 // 776 // See SendMany to use the default number of minimum confirmations and 777 // SendManyComment for additional options. 778 // 779 // NOTE: This function requires to the wallet to be unlocked. See the 780 // WalletPassphrase function for more details. 781 func (c *Client) SendManyMinConf(fromAccount string, 782 amounts map[btcutil.Address]btcutil.Amount, 783 minConfirms int, addrType *string) (*chainhash.Hash, error) { 784 785 return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms, 786 addrType).Receive() 787 } 788 789 // SendManyCommentAsync returns an instance of a type that can be used to get 790 // the result of the RPC at some future time by invoking the Receive function on 791 // the returned instance. 792 // 793 // See SendManyComment for the blocking version and more details. 794 func (c *Client) SendManyCommentAsync(fromAccount string, 795 amounts map[btcutil.Address]btcutil.Amount, minConfirms int, 796 addrType *string, comment string) FutureSendManyResult { 797 798 convertedAmounts := make(map[string]float64, len(amounts)) 799 for addr, amount := range amounts { 800 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 801 } 802 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, 803 &minConfirms, &comment, addrType) 804 return c.SendCmd(cmd) 805 } 806 807 // SendManyComment sends multiple amounts to multiple addresses using the 808 // provided account as a source of funds in a single transaction and stores the 809 // provided comment in the wallet. The comment parameter is intended to be used 810 // for the purpose of the transaction Only funds with the passed number of 811 // minimum confirmations will be used. 812 // 813 // See SendMany and SendManyMinConf to use defaults. 814 // 815 // NOTE: This function requires to the wallet to be unlocked. See the 816 // WalletPassphrase function for more details. 817 func (c *Client) SendManyComment(fromAccount string, 818 amounts map[btcutil.Address]btcutil.Amount, minConfirms int, 819 addrType *string, comment string) (*chainhash.Hash, error) { 820 821 return c.SendManyCommentAsync(fromAccount, amounts, minConfirms, 822 addrType, comment).Receive() 823 } 824 825 // ************************* 826 // Address/Account Functions 827 // ************************* 828 829 // FutureAddMultisigAddressResult is a future promise to deliver the result of a 830 // AddMultisigAddressAsync RPC invocation (or an applicable error). 831 type FutureAddMultisigAddressResult struct { 832 responseChannel chan *Response 833 network *chaincfg.Params 834 } 835 836 // Receive waits for the Response promised by the future and returns the 837 // multisignature address that requires the specified number of signatures for 838 // the provided addresses. 839 func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) { 840 res, err := ReceiveFuture(r.responseChannel) 841 if err != nil { 842 return nil, err 843 } 844 845 // Unmarshal result as a string. 846 var addr string 847 err = json.Unmarshal(res, &addr) 848 if err != nil { 849 return nil, err 850 } 851 852 return btcutil.DecodeAddress(addr, r.network) 853 } 854 855 // AddMultisigAddressAsync returns an instance of a type that can be used to get 856 // the result of the RPC at some future time by invoking the Receive function on 857 // the returned instance. 858 // 859 // See AddMultisigAddress for the blocking version and more details. 860 func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []btcutil.Address, account string) FutureAddMultisigAddressResult { 861 addrs := make([]string, 0, len(addresses)) 862 for _, addr := range addresses { 863 addrs = append(addrs, addr.String()) 864 } 865 866 cmd := btcjson.NewAddMultisigAddressCmd(requiredSigs, addrs, &account) 867 result := FutureAddMultisigAddressResult{ 868 network: c.chainParams, 869 responseChannel: c.SendCmd(cmd), 870 } 871 return result 872 } 873 874 // AddMultisigAddress adds a multisignature address that requires the specified 875 // number of signatures for the provided addresses to the wallet. 876 func (c *Client) AddMultisigAddress(requiredSigs int, addresses []btcutil.Address, account string) (btcutil.Address, error) { 877 return c.AddMultisigAddressAsync(requiredSigs, addresses, account).Receive() 878 } 879 880 // FutureCreateMultisigResult is a future promise to deliver the result of a 881 // CreateMultisigAsync RPC invocation (or an applicable error). 882 type FutureCreateMultisigResult chan *Response 883 884 // Receive waits for the Response promised by the future and returns the 885 // multisignature address and script needed to redeem it. 886 func (r FutureCreateMultisigResult) Receive() (*btcjson.CreateMultiSigResult, error) { 887 res, err := ReceiveFuture(r) 888 if err != nil { 889 return nil, err 890 } 891 892 // Unmarshal result as a createmultisig result object. 893 var multisigRes btcjson.CreateMultiSigResult 894 err = json.Unmarshal(res, &multisigRes) 895 if err != nil { 896 return nil, err 897 } 898 899 return &multisigRes, nil 900 } 901 902 // CreateMultisigAsync returns an instance of a type that can be used to get 903 // the result of the RPC at some future time by invoking the Receive function on 904 // the returned instance. 905 // 906 // See CreateMultisig for the blocking version and more details. 907 func (c *Client) CreateMultisigAsync(requiredSigs int, addresses []btcutil.Address) FutureCreateMultisigResult { 908 addrs := make([]string, 0, len(addresses)) 909 for _, addr := range addresses { 910 addrs = append(addrs, addr.String()) 911 } 912 913 cmd := btcjson.NewCreateMultisigCmd(requiredSigs, addrs) 914 return c.SendCmd(cmd) 915 } 916 917 // CreateMultisig creates a multisignature address that requires the specified 918 // number of signatures for the provided addresses and returns the 919 // multisignature address and script needed to redeem it. 920 func (c *Client) CreateMultisig(requiredSigs int, addresses []btcutil.Address) (*btcjson.CreateMultiSigResult, error) { 921 return c.CreateMultisigAsync(requiredSigs, addresses).Receive() 922 } 923 924 // FutureCreateNewAccountResult is a future promise to deliver the result of a 925 // CreateNewAccountAsync RPC invocation (or an applicable error). 926 type FutureCreateNewAccountResult chan *Response 927 928 // Receive waits for the Response promised by the future and returns the 929 // result of creating new account. 930 func (r FutureCreateNewAccountResult) Receive() error { 931 _, err := ReceiveFuture(r) 932 return err 933 } 934 935 // CreateNewAccountAsync returns an instance of a type that can be used to get the 936 // result of the RPC at some future time by invoking the Receive function on the 937 // returned instance. 938 // 939 // See CreateNewAccount for the blocking version and more details. 940 func (c *Client) CreateNewAccountAsync(account string) FutureCreateNewAccountResult { 941 cmd := btcjson.NewCreateNewAccountCmd(account) 942 return c.SendCmd(cmd) 943 } 944 945 // CreateNewAccount creates a new wallet account. 946 func (c *Client) CreateNewAccount(account string) error { 947 return c.CreateNewAccountAsync(account).Receive() 948 } 949 950 // FutureCreateWalletResult is a future promise to deliver the result of a 951 // CreateWalletAsync RPC invocation (or an applicable error). 952 type FutureCreateWalletResult chan *Response 953 954 // Receive waits for the Response promised by the future and returns the 955 // result of creating a new wallet. 956 func (r FutureCreateWalletResult) Receive() (*btcjson.CreateWalletResult, error) { 957 res, err := ReceiveFuture(r) 958 if err != nil { 959 return nil, err 960 } 961 962 var createWalletResult btcjson.CreateWalletResult 963 err = json.Unmarshal(res, &createWalletResult) 964 if err != nil { 965 return nil, err 966 } 967 return &createWalletResult, nil 968 } 969 970 // CreateWalletOpt defines a functional-option to be used with CreateWallet 971 // method. 972 type CreateWalletOpt func(*btcjson.CreateWalletCmd) 973 974 // WithCreateWalletDisablePrivateKeys disables the possibility of private keys 975 // to be used with a wallet created using the CreateWallet method. Using this 976 // option will make the wallet watch-only. 977 func WithCreateWalletDisablePrivateKeys() CreateWalletOpt { 978 return func(c *btcjson.CreateWalletCmd) { 979 c.DisablePrivateKeys = btcjson.Bool(true) 980 } 981 } 982 983 // WithCreateWalletBlank specifies creation of a blank wallet. 984 func WithCreateWalletBlank() CreateWalletOpt { 985 return func(c *btcjson.CreateWalletCmd) { 986 c.Blank = btcjson.Bool(true) 987 } 988 } 989 990 // WithCreateWalletPassphrase specifies a passphrase to encrypt the wallet 991 // with. 992 func WithCreateWalletPassphrase(value string) CreateWalletOpt { 993 return func(c *btcjson.CreateWalletCmd) { 994 c.Passphrase = btcjson.String(value) 995 } 996 } 997 998 // WithCreateWalletAvoidReuse specifies keeping track of coin reuse, and 999 // treat dirty and clean coins differently with privacy considerations in mind. 1000 func WithCreateWalletAvoidReuse() CreateWalletOpt { 1001 return func(c *btcjson.CreateWalletCmd) { 1002 c.AvoidReuse = btcjson.Bool(true) 1003 } 1004 } 1005 1006 // CreateWalletAsync 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 CreateWallet for the blocking version and more details. 1011 func (c *Client) CreateWalletAsync(name string, opts ...CreateWalletOpt) FutureCreateWalletResult { 1012 cmd := btcjson.NewCreateWalletCmd(name, nil, nil, nil, nil) 1013 1014 // Apply each specified option to mutate the default command. 1015 for _, opt := range opts { 1016 opt(cmd) 1017 } 1018 1019 return c.SendCmd(cmd) 1020 } 1021 1022 // CreateWallet creates a new wallet account, with the possibility to use 1023 // private keys. 1024 // 1025 // Optional parameters can be specified using functional-options pattern. The 1026 // following functions are available: 1027 // - WithCreateWalletDisablePrivateKeys 1028 // - WithCreateWalletBlank 1029 // - WithCreateWalletPassphrase 1030 // - WithCreateWalletAvoidReuse 1031 func (c *Client) CreateWallet(name string, opts ...CreateWalletOpt) (*btcjson.CreateWalletResult, error) { 1032 return c.CreateWalletAsync(name, opts...).Receive() 1033 } 1034 1035 // FutureGetAddressInfoResult is a future promise to deliver the result of an 1036 // GetAddressInfoAsync RPC invocation (or an applicable error). 1037 type FutureGetAddressInfoResult chan *Response 1038 1039 // Receive waits for the Response promised by the future and returns the information 1040 // about the given bitcoin address. 1041 func (r FutureGetAddressInfoResult) Receive() (*btcjson.GetAddressInfoResult, error) { 1042 res, err := ReceiveFuture(r) 1043 if err != nil { 1044 return nil, err 1045 } 1046 1047 var getAddressInfoResult btcjson.GetAddressInfoResult 1048 err = json.Unmarshal(res, &getAddressInfoResult) 1049 if err != nil { 1050 return nil, err 1051 } 1052 return &getAddressInfoResult, nil 1053 } 1054 1055 // GetAddressInfoAsync returns an instance of a type that can be used to get the result 1056 // of the RPC at some future time by invoking the Receive function on the 1057 // returned instance. 1058 // 1059 // See GetAddressInfo for the blocking version and more details. 1060 func (c *Client) GetAddressInfoAsync(address string) FutureGetAddressInfoResult { 1061 cmd := btcjson.NewGetAddressInfoCmd(address) 1062 return c.SendCmd(cmd) 1063 } 1064 1065 // GetAddressInfo returns information about the given bitcoin address. 1066 func (c *Client) GetAddressInfo(address string) (*btcjson.GetAddressInfoResult, error) { 1067 return c.GetAddressInfoAsync(address).Receive() 1068 } 1069 1070 // FutureGetNewAddressResult is a future promise to deliver the result of a 1071 // GetNewAddressAsync RPC invocation (or an applicable error). 1072 type FutureGetNewAddressResult struct { 1073 responseChannel chan *Response 1074 network *chaincfg.Params 1075 } 1076 1077 // Receive waits for the Response promised by the future and returns a new 1078 // address. 1079 func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) { 1080 res, err := ReceiveFuture(r.responseChannel) 1081 if err != nil { 1082 return nil, err 1083 } 1084 1085 // Unmarshal result as a string. 1086 var addr string 1087 err = json.Unmarshal(res, &addr) 1088 if err != nil { 1089 return nil, err 1090 } 1091 1092 return btcutil.DecodeAddress(addr, r.network) 1093 } 1094 1095 // GetNewAddressAsync returns an instance of a type that can be used to get the 1096 // result of the RPC at some future time by invoking the Receive function on the 1097 // returned instance. 1098 // 1099 // See GetNewAddress for the blocking version and more details. 1100 func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult { 1101 cmd := btcjson.NewGetNewAddressCmd(&account) 1102 result := FutureGetNewAddressResult{ 1103 network: c.chainParams, 1104 responseChannel: c.SendCmd(cmd), 1105 } 1106 return result 1107 } 1108 1109 // GetNewAddress returns a new address, and decodes based on the client's 1110 // chain params. 1111 func (c *Client) GetNewAddress(account string) (btcutil.Address, error) { 1112 return c.GetNewAddressAsync(account).Receive() 1113 } 1114 1115 // FutureGetRawChangeAddressResult is a future promise to deliver the result of 1116 // a GetRawChangeAddressAsync RPC invocation (or an applicable error). 1117 type FutureGetRawChangeAddressResult struct { 1118 responseChannel chan *Response 1119 network *chaincfg.Params 1120 } 1121 1122 // Receive waits for the Response promised by the future and returns a new 1123 // address for receiving change that will be associated with the provided 1124 // account. Note that this is only for raw transactions and NOT for normal use. 1125 func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) { 1126 res, err := ReceiveFuture(r.responseChannel) 1127 if err != nil { 1128 return nil, err 1129 } 1130 1131 // Unmarshal result as a string. 1132 var addr string 1133 err = json.Unmarshal(res, &addr) 1134 if err != nil { 1135 return nil, err 1136 } 1137 1138 return btcutil.DecodeAddress(addr, r.network) 1139 } 1140 1141 // GetRawChangeAddressAsync returns an instance of a type that can be used to 1142 // get the result of the RPC at some future time by invoking the Receive 1143 // function on the returned instance. 1144 // 1145 // See GetRawChangeAddress for the blocking version and more details. 1146 func (c *Client) GetRawChangeAddressAsync(account *string) FutureGetRawChangeAddressResult { 1147 cmd := btcjson.NewGetRawChangeAddressCmd(account) 1148 result := FutureGetRawChangeAddressResult{ 1149 network: c.chainParams, 1150 responseChannel: c.SendCmd(cmd), 1151 } 1152 return result 1153 } 1154 1155 // GetRawChangeAddress returns a new address for receiving change that will be 1156 // associated with the provided account. Note that this is only for raw 1157 // transactions and NOT for normal use. 1158 func (c *Client) GetRawChangeAddress(account *string) (btcutil.Address, error) { 1159 return c.GetRawChangeAddressAsync(account).Receive() 1160 } 1161 1162 // FutureAddWitnessAddressResult is a future promise to deliver the result of 1163 // a AddWitnessAddressAsync RPC invocation (or an applicable error). 1164 type FutureAddWitnessAddressResult struct { 1165 responseChannel chan *Response 1166 network *chaincfg.Params 1167 } 1168 1169 // Receive waits for the Response promised by the future and returns the new 1170 // address. 1171 func (r FutureAddWitnessAddressResult) Receive() (btcutil.Address, error) { 1172 res, err := ReceiveFuture(r.responseChannel) 1173 if err != nil { 1174 return nil, err 1175 } 1176 1177 // Unmarshal result as a string. 1178 var addr string 1179 err = json.Unmarshal(res, &addr) 1180 if err != nil { 1181 return nil, err 1182 } 1183 1184 return btcutil.DecodeAddress(addr, r.network) 1185 } 1186 1187 // AddWitnessAddressAsync returns an instance of a type that can be used to get 1188 // the result of the RPC at some future time by invoking the Receive function on 1189 // the returned instance. 1190 // 1191 // See AddWitnessAddress for the blocking version and more details. 1192 func (c *Client) AddWitnessAddressAsync(address string) FutureAddWitnessAddressResult { 1193 cmd := btcjson.NewAddWitnessAddressCmd(address) 1194 response := FutureAddWitnessAddressResult{ 1195 network: c.chainParams, 1196 responseChannel: c.SendCmd(cmd), 1197 } 1198 return response 1199 } 1200 1201 // AddWitnessAddress adds a witness address for a script and returns the new 1202 // address (P2SH of the witness script). 1203 func (c *Client) AddWitnessAddress(address string) (btcutil.Address, error) { 1204 return c.AddWitnessAddressAsync(address).Receive() 1205 } 1206 1207 // FutureGetAccountAddressResult is a future promise to deliver the result of a 1208 // GetAccountAddressAsync RPC invocation (or an applicable error). 1209 type FutureGetAccountAddressResult struct { 1210 responseChannel chan *Response 1211 network *chaincfg.Params 1212 } 1213 1214 // Receive waits for the Response promised by the future and returns the current 1215 // Bitcoin address for receiving payments to the specified account. 1216 func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) { 1217 res, err := ReceiveFuture(r.responseChannel) 1218 if err != nil { 1219 return nil, err 1220 } 1221 1222 // Unmarshal result as a string. 1223 var addr string 1224 err = json.Unmarshal(res, &addr) 1225 if err != nil { 1226 return nil, err 1227 } 1228 1229 return btcutil.DecodeAddress(addr, r.network) 1230 } 1231 1232 // GetAccountAddressAsync returns an instance of a type that can be used to get 1233 // the result of the RPC at some future time by invoking the Receive function on 1234 // the returned instance. 1235 // 1236 // See GetAccountAddress for the blocking version and more details. 1237 func (c *Client) GetAccountAddressAsync(account *string) FutureGetAccountAddressResult { 1238 cmd := btcjson.NewGetAccountAddressCmd(account) 1239 result := FutureGetAccountAddressResult{ 1240 network: c.chainParams, 1241 responseChannel: c.SendCmd(cmd), 1242 } 1243 return result 1244 } 1245 1246 // GetAccountAddress returns the current Bitcoin address for receiving payments 1247 // to the specified account. 1248 func (c *Client) GetAccountAddress(account *string) (btcutil.Address, error) { 1249 return c.GetAccountAddressAsync(account).Receive() 1250 } 1251 1252 // FutureGetAccountResult is a future promise to deliver the result of a 1253 // GetAccountAsync RPC invocation (or an applicable error). 1254 type FutureGetAccountResult chan *Response 1255 1256 // Receive waits for the Response promised by the future and returns the account 1257 // associated with the passed address. 1258 func (r FutureGetAccountResult) Receive() (string, error) { 1259 res, err := ReceiveFuture(r) 1260 if err != nil { 1261 return "", err 1262 } 1263 1264 // Unmarshal result as a string. 1265 var account string 1266 err = json.Unmarshal(res, &account) 1267 if err != nil { 1268 return "", err 1269 } 1270 1271 return account, nil 1272 } 1273 1274 // GetAccountAsync returns an instance of a type that can be used to get the 1275 // result of the RPC at some future time by invoking the Receive function on the 1276 // returned instance. 1277 // 1278 // See GetAccount for the blocking version and more details. 1279 func (c *Client) GetAccountAsync(address btcutil.Address) FutureGetAccountResult { 1280 addr := address.EncodeAddress() 1281 cmd := btcjson.NewGetAccountCmd(addr) 1282 return c.SendCmd(cmd) 1283 } 1284 1285 // GetAccount returns the account associated with the passed address. 1286 func (c *Client) GetAccount(address btcutil.Address) (string, error) { 1287 return c.GetAccountAsync(address).Receive() 1288 } 1289 1290 // FutureGetAddressesByAccountResult is a future promise to deliver the result 1291 // of a GetAddressesByAccountAsync RPC invocation (or an applicable error). 1292 type FutureGetAddressesByAccountResult struct { 1293 responseChannel chan *Response 1294 network *chaincfg.Params 1295 } 1296 1297 // Receive waits for the Response promised by the future and returns the list of 1298 // addresses associated with the passed account. 1299 func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error) { 1300 res, err := ReceiveFuture(r.responseChannel) 1301 if err != nil { 1302 return nil, err 1303 } 1304 1305 // Unmashal result as an array of string. 1306 var addrStrings []string 1307 err = json.Unmarshal(res, &addrStrings) 1308 if err != nil { 1309 return nil, err 1310 } 1311 1312 addresses := make([]btcutil.Address, len(addrStrings)) 1313 for i, addrString := range addrStrings { 1314 addresses[i], err = btcutil.DecodeAddress(addrString, r.network) 1315 if err != nil { 1316 return nil, err 1317 } 1318 } 1319 1320 return addresses, nil 1321 } 1322 1323 // GetAddressesByAccountAsync returns an instance of a type that can be used to 1324 // get the result of the RPC at some future time by invoking the Receive 1325 // function on the returned instance. 1326 // 1327 // See GetAddressesByAccount for the blocking version and more details. 1328 func (c *Client) GetAddressesByAccountAsync(account *string) FutureGetAddressesByAccountResult { 1329 cmd := btcjson.NewGetAddressesByAccountCmd(account) 1330 result := FutureGetAddressesByAccountResult{ 1331 network: c.chainParams, 1332 responseChannel: c.SendCmd(cmd), 1333 } 1334 return result 1335 } 1336 1337 // GetAddressesByAccount returns the list of addresses associated with the 1338 // passed account. 1339 func (c *Client) GetAddressesByAccount(account *string) ([]btcutil.Address, error) { 1340 return c.GetAddressesByAccountAsync(account).Receive() 1341 } 1342 1343 // FutureMoveResult is a future promise to deliver the result of a MoveAsync, 1344 // MoveMinConfAsync, or MoveCommentAsync RPC invocation (or an applicable 1345 // error). 1346 type FutureMoveResult chan *Response 1347 1348 // Receive waits for the Response promised by the future and returns the result 1349 // of the move operation. 1350 func (r FutureMoveResult) Receive() (bool, error) { 1351 res, err := ReceiveFuture(r) 1352 if err != nil { 1353 return false, err 1354 } 1355 1356 // Unmarshal result as a boolean. 1357 var moveResult bool 1358 err = json.Unmarshal(res, &moveResult) 1359 if err != nil { 1360 return false, err 1361 } 1362 1363 return moveResult, nil 1364 } 1365 1366 // FutureRenameAccountResult is a future promise to deliver the result of a 1367 // RenameAccountAsync RPC invocation (or an applicable error). 1368 type FutureRenameAccountResult chan *Response 1369 1370 // Receive waits for the Response promised by the future and returns the 1371 // result of creating new account. 1372 func (r FutureRenameAccountResult) Receive() error { 1373 _, err := ReceiveFuture(r) 1374 return err 1375 } 1376 1377 // RenameAccountAsync returns an instance of a type that can be used to get the 1378 // result of the RPC at some future time by invoking the Receive function on the 1379 // returned instance. 1380 // 1381 // See RenameAccount for the blocking version and more details. 1382 func (c *Client) RenameAccountAsync(oldAccount, newAccount string) FutureRenameAccountResult { 1383 cmd := btcjson.NewRenameAccountCmd(oldAccount, newAccount) 1384 return c.SendCmd(cmd) 1385 } 1386 1387 // RenameAccount creates a new wallet account. 1388 func (c *Client) RenameAccount(oldAccount, newAccount string) error { 1389 return c.RenameAccountAsync(oldAccount, newAccount).Receive() 1390 } 1391 1392 // FutureValidateAddressResult is a future promise to deliver the result of a 1393 // ValidateAddressAsync RPC invocation (or an applicable error). 1394 type FutureValidateAddressResult chan *Response 1395 1396 // Receive waits for the Response promised by the future and returns information 1397 // about the given bitcoin address. 1398 func (r FutureValidateAddressResult) Receive() (*btcjson.ValidateAddressWalletResult, error) { 1399 res, err := ReceiveFuture(r) 1400 if err != nil { 1401 return nil, err 1402 } 1403 1404 // Unmarshal result as a validateaddress result object. 1405 var addrResult btcjson.ValidateAddressWalletResult 1406 err = json.Unmarshal(res, &addrResult) 1407 if err != nil { 1408 return nil, err 1409 } 1410 1411 return &addrResult, nil 1412 } 1413 1414 // ValidateAddressAsync returns an instance of a type that can be used to get 1415 // the result of the RPC at some future time by invoking the Receive function on 1416 // the returned instance. 1417 // 1418 // See ValidateAddress for the blocking version and more details. 1419 func (c *Client) ValidateAddressAsync(address btcutil.Address) FutureValidateAddressResult { 1420 addr := address.EncodeAddress() 1421 cmd := btcjson.NewValidateAddressCmd(addr) 1422 return c.SendCmd(cmd) 1423 } 1424 1425 // ValidateAddress returns information about the given bitcoin address. 1426 func (c *Client) ValidateAddress(address btcutil.Address) (*btcjson.ValidateAddressWalletResult, error) { 1427 return c.ValidateAddressAsync(address).Receive() 1428 } 1429 1430 // FutureKeyPoolRefillResult is a future promise to deliver the result of a 1431 // KeyPoolRefillAsync RPC invocation (or an applicable error). 1432 type FutureKeyPoolRefillResult chan *Response 1433 1434 // Receive waits for the Response promised by the future and returns the result 1435 // of refilling the key pool. 1436 func (r FutureKeyPoolRefillResult) Receive() error { 1437 _, err := ReceiveFuture(r) 1438 return err 1439 } 1440 1441 // KeyPoolRefillAsync returns an instance of a type that can be used to get the 1442 // result of the RPC at some future time by invoking the Receive function on the 1443 // returned instance. 1444 // 1445 // See KeyPoolRefill for the blocking version and more details. 1446 func (c *Client) KeyPoolRefillAsync() FutureKeyPoolRefillResult { 1447 cmd := btcjson.NewKeyPoolRefillCmd(nil) 1448 return c.SendCmd(cmd) 1449 } 1450 1451 // KeyPoolRefill fills the key pool as necessary to reach the default size. 1452 // 1453 // See KeyPoolRefillSize to override the size of the key pool. 1454 func (c *Client) KeyPoolRefill() error { 1455 return c.KeyPoolRefillAsync().Receive() 1456 } 1457 1458 // KeyPoolRefillSizeAsync returns an instance of a type that can be used to get 1459 // the result of the RPC at some future time by invoking the Receive function on 1460 // the returned instance. 1461 // 1462 // See KeyPoolRefillSize for the blocking version and more details. 1463 func (c *Client) KeyPoolRefillSizeAsync(newSize uint) FutureKeyPoolRefillResult { 1464 cmd := btcjson.NewKeyPoolRefillCmd(&newSize) 1465 return c.SendCmd(cmd) 1466 } 1467 1468 // KeyPoolRefillSize fills the key pool as necessary to reach the specified 1469 // size. 1470 func (c *Client) KeyPoolRefillSize(newSize uint) error { 1471 return c.KeyPoolRefillSizeAsync(newSize).Receive() 1472 } 1473 1474 // ************************ 1475 // Amount/Balance Functions 1476 // ************************ 1477 1478 // FutureListAccountsResult is a future promise to deliver the result of a 1479 // ListAccountsAsync or ListAccountsMinConfAsync RPC invocation (or an 1480 // applicable error). 1481 type FutureListAccountsResult chan *Response 1482 1483 // Receive waits for the Response promised by the future and returns returns a 1484 // map of account names and their associated balances. 1485 func (r FutureListAccountsResult) Receive() (map[string]btcutil.Amount, error) { 1486 res, err := ReceiveFuture(r) 1487 if err != nil { 1488 return nil, err 1489 } 1490 1491 // Unmarshal result as a json object. 1492 var accounts map[string]float64 1493 err = json.Unmarshal(res, &accounts) 1494 if err != nil { 1495 return nil, err 1496 } 1497 1498 accountsMap := make(map[string]btcutil.Amount) 1499 for k, v := range accounts { 1500 amount, err := btcutil.NewAmount(v) 1501 if err != nil { 1502 return nil, err 1503 } 1504 1505 accountsMap[k] = amount 1506 } 1507 1508 return accountsMap, nil 1509 } 1510 1511 // ListAccountsAsync returns an instance of a type that can be used to get the 1512 // result of the RPC at some future time by invoking the Receive function on the 1513 // returned instance. 1514 // 1515 // See ListAccounts for the blocking version and more details. 1516 func (c *Client) ListAccountsAsync() FutureListAccountsResult { 1517 cmd := btcjson.NewListAccountsCmd(nil) 1518 return c.SendCmd(cmd) 1519 } 1520 1521 // ListAccounts returns a map of account names and their associated balances 1522 // using the default number of minimum confirmations. 1523 // 1524 // See ListAccountsMinConf to override the minimum number of confirmations. 1525 func (c *Client) ListAccounts() (map[string]btcutil.Amount, error) { 1526 return c.ListAccountsAsync().Receive() 1527 } 1528 1529 // ListAccountsMinConfAsync returns an instance of a type that can be used to 1530 // get the result of the RPC at some future time by invoking the Receive 1531 // function on the returned instance. 1532 // 1533 // See ListAccountsMinConf for the blocking version and more details. 1534 func (c *Client) ListAccountsMinConfAsync(minConfirms int) FutureListAccountsResult { 1535 cmd := btcjson.NewListAccountsCmd(&minConfirms) 1536 return c.SendCmd(cmd) 1537 } 1538 1539 // ListAccountsMinConf returns a map of account names and their associated 1540 // balances using the specified number of minimum confirmations. 1541 // 1542 // See ListAccounts to use the default minimum number of confirmations. 1543 func (c *Client) ListAccountsMinConf(minConfirms int) (map[string]btcutil.Amount, error) { 1544 return c.ListAccountsMinConfAsync(minConfirms).Receive() 1545 } 1546 1547 // FutureGetBalanceResult is a future promise to deliver the result of a 1548 // GetBalanceAsync or GetBalanceMinConfAsync RPC invocation (or an applicable 1549 // error). 1550 type FutureGetBalanceResult chan *Response 1551 1552 // Receive waits for the Response promised by the future and returns the 1553 // available balance from the server for the specified account. 1554 func (r FutureGetBalanceResult) Receive() (btcutil.Amount, error) { 1555 res, err := ReceiveFuture(r) 1556 if err != nil { 1557 return 0, err 1558 } 1559 1560 // Unmarshal result as a floating point number. 1561 var balance float64 1562 err = json.Unmarshal(res, &balance) 1563 if err != nil { 1564 return 0, err 1565 } 1566 1567 amount, err := btcutil.NewAmount(balance) 1568 if err != nil { 1569 return 0, err 1570 } 1571 1572 return amount, nil 1573 } 1574 1575 // FutureGetBalanceParseResult is same as FutureGetBalanceResult except 1576 // that the result is expected to be a string which is then parsed into 1577 // a float64 value 1578 // This is required for compatibility with servers like blockchain.info 1579 type FutureGetBalanceParseResult chan *Response 1580 1581 // Receive waits for the Response promised by the future and returns the 1582 // available balance from the server for the specified account. 1583 func (r FutureGetBalanceParseResult) Receive() (btcutil.Amount, error) { 1584 res, err := ReceiveFuture(r) 1585 if err != nil { 1586 return 0, err 1587 } 1588 1589 // Unmarshal result as a string 1590 var balanceString string 1591 err = json.Unmarshal(res, &balanceString) 1592 if err != nil { 1593 return 0, err 1594 } 1595 1596 balance, err := strconv.ParseFloat(balanceString, 64) 1597 if err != nil { 1598 return 0, err 1599 } 1600 amount, err := btcutil.NewAmount(balance) 1601 if err != nil { 1602 return 0, err 1603 } 1604 1605 return amount, nil 1606 } 1607 1608 // GetBalanceAsync returns an instance of a type that can be used to get the 1609 // result of the RPC at some future time by invoking the Receive function on the 1610 // returned instance. 1611 // 1612 // See GetBalance for the blocking version and more details. 1613 func (c *Client) GetBalanceAsync(account string) FutureGetBalanceResult { 1614 cmd := btcjson.NewGetBalanceCmd(&account, nil) 1615 return c.SendCmd(cmd) 1616 } 1617 1618 // GetBalance returns the available balance from the server for the specified 1619 // account using the default number of minimum confirmations. The account may 1620 // be "*" for all accounts. 1621 // 1622 // See GetBalanceMinConf to override the minimum number of confirmations. 1623 func (c *Client) GetBalance(account string) (btcutil.Amount, error) { 1624 return c.GetBalanceAsync(account).Receive() 1625 } 1626 1627 // GetBalanceMinConfAsync returns an instance of a type that can be used to get 1628 // the result of the RPC at some future time by invoking the Receive function on 1629 // the returned instance. 1630 // 1631 // See GetBalanceMinConf for the blocking version and more details. 1632 func (c *Client) GetBalanceMinConfAsync(account string, minConfirms int) FutureGetBalanceResult { 1633 cmd := btcjson.NewGetBalanceCmd(&account, &minConfirms) 1634 return c.SendCmd(cmd) 1635 } 1636 1637 // GetBalanceMinConf returns the available balance from the server for the 1638 // specified account using the specified number of minimum confirmations. The 1639 // account may be "*" for all accounts. 1640 // 1641 // See GetBalance to use the default minimum number of confirmations. 1642 func (c *Client) GetBalanceMinConf(account string, minConfirms int) (btcutil.Amount, error) { 1643 if c.config.EnableBCInfoHacks { 1644 response := c.GetBalanceMinConfAsync(account, minConfirms) 1645 return FutureGetBalanceParseResult(response).Receive() 1646 } 1647 return c.GetBalanceMinConfAsync(account, minConfirms).Receive() 1648 } 1649 1650 // FutureGetBalancesResult is a future promise to deliver the result of a 1651 // GetBalancesAsync RPC invocation (or an applicable error). 1652 type FutureGetBalancesResult chan *Response 1653 1654 // Receive waits for the Response promised by the future and returns the 1655 // available balances from the server. 1656 func (r FutureGetBalancesResult) Receive() (*btcjson.GetBalancesResult, error) { 1657 res, err := ReceiveFuture(r) 1658 if err != nil { 1659 return nil, err 1660 } 1661 1662 // Unmarshal result as a floating point number. 1663 var balances btcjson.GetBalancesResult 1664 err = json.Unmarshal(res, &balances) 1665 if err != nil { 1666 return nil, err 1667 } 1668 1669 return &balances, nil 1670 } 1671 1672 // GetBalancesAsync returns an instance of a type that can be used to get the 1673 // result of the RPC at some future time by invoking the Receive function on the 1674 // returned instance. 1675 // 1676 // See GetBalances for the blocking version and more details. 1677 func (c *Client) GetBalancesAsync() FutureGetBalancesResult { 1678 cmd := btcjson.NewGetBalancesCmd() 1679 return c.SendCmd(cmd) 1680 } 1681 1682 // GetBalances returns the available balances from the server. 1683 func (c *Client) GetBalances() (*btcjson.GetBalancesResult, error) { 1684 return c.GetBalancesAsync().Receive() 1685 } 1686 1687 // FutureGetReceivedByAccountResult is a future promise to deliver the result of 1688 // a GetReceivedByAccountAsync or GetReceivedByAccountMinConfAsync RPC 1689 // invocation (or an applicable error). 1690 type FutureGetReceivedByAccountResult chan *Response 1691 1692 // Receive waits for the Response promised by the future and returns the total 1693 // amount received with the specified account. 1694 func (r FutureGetReceivedByAccountResult) Receive() (btcutil.Amount, error) { 1695 res, err := ReceiveFuture(r) 1696 if err != nil { 1697 return 0, err 1698 } 1699 1700 // Unmarshal result as a floating point number. 1701 var balance float64 1702 err = json.Unmarshal(res, &balance) 1703 if err != nil { 1704 return 0, err 1705 } 1706 1707 amount, err := btcutil.NewAmount(balance) 1708 if err != nil { 1709 return 0, err 1710 } 1711 1712 return amount, nil 1713 } 1714 1715 // GetReceivedByAccountAsync returns an instance of a type that can be used to 1716 // get the result of the RPC at some future time by invoking the Receive 1717 // function on the returned instance. 1718 // 1719 // See GetReceivedByAccount for the blocking version and more details. 1720 func (c *Client) GetReceivedByAccountAsync(account *string) FutureGetReceivedByAccountResult { 1721 cmd := btcjson.NewGetReceivedByAccountCmd(account, nil) 1722 return c.SendCmd(cmd) 1723 } 1724 1725 // GetReceivedByAccount returns the total amount received with the specified 1726 // account with at least the default number of minimum confirmations. 1727 // 1728 // See GetReceivedByAccountMinConf to override the minimum number of 1729 // confirmations. 1730 func (c *Client) GetReceivedByAccount(account *string) (btcutil.Amount, error) { 1731 return c.GetReceivedByAccountAsync(account).Receive() 1732 } 1733 1734 // GetReceivedByAccountMinConfAsync returns an instance of a type that can be 1735 // used to get the result of the RPC at some future time by invoking the Receive 1736 // function on the returned instance. 1737 // 1738 // See GetReceivedByAccountMinConf for the blocking version and more details. 1739 func (c *Client) GetReceivedByAccountMinConfAsync(account *string, minConfirms *int) FutureGetReceivedByAccountResult { 1740 cmd := btcjson.NewGetReceivedByAccountCmd(account, minConfirms) 1741 return c.SendCmd(cmd) 1742 } 1743 1744 // GetReceivedByAccountMinConf returns the total amount received with the 1745 // specified account with at least the specified number of minimum 1746 // confirmations. 1747 // 1748 // See GetReceivedByAccount to use the default minimum number of confirmations. 1749 func (c *Client) GetReceivedByAccountMinConf(account *string, minConfirms *int) (btcutil.Amount, error) { 1750 return c.GetReceivedByAccountMinConfAsync(account, minConfirms).Receive() 1751 } 1752 1753 // FutureGetUnconfirmedBalanceResult is a future promise to deliver the result 1754 // of a GetUnconfirmedBalanceAsync RPC invocation (or an applicable error). 1755 type FutureGetUnconfirmedBalanceResult chan *Response 1756 1757 // Receive waits for the Response promised by the future and returns returns the 1758 // unconfirmed balance from the server for the specified account. 1759 func (r FutureGetUnconfirmedBalanceResult) Receive() (btcutil.Amount, error) { 1760 res, err := ReceiveFuture(r) 1761 if err != nil { 1762 return 0, err 1763 } 1764 1765 // Unmarshal result as a floating point number. 1766 var balance float64 1767 err = json.Unmarshal(res, &balance) 1768 if err != nil { 1769 return 0, err 1770 } 1771 1772 amount, err := btcutil.NewAmount(balance) 1773 if err != nil { 1774 return 0, err 1775 } 1776 1777 return amount, nil 1778 } 1779 1780 // GetUnconfirmedBalanceAsync returns an instance of a type that can be used to 1781 // get the result of the RPC at some future time by invoking the Receive 1782 // function on the returned instance. 1783 // 1784 // See GetUnconfirmedBalance for the blocking version and more details. 1785 func (c *Client) GetUnconfirmedBalanceAsync(account string) FutureGetUnconfirmedBalanceResult { 1786 cmd := btcjson.NewGetUnconfirmedBalanceCmd(&account) 1787 return c.SendCmd(cmd) 1788 } 1789 1790 // GetUnconfirmedBalance returns the unconfirmed balance from the server for 1791 // the specified account. 1792 func (c *Client) GetUnconfirmedBalance(account string) (btcutil.Amount, error) { 1793 return c.GetUnconfirmedBalanceAsync(account).Receive() 1794 } 1795 1796 // FutureGetReceivedByAddressResult is a future promise to deliver the result of 1797 // a GetReceivedByAddressAsync or GetReceivedByAddressMinConfAsync RPC 1798 // invocation (or an applicable error). 1799 type FutureGetReceivedByAddressResult chan *Response 1800 1801 // Receive waits for the Response promised by the future and returns the total 1802 // amount received by the specified address. 1803 func (r FutureGetReceivedByAddressResult) Receive() (btcutil.Amount, error) { 1804 res, err := ReceiveFuture(r) 1805 if err != nil { 1806 return 0, err 1807 } 1808 1809 // Unmarshal result as a floating point number. 1810 var balance float64 1811 err = json.Unmarshal(res, &balance) 1812 if err != nil { 1813 return 0, err 1814 } 1815 1816 amount, err := btcutil.NewAmount(balance) 1817 if err != nil { 1818 return 0, err 1819 } 1820 1821 return amount, nil 1822 } 1823 1824 // GetReceivedByAddressAsync returns an instance of a type that can be used to 1825 // get the result of the RPC at some future time by invoking the Receive 1826 // function on the returned instance. 1827 // 1828 // See GetReceivedByAddress for the blocking version and more details. 1829 func (c *Client) GetReceivedByAddressAsync(address btcutil.Address) FutureGetReceivedByAddressResult { 1830 addr := address.EncodeAddress() 1831 cmd := btcjson.NewGetReceivedByAddressCmd(addr, nil) 1832 return c.SendCmd(cmd) 1833 1834 } 1835 1836 // GetReceivedByAddress returns the total amount received by the specified 1837 // address with at least the default number of minimum confirmations. 1838 // 1839 // See GetReceivedByAddressMinConf to override the minimum number of 1840 // confirmations. 1841 func (c *Client) GetReceivedByAddress(address btcutil.Address) (btcutil.Amount, error) { 1842 return c.GetReceivedByAddressAsync(address).Receive() 1843 } 1844 1845 // GetReceivedByAddressMinConfAsync returns an instance of a type that can be 1846 // used to get the result of the RPC at some future time by invoking the Receive 1847 // function on the returned instance. 1848 // 1849 // See GetReceivedByAddressMinConf for the blocking version and more details. 1850 func (c *Client) GetReceivedByAddressMinConfAsync(address btcutil.Address, minConfirms int) FutureGetReceivedByAddressResult { 1851 addr := address.EncodeAddress() 1852 cmd := btcjson.NewGetReceivedByAddressCmd(addr, &minConfirms) 1853 return c.SendCmd(cmd) 1854 } 1855 1856 // GetReceivedByAddressMinConf returns the total amount received by the specified 1857 // address with at least the specified number of minimum confirmations. 1858 // 1859 // See GetReceivedByAddress to use the default minimum number of confirmations. 1860 func (c *Client) GetReceivedByAddressMinConf(address btcutil.Address, minConfirms int) (btcutil.Amount, error) { 1861 return c.GetReceivedByAddressMinConfAsync(address, minConfirms).Receive() 1862 } 1863 1864 // FutureListReceivedByAccountResult is a future promise to deliver the result 1865 // of a ListReceivedByAccountAsync, ListReceivedByAccountMinConfAsync, or 1866 // ListReceivedByAccountIncludeEmptyAsync RPC invocation (or an applicable 1867 // error). 1868 type FutureListReceivedByAccountResult chan *Response 1869 1870 // Receive waits for the Response promised by the future and returns a list of 1871 // balances by account. 1872 func (r FutureListReceivedByAccountResult) Receive() ([]btcjson.ListReceivedByAccountResult, error) { 1873 res, err := ReceiveFuture(r) 1874 if err != nil { 1875 return nil, err 1876 } 1877 1878 // Unmarshal as an array of listreceivedbyaccount result objects. 1879 var received []btcjson.ListReceivedByAccountResult 1880 err = json.Unmarshal(res, &received) 1881 if err != nil { 1882 return nil, err 1883 } 1884 1885 return received, nil 1886 } 1887 1888 // ListReceivedByAccountAsync returns an instance of a type that can be used to 1889 // get the result of the RPC at some future time by invoking the Receive 1890 // function on the returned instance. 1891 // 1892 // See ListReceivedByAccount for the blocking version and more details. 1893 func (c *Client) ListReceivedByAccountAsync() FutureListReceivedByAccountResult { 1894 cmd := btcjson.NewListReceivedByAccountCmd(nil, nil, nil) 1895 return c.SendCmd(cmd) 1896 } 1897 1898 // ListReceivedByAccount lists balances by account using the default number 1899 // of minimum confirmations and including accounts that haven't received any 1900 // payments. 1901 // 1902 // See ListReceivedByAccountMinConf to override the minimum number of 1903 // confirmations and ListReceivedByAccountIncludeEmpty to filter accounts that 1904 // haven't received any payments from the results. 1905 func (c *Client) ListReceivedByAccount() ([]btcjson.ListReceivedByAccountResult, error) { 1906 return c.ListReceivedByAccountAsync().Receive() 1907 } 1908 1909 // ListReceivedByAccountMinConfAsync returns an instance of a type that can be 1910 // used to get the result of the RPC at some future time by invoking the Receive 1911 // function on the returned instance. 1912 // 1913 // See ListReceivedByAccountMinConf for the blocking version and more details. 1914 func (c *Client) ListReceivedByAccountMinConfAsync(minConfirms int) FutureListReceivedByAccountResult { 1915 cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, nil, nil) 1916 return c.SendCmd(cmd) 1917 } 1918 1919 // ListReceivedByAccountMinConf lists balances by account using the specified 1920 // number of minimum confirmations not including accounts that haven't received 1921 // any payments. 1922 // 1923 // See ListReceivedByAccount to use the default minimum number of confirmations 1924 // and ListReceivedByAccountIncludeEmpty to also include accounts that haven't 1925 // received any payments in the results. 1926 func (c *Client) ListReceivedByAccountMinConf(minConfirms int) ([]btcjson.ListReceivedByAccountResult, error) { 1927 return c.ListReceivedByAccountMinConfAsync(minConfirms).Receive() 1928 } 1929 1930 // ListReceivedByAccountIncludeEmptyAsync returns an instance of a type that can 1931 // be used to get the result of the RPC at some future time by invoking the 1932 // Receive function on the returned instance. 1933 // 1934 // See ListReceivedByAccountIncludeEmpty for the blocking version and more details. 1935 func (c *Client) ListReceivedByAccountIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAccountResult { 1936 cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, &includeEmpty, 1937 nil) 1938 return c.SendCmd(cmd) 1939 } 1940 1941 // ListReceivedByAccountIncludeEmpty lists balances by account using the 1942 // specified number of minimum confirmations and including accounts that 1943 // haven't received any payments depending on specified flag. 1944 // 1945 // See ListReceivedByAccount and ListReceivedByAccountMinConf to use defaults. 1946 func (c *Client) ListReceivedByAccountIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAccountResult, error) { 1947 return c.ListReceivedByAccountIncludeEmptyAsync(minConfirms, 1948 includeEmpty).Receive() 1949 } 1950 1951 // FutureListReceivedByAddressResult is a future promise to deliver the result 1952 // of a ListReceivedByAddressAsync, ListReceivedByAddressMinConfAsync, or 1953 // ListReceivedByAddressIncludeEmptyAsync RPC invocation (or an applicable 1954 // error). 1955 type FutureListReceivedByAddressResult chan *Response 1956 1957 // Receive waits for the Response promised by the future and returns a list of 1958 // balances by address. 1959 func (r FutureListReceivedByAddressResult) Receive() ([]btcjson.ListReceivedByAddressResult, error) { 1960 res, err := ReceiveFuture(r) 1961 if err != nil { 1962 return nil, err 1963 } 1964 1965 // Unmarshal as an array of listreceivedbyaddress result objects. 1966 var received []btcjson.ListReceivedByAddressResult 1967 err = json.Unmarshal(res, &received) 1968 if err != nil { 1969 return nil, err 1970 } 1971 1972 return received, nil 1973 } 1974 1975 // ListReceivedByAddressAsync returns an instance of a type that can be used to 1976 // get the result of the RPC at some future time by invoking the Receive 1977 // function on the returned instance. 1978 // 1979 // See ListReceivedByAddress for the blocking version and more details. 1980 func (c *Client) ListReceivedByAddressAsync() FutureListReceivedByAddressResult { 1981 cmd := btcjson.NewListReceivedByAddressCmd(nil, nil, nil) 1982 return c.SendCmd(cmd) 1983 } 1984 1985 // ListReceivedByAddress lists balances by address using the default number 1986 // of minimum confirmations not including addresses that haven't received any 1987 // payments or watching only addresses. 1988 // 1989 // See ListReceivedByAddressMinConf to override the minimum number of 1990 // confirmations and ListReceivedByAddressIncludeEmpty to also include addresses 1991 // that haven't received any payments in the results. 1992 func (c *Client) ListReceivedByAddress() ([]btcjson.ListReceivedByAddressResult, error) { 1993 return c.ListReceivedByAddressAsync().Receive() 1994 } 1995 1996 // ListReceivedByAddressMinConfAsync returns an instance of a type that can be 1997 // used to get the result of the RPC at some future time by invoking the Receive 1998 // function on the returned instance. 1999 // 2000 // See ListReceivedByAddressMinConf for the blocking version and more details. 2001 func (c *Client) ListReceivedByAddressMinConfAsync(minConfirms int) FutureListReceivedByAddressResult { 2002 cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, nil, nil) 2003 return c.SendCmd(cmd) 2004 } 2005 2006 // ListReceivedByAddressMinConf lists balances by address using the specified 2007 // number of minimum confirmations not including addresses that haven't received 2008 // any payments. 2009 // 2010 // See ListReceivedByAddress to use the default minimum number of confirmations 2011 // and ListReceivedByAddressIncludeEmpty to also include addresses that haven't 2012 // received any payments in the results. 2013 func (c *Client) ListReceivedByAddressMinConf(minConfirms int) ([]btcjson.ListReceivedByAddressResult, error) { 2014 return c.ListReceivedByAddressMinConfAsync(minConfirms).Receive() 2015 } 2016 2017 // ListReceivedByAddressIncludeEmptyAsync returns an instance of a type that can 2018 // be used to get the result of the RPC at some future time by invoking the 2019 // Receive function on the returned instance. 2020 // 2021 // See ListReceivedByAccountIncludeEmpty for the blocking version and more details. 2022 func (c *Client) ListReceivedByAddressIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAddressResult { 2023 cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, &includeEmpty, 2024 nil) 2025 return c.SendCmd(cmd) 2026 } 2027 2028 // ListReceivedByAddressIncludeEmpty lists balances by address using the 2029 // specified number of minimum confirmations and including addresses that 2030 // haven't received any payments depending on specified flag. 2031 // 2032 // See ListReceivedByAddress and ListReceivedByAddressMinConf to use defaults. 2033 func (c *Client) ListReceivedByAddressIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAddressResult, error) { 2034 return c.ListReceivedByAddressIncludeEmptyAsync(minConfirms, 2035 includeEmpty).Receive() 2036 } 2037 2038 // FutureRescanBlockchainResult is a future promise to deliver the error result of a 2039 // RescanBlockchainAsync RPC invocation. 2040 type FutureRescanBlockchainResult chan *Response 2041 2042 // Receive waits for the Response promised by the future and returns the result 2043 // of locking or unlocking the unspent output(s). 2044 func (r FutureRescanBlockchainResult) Receive() (*btcjson.RescanBlockchainResult, error) { 2045 res, err := ReceiveFuture(r) 2046 if err != nil { 2047 return nil, err 2048 } 2049 2050 // Unmarshal as an array of listreceivedbyaddress result objects. 2051 var received btcjson.RescanBlockchainResult 2052 err = json.Unmarshal(res, &received) 2053 if err != nil { 2054 return nil, err 2055 } 2056 2057 return &received, nil 2058 } 2059 2060 // RescanBlockchainAsync returns an instance of a type that can be used to get the 2061 // result of the RPC at some future time by invoking the Receive function on the 2062 // returned instance. 2063 // 2064 // See RescanBlockchain for the blocking version and more details. 2065 func (c *Client) RescanBlockchainAsync(startHeight *int32, stopHeight *int32) FutureRescanBlockchainResult { 2066 cmd := btcjson.NewRescanBlockchainCmd(startHeight, stopHeight) 2067 return c.SendCmd(cmd) 2068 } 2069 2070 // RescanBlockchain rescans the local blockchain for wallet related 2071 // transactions from the startHeight to the the inclusive stopHeight. 2072 func (c *Client) RescanBlockchain(startHeight *int32, stopHeight *int32) (*btcjson.RescanBlockchainResult, error) { 2073 return c.RescanBlockchainAsync(startHeight, stopHeight).Receive() 2074 } 2075 2076 // ************************ 2077 // Wallet Locking Functions 2078 // ************************ 2079 2080 // FutureWalletLockResult is a future promise to deliver the result of a 2081 // WalletLockAsync RPC invocation (or an applicable error). 2082 type FutureWalletLockResult chan *Response 2083 2084 // Receive waits for the Response promised by the future and returns the result 2085 // of locking the wallet. 2086 func (r FutureWalletLockResult) Receive() error { 2087 _, err := ReceiveFuture(r) 2088 return err 2089 } 2090 2091 // WalletLockAsync returns an instance of a type that can be used to get the 2092 // result of the RPC at some future time by invoking the Receive function on the 2093 // returned instance. 2094 // 2095 // See WalletLock for the blocking version and more details. 2096 func (c *Client) WalletLockAsync() FutureWalletLockResult { 2097 cmd := btcjson.NewWalletLockCmd() 2098 return c.SendCmd(cmd) 2099 } 2100 2101 // WalletLock locks the wallet by removing the encryption key from memory. 2102 // 2103 // After calling this function, the WalletPassphrase function must be used to 2104 // unlock the wallet prior to calling any other function which requires the 2105 // wallet to be unlocked. 2106 func (c *Client) WalletLock() error { 2107 return c.WalletLockAsync().Receive() 2108 } 2109 2110 // WalletPassphrase unlocks the wallet by using the passphrase to derive the 2111 // decryption key which is then stored in memory for the specified timeout 2112 // (in seconds). 2113 func (c *Client) WalletPassphrase(passphrase string, timeoutSecs int64) error { 2114 cmd := btcjson.NewWalletPassphraseCmd(passphrase, timeoutSecs) 2115 _, err := c.sendCmdAndWait(cmd) 2116 return err 2117 } 2118 2119 // FutureWalletPassphraseChangeResult is a future promise to deliver the result 2120 // of a WalletPassphraseChangeAsync RPC invocation (or an applicable error). 2121 type FutureWalletPassphraseChangeResult chan *Response 2122 2123 // Receive waits for the Response promised by the future and returns the result 2124 // of changing the wallet passphrase. 2125 func (r FutureWalletPassphraseChangeResult) Receive() error { 2126 _, err := ReceiveFuture(r) 2127 return err 2128 } 2129 2130 // WalletPassphraseChangeAsync returns an instance of a type that can be used to 2131 // get the result of the RPC at some future time by invoking the Receive 2132 // function on the returned instance. 2133 // 2134 // See WalletPassphraseChange for the blocking version and more details. 2135 func (c *Client) WalletPassphraseChangeAsync(old, new string) FutureWalletPassphraseChangeResult { 2136 cmd := btcjson.NewWalletPassphraseChangeCmd(old, new) 2137 return c.SendCmd(cmd) 2138 } 2139 2140 // WalletPassphraseChange changes the wallet passphrase from the specified old 2141 // to new passphrase. 2142 func (c *Client) WalletPassphraseChange(old, new string) error { 2143 return c.WalletPassphraseChangeAsync(old, new).Receive() 2144 } 2145 2146 // ************************* 2147 // Message Signing Functions 2148 // ************************* 2149 2150 // FutureSignMessageResult is a future promise to deliver the result of a 2151 // SignMessageAsync RPC invocation (or an applicable error). 2152 type FutureSignMessageResult chan *Response 2153 2154 // Receive waits for the Response promised by the future and returns the message 2155 // signed with the private key of the specified address. 2156 func (r FutureSignMessageResult) Receive() (string, error) { 2157 res, err := ReceiveFuture(r) 2158 if err != nil { 2159 return "", err 2160 } 2161 2162 // Unmarshal result as a string. 2163 var b64 string 2164 err = json.Unmarshal(res, &b64) 2165 if err != nil { 2166 return "", err 2167 } 2168 2169 return b64, nil 2170 } 2171 2172 // SignMessageAsync returns an instance of a type that can be used to get the 2173 // result of the RPC at some future time by invoking the Receive function on the 2174 // returned instance. 2175 // 2176 // See SignMessage for the blocking version and more details. 2177 func (c *Client) SignMessageAsync(address btcutil.Address, message string) FutureSignMessageResult { 2178 addr := address.EncodeAddress() 2179 cmd := btcjson.NewSignMessageCmd(addr, message) 2180 return c.SendCmd(cmd) 2181 } 2182 2183 // SignMessage signs a message with the private key of the specified address. 2184 // 2185 // NOTE: This function requires to the wallet to be unlocked. See the 2186 // WalletPassphrase function for more details. 2187 func (c *Client) SignMessage(address btcutil.Address, message string) (string, error) { 2188 return c.SignMessageAsync(address, message).Receive() 2189 } 2190 2191 // FutureVerifyMessageResult is a future promise to deliver the result of a 2192 // VerifyMessageAsync RPC invocation (or an applicable error). 2193 type FutureVerifyMessageResult chan *Response 2194 2195 // Receive waits for the Response promised by the future and returns whether or 2196 // not the message was successfully verified. 2197 func (r FutureVerifyMessageResult) Receive() (bool, error) { 2198 res, err := ReceiveFuture(r) 2199 if err != nil { 2200 return false, err 2201 } 2202 2203 // Unmarshal result as a boolean. 2204 var verified bool 2205 err = json.Unmarshal(res, &verified) 2206 if err != nil { 2207 return false, err 2208 } 2209 2210 return verified, nil 2211 } 2212 2213 // VerifyMessageAsync returns an instance of a type that can be used to get the 2214 // result of the RPC at some future time by invoking the Receive function on the 2215 // returned instance. 2216 // 2217 // See VerifyMessage for the blocking version and more details. 2218 func (c *Client) VerifyMessageAsync(address btcutil.Address, signature, message string) FutureVerifyMessageResult { 2219 addr := address.EncodeAddress() 2220 cmd := btcjson.NewVerifyMessageCmd(addr, signature, message) 2221 return c.SendCmd(cmd) 2222 } 2223 2224 // VerifyMessage verifies a signed message. 2225 // 2226 // NOTE: This function requires to the wallet to be unlocked. See the 2227 // WalletPassphrase function for more details. 2228 func (c *Client) VerifyMessage(address btcutil.Address, signature, message string) (bool, error) { 2229 return c.VerifyMessageAsync(address, signature, message).Receive() 2230 } 2231 2232 // ********************* 2233 // Dump/Import Functions 2234 // ********************* 2235 2236 // FutureDumpPrivKeyResult is a future promise to deliver the result of a 2237 // DumpPrivKeyAsync RPC invocation (or an applicable error). 2238 type FutureDumpPrivKeyResult chan *Response 2239 2240 // Receive waits for the Response promised by the future and returns the private 2241 // key corresponding to the passed address encoded in the wallet import format 2242 // (WIF) 2243 func (r FutureDumpPrivKeyResult) Receive() (*btcutil.WIF, error) { 2244 res, err := ReceiveFuture(r) 2245 if err != nil { 2246 return nil, err 2247 } 2248 2249 // Unmarshal result as a string. 2250 var privKeyWIF string 2251 err = json.Unmarshal(res, &privKeyWIF) 2252 if err != nil { 2253 return nil, err 2254 } 2255 2256 return btcutil.DecodeWIF(privKeyWIF) 2257 } 2258 2259 // DumpPrivKeyAsync returns an instance of a type that can be used to get the 2260 // result of the RPC at some future time by invoking the Receive function on the 2261 // returned instance. 2262 // 2263 // See DumpPrivKey for the blocking version and more details. 2264 func (c *Client) DumpPrivKeyAsync(address btcutil.Address) FutureDumpPrivKeyResult { 2265 addr := address.EncodeAddress() 2266 cmd := btcjson.NewDumpPrivKeyCmd(addr) 2267 return c.SendCmd(cmd) 2268 } 2269 2270 // DumpPrivKey gets the private key corresponding to the passed address encoded 2271 // in the wallet import format (WIF). 2272 // 2273 // NOTE: This function requires to the wallet to be unlocked. See the 2274 // WalletPassphrase function for more details. 2275 func (c *Client) DumpPrivKey(address btcutil.Address) (*btcutil.WIF, error) { 2276 return c.DumpPrivKeyAsync(address).Receive() 2277 } 2278 2279 // FutureImportAddressResult is a future promise to deliver the result of an 2280 // ImportAddressAsync RPC invocation (or an applicable error). 2281 type FutureImportAddressResult chan *Response 2282 2283 // Receive waits for the Response promised by the future and returns the result 2284 // of importing the passed public address. 2285 func (r FutureImportAddressResult) Receive() error { 2286 _, err := ReceiveFuture(r) 2287 return err 2288 } 2289 2290 // ImportAddressAsync returns an instance of a type that can be used to get the 2291 // result of the RPC at some future time by invoking the Receive function on the 2292 // returned instance. 2293 // 2294 // See ImportAddress for the blocking version and more details. 2295 func (c *Client) ImportAddressAsync(address string) FutureImportAddressResult { 2296 cmd := btcjson.NewImportAddressCmd(address, "", nil) 2297 return c.SendCmd(cmd) 2298 } 2299 2300 // ImportAddress imports the passed public address. 2301 func (c *Client) ImportAddress(address string) error { 2302 return c.ImportAddressAsync(address).Receive() 2303 } 2304 2305 // ImportAddressRescanAsync returns an instance of a type that can be used to get the 2306 // result of the RPC at some future time by invoking the Receive function on the 2307 // returned instance. 2308 // 2309 // See ImportAddress for the blocking version and more details. 2310 func (c *Client) ImportAddressRescanAsync(address string, account string, rescan bool) FutureImportAddressResult { 2311 cmd := btcjson.NewImportAddressCmd(address, account, &rescan) 2312 return c.SendCmd(cmd) 2313 } 2314 2315 // ImportAddressRescan imports the passed public address. When rescan is true, 2316 // the block history is scanned for transactions addressed to provided address. 2317 func (c *Client) ImportAddressRescan(address string, account string, rescan bool) error { 2318 return c.ImportAddressRescanAsync(address, account, rescan).Receive() 2319 } 2320 2321 // FutureImportMultiResult is a future promise to deliver the result of an 2322 // ImportMultiAsync RPC invocation (or an applicable error). 2323 type FutureImportMultiResult chan *Response 2324 2325 // Receive waits for the Response promised by the future and returns the result 2326 // of importing multiple addresses/scripts. 2327 func (r FutureImportMultiResult) Receive() (btcjson.ImportMultiResults, error) { 2328 res, err := ReceiveFuture(r) 2329 if err != nil { 2330 return nil, err 2331 } 2332 2333 var importMultiResults btcjson.ImportMultiResults 2334 err = json.Unmarshal(res, &importMultiResults) 2335 if err != nil { 2336 return nil, err 2337 } 2338 return importMultiResults, nil 2339 } 2340 2341 // ImportMultiAsync returns an instance of a type that can be used to get the result 2342 // of the RPC at some future time by invoking the Receive function on the 2343 // returned instance. 2344 // 2345 // See ImportMulti for the blocking version and more details. 2346 func (c *Client) ImportMultiAsync(requests []btcjson.ImportMultiRequest, options *btcjson.ImportMultiOptions) FutureImportMultiResult { 2347 cmd := btcjson.NewImportMultiCmd(requests, options) 2348 return c.SendCmd(cmd) 2349 } 2350 2351 // ImportMulti imports addresses/scripts, optionally rescanning the blockchain 2352 // from the earliest creation time of the imported scripts. 2353 // 2354 // See btcjson.ImportMultiRequest for details on the requests parameter. 2355 func (c *Client) ImportMulti(requests []btcjson.ImportMultiRequest, options *btcjson.ImportMultiOptions) (btcjson.ImportMultiResults, error) { 2356 return c.ImportMultiAsync(requests, options).Receive() 2357 } 2358 2359 // FutureImportPrivKeyResult is a future promise to deliver the result of an 2360 // ImportPrivKeyAsync RPC invocation (or an applicable error). 2361 type FutureImportPrivKeyResult chan *Response 2362 2363 // Receive waits for the Response promised by the future and returns the result 2364 // of importing the passed private key which must be the wallet import format 2365 // (WIF). 2366 func (r FutureImportPrivKeyResult) Receive() error { 2367 _, err := ReceiveFuture(r) 2368 return err 2369 } 2370 2371 // ImportPrivKeyAsync returns an instance of a type that can be used to get the 2372 // result of the RPC at some future time by invoking the Receive function on the 2373 // returned instance. 2374 // 2375 // See ImportPrivKey for the blocking version and more details. 2376 func (c *Client) ImportPrivKeyAsync(privKeyWIF *btcutil.WIF) FutureImportPrivKeyResult { 2377 wif := "" 2378 if privKeyWIF != nil { 2379 wif = privKeyWIF.String() 2380 } 2381 2382 cmd := btcjson.NewImportPrivKeyCmd(wif, nil, nil) 2383 return c.SendCmd(cmd) 2384 } 2385 2386 // ImportPrivKey imports the passed private key which must be the wallet import 2387 // format (WIF). 2388 func (c *Client) ImportPrivKey(privKeyWIF *btcutil.WIF) error { 2389 return c.ImportPrivKeyAsync(privKeyWIF).Receive() 2390 } 2391 2392 // ImportPrivKeyLabelAsync returns an instance of a type that can be used to get the 2393 // result of the RPC at some future time by invoking the Receive function on the 2394 // returned instance. 2395 // 2396 // See ImportPrivKey for the blocking version and more details. 2397 func (c *Client) ImportPrivKeyLabelAsync(privKeyWIF *btcutil.WIF, label string) FutureImportPrivKeyResult { 2398 wif := "" 2399 if privKeyWIF != nil { 2400 wif = privKeyWIF.String() 2401 } 2402 2403 cmd := btcjson.NewImportPrivKeyCmd(wif, &label, nil) 2404 return c.SendCmd(cmd) 2405 } 2406 2407 // ImportPrivKeyLabel imports the passed private key which must be the wallet import 2408 // format (WIF). It sets the account label to the one provided. 2409 func (c *Client) ImportPrivKeyLabel(privKeyWIF *btcutil.WIF, label string) error { 2410 return c.ImportPrivKeyLabelAsync(privKeyWIF, label).Receive() 2411 } 2412 2413 // ImportPrivKeyRescanAsync returns an instance of a type that can be used to get the 2414 // result of the RPC at some future time by invoking the Receive function on the 2415 // returned instance. 2416 // 2417 // See ImportPrivKey for the blocking version and more details. 2418 func (c *Client) ImportPrivKeyRescanAsync(privKeyWIF *btcutil.WIF, label string, rescan bool) FutureImportPrivKeyResult { 2419 wif := "" 2420 if privKeyWIF != nil { 2421 wif = privKeyWIF.String() 2422 } 2423 2424 cmd := btcjson.NewImportPrivKeyCmd(wif, &label, &rescan) 2425 return c.SendCmd(cmd) 2426 } 2427 2428 // ImportPrivKeyRescan imports the passed private key which must be the wallet import 2429 // format (WIF). It sets the account label to the one provided. When rescan is true, 2430 // the block history is scanned for transactions addressed to provided privKey. 2431 func (c *Client) ImportPrivKeyRescan(privKeyWIF *btcutil.WIF, label string, rescan bool) error { 2432 return c.ImportPrivKeyRescanAsync(privKeyWIF, label, rescan).Receive() 2433 } 2434 2435 // FutureImportPubKeyResult is a future promise to deliver the result of an 2436 // ImportPubKeyAsync RPC invocation (or an applicable error). 2437 type FutureImportPubKeyResult chan *Response 2438 2439 // Receive waits for the Response promised by the future and returns the result 2440 // of importing the passed public key. 2441 func (r FutureImportPubKeyResult) Receive() error { 2442 _, err := ReceiveFuture(r) 2443 return err 2444 } 2445 2446 // ImportPubKeyAsync returns an instance of a type that can be used to get the 2447 // result of the RPC at some future time by invoking the Receive function on the 2448 // returned instance. 2449 // 2450 // See ImportPubKey for the blocking version and more details. 2451 func (c *Client) ImportPubKeyAsync(pubKey string) FutureImportPubKeyResult { 2452 cmd := btcjson.NewImportPubKeyCmd(pubKey, nil) 2453 return c.SendCmd(cmd) 2454 } 2455 2456 // ImportPubKey imports the passed public key. 2457 func (c *Client) ImportPubKey(pubKey string) error { 2458 return c.ImportPubKeyAsync(pubKey).Receive() 2459 } 2460 2461 // ImportPubKeyRescanAsync returns an instance of a type that can be used to get the 2462 // result of the RPC at some future time by invoking the Receive function on the 2463 // returned instance. 2464 // 2465 // See ImportPubKey for the blocking version and more details. 2466 func (c *Client) ImportPubKeyRescanAsync(pubKey string, rescan bool) FutureImportPubKeyResult { 2467 cmd := btcjson.NewImportPubKeyCmd(pubKey, &rescan) 2468 return c.SendCmd(cmd) 2469 } 2470 2471 // ImportPubKeyRescan imports the passed public key. When rescan is true, the 2472 // block history is scanned for transactions addressed to provided pubkey. 2473 func (c *Client) ImportPubKeyRescan(pubKey string, rescan bool) error { 2474 return c.ImportPubKeyRescanAsync(pubKey, rescan).Receive() 2475 } 2476 2477 // *********************** 2478 // Miscellaneous Functions 2479 // *********************** 2480 2481 // NOTE: While getinfo is implemented here (in wallet.go), a btcd chain server 2482 // will respond to getinfo requests as well, excluding any wallet information. 2483 2484 // FutureGetInfoResult is a future promise to deliver the result of a 2485 // GetInfoAsync RPC invocation (or an applicable error). 2486 type FutureGetInfoResult chan *Response 2487 2488 // Receive waits for the Response promised by the future and returns the info 2489 // provided by the server. 2490 func (r FutureGetInfoResult) Receive() (*btcjson.InfoWalletResult, error) { 2491 res, err := ReceiveFuture(r) 2492 if err != nil { 2493 return nil, err 2494 } 2495 2496 // Unmarshal result as a getinfo result object. 2497 var infoRes btcjson.InfoWalletResult 2498 err = json.Unmarshal(res, &infoRes) 2499 if err != nil { 2500 return nil, err 2501 } 2502 2503 return &infoRes, nil 2504 } 2505 2506 // GetInfoAsync returns an instance of a type that can be used to get the result 2507 // of the RPC at some future time by invoking the Receive function on the 2508 // returned instance. 2509 // 2510 // See GetInfo for the blocking version and more details. 2511 func (c *Client) GetInfoAsync() FutureGetInfoResult { 2512 cmd := btcjson.NewGetInfoCmd() 2513 return c.SendCmd(cmd) 2514 } 2515 2516 // GetInfo returns miscellaneous info regarding the RPC server. The returned 2517 // info object may be void of wallet information if the remote server does 2518 // not include wallet functionality. 2519 func (c *Client) GetInfo() (*btcjson.InfoWalletResult, error) { 2520 return c.GetInfoAsync().Receive() 2521 } 2522 2523 // FutureImportPubKeyResult is a future promise to deliver the result of an 2524 // WalletCreateFundedPsbt RPC invocation (or an applicable error). 2525 type FutureWalletCreateFundedPsbtResult chan *Response 2526 2527 // Receive waits for the Response promised by the future and returns the 2528 // partially signed transaction in PSBT format along with the resulting fee 2529 // and change output index. 2530 func (r FutureWalletCreateFundedPsbtResult) Receive() (*btcjson.WalletCreateFundedPsbtResult, error) { 2531 res, err := ReceiveFuture(r) 2532 if err != nil { 2533 return nil, err 2534 } 2535 2536 // Unmarshal result as a getinfo result object. 2537 var psbtRes btcjson.WalletCreateFundedPsbtResult 2538 err = json.Unmarshal(res, &psbtRes) 2539 if err != nil { 2540 return nil, err 2541 } 2542 2543 return &psbtRes, nil 2544 } 2545 2546 // WalletCreateFundedPsbtAsync returns an instance of a type that can be used 2547 // to get the result of the RPC at some future time by invoking the Receive 2548 // function on the returned instance. 2549 // 2550 // See WalletCreateFundedPsbt for the blocking version and more details. 2551 func (c *Client) WalletCreateFundedPsbtAsync( 2552 inputs []btcjson.PsbtInput, outputs []btcjson.PsbtOutput, locktime *uint32, 2553 options *btcjson.WalletCreateFundedPsbtOpts, bip32Derivs *bool, 2554 ) FutureWalletCreateFundedPsbtResult { 2555 cmd := btcjson.NewWalletCreateFundedPsbtCmd(inputs, outputs, locktime, options, bip32Derivs) 2556 return c.SendCmd(cmd) 2557 } 2558 2559 // WalletCreateFundedPsbt creates and funds a transaction in the Partially 2560 // Signed Transaction format. Inputs will be added if supplied inputs are not 2561 // enough. 2562 func (c *Client) WalletCreateFundedPsbt( 2563 inputs []btcjson.PsbtInput, outputs []btcjson.PsbtOutput, locktime *uint32, 2564 options *btcjson.WalletCreateFundedPsbtOpts, bip32Derivs *bool, 2565 ) (*btcjson.WalletCreateFundedPsbtResult, error) { 2566 return c.WalletCreateFundedPsbtAsync(inputs, outputs, locktime, options, bip32Derivs).Receive() 2567 } 2568 2569 // FutureWalletProcessPsbtResult is a future promise to deliver the result of a 2570 // WalletCreateFundedPsb RPC invocation (or an applicable error). 2571 type FutureWalletProcessPsbtResult chan *Response 2572 2573 // Receive waits for the Response promised by the future and returns an updated 2574 // PSBT with signed inputs from the wallet and a boolen indicating if the the 2575 // transaction has a complete set of signatures. 2576 func (r FutureWalletProcessPsbtResult) Receive() (*btcjson.WalletProcessPsbtResult, error) { 2577 res, err := ReceiveFuture(r) 2578 if err != nil { 2579 return nil, err 2580 } 2581 2582 // Unmarshal result as a getinfo result object. 2583 var psbtRes btcjson.WalletProcessPsbtResult 2584 err = json.Unmarshal(res, &psbtRes) 2585 if err != nil { 2586 return nil, err 2587 } 2588 2589 return &psbtRes, nil 2590 } 2591 2592 // WalletProcessPsbtAsync returns an instance of a type that can be used 2593 // to get the result of the RPC at some future time by invoking the Receive 2594 // function on the returned instance. 2595 // 2596 // See WalletProcessPsbt for the blocking version and more details. 2597 func (c *Client) WalletProcessPsbtAsync( 2598 psbt string, sign *bool, sighashType SigHashType, bip32Derivs *bool, 2599 ) FutureWalletProcessPsbtResult { 2600 cmd := btcjson.NewWalletProcessPsbtCmd(psbt, sign, btcjson.String(sighashType.String()), bip32Derivs) 2601 return c.SendCmd(cmd) 2602 } 2603 2604 // WalletProcessPsbt updates a PSBT with input information from our wallet and 2605 // then signs inputs. 2606 func (c *Client) WalletProcessPsbt( 2607 psbt string, sign *bool, sighashType SigHashType, bip32Derivs *bool, 2608 ) (*btcjson.WalletProcessPsbtResult, error) { 2609 return c.WalletProcessPsbtAsync(psbt, sign, sighashType, bip32Derivs).Receive() 2610 } 2611 2612 // FutureGetWalletInfoResult is a future promise to deliver the result of an 2613 // GetWalletInfoAsync RPC invocation (or an applicable error). 2614 type FutureGetWalletInfoResult chan *Response 2615 2616 // Receive waits for the Response promised by the future and returns the result 2617 // of wallet state info. 2618 func (r FutureGetWalletInfoResult) Receive() (*btcjson.GetWalletInfoResult, error) { 2619 res, err := ReceiveFuture(r) 2620 if err != nil { 2621 return nil, err 2622 } 2623 2624 var getWalletInfoResult btcjson.GetWalletInfoResult 2625 err = json.Unmarshal(res, &getWalletInfoResult) 2626 if err != nil { 2627 return nil, err 2628 } 2629 return &getWalletInfoResult, nil 2630 } 2631 2632 // GetWalletInfoAsync returns an instance of a type that can be used to get the result 2633 // of the RPC at some future time by invoking the Receive function on the 2634 // returned instance. 2635 // 2636 // See GetWalletInfo for the blocking version and more details. 2637 func (c *Client) GetWalletInfoAsync() FutureGetWalletInfoResult { 2638 cmd := btcjson.NewGetWalletInfoCmd() 2639 return c.SendCmd(cmd) 2640 } 2641 2642 // GetWalletInfo returns various wallet state info. 2643 func (c *Client) GetWalletInfo() (*btcjson.GetWalletInfoResult, error) { 2644 return c.GetWalletInfoAsync().Receive() 2645 } 2646 2647 // FutureBackupWalletResult is a future promise to deliver the result of an 2648 // BackupWalletAsync RPC invocation (or an applicable error) 2649 type FutureBackupWalletResult chan *Response 2650 2651 // Receive waits for the Response promised by the future 2652 func (r FutureBackupWalletResult) Receive() error { 2653 _, err := ReceiveFuture(r) 2654 return err 2655 } 2656 2657 // BackupWalletAsync returns an instance of a type that can be used to get the result 2658 // of the RPC at some future time by invoking the Receive function on the 2659 // returned instance. 2660 // 2661 // See BackupWallet for the blocking version and more details. 2662 func (c *Client) BackupWalletAsync(destination string) FutureBackupWalletResult { 2663 return c.SendCmd(btcjson.NewBackupWalletCmd(destination)) 2664 } 2665 2666 // BackupWallet safely copies current wallet file to destination, which can 2667 // be a directory or a path with filename 2668 func (c *Client) BackupWallet(destination string) error { 2669 return c.BackupWalletAsync(destination).Receive() 2670 } 2671 2672 // FutureDumpWalletResult is a future promise to deliver the result of an 2673 // DumpWallet RPC invocation (or an applicable error) 2674 type FutureDumpWalletResult chan *Response 2675 2676 // Receive waits for the Response promised by the future 2677 func (r FutureDumpWalletResult) Receive() (*btcjson.DumpWalletResult, error) { 2678 bytes, err := ReceiveFuture(r) 2679 if err != nil { 2680 return nil, err 2681 } 2682 2683 var res btcjson.DumpWalletResult 2684 err = json.Unmarshal(bytes, &res) 2685 return &res, err 2686 } 2687 2688 // DumpWalletAsync returns an instance of a type that can be used to get the result 2689 // of the RPC at some future time by invoking the Receive function on the 2690 // returned instance. 2691 // 2692 // See DumpWalletAsync for the blocking version and more details. 2693 func (c *Client) DumpWalletAsync(destination string) FutureDumpWalletResult { 2694 return c.SendCmd(btcjson.NewDumpWalletCmd(destination)) 2695 } 2696 2697 // DumpWallet dumps all wallet keys in a human-readable format to a server-side file. 2698 func (c *Client) DumpWallet(destination string) (*btcjson.DumpWalletResult, error) { 2699 return c.DumpWalletAsync(destination).Receive() 2700 } 2701 2702 // FutureImportWalletResult is a future promise to deliver the result of an 2703 // ImportWalletAsync RPC invocation (or an applicable error) 2704 type FutureImportWalletResult chan *Response 2705 2706 // Receive waits for the Response promised by the future 2707 func (r FutureImportWalletResult) Receive() error { 2708 _, err := ReceiveFuture(r) 2709 return err 2710 } 2711 2712 // ImportWalletAsync returns an instance of a type that can be used to get the result 2713 // of the RPC at some future time by invoking the Receive function on the 2714 // returned instance. 2715 // 2716 // See ImportWallet for the blocking version and more details. 2717 func (c *Client) ImportWalletAsync(filename string) FutureImportWalletResult { 2718 return c.SendCmd(btcjson.NewImportWalletCmd(filename)) 2719 } 2720 2721 // ImportWallet imports keys from a wallet dump file (see DumpWallet). 2722 func (c *Client) ImportWallet(filename string) error { 2723 return c.ImportWalletAsync(filename).Receive() 2724 } 2725 2726 // FutureUnloadWalletResult is a future promise to deliver the result of an 2727 // UnloadWalletAsync RPC invocation (or an applicable error) 2728 type FutureUnloadWalletResult chan *Response 2729 2730 // Receive waits for the Response promised by the future 2731 func (r FutureUnloadWalletResult) Receive() error { 2732 _, err := ReceiveFuture(r) 2733 return err 2734 } 2735 2736 // UnloadWalletAsync returns an instance of a type that can be used to get the result 2737 // of the RPC at some future time by invoking the Receive function on the 2738 // returned instance. 2739 // 2740 // See UnloadWallet for the blocking version and more details. 2741 func (c *Client) UnloadWalletAsync(walletName *string) FutureUnloadWalletResult { 2742 return c.SendCmd(btcjson.NewUnloadWalletCmd(walletName)) 2743 } 2744 2745 // UnloadWallet unloads the referenced wallet. If the RPC server URL already 2746 // contains the name of the wallet, like http://127.0.0.1:9245/wallet/<walletname>, 2747 // the parameter must be nil, or it'll return an error. 2748 func (c *Client) UnloadWallet(walletName *string) error { 2749 return c.UnloadWalletAsync(walletName).Receive() 2750 } 2751 2752 // FutureLoadWalletResult is a future promise to deliver the result of an 2753 // LoadWalletAsync RPC invocation (or an applicable error) 2754 type FutureLoadWalletResult chan *Response 2755 2756 // Receive waits for the Response promised by the future 2757 func (r FutureLoadWalletResult) Receive() (*btcjson.LoadWalletResult, error) { 2758 bytes, err := ReceiveFuture(r) 2759 if err != nil { 2760 return nil, err 2761 } 2762 var result btcjson.LoadWalletResult 2763 err = json.Unmarshal(bytes, &result) 2764 return &result, err 2765 } 2766 2767 // LoadWalletAsync returns an instance of a type that can be used to get the result 2768 // of the RPC at some future time by invoking the Receive function on the 2769 // returned instance. 2770 // 2771 // See LoadWallet for the blocking version and more details. 2772 func (c *Client) LoadWalletAsync(walletName string) FutureLoadWalletResult { 2773 return c.SendCmd(btcjson.NewLoadWalletCmd(walletName)) 2774 } 2775 2776 // LoadWallet loads a wallet from a wallet file or directory. 2777 func (c *Client) LoadWallet(walletName string) (*btcjson.LoadWalletResult, error) { 2778 return c.LoadWalletAsync(walletName).Receive() 2779 } 2780 2781 // TODO(davec): Implement 2782 // encryptwallet (Won't be supported by btcwallet since it's always encrypted) 2783 // listaddressgroupings (NYI in btcwallet) 2784 // listreceivedbyaccount (NYI in btcwallet)