github.com/palcoin-project/palcd@v1.0.0/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/palcoin-project/palcd/btcjson" 12 "github.com/palcoin-project/palcd/chaincfg" 13 "github.com/palcoin-project/palcd/chaincfg/chainhash" 14 "github.com/palcoin-project/palcd/wire" 15 "github.com/palcoin-project/palcutil" 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 []palcutil.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 []palcutil.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 palcutil.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 palcutil.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 palcutil.Address, amount palcutil.Amount) FutureSendToAddressResult { 540 addr := address.EncodeAddress() 541 cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), nil, nil) 542 return c.sendCmd(cmd) 543 } 544 545 // SendToAddress sends the passed amount to the given address. 546 // 547 // See SendToAddressComment to associate comments with the transaction in the 548 // wallet. The comments are not part of the transaction and are only internal 549 // to the wallet. 550 // 551 // NOTE: This function requires to the wallet to be unlocked. See the 552 // WalletPassphrase function for more details. 553 func (c *Client) SendToAddress(address palcutil.Address, amount palcutil.Amount) (*chainhash.Hash, error) { 554 return c.SendToAddressAsync(address, amount).Receive() 555 } 556 557 // SendToAddressCommentAsync returns an instance of a type that can be used to 558 // get the result of the RPC at some future time by invoking the Receive 559 // function on the returned instance. 560 // 561 // See SendToAddressComment for the blocking version and more details. 562 func (c *Client) SendToAddressCommentAsync(address palcutil.Address, 563 amount palcutil.Amount, comment, 564 commentTo string) FutureSendToAddressResult { 565 566 addr := address.EncodeAddress() 567 cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), &comment, 568 &commentTo) 569 return c.sendCmd(cmd) 570 } 571 572 // SendToAddressComment sends the passed amount to the given address and stores 573 // the provided comment and comment to in the wallet. The comment parameter is 574 // intended to be used for the purpose of the transaction while the commentTo 575 // parameter is intended to be used for who the transaction is being sent to. 576 // 577 // The comments are not part of the transaction and are only internal 578 // to the wallet. 579 // 580 // See SendToAddress to avoid using comments. 581 // 582 // NOTE: This function requires to the wallet to be unlocked. See the 583 // WalletPassphrase function for more details. 584 func (c *Client) SendToAddressComment(address palcutil.Address, amount palcutil.Amount, comment, commentTo string) (*chainhash.Hash, error) { 585 return c.SendToAddressCommentAsync(address, amount, comment, 586 commentTo).Receive() 587 } 588 589 // FutureSendFromResult is a future promise to deliver the result of a 590 // SendFromAsync, SendFromMinConfAsync, or SendFromCommentAsync RPC invocation 591 // (or an applicable error). 592 type FutureSendFromResult chan *response 593 594 // Receive waits for the response promised by the future and returns the hash 595 // of the transaction sending amount to the given address using the provided 596 // account as a source of funds. 597 func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) { 598 res, err := receiveFuture(r) 599 if err != nil { 600 return nil, err 601 } 602 603 // Unmarshal result as a string. 604 var txHash string 605 err = json.Unmarshal(res, &txHash) 606 if err != nil { 607 return nil, err 608 } 609 610 return chainhash.NewHashFromStr(txHash) 611 } 612 613 // SendFromAsync returns an instance of a type that can be used to get the 614 // result of the RPC at some future time by invoking the Receive function on the 615 // returned instance. 616 // 617 // See SendFrom for the blocking version and more details. 618 func (c *Client) SendFromAsync(fromAccount string, toAddress palcutil.Address, amount palcutil.Amount) FutureSendFromResult { 619 addr := toAddress.EncodeAddress() 620 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil, 621 nil, nil) 622 return c.sendCmd(cmd) 623 } 624 625 // SendFrom sends the passed amount to the given address using the provided 626 // account as a source of funds. Only funds with the default number of minimum 627 // confirmations will be used. 628 // 629 // See SendFromMinConf and SendFromComment for different options. 630 // 631 // NOTE: This function requires to the wallet to be unlocked. See the 632 // WalletPassphrase function for more details. 633 func (c *Client) SendFrom(fromAccount string, toAddress palcutil.Address, amount palcutil.Amount) (*chainhash.Hash, error) { 634 return c.SendFromAsync(fromAccount, toAddress, amount).Receive() 635 } 636 637 // SendFromMinConfAsync returns an instance of a type that can be used to get 638 // the result of the RPC at some future time by invoking the Receive function on 639 // the returned instance. 640 // 641 // See SendFromMinConf for the blocking version and more details. 642 func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress palcutil.Address, amount palcutil.Amount, minConfirms int) FutureSendFromResult { 643 addr := toAddress.EncodeAddress() 644 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), 645 &minConfirms, nil, nil) 646 return c.sendCmd(cmd) 647 } 648 649 // SendFromMinConf sends the passed amount to the given address using the 650 // provided account as a source of funds. Only funds with the passed number of 651 // minimum confirmations will be used. 652 // 653 // See SendFrom to use the default number of minimum confirmations and 654 // SendFromComment for additional options. 655 // 656 // NOTE: This function requires to the wallet to be unlocked. See the 657 // WalletPassphrase function for more details. 658 func (c *Client) SendFromMinConf(fromAccount string, toAddress palcutil.Address, amount palcutil.Amount, minConfirms int) (*chainhash.Hash, error) { 659 return c.SendFromMinConfAsync(fromAccount, toAddress, amount, 660 minConfirms).Receive() 661 } 662 663 // SendFromCommentAsync returns an instance of a type that can be used to get 664 // the result of the RPC at some future time by invoking the Receive function on 665 // the returned instance. 666 // 667 // See SendFromComment for the blocking version and more details. 668 func (c *Client) SendFromCommentAsync(fromAccount string, 669 toAddress palcutil.Address, amount palcutil.Amount, minConfirms int, 670 comment, commentTo string) FutureSendFromResult { 671 672 addr := toAddress.EncodeAddress() 673 cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), 674 &minConfirms, &comment, &commentTo) 675 return c.sendCmd(cmd) 676 } 677 678 // SendFromComment sends the passed amount to the given address using the 679 // provided account as a source of funds and stores the provided comment and 680 // comment to in the wallet. The comment parameter is intended to be used for 681 // the purpose of the transaction while the commentTo parameter is intended to 682 // be used for who the transaction is being sent to. Only funds with the passed 683 // number of minimum confirmations will be used. 684 // 685 // See SendFrom and SendFromMinConf to use defaults. 686 // 687 // NOTE: This function requires to the wallet to be unlocked. See the 688 // WalletPassphrase function for more details. 689 func (c *Client) SendFromComment(fromAccount string, toAddress palcutil.Address, 690 amount palcutil.Amount, minConfirms int, 691 comment, commentTo string) (*chainhash.Hash, error) { 692 693 return c.SendFromCommentAsync(fromAccount, toAddress, amount, 694 minConfirms, comment, commentTo).Receive() 695 } 696 697 // FutureSendManyResult is a future promise to deliver the result of a 698 // SendManyAsync, SendManyMinConfAsync, or SendManyCommentAsync RPC invocation 699 // (or an applicable error). 700 type FutureSendManyResult chan *response 701 702 // Receive waits for the response promised by the future and returns the hash 703 // of the transaction sending multiple amounts to multiple addresses using the 704 // provided account as a source of funds. 705 func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) { 706 res, err := receiveFuture(r) 707 if err != nil { 708 return nil, err 709 } 710 711 // Unmashal result as a string. 712 var txHash string 713 err = json.Unmarshal(res, &txHash) 714 if err != nil { 715 return nil, err 716 } 717 718 return chainhash.NewHashFromStr(txHash) 719 } 720 721 // SendManyAsync returns an instance of a type that can be used to get the 722 // result of the RPC at some future time by invoking the Receive function on the 723 // returned instance. 724 // 725 // See SendMany for the blocking version and more details. 726 func (c *Client) SendManyAsync(fromAccount string, amounts map[palcutil.Address]palcutil.Amount) FutureSendManyResult { 727 convertedAmounts := make(map[string]float64, len(amounts)) 728 for addr, amount := range amounts { 729 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 730 } 731 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil) 732 return c.sendCmd(cmd) 733 } 734 735 // SendMany sends multiple amounts to multiple addresses using the provided 736 // account as a source of funds in a single transaction. Only funds with the 737 // default number of minimum confirmations will be used. 738 // 739 // See SendManyMinConf and SendManyComment for different options. 740 // 741 // NOTE: This function requires to the wallet to be unlocked. See the 742 // WalletPassphrase function for more details. 743 func (c *Client) SendMany(fromAccount string, amounts map[palcutil.Address]palcutil.Amount) (*chainhash.Hash, error) { 744 return c.SendManyAsync(fromAccount, amounts).Receive() 745 } 746 747 // SendManyMinConfAsync returns an instance of a type that can be used to get 748 // the result of the RPC at some future time by invoking the Receive function on 749 // the returned instance. 750 // 751 // See SendManyMinConf for the blocking version and more details. 752 func (c *Client) SendManyMinConfAsync(fromAccount string, 753 amounts map[palcutil.Address]palcutil.Amount, 754 minConfirms int) FutureSendManyResult { 755 756 convertedAmounts := make(map[string]float64, len(amounts)) 757 for addr, amount := range amounts { 758 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 759 } 760 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, 761 &minConfirms, nil) 762 return c.sendCmd(cmd) 763 } 764 765 // SendManyMinConf sends multiple amounts to multiple addresses using the 766 // provided account as a source of funds in a single transaction. Only funds 767 // with the passed number of minimum confirmations will be used. 768 // 769 // See SendMany to use the default number of minimum confirmations and 770 // SendManyComment for additional options. 771 // 772 // NOTE: This function requires to the wallet to be unlocked. See the 773 // WalletPassphrase function for more details. 774 func (c *Client) SendManyMinConf(fromAccount string, 775 amounts map[palcutil.Address]palcutil.Amount, 776 minConfirms int) (*chainhash.Hash, error) { 777 778 return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive() 779 } 780 781 // SendManyCommentAsync returns an instance of a type that can be used to get 782 // the result of the RPC at some future time by invoking the Receive function on 783 // the returned instance. 784 // 785 // See SendManyComment for the blocking version and more details. 786 func (c *Client) SendManyCommentAsync(fromAccount string, 787 amounts map[palcutil.Address]palcutil.Amount, minConfirms int, 788 comment string) FutureSendManyResult { 789 790 convertedAmounts := make(map[string]float64, len(amounts)) 791 for addr, amount := range amounts { 792 convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() 793 } 794 cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, 795 &minConfirms, &comment) 796 return c.sendCmd(cmd) 797 } 798 799 // SendManyComment sends multiple amounts to multiple addresses using the 800 // provided account as a source of funds in a single transaction and stores the 801 // provided comment in the wallet. The comment parameter is intended to be used 802 // for the purpose of the transaction Only funds with the passed number of 803 // minimum confirmations will be used. 804 // 805 // See SendMany and SendManyMinConf to use defaults. 806 // 807 // NOTE: This function requires to the wallet to be unlocked. See the 808 // WalletPassphrase function for more details. 809 func (c *Client) SendManyComment(fromAccount string, 810 amounts map[palcutil.Address]palcutil.Amount, minConfirms int, 811 comment string) (*chainhash.Hash, error) { 812 813 return c.SendManyCommentAsync(fromAccount, amounts, minConfirms, 814 comment).Receive() 815 } 816 817 // ************************* 818 // Address/Account Functions 819 // ************************* 820 821 // FutureAddMultisigAddressResult is a future promise to deliver the result of a 822 // AddMultisigAddressAsync RPC invocation (or an applicable error). 823 type FutureAddMultisigAddressResult struct { 824 responseChannel chan *response 825 network *chaincfg.Params 826 } 827 828 // Receive waits for the response promised by the future and returns the 829 // multisignature address that requires the specified number of signatures for 830 // the provided addresses. 831 func (r FutureAddMultisigAddressResult) Receive() (palcutil.Address, error) { 832 res, err := receiveFuture(r.responseChannel) 833 if err != nil { 834 return nil, err 835 } 836 837 // Unmarshal result as a string. 838 var addr string 839 err = json.Unmarshal(res, &addr) 840 if err != nil { 841 return nil, err 842 } 843 844 return palcutil.DecodeAddress(addr, r.network) 845 } 846 847 // AddMultisigAddressAsync returns an instance of a type that can be used to get 848 // the result of the RPC at some future time by invoking the Receive function on 849 // the returned instance. 850 // 851 // See AddMultisigAddress for the blocking version and more details. 852 func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []palcutil.Address, account string) FutureAddMultisigAddressResult { 853 addrs := make([]string, 0, len(addresses)) 854 for _, addr := range addresses { 855 addrs = append(addrs, addr.String()) 856 } 857 858 cmd := btcjson.NewAddMultisigAddressCmd(requiredSigs, addrs, &account) 859 result := FutureAddMultisigAddressResult{ 860 network: c.chainParams, 861 responseChannel: c.sendCmd(cmd), 862 } 863 return result 864 } 865 866 // AddMultisigAddress adds a multisignature address that requires the specified 867 // number of signatures for the provided addresses to the wallet. 868 func (c *Client) AddMultisigAddress(requiredSigs int, addresses []palcutil.Address, account string) (palcutil.Address, error) { 869 return c.AddMultisigAddressAsync(requiredSigs, addresses, account).Receive() 870 } 871 872 // FutureCreateMultisigResult is a future promise to deliver the result of a 873 // CreateMultisigAsync RPC invocation (or an applicable error). 874 type FutureCreateMultisigResult chan *response 875 876 // Receive waits for the response promised by the future and returns the 877 // multisignature address and script needed to redeem it. 878 func (r FutureCreateMultisigResult) Receive() (*btcjson.CreateMultiSigResult, error) { 879 res, err := receiveFuture(r) 880 if err != nil { 881 return nil, err 882 } 883 884 // Unmarshal result as a createmultisig result object. 885 var multisigRes btcjson.CreateMultiSigResult 886 err = json.Unmarshal(res, &multisigRes) 887 if err != nil { 888 return nil, err 889 } 890 891 return &multisigRes, nil 892 } 893 894 // CreateMultisigAsync returns an instance of a type that can be used to get 895 // the result of the RPC at some future time by invoking the Receive function on 896 // the returned instance. 897 // 898 // See CreateMultisig for the blocking version and more details. 899 func (c *Client) CreateMultisigAsync(requiredSigs int, addresses []palcutil.Address) FutureCreateMultisigResult { 900 addrs := make([]string, 0, len(addresses)) 901 for _, addr := range addresses { 902 addrs = append(addrs, addr.String()) 903 } 904 905 cmd := btcjson.NewCreateMultisigCmd(requiredSigs, addrs) 906 return c.sendCmd(cmd) 907 } 908 909 // CreateMultisig creates a multisignature address that requires the specified 910 // number of signatures for the provided addresses and returns the 911 // multisignature address and script needed to redeem it. 912 func (c *Client) CreateMultisig(requiredSigs int, addresses []palcutil.Address) (*btcjson.CreateMultiSigResult, error) { 913 return c.CreateMultisigAsync(requiredSigs, addresses).Receive() 914 } 915 916 // FutureCreateNewAccountResult is a future promise to deliver the result of a 917 // CreateNewAccountAsync RPC invocation (or an applicable error). 918 type FutureCreateNewAccountResult chan *response 919 920 // Receive waits for the response promised by the future and returns the 921 // result of creating new account. 922 func (r FutureCreateNewAccountResult) Receive() error { 923 _, err := receiveFuture(r) 924 return err 925 } 926 927 // CreateNewAccountAsync returns an instance of a type that can be used to get the 928 // result of the RPC at some future time by invoking the Receive function on the 929 // returned instance. 930 // 931 // See CreateNewAccount for the blocking version and more details. 932 func (c *Client) CreateNewAccountAsync(account string) FutureCreateNewAccountResult { 933 cmd := btcjson.NewCreateNewAccountCmd(account) 934 return c.sendCmd(cmd) 935 } 936 937 // CreateNewAccount creates a new wallet account. 938 func (c *Client) CreateNewAccount(account string) error { 939 return c.CreateNewAccountAsync(account).Receive() 940 } 941 942 // FutureCreateWalletResult is a future promise to deliver the result of a 943 // CreateWalletAsync RPC invocation (or an applicable error). 944 type FutureCreateWalletResult chan *response 945 946 // Receive waits for the response promised by the future and returns the 947 // result of creating a new wallet. 948 func (r FutureCreateWalletResult) Receive() (*btcjson.CreateWalletResult, error) { 949 res, err := receiveFuture(r) 950 if err != nil { 951 return nil, err 952 } 953 954 var createWalletResult btcjson.CreateWalletResult 955 err = json.Unmarshal(res, &createWalletResult) 956 if err != nil { 957 return nil, err 958 } 959 return &createWalletResult, nil 960 } 961 962 // CreateWalletOpt defines a functional-option to be used with CreateWallet 963 // method. 964 type CreateWalletOpt func(*btcjson.CreateWalletCmd) 965 966 // WithCreateWalletDisablePrivateKeys disables the possibility of private keys 967 // to be used with a wallet created using the CreateWallet method. Using this 968 // option will make the wallet watch-only. 969 func WithCreateWalletDisablePrivateKeys() CreateWalletOpt { 970 return func(c *btcjson.CreateWalletCmd) { 971 c.DisablePrivateKeys = btcjson.Bool(true) 972 } 973 } 974 975 // WithCreateWalletBlank specifies creation of a blank wallet. 976 func WithCreateWalletBlank() CreateWalletOpt { 977 return func(c *btcjson.CreateWalletCmd) { 978 c.Blank = btcjson.Bool(true) 979 } 980 } 981 982 // WithCreateWalletPassphrase specifies a passphrase to encrypt the wallet 983 // with. 984 func WithCreateWalletPassphrase(value string) CreateWalletOpt { 985 return func(c *btcjson.CreateWalletCmd) { 986 c.Passphrase = btcjson.String(value) 987 } 988 } 989 990 // WithCreateWalletAvoidReuse specifies keeping track of coin reuse, and 991 // treat dirty and clean coins differently with privacy considerations in mind. 992 func WithCreateWalletAvoidReuse() CreateWalletOpt { 993 return func(c *btcjson.CreateWalletCmd) { 994 c.AvoidReuse = btcjson.Bool(true) 995 } 996 } 997 998 // CreateWalletAsync returns an instance of a type that can be used to get the 999 // result of the RPC at some future time by invoking the Receive function on the 1000 // returned instance. 1001 // 1002 // See CreateWallet for the blocking version and more details. 1003 func (c *Client) CreateWalletAsync(name string, opts ...CreateWalletOpt) FutureCreateWalletResult { 1004 cmd := btcjson.NewCreateWalletCmd(name, nil, nil, nil, nil) 1005 1006 // Apply each specified option to mutate the default command. 1007 for _, opt := range opts { 1008 opt(cmd) 1009 } 1010 1011 return c.sendCmd(cmd) 1012 } 1013 1014 // CreateWallet creates a new wallet account, with the possibility to use 1015 // private keys. 1016 // 1017 // Optional parameters can be specified using functional-options pattern. The 1018 // following functions are available: 1019 // * WithCreateWalletDisablePrivateKeys 1020 // * WithCreateWalletBlank 1021 // * WithCreateWalletPassphrase 1022 // * WithCreateWalletAvoidReuse 1023 func (c *Client) CreateWallet(name string, opts ...CreateWalletOpt) (*btcjson.CreateWalletResult, error) { 1024 return c.CreateWalletAsync(name, opts...).Receive() 1025 } 1026 1027 // FutureGetAddressInfoResult is a future promise to deliver the result of an 1028 // GetAddressInfoAsync RPC invocation (or an applicable error). 1029 type FutureGetAddressInfoResult chan *response 1030 1031 // Receive waits for the response promised by the future and returns the information 1032 // about the given bitcoin address. 1033 func (r FutureGetAddressInfoResult) Receive() (*btcjson.GetAddressInfoResult, error) { 1034 res, err := receiveFuture(r) 1035 if err != nil { 1036 return nil, err 1037 } 1038 1039 var getAddressInfoResult btcjson.GetAddressInfoResult 1040 err = json.Unmarshal(res, &getAddressInfoResult) 1041 if err != nil { 1042 return nil, err 1043 } 1044 return &getAddressInfoResult, nil 1045 } 1046 1047 // GetAddressInfoAsync returns an instance of a type that can be used to get the result 1048 // of the RPC at some future time by invoking the Receive function on the 1049 // returned instance. 1050 // 1051 // See GetAddressInfo for the blocking version and more details. 1052 func (c *Client) GetAddressInfoAsync(address string) FutureGetAddressInfoResult { 1053 cmd := btcjson.NewGetAddressInfoCmd(address) 1054 return c.sendCmd(cmd) 1055 } 1056 1057 // GetAddressInfo returns information about the given bitcoin address. 1058 func (c *Client) GetAddressInfo(address string) (*btcjson.GetAddressInfoResult, error) { 1059 return c.GetAddressInfoAsync(address).Receive() 1060 } 1061 1062 // FutureGetNewAddressResult is a future promise to deliver the result of a 1063 // GetNewAddressAsync RPC invocation (or an applicable error). 1064 type FutureGetNewAddressResult struct { 1065 responseChannel chan *response 1066 network *chaincfg.Params 1067 } 1068 1069 // Receive waits for the response promised by the future and returns a new 1070 // address. 1071 func (r FutureGetNewAddressResult) Receive() (palcutil.Address, error) { 1072 res, err := receiveFuture(r.responseChannel) 1073 if err != nil { 1074 return nil, err 1075 } 1076 1077 // Unmarshal result as a string. 1078 var addr string 1079 err = json.Unmarshal(res, &addr) 1080 if err != nil { 1081 return nil, err 1082 } 1083 1084 return palcutil.DecodeAddress(addr, r.network) 1085 } 1086 1087 // GetNewAddressAsync returns an instance of a type that can be used to get the 1088 // result of the RPC at some future time by invoking the Receive function on the 1089 // returned instance. 1090 // 1091 // See GetNewAddress for the blocking version and more details. 1092 func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult { 1093 cmd := btcjson.NewGetNewAddressCmd(&account) 1094 result := FutureGetNewAddressResult{ 1095 network: c.chainParams, 1096 responseChannel: c.sendCmd(cmd), 1097 } 1098 return result 1099 } 1100 1101 // GetNewAddress returns a new address, and decodes based on the client's 1102 // chain params. 1103 func (c *Client) GetNewAddress(account string) (palcutil.Address, error) { 1104 return c.GetNewAddressAsync(account).Receive() 1105 } 1106 1107 // FutureGetRawChangeAddressResult is a future promise to deliver the result of 1108 // a GetRawChangeAddressAsync RPC invocation (or an applicable error). 1109 type FutureGetRawChangeAddressResult struct { 1110 responseChannel chan *response 1111 network *chaincfg.Params 1112 } 1113 1114 // Receive waits for the response promised by the future and returns a new 1115 // address for receiving change that will be associated with the provided 1116 // account. Note that this is only for raw transactions and NOT for normal use. 1117 func (r FutureGetRawChangeAddressResult) Receive() (palcutil.Address, error) { 1118 res, err := receiveFuture(r.responseChannel) 1119 if err != nil { 1120 return nil, err 1121 } 1122 1123 // Unmarshal result as a string. 1124 var addr string 1125 err = json.Unmarshal(res, &addr) 1126 if err != nil { 1127 return nil, err 1128 } 1129 1130 return palcutil.DecodeAddress(addr, r.network) 1131 } 1132 1133 // GetRawChangeAddressAsync returns an instance of a type that can be used to 1134 // get the result of the RPC at some future time by invoking the Receive 1135 // function on the returned instance. 1136 // 1137 // See GetRawChangeAddress for the blocking version and more details. 1138 func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddressResult { 1139 cmd := btcjson.NewGetRawChangeAddressCmd(&account) 1140 result := FutureGetRawChangeAddressResult{ 1141 network: c.chainParams, 1142 responseChannel: c.sendCmd(cmd), 1143 } 1144 return result 1145 } 1146 1147 // GetRawChangeAddress returns a new address for receiving change that will be 1148 // associated with the provided account. Note that this is only for raw 1149 // transactions and NOT for normal use. 1150 func (c *Client) GetRawChangeAddress(account string) (palcutil.Address, error) { 1151 return c.GetRawChangeAddressAsync(account).Receive() 1152 } 1153 1154 // FutureAddWitnessAddressResult is a future promise to deliver the result of 1155 // a AddWitnessAddressAsync RPC invocation (or an applicable error). 1156 type FutureAddWitnessAddressResult struct { 1157 responseChannel chan *response 1158 network *chaincfg.Params 1159 } 1160 1161 // Receive waits for the response promised by the future and returns the new 1162 // address. 1163 func (r FutureAddWitnessAddressResult) Receive() (palcutil.Address, error) { 1164 res, err := receiveFuture(r.responseChannel) 1165 if err != nil { 1166 return nil, err 1167 } 1168 1169 // Unmarshal result as a string. 1170 var addr string 1171 err = json.Unmarshal(res, &addr) 1172 if err != nil { 1173 return nil, err 1174 } 1175 1176 return palcutil.DecodeAddress(addr, r.network) 1177 } 1178 1179 // AddWitnessAddressAsync returns an instance of a type that can be used to get 1180 // the result of the RPC at some future time by invoking the Receive function on 1181 // the returned instance. 1182 // 1183 // See AddWitnessAddress for the blocking version and more details. 1184 func (c *Client) AddWitnessAddressAsync(address string) FutureAddWitnessAddressResult { 1185 cmd := btcjson.NewAddWitnessAddressCmd(address) 1186 response := FutureAddWitnessAddressResult{ 1187 network: c.chainParams, 1188 responseChannel: c.sendCmd(cmd), 1189 } 1190 return response 1191 } 1192 1193 // AddWitnessAddress adds a witness address for a script and returns the new 1194 // address (P2SH of the witness script). 1195 func (c *Client) AddWitnessAddress(address string) (palcutil.Address, error) { 1196 return c.AddWitnessAddressAsync(address).Receive() 1197 } 1198 1199 // FutureGetAccountAddressResult is a future promise to deliver the result of a 1200 // GetAccountAddressAsync RPC invocation (or an applicable error). 1201 type FutureGetAccountAddressResult struct { 1202 responseChannel chan *response 1203 network *chaincfg.Params 1204 } 1205 1206 // Receive waits for the response promised by the future and returns the current 1207 // Bitcoin address for receiving payments to the specified account. 1208 func (r FutureGetAccountAddressResult) Receive() (palcutil.Address, error) { 1209 res, err := receiveFuture(r.responseChannel) 1210 if err != nil { 1211 return nil, err 1212 } 1213 1214 // Unmarshal result as a string. 1215 var addr string 1216 err = json.Unmarshal(res, &addr) 1217 if err != nil { 1218 return nil, err 1219 } 1220 1221 return palcutil.DecodeAddress(addr, r.network) 1222 } 1223 1224 // GetAccountAddressAsync returns an instance of a type that can be used to get 1225 // the result of the RPC at some future time by invoking the Receive function on 1226 // the returned instance. 1227 // 1228 // See GetAccountAddress for the blocking version and more details. 1229 func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressResult { 1230 cmd := btcjson.NewGetAccountAddressCmd(account) 1231 result := FutureGetAccountAddressResult{ 1232 network: c.chainParams, 1233 responseChannel: c.sendCmd(cmd), 1234 } 1235 return result 1236 } 1237 1238 // GetAccountAddress returns the current Bitcoin address for receiving payments 1239 // to the specified account. 1240 func (c *Client) GetAccountAddress(account string) (palcutil.Address, error) { 1241 return c.GetAccountAddressAsync(account).Receive() 1242 } 1243 1244 // FutureGetAccountResult is a future promise to deliver the result of a 1245 // GetAccountAsync RPC invocation (or an applicable error). 1246 type FutureGetAccountResult chan *response 1247 1248 // Receive waits for the response promised by the future and returns the account 1249 // associated with the passed address. 1250 func (r FutureGetAccountResult) Receive() (string, error) { 1251 res, err := receiveFuture(r) 1252 if err != nil { 1253 return "", err 1254 } 1255 1256 // Unmarshal result as a string. 1257 var account string 1258 err = json.Unmarshal(res, &account) 1259 if err != nil { 1260 return "", err 1261 } 1262 1263 return account, nil 1264 } 1265 1266 // GetAccountAsync returns an instance of a type that can be used to get the 1267 // result of the RPC at some future time by invoking the Receive function on the 1268 // returned instance. 1269 // 1270 // See GetAccount for the blocking version and more details. 1271 func (c *Client) GetAccountAsync(address palcutil.Address) FutureGetAccountResult { 1272 addr := address.EncodeAddress() 1273 cmd := btcjson.NewGetAccountCmd(addr) 1274 return c.sendCmd(cmd) 1275 } 1276 1277 // GetAccount returns the account associated with the passed address. 1278 func (c *Client) GetAccount(address palcutil.Address) (string, error) { 1279 return c.GetAccountAsync(address).Receive() 1280 } 1281 1282 // FutureSetAccountResult is a future promise to deliver the result of a 1283 // SetAccountAsync RPC invocation (or an applicable error). 1284 type FutureSetAccountResult chan *response 1285 1286 // Receive waits for the response promised by the future and returns the result 1287 // of setting the account to be associated with the passed address. 1288 func (r FutureSetAccountResult) Receive() error { 1289 _, err := receiveFuture(r) 1290 return err 1291 } 1292 1293 // SetAccountAsync returns an instance of a type that can be used to get the 1294 // result of the RPC at some future time by invoking the Receive function on the 1295 // returned instance. 1296 // 1297 // See SetAccount for the blocking version and more details. 1298 func (c *Client) SetAccountAsync(address palcutil.Address, account string) FutureSetAccountResult { 1299 addr := address.EncodeAddress() 1300 cmd := btcjson.NewSetAccountCmd(addr, account) 1301 return c.sendCmd(cmd) 1302 } 1303 1304 // SetAccount sets the account associated with the passed address. 1305 func (c *Client) SetAccount(address palcutil.Address, account string) error { 1306 return c.SetAccountAsync(address, account).Receive() 1307 } 1308 1309 // FutureGetAddressesByAccountResult is a future promise to deliver the result 1310 // of a GetAddressesByAccountAsync RPC invocation (or an applicable error). 1311 type FutureGetAddressesByAccountResult struct { 1312 responseChannel chan *response 1313 network *chaincfg.Params 1314 } 1315 1316 // Receive waits for the response promised by the future and returns the list of 1317 // addresses associated with the passed account. 1318 func (r FutureGetAddressesByAccountResult) Receive() ([]palcutil.Address, error) { 1319 res, err := receiveFuture(r.responseChannel) 1320 if err != nil { 1321 return nil, err 1322 } 1323 1324 // Unmashal result as an array of string. 1325 var addrStrings []string 1326 err = json.Unmarshal(res, &addrStrings) 1327 if err != nil { 1328 return nil, err 1329 } 1330 1331 addresses := make([]palcutil.Address, len(addrStrings)) 1332 for i, addrString := range addrStrings { 1333 addresses[i], err = palcutil.DecodeAddress(addrString, r.network) 1334 if err != nil { 1335 return nil, err 1336 } 1337 } 1338 1339 return addresses, nil 1340 } 1341 1342 // GetAddressesByAccountAsync returns an instance of a type that can be used to 1343 // get the result of the RPC at some future time by invoking the Receive 1344 // function on the returned instance. 1345 // 1346 // See GetAddressesByAccount for the blocking version and more details. 1347 func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesByAccountResult { 1348 cmd := btcjson.NewGetAddressesByAccountCmd(account) 1349 result := FutureGetAddressesByAccountResult{ 1350 network: c.chainParams, 1351 responseChannel: c.sendCmd(cmd), 1352 } 1353 return result 1354 } 1355 1356 // GetAddressesByAccount returns the list of addresses associated with the 1357 // passed account. 1358 func (c *Client) GetAddressesByAccount(account string) ([]palcutil.Address, error) { 1359 return c.GetAddressesByAccountAsync(account).Receive() 1360 } 1361 1362 // FutureMoveResult is a future promise to deliver the result of a MoveAsync, 1363 // MoveMinConfAsync, or MoveCommentAsync RPC invocation (or an applicable 1364 // error). 1365 type FutureMoveResult chan *response 1366 1367 // Receive waits for the response promised by the future and returns the result 1368 // of the move operation. 1369 func (r FutureMoveResult) Receive() (bool, error) { 1370 res, err := receiveFuture(r) 1371 if err != nil { 1372 return false, err 1373 } 1374 1375 // Unmarshal result as a boolean. 1376 var moveResult bool 1377 err = json.Unmarshal(res, &moveResult) 1378 if err != nil { 1379 return false, err 1380 } 1381 1382 return moveResult, nil 1383 } 1384 1385 // MoveAsync returns an instance of a type that can be used to get the result of 1386 // the RPC at some future time by invoking the Receive function on the returned 1387 // instance. 1388 // 1389 // See Move for the blocking version and more details. 1390 func (c *Client) MoveAsync(fromAccount, toAccount string, amount palcutil.Amount) FutureMoveResult { 1391 cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(), nil, 1392 nil) 1393 return c.sendCmd(cmd) 1394 } 1395 1396 // Move moves specified amount from one account in your wallet to another. Only 1397 // funds with the default number of minimum confirmations will be used. 1398 // 1399 // See MoveMinConf and MoveComment for different options. 1400 func (c *Client) Move(fromAccount, toAccount string, amount palcutil.Amount) (bool, error) { 1401 return c.MoveAsync(fromAccount, toAccount, amount).Receive() 1402 } 1403 1404 // MoveMinConfAsync returns an instance of a type that can be used to get the 1405 // result of the RPC at some future time by invoking the Receive function on the 1406 // returned instance. 1407 // 1408 // See MoveMinConf for the blocking version and more details. 1409 func (c *Client) MoveMinConfAsync(fromAccount, toAccount string, 1410 amount palcutil.Amount, minConfirms int) FutureMoveResult { 1411 1412 cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(), 1413 &minConfirms, nil) 1414 return c.sendCmd(cmd) 1415 } 1416 1417 // MoveMinConf moves specified amount from one account in your wallet to 1418 // another. Only funds with the passed number of minimum confirmations will be 1419 // used. 1420 // 1421 // See Move to use the default number of minimum confirmations and MoveComment 1422 // for additional options. 1423 func (c *Client) MoveMinConf(fromAccount, toAccount string, amount palcutil.Amount, minConf int) (bool, error) { 1424 return c.MoveMinConfAsync(fromAccount, toAccount, amount, minConf).Receive() 1425 } 1426 1427 // MoveCommentAsync returns an instance of a type that can be used to get the 1428 // result of the RPC at some future time by invoking the Receive function on the 1429 // returned instance. 1430 // 1431 // See MoveComment for the blocking version and more details. 1432 func (c *Client) MoveCommentAsync(fromAccount, toAccount string, 1433 amount palcutil.Amount, minConfirms int, comment string) FutureMoveResult { 1434 1435 cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(), 1436 &minConfirms, &comment) 1437 return c.sendCmd(cmd) 1438 } 1439 1440 // MoveComment moves specified amount from one account in your wallet to 1441 // another and stores the provided comment in the wallet. The comment 1442 // parameter is only available in the wallet. Only funds with the passed number 1443 // of minimum confirmations will be used. 1444 // 1445 // See Move and MoveMinConf to use defaults. 1446 func (c *Client) MoveComment(fromAccount, toAccount string, amount palcutil.Amount, 1447 minConf int, comment string) (bool, error) { 1448 1449 return c.MoveCommentAsync(fromAccount, toAccount, amount, minConf, 1450 comment).Receive() 1451 } 1452 1453 // FutureRenameAccountResult is a future promise to deliver the result of a 1454 // RenameAccountAsync RPC invocation (or an applicable error). 1455 type FutureRenameAccountResult chan *response 1456 1457 // Receive waits for the response promised by the future and returns the 1458 // result of creating new account. 1459 func (r FutureRenameAccountResult) Receive() error { 1460 _, err := receiveFuture(r) 1461 return err 1462 } 1463 1464 // RenameAccountAsync returns an instance of a type that can be used to get the 1465 // result of the RPC at some future time by invoking the Receive function on the 1466 // returned instance. 1467 // 1468 // See RenameAccount for the blocking version and more details. 1469 func (c *Client) RenameAccountAsync(oldAccount, newAccount string) FutureRenameAccountResult { 1470 cmd := btcjson.NewRenameAccountCmd(oldAccount, newAccount) 1471 return c.sendCmd(cmd) 1472 } 1473 1474 // RenameAccount creates a new wallet account. 1475 func (c *Client) RenameAccount(oldAccount, newAccount string) error { 1476 return c.RenameAccountAsync(oldAccount, newAccount).Receive() 1477 } 1478 1479 // FutureValidateAddressResult is a future promise to deliver the result of a 1480 // ValidateAddressAsync RPC invocation (or an applicable error). 1481 type FutureValidateAddressResult chan *response 1482 1483 // Receive waits for the response promised by the future and returns information 1484 // about the given bitcoin address. 1485 func (r FutureValidateAddressResult) Receive() (*btcjson.ValidateAddressWalletResult, error) { 1486 res, err := receiveFuture(r) 1487 if err != nil { 1488 return nil, err 1489 } 1490 1491 // Unmarshal result as a validateaddress result object. 1492 var addrResult btcjson.ValidateAddressWalletResult 1493 err = json.Unmarshal(res, &addrResult) 1494 if err != nil { 1495 return nil, err 1496 } 1497 1498 return &addrResult, nil 1499 } 1500 1501 // ValidateAddressAsync returns an instance of a type that can be used to get 1502 // the result of the RPC at some future time by invoking the Receive function on 1503 // the returned instance. 1504 // 1505 // See ValidateAddress for the blocking version and more details. 1506 func (c *Client) ValidateAddressAsync(address palcutil.Address) FutureValidateAddressResult { 1507 addr := address.EncodeAddress() 1508 cmd := btcjson.NewValidateAddressCmd(addr) 1509 return c.sendCmd(cmd) 1510 } 1511 1512 // ValidateAddress returns information about the given bitcoin address. 1513 func (c *Client) ValidateAddress(address palcutil.Address) (*btcjson.ValidateAddressWalletResult, error) { 1514 return c.ValidateAddressAsync(address).Receive() 1515 } 1516 1517 // FutureKeyPoolRefillResult is a future promise to deliver the result of a 1518 // KeyPoolRefillAsync RPC invocation (or an applicable error). 1519 type FutureKeyPoolRefillResult chan *response 1520 1521 // Receive waits for the response promised by the future and returns the result 1522 // of refilling the key pool. 1523 func (r FutureKeyPoolRefillResult) Receive() error { 1524 _, err := receiveFuture(r) 1525 return err 1526 } 1527 1528 // KeyPoolRefillAsync returns an instance of a type that can be used to get the 1529 // result of the RPC at some future time by invoking the Receive function on the 1530 // returned instance. 1531 // 1532 // See KeyPoolRefill for the blocking version and more details. 1533 func (c *Client) KeyPoolRefillAsync() FutureKeyPoolRefillResult { 1534 cmd := btcjson.NewKeyPoolRefillCmd(nil) 1535 return c.sendCmd(cmd) 1536 } 1537 1538 // KeyPoolRefill fills the key pool as necessary to reach the default size. 1539 // 1540 // See KeyPoolRefillSize to override the size of the key pool. 1541 func (c *Client) KeyPoolRefill() error { 1542 return c.KeyPoolRefillAsync().Receive() 1543 } 1544 1545 // KeyPoolRefillSizeAsync returns an instance of a type that can be used to get 1546 // the result of the RPC at some future time by invoking the Receive function on 1547 // the returned instance. 1548 // 1549 // See KeyPoolRefillSize for the blocking version and more details. 1550 func (c *Client) KeyPoolRefillSizeAsync(newSize uint) FutureKeyPoolRefillResult { 1551 cmd := btcjson.NewKeyPoolRefillCmd(&newSize) 1552 return c.sendCmd(cmd) 1553 } 1554 1555 // KeyPoolRefillSize fills the key pool as necessary to reach the specified 1556 // size. 1557 func (c *Client) KeyPoolRefillSize(newSize uint) error { 1558 return c.KeyPoolRefillSizeAsync(newSize).Receive() 1559 } 1560 1561 // ************************ 1562 // Amount/Balance Functions 1563 // ************************ 1564 1565 // FutureListAccountsResult is a future promise to deliver the result of a 1566 // ListAccountsAsync or ListAccountsMinConfAsync RPC invocation (or an 1567 // applicable error). 1568 type FutureListAccountsResult chan *response 1569 1570 // Receive waits for the response promised by the future and returns returns a 1571 // map of account names and their associated balances. 1572 func (r FutureListAccountsResult) Receive() (map[string]palcutil.Amount, error) { 1573 res, err := receiveFuture(r) 1574 if err != nil { 1575 return nil, err 1576 } 1577 1578 // Unmarshal result as a json object. 1579 var accounts map[string]float64 1580 err = json.Unmarshal(res, &accounts) 1581 if err != nil { 1582 return nil, err 1583 } 1584 1585 accountsMap := make(map[string]palcutil.Amount) 1586 for k, v := range accounts { 1587 amount, err := palcutil.NewAmount(v) 1588 if err != nil { 1589 return nil, err 1590 } 1591 1592 accountsMap[k] = amount 1593 } 1594 1595 return accountsMap, nil 1596 } 1597 1598 // ListAccountsAsync returns an instance of a type that can be used to get the 1599 // result of the RPC at some future time by invoking the Receive function on the 1600 // returned instance. 1601 // 1602 // See ListAccounts for the blocking version and more details. 1603 func (c *Client) ListAccountsAsync() FutureListAccountsResult { 1604 cmd := btcjson.NewListAccountsCmd(nil) 1605 return c.sendCmd(cmd) 1606 } 1607 1608 // ListAccounts returns a map of account names and their associated balances 1609 // using the default number of minimum confirmations. 1610 // 1611 // See ListAccountsMinConf to override the minimum number of confirmations. 1612 func (c *Client) ListAccounts() (map[string]palcutil.Amount, error) { 1613 return c.ListAccountsAsync().Receive() 1614 } 1615 1616 // ListAccountsMinConfAsync returns an instance of a type that can be used to 1617 // get the result of the RPC at some future time by invoking the Receive 1618 // function on the returned instance. 1619 // 1620 // See ListAccountsMinConf for the blocking version and more details. 1621 func (c *Client) ListAccountsMinConfAsync(minConfirms int) FutureListAccountsResult { 1622 cmd := btcjson.NewListAccountsCmd(&minConfirms) 1623 return c.sendCmd(cmd) 1624 } 1625 1626 // ListAccountsMinConf returns a map of account names and their associated 1627 // balances using the specified number of minimum confirmations. 1628 // 1629 // See ListAccounts to use the default minimum number of confirmations. 1630 func (c *Client) ListAccountsMinConf(minConfirms int) (map[string]palcutil.Amount, error) { 1631 return c.ListAccountsMinConfAsync(minConfirms).Receive() 1632 } 1633 1634 // FutureGetBalanceResult is a future promise to deliver the result of a 1635 // GetBalanceAsync or GetBalanceMinConfAsync RPC invocation (or an applicable 1636 // error). 1637 type FutureGetBalanceResult chan *response 1638 1639 // Receive waits for the response promised by the future and returns the 1640 // available balance from the server for the specified account. 1641 func (r FutureGetBalanceResult) Receive() (palcutil.Amount, error) { 1642 res, err := receiveFuture(r) 1643 if err != nil { 1644 return 0, err 1645 } 1646 1647 // Unmarshal result as a floating point number. 1648 var balance float64 1649 err = json.Unmarshal(res, &balance) 1650 if err != nil { 1651 return 0, err 1652 } 1653 1654 amount, err := palcutil.NewAmount(balance) 1655 if err != nil { 1656 return 0, err 1657 } 1658 1659 return amount, nil 1660 } 1661 1662 // FutureGetBalanceParseResult is same as FutureGetBalanceResult except 1663 // that the result is expected to be a string which is then parsed into 1664 // a float64 value 1665 // This is required for compatibility with servers like blockchain.info 1666 type FutureGetBalanceParseResult chan *response 1667 1668 // Receive waits for the response promised by the future and returns the 1669 // available balance from the server for the specified account. 1670 func (r FutureGetBalanceParseResult) Receive() (palcutil.Amount, error) { 1671 res, err := receiveFuture(r) 1672 if err != nil { 1673 return 0, err 1674 } 1675 1676 // Unmarshal result as a string 1677 var balanceString string 1678 err = json.Unmarshal(res, &balanceString) 1679 if err != nil { 1680 return 0, err 1681 } 1682 1683 balance, err := strconv.ParseFloat(balanceString, 64) 1684 if err != nil { 1685 return 0, err 1686 } 1687 amount, err := palcutil.NewAmount(balance) 1688 if err != nil { 1689 return 0, err 1690 } 1691 1692 return amount, nil 1693 } 1694 1695 // GetBalanceAsync returns an instance of a type that can be used to get the 1696 // result of the RPC at some future time by invoking the Receive function on the 1697 // returned instance. 1698 // 1699 // See GetBalance for the blocking version and more details. 1700 func (c *Client) GetBalanceAsync(account string) FutureGetBalanceResult { 1701 cmd := btcjson.NewGetBalanceCmd(&account, nil) 1702 return c.sendCmd(cmd) 1703 } 1704 1705 // GetBalance returns the available balance from the server for the specified 1706 // account using the default number of minimum confirmations. The account may 1707 // be "*" for all accounts. 1708 // 1709 // See GetBalanceMinConf to override the minimum number of confirmations. 1710 func (c *Client) GetBalance(account string) (palcutil.Amount, error) { 1711 return c.GetBalanceAsync(account).Receive() 1712 } 1713 1714 // GetBalanceMinConfAsync returns an instance of a type that can be used to get 1715 // the result of the RPC at some future time by invoking the Receive function on 1716 // the returned instance. 1717 // 1718 // See GetBalanceMinConf for the blocking version and more details. 1719 func (c *Client) GetBalanceMinConfAsync(account string, minConfirms int) FutureGetBalanceResult { 1720 cmd := btcjson.NewGetBalanceCmd(&account, &minConfirms) 1721 return c.sendCmd(cmd) 1722 } 1723 1724 // GetBalanceMinConf returns the available balance from the server for the 1725 // specified account using the specified number of minimum confirmations. The 1726 // account may be "*" for all accounts. 1727 // 1728 // See GetBalance to use the default minimum number of confirmations. 1729 func (c *Client) GetBalanceMinConf(account string, minConfirms int) (palcutil.Amount, error) { 1730 if c.config.EnableBCInfoHacks { 1731 response := c.GetBalanceMinConfAsync(account, minConfirms) 1732 return FutureGetBalanceParseResult(response).Receive() 1733 } 1734 return c.GetBalanceMinConfAsync(account, minConfirms).Receive() 1735 } 1736 1737 // FutureGetBalancesResult is a future promise to deliver the result of a 1738 // GetBalancesAsync RPC invocation (or an applicable error). 1739 type FutureGetBalancesResult chan *response 1740 1741 // Receive waits for the response promised by the future and returns the 1742 // available balances from the server. 1743 func (r FutureGetBalancesResult) Receive() (*btcjson.GetBalancesResult, error) { 1744 res, err := receiveFuture(r) 1745 if err != nil { 1746 return nil, err 1747 } 1748 1749 // Unmarshal result as a floating point number. 1750 var balances btcjson.GetBalancesResult 1751 err = json.Unmarshal(res, &balances) 1752 if err != nil { 1753 return nil, err 1754 } 1755 1756 return &balances, nil 1757 } 1758 1759 // GetBalancesAsync returns an instance of a type that can be used to get the 1760 // result of the RPC at some future time by invoking the Receive function on the 1761 // returned instance. 1762 // 1763 // See GetBalances for the blocking version and more details. 1764 func (c *Client) GetBalancesAsync() FutureGetBalancesResult { 1765 cmd := btcjson.NewGetBalancesCmd() 1766 return c.sendCmd(cmd) 1767 } 1768 1769 // GetBalances returns the available balances from the server. 1770 func (c *Client) GetBalances() (*btcjson.GetBalancesResult, error) { 1771 return c.GetBalancesAsync().Receive() 1772 } 1773 1774 // FutureGetReceivedByAccountResult is a future promise to deliver the result of 1775 // a GetReceivedByAccountAsync or GetReceivedByAccountMinConfAsync RPC 1776 // invocation (or an applicable error). 1777 type FutureGetReceivedByAccountResult chan *response 1778 1779 // Receive waits for the response promised by the future and returns the total 1780 // amount received with the specified account. 1781 func (r FutureGetReceivedByAccountResult) Receive() (palcutil.Amount, error) { 1782 res, err := receiveFuture(r) 1783 if err != nil { 1784 return 0, err 1785 } 1786 1787 // Unmarshal result as a floating point number. 1788 var balance float64 1789 err = json.Unmarshal(res, &balance) 1790 if err != nil { 1791 return 0, err 1792 } 1793 1794 amount, err := palcutil.NewAmount(balance) 1795 if err != nil { 1796 return 0, err 1797 } 1798 1799 return amount, nil 1800 } 1801 1802 // GetReceivedByAccountAsync returns an instance of a type that can be used to 1803 // get the result of the RPC at some future time by invoking the Receive 1804 // function on the returned instance. 1805 // 1806 // See GetReceivedByAccount for the blocking version and more details. 1807 func (c *Client) GetReceivedByAccountAsync(account string) FutureGetReceivedByAccountResult { 1808 cmd := btcjson.NewGetReceivedByAccountCmd(account, nil) 1809 return c.sendCmd(cmd) 1810 } 1811 1812 // GetReceivedByAccount returns the total amount received with the specified 1813 // account with at least the default number of minimum confirmations. 1814 // 1815 // See GetReceivedByAccountMinConf to override the minimum number of 1816 // confirmations. 1817 func (c *Client) GetReceivedByAccount(account string) (palcutil.Amount, error) { 1818 return c.GetReceivedByAccountAsync(account).Receive() 1819 } 1820 1821 // GetReceivedByAccountMinConfAsync returns an instance of a type that can be 1822 // used to get the result of the RPC at some future time by invoking the Receive 1823 // function on the returned instance. 1824 // 1825 // See GetReceivedByAccountMinConf for the blocking version and more details. 1826 func (c *Client) GetReceivedByAccountMinConfAsync(account string, minConfirms int) FutureGetReceivedByAccountResult { 1827 cmd := btcjson.NewGetReceivedByAccountCmd(account, &minConfirms) 1828 return c.sendCmd(cmd) 1829 } 1830 1831 // GetReceivedByAccountMinConf returns the total amount received with the 1832 // specified account with at least the specified number of minimum 1833 // confirmations. 1834 // 1835 // See GetReceivedByAccount to use the default minimum number of confirmations. 1836 func (c *Client) GetReceivedByAccountMinConf(account string, minConfirms int) (palcutil.Amount, error) { 1837 return c.GetReceivedByAccountMinConfAsync(account, minConfirms).Receive() 1838 } 1839 1840 // FutureGetUnconfirmedBalanceResult is a future promise to deliver the result 1841 // of a GetUnconfirmedBalanceAsync RPC invocation (or an applicable error). 1842 type FutureGetUnconfirmedBalanceResult chan *response 1843 1844 // Receive waits for the response promised by the future and returns returns the 1845 // unconfirmed balance from the server for the specified account. 1846 func (r FutureGetUnconfirmedBalanceResult) Receive() (palcutil.Amount, error) { 1847 res, err := receiveFuture(r) 1848 if err != nil { 1849 return 0, err 1850 } 1851 1852 // Unmarshal result as a floating point number. 1853 var balance float64 1854 err = json.Unmarshal(res, &balance) 1855 if err != nil { 1856 return 0, err 1857 } 1858 1859 amount, err := palcutil.NewAmount(balance) 1860 if err != nil { 1861 return 0, err 1862 } 1863 1864 return amount, nil 1865 } 1866 1867 // GetUnconfirmedBalanceAsync returns an instance of a type that can be used to 1868 // get the result of the RPC at some future time by invoking the Receive 1869 // function on the returned instance. 1870 // 1871 // See GetUnconfirmedBalance for the blocking version and more details. 1872 func (c *Client) GetUnconfirmedBalanceAsync(account string) FutureGetUnconfirmedBalanceResult { 1873 cmd := btcjson.NewGetUnconfirmedBalanceCmd(&account) 1874 return c.sendCmd(cmd) 1875 } 1876 1877 // GetUnconfirmedBalance returns the unconfirmed balance from the server for 1878 // the specified account. 1879 func (c *Client) GetUnconfirmedBalance(account string) (palcutil.Amount, error) { 1880 return c.GetUnconfirmedBalanceAsync(account).Receive() 1881 } 1882 1883 // FutureGetReceivedByAddressResult is a future promise to deliver the result of 1884 // a GetReceivedByAddressAsync or GetReceivedByAddressMinConfAsync RPC 1885 // invocation (or an applicable error). 1886 type FutureGetReceivedByAddressResult chan *response 1887 1888 // Receive waits for the response promised by the future and returns the total 1889 // amount received by the specified address. 1890 func (r FutureGetReceivedByAddressResult) Receive() (palcutil.Amount, error) { 1891 res, err := receiveFuture(r) 1892 if err != nil { 1893 return 0, err 1894 } 1895 1896 // Unmarshal result as a floating point number. 1897 var balance float64 1898 err = json.Unmarshal(res, &balance) 1899 if err != nil { 1900 return 0, err 1901 } 1902 1903 amount, err := palcutil.NewAmount(balance) 1904 if err != nil { 1905 return 0, err 1906 } 1907 1908 return amount, nil 1909 } 1910 1911 // GetReceivedByAddressAsync returns an instance of a type that can be used to 1912 // get the result of the RPC at some future time by invoking the Receive 1913 // function on the returned instance. 1914 // 1915 // See GetReceivedByAddress for the blocking version and more details. 1916 func (c *Client) GetReceivedByAddressAsync(address palcutil.Address) FutureGetReceivedByAddressResult { 1917 addr := address.EncodeAddress() 1918 cmd := btcjson.NewGetReceivedByAddressCmd(addr, nil) 1919 return c.sendCmd(cmd) 1920 1921 } 1922 1923 // GetReceivedByAddress returns the total amount received by the specified 1924 // address with at least the default number of minimum confirmations. 1925 // 1926 // See GetReceivedByAddressMinConf to override the minimum number of 1927 // confirmations. 1928 func (c *Client) GetReceivedByAddress(address palcutil.Address) (palcutil.Amount, error) { 1929 return c.GetReceivedByAddressAsync(address).Receive() 1930 } 1931 1932 // GetReceivedByAddressMinConfAsync returns an instance of a type that can be 1933 // used to get the result of the RPC at some future time by invoking the Receive 1934 // function on the returned instance. 1935 // 1936 // See GetReceivedByAddressMinConf for the blocking version and more details. 1937 func (c *Client) GetReceivedByAddressMinConfAsync(address palcutil.Address, minConfirms int) FutureGetReceivedByAddressResult { 1938 addr := address.EncodeAddress() 1939 cmd := btcjson.NewGetReceivedByAddressCmd(addr, &minConfirms) 1940 return c.sendCmd(cmd) 1941 } 1942 1943 // GetReceivedByAddressMinConf returns the total amount received by the specified 1944 // address with at least the specified number of minimum confirmations. 1945 // 1946 // See GetReceivedByAddress to use the default minimum number of confirmations. 1947 func (c *Client) GetReceivedByAddressMinConf(address palcutil.Address, minConfirms int) (palcutil.Amount, error) { 1948 return c.GetReceivedByAddressMinConfAsync(address, minConfirms).Receive() 1949 } 1950 1951 // FutureListReceivedByAccountResult is a future promise to deliver the result 1952 // of a ListReceivedByAccountAsync, ListReceivedByAccountMinConfAsync, or 1953 // ListReceivedByAccountIncludeEmptyAsync RPC invocation (or an applicable 1954 // error). 1955 type FutureListReceivedByAccountResult chan *response 1956 1957 // Receive waits for the response promised by the future and returns a list of 1958 // balances by account. 1959 func (r FutureListReceivedByAccountResult) Receive() ([]btcjson.ListReceivedByAccountResult, error) { 1960 res, err := receiveFuture(r) 1961 if err != nil { 1962 return nil, err 1963 } 1964 1965 // Unmarshal as an array of listreceivedbyaccount result objects. 1966 var received []btcjson.ListReceivedByAccountResult 1967 err = json.Unmarshal(res, &received) 1968 if err != nil { 1969 return nil, err 1970 } 1971 1972 return received, nil 1973 } 1974 1975 // ListReceivedByAccountAsync 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 ListReceivedByAccount for the blocking version and more details. 1980 func (c *Client) ListReceivedByAccountAsync() FutureListReceivedByAccountResult { 1981 cmd := btcjson.NewListReceivedByAccountCmd(nil, nil, nil) 1982 return c.sendCmd(cmd) 1983 } 1984 1985 // ListReceivedByAccount lists balances by account using the default number 1986 // of minimum confirmations and including accounts that haven't received any 1987 // payments. 1988 // 1989 // See ListReceivedByAccountMinConf to override the minimum number of 1990 // confirmations and ListReceivedByAccountIncludeEmpty to filter accounts that 1991 // haven't received any payments from the results. 1992 func (c *Client) ListReceivedByAccount() ([]btcjson.ListReceivedByAccountResult, error) { 1993 return c.ListReceivedByAccountAsync().Receive() 1994 } 1995 1996 // ListReceivedByAccountMinConfAsync 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 ListReceivedByAccountMinConf for the blocking version and more details. 2001 func (c *Client) ListReceivedByAccountMinConfAsync(minConfirms int) FutureListReceivedByAccountResult { 2002 cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, nil, nil) 2003 return c.sendCmd(cmd) 2004 } 2005 2006 // ListReceivedByAccountMinConf lists balances by account using the specified 2007 // number of minimum confirmations not including accounts that haven't received 2008 // any payments. 2009 // 2010 // See ListReceivedByAccount to use the default minimum number of confirmations 2011 // and ListReceivedByAccountIncludeEmpty to also include accounts that haven't 2012 // received any payments in the results. 2013 func (c *Client) ListReceivedByAccountMinConf(minConfirms int) ([]btcjson.ListReceivedByAccountResult, error) { 2014 return c.ListReceivedByAccountMinConfAsync(minConfirms).Receive() 2015 } 2016 2017 // ListReceivedByAccountIncludeEmptyAsync 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) ListReceivedByAccountIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAccountResult { 2023 cmd := btcjson.NewListReceivedByAccountCmd(&minConfirms, &includeEmpty, 2024 nil) 2025 return c.sendCmd(cmd) 2026 } 2027 2028 // ListReceivedByAccountIncludeEmpty lists balances by account using the 2029 // specified number of minimum confirmations and including accounts that 2030 // haven't received any payments depending on specified flag. 2031 // 2032 // See ListReceivedByAccount and ListReceivedByAccountMinConf to use defaults. 2033 func (c *Client) ListReceivedByAccountIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAccountResult, error) { 2034 return c.ListReceivedByAccountIncludeEmptyAsync(minConfirms, 2035 includeEmpty).Receive() 2036 } 2037 2038 // FutureListReceivedByAddressResult is a future promise to deliver the result 2039 // of a ListReceivedByAddressAsync, ListReceivedByAddressMinConfAsync, or 2040 // ListReceivedByAddressIncludeEmptyAsync RPC invocation (or an applicable 2041 // error). 2042 type FutureListReceivedByAddressResult chan *response 2043 2044 // Receive waits for the response promised by the future and returns a list of 2045 // balances by address. 2046 func (r FutureListReceivedByAddressResult) Receive() ([]btcjson.ListReceivedByAddressResult, error) { 2047 res, err := receiveFuture(r) 2048 if err != nil { 2049 return nil, err 2050 } 2051 2052 // Unmarshal as an array of listreceivedbyaddress result objects. 2053 var received []btcjson.ListReceivedByAddressResult 2054 err = json.Unmarshal(res, &received) 2055 if err != nil { 2056 return nil, err 2057 } 2058 2059 return received, nil 2060 } 2061 2062 // ListReceivedByAddressAsync returns an instance of a type that can be used to 2063 // get the result of the RPC at some future time by invoking the Receive 2064 // function on the returned instance. 2065 // 2066 // See ListReceivedByAddress for the blocking version and more details. 2067 func (c *Client) ListReceivedByAddressAsync() FutureListReceivedByAddressResult { 2068 cmd := btcjson.NewListReceivedByAddressCmd(nil, nil, nil) 2069 return c.sendCmd(cmd) 2070 } 2071 2072 // ListReceivedByAddress lists balances by address using the default number 2073 // of minimum confirmations not including addresses that haven't received any 2074 // payments or watching only addresses. 2075 // 2076 // See ListReceivedByAddressMinConf to override the minimum number of 2077 // confirmations and ListReceivedByAddressIncludeEmpty to also include addresses 2078 // that haven't received any payments in the results. 2079 func (c *Client) ListReceivedByAddress() ([]btcjson.ListReceivedByAddressResult, error) { 2080 return c.ListReceivedByAddressAsync().Receive() 2081 } 2082 2083 // ListReceivedByAddressMinConfAsync returns an instance of a type that can be 2084 // used to get the result of the RPC at some future time by invoking the Receive 2085 // function on the returned instance. 2086 // 2087 // See ListReceivedByAddressMinConf for the blocking version and more details. 2088 func (c *Client) ListReceivedByAddressMinConfAsync(minConfirms int) FutureListReceivedByAddressResult { 2089 cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, nil, nil) 2090 return c.sendCmd(cmd) 2091 } 2092 2093 // ListReceivedByAddressMinConf lists balances by address using the specified 2094 // number of minimum confirmations not including addresses that haven't received 2095 // any payments. 2096 // 2097 // See ListReceivedByAddress to use the default minimum number of confirmations 2098 // and ListReceivedByAddressIncludeEmpty to also include addresses that haven't 2099 // received any payments in the results. 2100 func (c *Client) ListReceivedByAddressMinConf(minConfirms int) ([]btcjson.ListReceivedByAddressResult, error) { 2101 return c.ListReceivedByAddressMinConfAsync(minConfirms).Receive() 2102 } 2103 2104 // ListReceivedByAddressIncludeEmptyAsync returns an instance of a type that can 2105 // be used to get the result of the RPC at some future time by invoking the 2106 // Receive function on the returned instance. 2107 // 2108 // See ListReceivedByAccountIncludeEmpty for the blocking version and more details. 2109 func (c *Client) ListReceivedByAddressIncludeEmptyAsync(minConfirms int, includeEmpty bool) FutureListReceivedByAddressResult { 2110 cmd := btcjson.NewListReceivedByAddressCmd(&minConfirms, &includeEmpty, 2111 nil) 2112 return c.sendCmd(cmd) 2113 } 2114 2115 // ListReceivedByAddressIncludeEmpty lists balances by address using the 2116 // specified number of minimum confirmations and including addresses that 2117 // haven't received any payments depending on specified flag. 2118 // 2119 // See ListReceivedByAddress and ListReceivedByAddressMinConf to use defaults. 2120 func (c *Client) ListReceivedByAddressIncludeEmpty(minConfirms int, includeEmpty bool) ([]btcjson.ListReceivedByAddressResult, error) { 2121 return c.ListReceivedByAddressIncludeEmptyAsync(minConfirms, 2122 includeEmpty).Receive() 2123 } 2124 2125 // ************************ 2126 // Wallet Locking Functions 2127 // ************************ 2128 2129 // FutureWalletLockResult is a future promise to deliver the result of a 2130 // WalletLockAsync RPC invocation (or an applicable error). 2131 type FutureWalletLockResult chan *response 2132 2133 // Receive waits for the response promised by the future and returns the result 2134 // of locking the wallet. 2135 func (r FutureWalletLockResult) Receive() error { 2136 _, err := receiveFuture(r) 2137 return err 2138 } 2139 2140 // WalletLockAsync returns an instance of a type that can be used to get the 2141 // result of the RPC at some future time by invoking the Receive function on the 2142 // returned instance. 2143 // 2144 // See WalletLock for the blocking version and more details. 2145 func (c *Client) WalletLockAsync() FutureWalletLockResult { 2146 cmd := btcjson.NewWalletLockCmd() 2147 return c.sendCmd(cmd) 2148 } 2149 2150 // WalletLock locks the wallet by removing the encryption key from memory. 2151 // 2152 // After calling this function, the WalletPassphrase function must be used to 2153 // unlock the wallet prior to calling any other function which requires the 2154 // wallet to be unlocked. 2155 func (c *Client) WalletLock() error { 2156 return c.WalletLockAsync().Receive() 2157 } 2158 2159 // WalletPassphrase unlocks the wallet by using the passphrase to derive the 2160 // decryption key which is then stored in memory for the specified timeout 2161 // (in seconds). 2162 func (c *Client) WalletPassphrase(passphrase string, timeoutSecs int64) error { 2163 cmd := btcjson.NewWalletPassphraseCmd(passphrase, timeoutSecs) 2164 _, err := c.sendCmdAndWait(cmd) 2165 return err 2166 } 2167 2168 // FutureWalletPassphraseChangeResult is a future promise to deliver the result 2169 // of a WalletPassphraseChangeAsync RPC invocation (or an applicable error). 2170 type FutureWalletPassphraseChangeResult chan *response 2171 2172 // Receive waits for the response promised by the future and returns the result 2173 // of changing the wallet passphrase. 2174 func (r FutureWalletPassphraseChangeResult) Receive() error { 2175 _, err := receiveFuture(r) 2176 return err 2177 } 2178 2179 // WalletPassphraseChangeAsync returns an instance of a type that can be used to 2180 // get the result of the RPC at some future time by invoking the Receive 2181 // function on the returned instance. 2182 // 2183 // See WalletPassphraseChange for the blocking version and more details. 2184 func (c *Client) WalletPassphraseChangeAsync(old, new string) FutureWalletPassphraseChangeResult { 2185 cmd := btcjson.NewWalletPassphraseChangeCmd(old, new) 2186 return c.sendCmd(cmd) 2187 } 2188 2189 // WalletPassphraseChange changes the wallet passphrase from the specified old 2190 // to new passphrase. 2191 func (c *Client) WalletPassphraseChange(old, new string) error { 2192 return c.WalletPassphraseChangeAsync(old, new).Receive() 2193 } 2194 2195 // ************************* 2196 // Message Signing Functions 2197 // ************************* 2198 2199 // FutureSignMessageResult is a future promise to deliver the result of a 2200 // SignMessageAsync RPC invocation (or an applicable error). 2201 type FutureSignMessageResult chan *response 2202 2203 // Receive waits for the response promised by the future and returns the message 2204 // signed with the private key of the specified address. 2205 func (r FutureSignMessageResult) Receive() (string, error) { 2206 res, err := receiveFuture(r) 2207 if err != nil { 2208 return "", err 2209 } 2210 2211 // Unmarshal result as a string. 2212 var b64 string 2213 err = json.Unmarshal(res, &b64) 2214 if err != nil { 2215 return "", err 2216 } 2217 2218 return b64, nil 2219 } 2220 2221 // SignMessageAsync returns an instance of a type that can be used to get the 2222 // result of the RPC at some future time by invoking the Receive function on the 2223 // returned instance. 2224 // 2225 // See SignMessage for the blocking version and more details. 2226 func (c *Client) SignMessageAsync(address palcutil.Address, message string) FutureSignMessageResult { 2227 addr := address.EncodeAddress() 2228 cmd := btcjson.NewSignMessageCmd(addr, message) 2229 return c.sendCmd(cmd) 2230 } 2231 2232 // SignMessage signs a message with the private key of the specified address. 2233 // 2234 // NOTE: This function requires to the wallet to be unlocked. See the 2235 // WalletPassphrase function for more details. 2236 func (c *Client) SignMessage(address palcutil.Address, message string) (string, error) { 2237 return c.SignMessageAsync(address, message).Receive() 2238 } 2239 2240 // FutureVerifyMessageResult is a future promise to deliver the result of a 2241 // VerifyMessageAsync RPC invocation (or an applicable error). 2242 type FutureVerifyMessageResult chan *response 2243 2244 // Receive waits for the response promised by the future and returns whether or 2245 // not the message was successfully verified. 2246 func (r FutureVerifyMessageResult) Receive() (bool, error) { 2247 res, err := receiveFuture(r) 2248 if err != nil { 2249 return false, err 2250 } 2251 2252 // Unmarshal result as a boolean. 2253 var verified bool 2254 err = json.Unmarshal(res, &verified) 2255 if err != nil { 2256 return false, err 2257 } 2258 2259 return verified, nil 2260 } 2261 2262 // VerifyMessageAsync returns an instance of a type that can be used to get the 2263 // result of the RPC at some future time by invoking the Receive function on the 2264 // returned instance. 2265 // 2266 // See VerifyMessage for the blocking version and more details. 2267 func (c *Client) VerifyMessageAsync(address palcutil.Address, signature, message string) FutureVerifyMessageResult { 2268 addr := address.EncodeAddress() 2269 cmd := btcjson.NewVerifyMessageCmd(addr, signature, message) 2270 return c.sendCmd(cmd) 2271 } 2272 2273 // VerifyMessage verifies a signed message. 2274 // 2275 // NOTE: This function requires to the wallet to be unlocked. See the 2276 // WalletPassphrase function for more details. 2277 func (c *Client) VerifyMessage(address palcutil.Address, signature, message string) (bool, error) { 2278 return c.VerifyMessageAsync(address, signature, message).Receive() 2279 } 2280 2281 // ********************* 2282 // Dump/Import Functions 2283 // ********************* 2284 2285 // FutureDumpPrivKeyResult is a future promise to deliver the result of a 2286 // DumpPrivKeyAsync RPC invocation (or an applicable error). 2287 type FutureDumpPrivKeyResult chan *response 2288 2289 // Receive waits for the response promised by the future and returns the private 2290 // key corresponding to the passed address encoded in the wallet import format 2291 // (WIF) 2292 func (r FutureDumpPrivKeyResult) Receive() (*palcutil.WIF, error) { 2293 res, err := receiveFuture(r) 2294 if err != nil { 2295 return nil, err 2296 } 2297 2298 // Unmarshal result as a string. 2299 var privKeyWIF string 2300 err = json.Unmarshal(res, &privKeyWIF) 2301 if err != nil { 2302 return nil, err 2303 } 2304 2305 return palcutil.DecodeWIF(privKeyWIF) 2306 } 2307 2308 // DumpPrivKeyAsync returns an instance of a type that can be used to get the 2309 // result of the RPC at some future time by invoking the Receive function on the 2310 // returned instance. 2311 // 2312 // See DumpPrivKey for the blocking version and more details. 2313 func (c *Client) DumpPrivKeyAsync(address palcutil.Address) FutureDumpPrivKeyResult { 2314 addr := address.EncodeAddress() 2315 cmd := btcjson.NewDumpPrivKeyCmd(addr) 2316 return c.sendCmd(cmd) 2317 } 2318 2319 // DumpPrivKey gets the private key corresponding to the passed address encoded 2320 // in the wallet import format (WIF). 2321 // 2322 // NOTE: This function requires to the wallet to be unlocked. See the 2323 // WalletPassphrase function for more details. 2324 func (c *Client) DumpPrivKey(address palcutil.Address) (*palcutil.WIF, error) { 2325 return c.DumpPrivKeyAsync(address).Receive() 2326 } 2327 2328 // FutureImportAddressResult is a future promise to deliver the result of an 2329 // ImportAddressAsync RPC invocation (or an applicable error). 2330 type FutureImportAddressResult chan *response 2331 2332 // Receive waits for the response promised by the future and returns the result 2333 // of importing the passed public address. 2334 func (r FutureImportAddressResult) Receive() error { 2335 _, err := receiveFuture(r) 2336 return err 2337 } 2338 2339 // ImportAddressAsync returns an instance of a type that can be used to get the 2340 // result of the RPC at some future time by invoking the Receive function on the 2341 // returned instance. 2342 // 2343 // See ImportAddress for the blocking version and more details. 2344 func (c *Client) ImportAddressAsync(address string) FutureImportAddressResult { 2345 cmd := btcjson.NewImportAddressCmd(address, "", nil) 2346 return c.sendCmd(cmd) 2347 } 2348 2349 // ImportAddress imports the passed public address. 2350 func (c *Client) ImportAddress(address string) error { 2351 return c.ImportAddressAsync(address).Receive() 2352 } 2353 2354 // ImportAddressRescanAsync returns an instance of a type that can be used to get the 2355 // result of the RPC at some future time by invoking the Receive function on the 2356 // returned instance. 2357 // 2358 // See ImportAddress for the blocking version and more details. 2359 func (c *Client) ImportAddressRescanAsync(address string, account string, rescan bool) FutureImportAddressResult { 2360 cmd := btcjson.NewImportAddressCmd(address, account, &rescan) 2361 return c.sendCmd(cmd) 2362 } 2363 2364 // ImportAddressRescan imports the passed public address. When rescan is true, 2365 // the block history is scanned for transactions addressed to provided address. 2366 func (c *Client) ImportAddressRescan(address string, account string, rescan bool) error { 2367 return c.ImportAddressRescanAsync(address, account, rescan).Receive() 2368 } 2369 2370 // FutureImportMultiResult is a future promise to deliver the result of an 2371 // ImportMultiAsync RPC invocation (or an applicable error). 2372 type FutureImportMultiResult chan *response 2373 2374 // Receive waits for the response promised by the future and returns the result 2375 // of importing multiple addresses/scripts. 2376 func (r FutureImportMultiResult) Receive() (btcjson.ImportMultiResults, error) { 2377 res, err := receiveFuture(r) 2378 if err != nil { 2379 return nil, err 2380 } 2381 2382 var importMultiResults btcjson.ImportMultiResults 2383 err = json.Unmarshal(res, &importMultiResults) 2384 if err != nil { 2385 return nil, err 2386 } 2387 return importMultiResults, nil 2388 } 2389 2390 // ImportMultiAsync returns an instance of a type that can be used to get the result 2391 // of the RPC at some future time by invoking the Receive function on the 2392 // returned instance. 2393 // 2394 // See ImportMulti for the blocking version and more details. 2395 func (c *Client) ImportMultiAsync(requests []btcjson.ImportMultiRequest, options *btcjson.ImportMultiOptions) FutureImportMultiResult { 2396 cmd := btcjson.NewImportMultiCmd(requests, options) 2397 return c.sendCmd(cmd) 2398 } 2399 2400 // ImportMulti imports addresses/scripts, optionally rescanning the blockchain 2401 // from the earliest creation time of the imported scripts. 2402 // 2403 // See btcjson.ImportMultiRequest for details on the requests parameter. 2404 func (c *Client) ImportMulti(requests []btcjson.ImportMultiRequest, options *btcjson.ImportMultiOptions) (btcjson.ImportMultiResults, error) { 2405 return c.ImportMultiAsync(requests, options).Receive() 2406 } 2407 2408 // FutureImportPrivKeyResult is a future promise to deliver the result of an 2409 // ImportPrivKeyAsync RPC invocation (or an applicable error). 2410 type FutureImportPrivKeyResult chan *response 2411 2412 // Receive waits for the response promised by the future and returns the result 2413 // of importing the passed private key which must be the wallet import format 2414 // (WIF). 2415 func (r FutureImportPrivKeyResult) Receive() error { 2416 _, err := receiveFuture(r) 2417 return err 2418 } 2419 2420 // ImportPrivKeyAsync returns an instance of a type that can be used to get the 2421 // result of the RPC at some future time by invoking the Receive function on the 2422 // returned instance. 2423 // 2424 // See ImportPrivKey for the blocking version and more details. 2425 func (c *Client) ImportPrivKeyAsync(privKeyWIF *palcutil.WIF) FutureImportPrivKeyResult { 2426 wif := "" 2427 if privKeyWIF != nil { 2428 wif = privKeyWIF.String() 2429 } 2430 2431 cmd := btcjson.NewImportPrivKeyCmd(wif, nil, nil) 2432 return c.sendCmd(cmd) 2433 } 2434 2435 // ImportPrivKey imports the passed private key which must be the wallet import 2436 // format (WIF). 2437 func (c *Client) ImportPrivKey(privKeyWIF *palcutil.WIF) error { 2438 return c.ImportPrivKeyAsync(privKeyWIF).Receive() 2439 } 2440 2441 // ImportPrivKeyLabelAsync returns an instance of a type that can be used to get the 2442 // result of the RPC at some future time by invoking the Receive function on the 2443 // returned instance. 2444 // 2445 // See ImportPrivKey for the blocking version and more details. 2446 func (c *Client) ImportPrivKeyLabelAsync(privKeyWIF *palcutil.WIF, label string) FutureImportPrivKeyResult { 2447 wif := "" 2448 if privKeyWIF != nil { 2449 wif = privKeyWIF.String() 2450 } 2451 2452 cmd := btcjson.NewImportPrivKeyCmd(wif, &label, nil) 2453 return c.sendCmd(cmd) 2454 } 2455 2456 // ImportPrivKeyLabel imports the passed private key which must be the wallet import 2457 // format (WIF). It sets the account label to the one provided. 2458 func (c *Client) ImportPrivKeyLabel(privKeyWIF *palcutil.WIF, label string) error { 2459 return c.ImportPrivKeyLabelAsync(privKeyWIF, label).Receive() 2460 } 2461 2462 // ImportPrivKeyRescanAsync returns an instance of a type that can be used to get the 2463 // result of the RPC at some future time by invoking the Receive function on the 2464 // returned instance. 2465 // 2466 // See ImportPrivKey for the blocking version and more details. 2467 func (c *Client) ImportPrivKeyRescanAsync(privKeyWIF *palcutil.WIF, label string, rescan bool) FutureImportPrivKeyResult { 2468 wif := "" 2469 if privKeyWIF != nil { 2470 wif = privKeyWIF.String() 2471 } 2472 2473 cmd := btcjson.NewImportPrivKeyCmd(wif, &label, &rescan) 2474 return c.sendCmd(cmd) 2475 } 2476 2477 // ImportPrivKeyRescan imports the passed private key which must be the wallet import 2478 // format (WIF). It sets the account label to the one provided. When rescan is true, 2479 // the block history is scanned for transactions addressed to provided privKey. 2480 func (c *Client) ImportPrivKeyRescan(privKeyWIF *palcutil.WIF, label string, rescan bool) error { 2481 return c.ImportPrivKeyRescanAsync(privKeyWIF, label, rescan).Receive() 2482 } 2483 2484 // FutureImportPubKeyResult is a future promise to deliver the result of an 2485 // ImportPubKeyAsync RPC invocation (or an applicable error). 2486 type FutureImportPubKeyResult chan *response 2487 2488 // Receive waits for the response promised by the future and returns the result 2489 // of importing the passed public key. 2490 func (r FutureImportPubKeyResult) Receive() error { 2491 _, err := receiveFuture(r) 2492 return err 2493 } 2494 2495 // ImportPubKeyAsync returns an instance of a type that can be used to get the 2496 // result of the RPC at some future time by invoking the Receive function on the 2497 // returned instance. 2498 // 2499 // See ImportPubKey for the blocking version and more details. 2500 func (c *Client) ImportPubKeyAsync(pubKey string) FutureImportPubKeyResult { 2501 cmd := btcjson.NewImportPubKeyCmd(pubKey, nil) 2502 return c.sendCmd(cmd) 2503 } 2504 2505 // ImportPubKey imports the passed public key. 2506 func (c *Client) ImportPubKey(pubKey string) error { 2507 return c.ImportPubKeyAsync(pubKey).Receive() 2508 } 2509 2510 // ImportPubKeyRescanAsync returns an instance of a type that can be used to get the 2511 // result of the RPC at some future time by invoking the Receive function on the 2512 // returned instance. 2513 // 2514 // See ImportPubKey for the blocking version and more details. 2515 func (c *Client) ImportPubKeyRescanAsync(pubKey string, rescan bool) FutureImportPubKeyResult { 2516 cmd := btcjson.NewImportPubKeyCmd(pubKey, &rescan) 2517 return c.sendCmd(cmd) 2518 } 2519 2520 // ImportPubKeyRescan imports the passed public key. When rescan is true, the 2521 // block history is scanned for transactions addressed to provided pubkey. 2522 func (c *Client) ImportPubKeyRescan(pubKey string, rescan bool) error { 2523 return c.ImportPubKeyRescanAsync(pubKey, rescan).Receive() 2524 } 2525 2526 // *********************** 2527 // Miscellaneous Functions 2528 // *********************** 2529 2530 // NOTE: While getinfo is implemented here (in wallet.go), a btcd chain server 2531 // will respond to getinfo requests as well, excluding any wallet information. 2532 2533 // FutureGetInfoResult is a future promise to deliver the result of a 2534 // GetInfoAsync RPC invocation (or an applicable error). 2535 type FutureGetInfoResult chan *response 2536 2537 // Receive waits for the response promised by the future and returns the info 2538 // provided by the server. 2539 func (r FutureGetInfoResult) Receive() (*btcjson.InfoWalletResult, error) { 2540 res, err := receiveFuture(r) 2541 if err != nil { 2542 return nil, err 2543 } 2544 2545 // Unmarshal result as a getinfo result object. 2546 var infoRes btcjson.InfoWalletResult 2547 err = json.Unmarshal(res, &infoRes) 2548 if err != nil { 2549 return nil, err 2550 } 2551 2552 return &infoRes, nil 2553 } 2554 2555 // GetInfoAsync returns an instance of a type that can be used to get the result 2556 // of the RPC at some future time by invoking the Receive function on the 2557 // returned instance. 2558 // 2559 // See GetInfo for the blocking version and more details. 2560 func (c *Client) GetInfoAsync() FutureGetInfoResult { 2561 cmd := btcjson.NewGetInfoCmd() 2562 return c.sendCmd(cmd) 2563 } 2564 2565 // GetInfo returns miscellaneous info regarding the RPC server. The returned 2566 // info object may be void of wallet information if the remote server does 2567 // not include wallet functionality. 2568 func (c *Client) GetInfo() (*btcjson.InfoWalletResult, error) { 2569 return c.GetInfoAsync().Receive() 2570 } 2571 2572 // FutureImportPubKeyResult is a future promise to deliver the result of an 2573 // WalletCreateFundedPsbt RPC invocation (or an applicable error). 2574 type FutureWalletCreateFundedPsbtResult chan *response 2575 2576 // Receive waits for the response promised by the future and returns the 2577 // partially signed transaction in PSBT format along with the resulting fee 2578 // and change output index. 2579 func (r FutureWalletCreateFundedPsbtResult) Receive() (*btcjson.WalletCreateFundedPsbtResult, error) { 2580 res, err := receiveFuture(r) 2581 if err != nil { 2582 return nil, err 2583 } 2584 2585 // Unmarshal result as a getinfo result object. 2586 var psbtRes btcjson.WalletCreateFundedPsbtResult 2587 err = json.Unmarshal(res, &psbtRes) 2588 if err != nil { 2589 return nil, err 2590 } 2591 2592 return &psbtRes, nil 2593 } 2594 2595 // WalletCreateFundedPsbtAsync returns an instance of a type that can be used 2596 // to get the result of the RPC at some future time by invoking the Receive 2597 // function on the returned instance. 2598 // 2599 // See WalletCreateFundedPsbt for the blocking version and more details. 2600 func (c *Client) WalletCreateFundedPsbtAsync( 2601 inputs []btcjson.PsbtInput, outputs []btcjson.PsbtOutput, locktime *uint32, 2602 options *btcjson.WalletCreateFundedPsbtOpts, bip32Derivs *bool, 2603 ) FutureWalletCreateFundedPsbtResult { 2604 cmd := btcjson.NewWalletCreateFundedPsbtCmd(inputs, outputs, locktime, options, bip32Derivs) 2605 return c.sendCmd(cmd) 2606 } 2607 2608 // WalletCreateFundedPsbt creates and funds a transaction in the Partially 2609 // Signed Transaction format. Inputs will be added if supplied inputs are not 2610 // enough. 2611 func (c *Client) WalletCreateFundedPsbt( 2612 inputs []btcjson.PsbtInput, outputs []btcjson.PsbtOutput, locktime *uint32, 2613 options *btcjson.WalletCreateFundedPsbtOpts, bip32Derivs *bool, 2614 ) (*btcjson.WalletCreateFundedPsbtResult, error) { 2615 return c.WalletCreateFundedPsbtAsync(inputs, outputs, locktime, options, bip32Derivs).Receive() 2616 } 2617 2618 // FutureWalletProcessPsbtResult is a future promise to deliver the result of a 2619 // WalletCreateFundedPsb RPC invocation (or an applicable error). 2620 type FutureWalletProcessPsbtResult chan *response 2621 2622 // Receive waits for the response promised by the future and returns an updated 2623 // PSBT with signed inputs from the wallet and a boolen indicating if the the 2624 // transaction has a complete set of signatures. 2625 func (r FutureWalletProcessPsbtResult) Receive() (*btcjson.WalletProcessPsbtResult, error) { 2626 res, err := receiveFuture(r) 2627 if err != nil { 2628 return nil, err 2629 } 2630 2631 // Unmarshal result as a getinfo result object. 2632 var psbtRes btcjson.WalletProcessPsbtResult 2633 err = json.Unmarshal(res, &psbtRes) 2634 if err != nil { 2635 return nil, err 2636 } 2637 2638 return &psbtRes, nil 2639 } 2640 2641 // WalletProcessPsbtAsync returns an instance of a type that can be used 2642 // to get the result of the RPC at some future time by invoking the Receive 2643 // function on the returned instance. 2644 // 2645 // See WalletProcessPsbt for the blocking version and more details. 2646 func (c *Client) WalletProcessPsbtAsync( 2647 psbt string, sign *bool, sighashType SigHashType, bip32Derivs *bool, 2648 ) FutureWalletProcessPsbtResult { 2649 cmd := btcjson.NewWalletProcessPsbtCmd(psbt, sign, btcjson.String(sighashType.String()), bip32Derivs) 2650 return c.sendCmd(cmd) 2651 } 2652 2653 // WalletProcessPsbt updates a PSBT with input information from our wallet and 2654 // then signs inputs. 2655 func (c *Client) WalletProcessPsbt( 2656 psbt string, sign *bool, sighashType SigHashType, bip32Derivs *bool, 2657 ) (*btcjson.WalletProcessPsbtResult, error) { 2658 return c.WalletProcessPsbtAsync(psbt, sign, sighashType, bip32Derivs).Receive() 2659 } 2660 2661 // FutureGetWalletInfoResult is a future promise to deliver the result of an 2662 // GetWalletInfoAsync RPC invocation (or an applicable error). 2663 type FutureGetWalletInfoResult chan *response 2664 2665 // Receive waits for the response promised by the future and returns the result 2666 // of wallet state info. 2667 func (r FutureGetWalletInfoResult) Receive() (*btcjson.GetWalletInfoResult, error) { 2668 res, err := receiveFuture(r) 2669 if err != nil { 2670 return nil, err 2671 } 2672 2673 var getWalletInfoResult btcjson.GetWalletInfoResult 2674 err = json.Unmarshal(res, &getWalletInfoResult) 2675 if err != nil { 2676 return nil, err 2677 } 2678 return &getWalletInfoResult, nil 2679 } 2680 2681 // GetWalletInfoAsync returns an instance of a type that can be used to get the result 2682 // of the RPC at some future time by invoking the Receive function on the 2683 // returned instance. 2684 // 2685 // See GetWalletInfo for the blocking version and more details. 2686 func (c *Client) GetWalletInfoAsync() FutureGetWalletInfoResult { 2687 cmd := btcjson.NewGetWalletInfoCmd() 2688 return c.sendCmd(cmd) 2689 } 2690 2691 // GetWalletInfo returns various wallet state info. 2692 func (c *Client) GetWalletInfo() (*btcjson.GetWalletInfoResult, error) { 2693 return c.GetWalletInfoAsync().Receive() 2694 } 2695 2696 // FutureBackupWalletResult is a future promise to deliver the result of an 2697 // BackupWalletAsync RPC invocation (or an applicable error) 2698 type FutureBackupWalletResult chan *response 2699 2700 // Receive waits for the response promised by the future 2701 func (r FutureBackupWalletResult) Receive() error { 2702 _, err := receiveFuture(r) 2703 return err 2704 } 2705 2706 // BackupWalletAsync returns an instance of a type that can be used to get the result 2707 // of the RPC at some future time by invoking the Receive function on the 2708 // returned instance. 2709 // 2710 // See BackupWallet for the blocking version and more details. 2711 func (c *Client) BackupWalletAsync(destination string) FutureBackupWalletResult { 2712 return c.sendCmd(btcjson.NewBackupWalletCmd(destination)) 2713 } 2714 2715 // BackupWallet safely copies current wallet file to destination, which can 2716 // be a directory or a path with filename 2717 func (c *Client) BackupWallet(destination string) error { 2718 return c.BackupWalletAsync(destination).Receive() 2719 } 2720 2721 // FutureDumpWalletResult is a future promise to deliver the result of an 2722 // DumpWallet RPC invocation (or an applicable error) 2723 type FutureDumpWalletResult chan *response 2724 2725 // Receive waits for the response promised by the future 2726 func (r FutureDumpWalletResult) Receive() (*btcjson.DumpWalletResult, error) { 2727 bytes, err := receiveFuture(r) 2728 if err != nil { 2729 return nil, err 2730 } 2731 2732 var res btcjson.DumpWalletResult 2733 err = json.Unmarshal(bytes, &res) 2734 return &res, err 2735 } 2736 2737 // DumpWalletAsync returns an instance of a type that can be used to get the result 2738 // of the RPC at some future time by invoking the Receive function on the 2739 // returned instance. 2740 // 2741 // See DumpWalletAsync for the blocking version and more details. 2742 func (c *Client) DumpWalletAsync(destination string) FutureDumpWalletResult { 2743 return c.sendCmd(btcjson.NewDumpWalletCmd(destination)) 2744 } 2745 2746 // DumpWallet dumps all wallet keys in a human-readable format to a server-side file. 2747 func (c *Client) DumpWallet(destination string) (*btcjson.DumpWalletResult, error) { 2748 return c.DumpWalletAsync(destination).Receive() 2749 } 2750 2751 // FutureImportWalletResult is a future promise to deliver the result of an 2752 // ImportWalletAsync RPC invocation (or an applicable error) 2753 type FutureImportWalletResult chan *response 2754 2755 // Receive waits for the response promised by the future 2756 func (r FutureImportWalletResult) Receive() error { 2757 _, err := receiveFuture(r) 2758 return err 2759 } 2760 2761 // ImportWalletAsync returns an instance of a type that can be used to get the result 2762 // of the RPC at some future time by invoking the Receive function on the 2763 // returned instance. 2764 // 2765 // See ImportWallet for the blocking version and more details. 2766 func (c *Client) ImportWalletAsync(filename string) FutureImportWalletResult { 2767 return c.sendCmd(btcjson.NewImportWalletCmd(filename)) 2768 } 2769 2770 // ImportWallet imports keys from a wallet dump file (see DumpWallet). 2771 func (c *Client) ImportWallet(filename string) error { 2772 return c.ImportWalletAsync(filename).Receive() 2773 } 2774 2775 // FutureUnloadWalletResult is a future promise to deliver the result of an 2776 // UnloadWalletAsync RPC invocation (or an applicable error) 2777 type FutureUnloadWalletResult chan *response 2778 2779 // Receive waits for the response promised by the future 2780 func (r FutureUnloadWalletResult) Receive() error { 2781 _, err := receiveFuture(r) 2782 return err 2783 } 2784 2785 // UnloadWalletAsync returns an instance of a type that can be used to get the result 2786 // of the RPC at some future time by invoking the Receive function on the 2787 // returned instance. 2788 // 2789 // See UnloadWallet for the blocking version and more details. 2790 func (c *Client) UnloadWalletAsync(walletName *string) FutureUnloadWalletResult { 2791 return c.sendCmd(btcjson.NewUnloadWalletCmd(walletName)) 2792 } 2793 2794 // UnloadWallet unloads the referenced wallet. If the RPC server URL already 2795 // contains the name of the wallet, like http://127.0.0.1:1987/wallet/<walletname>, 2796 // the parameter must be nil, or it'll return an error. 2797 func (c *Client) UnloadWallet(walletName *string) error { 2798 return c.UnloadWalletAsync(walletName).Receive() 2799 } 2800 2801 // FutureLoadWalletResult is a future promise to deliver the result of an 2802 // LoadWalletAsync RPC invocation (or an applicable error) 2803 type FutureLoadWalletResult chan *response 2804 2805 // Receive waits for the response promised by the future 2806 func (r FutureLoadWalletResult) Receive() (*btcjson.LoadWalletResult, error) { 2807 bytes, err := receiveFuture(r) 2808 if err != nil { 2809 return nil, err 2810 } 2811 var result btcjson.LoadWalletResult 2812 err = json.Unmarshal(bytes, &result) 2813 return &result, err 2814 } 2815 2816 // LoadWalletAsync returns an instance of a type that can be used to get the result 2817 // of the RPC at some future time by invoking the Receive function on the 2818 // returned instance. 2819 // 2820 // See LoadWallet for the blocking version and more details. 2821 func (c *Client) LoadWalletAsync(walletName string) FutureLoadWalletResult { 2822 return c.sendCmd(btcjson.NewLoadWalletCmd(walletName)) 2823 } 2824 2825 // LoadWallet loads a wallet from a wallet file or directory. 2826 func (c *Client) LoadWallet(walletName string) (*btcjson.LoadWalletResult, error) { 2827 return c.LoadWalletAsync(walletName).Receive() 2828 } 2829 2830 // TODO(davec): Implement 2831 // encryptwallet (Won't be supported by btcwallet since it's always encrypted) 2832 // listaddressgroupings (NYI in btcwallet) 2833 // listreceivedbyaccount (NYI in btcwallet)