github.com/lbryio/lbcd@v0.22.119/rpcclient/rawtransactions.go (about) 1 // Copyright (c) 2014-2017 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package rpcclient 6 7 import ( 8 "bytes" 9 "encoding/hex" 10 "encoding/json" 11 12 "github.com/lbryio/lbcd/btcjson" 13 "github.com/lbryio/lbcd/chaincfg/chainhash" 14 "github.com/lbryio/lbcd/wire" 15 btcutil "github.com/lbryio/lbcutil" 16 ) 17 18 const ( 19 // defaultMaxFeeRate is the default maximum fee rate in sat/KB enforced 20 // by bitcoind v0.19.0 or after for transaction broadcast. 21 defaultMaxFeeRate = btcutil.SatoshiPerBitcoin / 2 22 ) 23 24 // SigHashType enumerates the available signature hashing types that the 25 // SignRawTransaction function accepts. 26 type SigHashType string 27 28 // Constants used to indicate the signature hash type for SignRawTransaction. 29 const ( 30 // SigHashAll indicates ALL of the outputs should be signed. 31 SigHashAll SigHashType = "ALL" 32 33 // SigHashNone indicates NONE of the outputs should be signed. This 34 // can be thought of as specifying the signer does not care where the 35 // bitcoins go. 36 SigHashNone SigHashType = "NONE" 37 38 // SigHashSingle indicates that a SINGLE output should be signed. This 39 // can be thought of specifying the signer only cares about where ONE of 40 // the outputs goes, but not any of the others. 41 SigHashSingle SigHashType = "SINGLE" 42 43 // SigHashAllAnyoneCanPay indicates that signer does not care where the 44 // other inputs to the transaction come from, so it allows other people 45 // to add inputs. In addition, it uses the SigHashAll signing method 46 // for outputs. 47 SigHashAllAnyoneCanPay SigHashType = "ALL|ANYONECANPAY" 48 49 // SigHashNoneAnyoneCanPay indicates that signer does not care where the 50 // other inputs to the transaction come from, so it allows other people 51 // to add inputs. In addition, it uses the SigHashNone signing method 52 // for outputs. 53 SigHashNoneAnyoneCanPay SigHashType = "NONE|ANYONECANPAY" 54 55 // SigHashSingleAnyoneCanPay indicates that signer does not care where 56 // the other inputs to the transaction come from, so it allows other 57 // people to add inputs. In addition, it uses the SigHashSingle signing 58 // method for outputs. 59 SigHashSingleAnyoneCanPay SigHashType = "SINGLE|ANYONECANPAY" 60 ) 61 62 // String returns the SighHashType in human-readable form. 63 func (s SigHashType) String() string { 64 return string(s) 65 } 66 67 // FutureGetRawTransactionResult is a future promise to deliver the result of a 68 // GetRawTransactionAsync RPC invocation (or an applicable error). 69 type FutureGetRawTransactionResult chan *Response 70 71 // Receive waits for the Response promised by the future and returns a 72 // transaction given its hash. 73 func (r FutureGetRawTransactionResult) Receive() (*btcutil.Tx, error) { 74 res, err := ReceiveFuture(r) 75 if err != nil { 76 return nil, err 77 } 78 79 // Unmarshal result as a string. 80 var txHex string 81 err = json.Unmarshal(res, &txHex) 82 if err != nil { 83 return nil, err 84 } 85 86 // Decode the serialized transaction hex to raw bytes. 87 serializedTx, err := hex.DecodeString(txHex) 88 if err != nil { 89 return nil, err 90 } 91 92 // Deserialize the transaction and return it. 93 var msgTx wire.MsgTx 94 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil { 95 return nil, err 96 } 97 return btcutil.NewTx(&msgTx), nil 98 } 99 100 // GetRawTransactionAsync returns an instance of a type that can be used to get 101 // the result of the RPC at some future time by invoking the Receive function on 102 // the returned instance. 103 // 104 // See GetRawTransaction for the blocking version and more details. 105 func (c *Client) GetRawTransactionAsync(txHash *chainhash.Hash) FutureGetRawTransactionResult { 106 hash := "" 107 if txHash != nil { 108 hash = txHash.String() 109 } 110 111 cmd := btcjson.NewGetRawTransactionCmd(hash, btcjson.Bool(false)) 112 return c.SendCmd(cmd) 113 } 114 115 // GetRawTransaction returns a transaction given its hash. 116 // 117 // See GetRawTransactionVerbose to obtain additional information about the 118 // transaction. 119 func (c *Client) GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error) { 120 return c.GetRawTransactionAsync(txHash).Receive() 121 } 122 123 // FutureGetRawTransactionVerboseResult is a future promise to deliver the 124 // result of a GetRawTransactionVerboseAsync RPC invocation (or an applicable 125 // error). 126 type FutureGetRawTransactionVerboseResult chan *Response 127 128 // Receive waits for the Response promised by the future and returns information 129 // about a transaction given its hash. 130 func (r FutureGetRawTransactionVerboseResult) Receive() (*btcjson.TxRawResult, error) { 131 res, err := ReceiveFuture(r) 132 if err != nil { 133 return nil, err 134 } 135 136 // Unmarshal result as a gettrawtransaction result object. 137 var rawTxResult btcjson.TxRawResult 138 err = json.Unmarshal(res, &rawTxResult) 139 if err != nil { 140 return nil, err 141 } 142 143 return &rawTxResult, nil 144 } 145 146 // GetRawTransactionVerboseAsync returns an instance of a type that can be used 147 // to get the result of the RPC at some future time by invoking the Receive 148 // function on the returned instance. 149 // 150 // See GetRawTransactionVerbose for the blocking version and more details. 151 func (c *Client) GetRawTransactionVerboseAsync(txHash *chainhash.Hash) FutureGetRawTransactionVerboseResult { 152 hash := "" 153 if txHash != nil { 154 hash = txHash.String() 155 } 156 157 cmd := btcjson.NewGetRawTransactionCmd(hash, btcjson.Bool(true)) 158 return c.SendCmd(cmd) 159 } 160 161 // GetRawTransactionVerbose returns information about a transaction given 162 // its hash. 163 // 164 // See GetRawTransaction to obtain only the transaction already deserialized. 165 func (c *Client) GetRawTransactionVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error) { 166 return c.GetRawTransactionVerboseAsync(txHash).Receive() 167 } 168 169 // FutureDecodeRawTransactionResult is a future promise to deliver the result 170 // of a DecodeRawTransactionAsync RPC invocation (or an applicable error). 171 type FutureDecodeRawTransactionResult chan *Response 172 173 // Receive waits for the Response promised by the future and returns information 174 // about a transaction given its serialized bytes. 175 func (r FutureDecodeRawTransactionResult) Receive() (*btcjson.TxRawResult, error) { 176 res, err := ReceiveFuture(r) 177 if err != nil { 178 return nil, err 179 } 180 181 // Unmarshal result as a decoderawtransaction result object. 182 var rawTxResult btcjson.TxRawResult 183 err = json.Unmarshal(res, &rawTxResult) 184 if err != nil { 185 return nil, err 186 } 187 188 return &rawTxResult, nil 189 } 190 191 // DecodeRawTransactionAsync returns an instance of a type that can be used to 192 // get the result of the RPC at some future time by invoking the Receive 193 // function on the returned instance. 194 // 195 // See DecodeRawTransaction for the blocking version and more details. 196 func (c *Client) DecodeRawTransactionAsync(serializedTx []byte) FutureDecodeRawTransactionResult { 197 txHex := hex.EncodeToString(serializedTx) 198 cmd := btcjson.NewDecodeRawTransactionCmd(txHex) 199 return c.SendCmd(cmd) 200 } 201 202 // DecodeRawTransaction returns information about a transaction given its 203 // serialized bytes. 204 func (c *Client) DecodeRawTransaction(serializedTx []byte) (*btcjson.TxRawResult, error) { 205 return c.DecodeRawTransactionAsync(serializedTx).Receive() 206 } 207 208 // FutureFundRawTransactionResult is a future promise to deliver the result 209 // of a FutureFundRawTransactionAsync RPC invocation (or an applicable error). 210 type FutureFundRawTransactionResult chan *Response 211 212 // Receive waits for the Response promised by the future and returns information 213 // about a funding attempt 214 func (r FutureFundRawTransactionResult) Receive() (*btcjson.FundRawTransactionResult, error) { 215 res, err := ReceiveFuture(r) 216 if err != nil { 217 return nil, err 218 } 219 220 var marshalled btcjson.FundRawTransactionResult 221 if err := json.Unmarshal(res, &marshalled); err != nil { 222 return nil, err 223 } 224 225 return &marshalled, nil 226 } 227 228 // FundRawTransactionAsync returns an instance of a type that can be used to 229 // get the result of the RPC at some future time by invoking the Receive 230 // function on the returned instance. 231 // 232 // See FundRawTransaction for the blocking version and more details. 233 func (c *Client) FundRawTransactionAsync(tx *wire.MsgTx, opts btcjson.FundRawTransactionOpts, isWitness *bool) FutureFundRawTransactionResult { 234 var txBuf bytes.Buffer 235 if err := tx.Serialize(&txBuf); err != nil { 236 return newFutureError(err) 237 } 238 239 cmd := btcjson.NewFundRawTransactionCmd(txBuf.Bytes(), opts, isWitness) 240 return c.SendCmd(cmd) 241 } 242 243 // FundRawTransaction returns the result of trying to fund the given transaction with 244 // funds from the node wallet 245 func (c *Client) FundRawTransaction(tx *wire.MsgTx, opts btcjson.FundRawTransactionOpts, isWitness *bool) (*btcjson.FundRawTransactionResult, error) { 246 return c.FundRawTransactionAsync(tx, opts, isWitness).Receive() 247 } 248 249 // FutureCreateRawTransactionResult is a future promise to deliver the result 250 // of a CreateRawTransactionAsync RPC invocation (or an applicable error). 251 type FutureCreateRawTransactionResult chan *Response 252 253 // Receive waits for the Response promised by the future and returns a new 254 // transaction spending the provided inputs and sending to the provided 255 // addresses. 256 func (r FutureCreateRawTransactionResult) Receive() (*wire.MsgTx, error) { 257 res, err := ReceiveFuture(r) 258 if err != nil { 259 return nil, err 260 } 261 262 // Unmarshal result as a string. 263 var txHex string 264 err = json.Unmarshal(res, &txHex) 265 if err != nil { 266 return nil, err 267 } 268 269 // Decode the serialized transaction hex to raw bytes. 270 serializedTx, err := hex.DecodeString(txHex) 271 if err != nil { 272 return nil, err 273 } 274 275 // Deserialize the transaction and return it. 276 var msgTx wire.MsgTx 277 // we try both the new and old encoding format 278 witnessErr := msgTx.Deserialize(bytes.NewReader(serializedTx)) 279 if witnessErr != nil { 280 legacyErr := msgTx.DeserializeNoWitness(bytes.NewReader(serializedTx)) 281 if legacyErr != nil { 282 return nil, legacyErr 283 } 284 } 285 return &msgTx, nil 286 } 287 288 // CreateRawTransactionAsync returns an instance of a type that can be used to 289 // get the result of the RPC at some future time by invoking the Receive 290 // function on the returned instance. 291 // 292 // See CreateRawTransaction for the blocking version and more details. 293 func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput, 294 outputs map[btcutil.Address]interface{}, lockTime *int64) FutureCreateRawTransactionResult { 295 296 convertedData := make(map[string]interface{}, len(outputs)) 297 for key, value := range outputs { 298 switch val := value.(type) { 299 case btcutil.Amount: 300 convertedData[key.String()] = val.ToBTC() 301 case string: 302 convertedData[key.String()] = val 303 } 304 } 305 cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedData, lockTime) 306 return c.SendCmd(cmd) 307 } 308 309 // CreateRawTransaction returns a new transaction spending the provided inputs 310 // and sending to the provided addresses. If the inputs are either nil or an 311 // empty slice, it is interpreted as an empty slice. 312 func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput, 313 outputs map[btcutil.Address]interface{}, lockTime *int64) (*wire.MsgTx, error) { 314 315 return c.CreateRawTransactionAsync(inputs, outputs, lockTime).Receive() 316 } 317 318 // FutureSendRawTransactionResult is a future promise to deliver the result 319 // of a SendRawTransactionAsync RPC invocation (or an applicable error). 320 type FutureSendRawTransactionResult chan *Response 321 322 // Receive waits for the Response promised by the future and returns the result 323 // of submitting the encoded transaction to the server which then relays it to 324 // the network. 325 func (r FutureSendRawTransactionResult) Receive() (*chainhash.Hash, error) { 326 res, err := ReceiveFuture(r) 327 if err != nil { 328 return nil, err 329 } 330 331 // Unmarshal result as a string. 332 var txHashStr string 333 err = json.Unmarshal(res, &txHashStr) 334 if err != nil { 335 return nil, err 336 } 337 338 return chainhash.NewHashFromStr(txHashStr) 339 } 340 341 // SendRawTransactionAsync returns an instance of a type that can be used to get 342 // the result of the RPC at some future time by invoking the Receive function on 343 // the returned instance. 344 // 345 // See SendRawTransaction for the blocking version and more details. 346 func (c *Client) SendRawTransactionAsync(tx *wire.MsgTx, allowHighFees bool) FutureSendRawTransactionResult { 347 txHex := "" 348 if tx != nil { 349 // Serialize the transaction and convert to hex string. 350 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 351 if err := tx.Serialize(buf); err != nil { 352 return newFutureError(err) 353 } 354 txHex = hex.EncodeToString(buf.Bytes()) 355 } 356 357 // Due to differences in the sendrawtransaction API for different 358 // backends, we'll need to inspect our version and construct the 359 // appropriate request. 360 version, err := c.BackendVersion() 361 if err != nil { 362 return newFutureError(err) 363 } 364 365 var cmd *btcjson.SendRawTransactionCmd 366 switch version { 367 // Starting from bitcoind v0.19.0, the MaxFeeRate field should be used. 368 case BitcoindPost19: 369 // Using a 0 MaxFeeRate is interpreted as a maximum fee rate not 370 // being enforced by bitcoind. 371 var maxFeeRate int32 372 if !allowHighFees { 373 maxFeeRate = defaultMaxFeeRate 374 } 375 cmd = btcjson.NewBitcoindSendRawTransactionCmd(txHex, maxFeeRate) 376 377 // Otherwise, use the AllowHighFees field. 378 default: 379 cmd = btcjson.NewSendRawTransactionCmd(txHex, &allowHighFees) 380 } 381 382 return c.SendCmd(cmd) 383 } 384 385 // SendRawTransaction submits the encoded transaction to the server which will 386 // then relay it to the network. 387 func (c *Client) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error) { 388 return c.SendRawTransactionAsync(tx, allowHighFees).Receive() 389 } 390 391 // FutureSignRawTransactionResult is a future promise to deliver the result 392 // of one of the SignRawTransactionAsync family of RPC invocations (or an 393 // applicable error). 394 type FutureSignRawTransactionResult chan *Response 395 396 // Receive waits for the Response promised by the future and returns the 397 // signed transaction as well as whether or not all inputs are now signed. 398 func (r FutureSignRawTransactionResult) Receive() (*wire.MsgTx, bool, error) { 399 res, err := ReceiveFuture(r) 400 if err != nil { 401 return nil, false, err 402 } 403 404 // Unmarshal as a signrawtransaction result. 405 var signRawTxResult btcjson.SignRawTransactionResult 406 err = json.Unmarshal(res, &signRawTxResult) 407 if err != nil { 408 return nil, false, err 409 } 410 411 // Decode the serialized transaction hex to raw bytes. 412 serializedTx, err := hex.DecodeString(signRawTxResult.Hex) 413 if err != nil { 414 return nil, false, err 415 } 416 417 // Deserialize the transaction and return it. 418 var msgTx wire.MsgTx 419 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil { 420 return nil, false, err 421 } 422 423 return &msgTx, signRawTxResult.Complete, nil 424 } 425 426 // SignRawTransactionAsync returns an instance of a type that can be used to get 427 // the result of the RPC at some future time by invoking the Receive function on 428 // the returned instance. 429 // 430 // See SignRawTransaction for the blocking version and more details. 431 func (c *Client) SignRawTransactionAsync(tx *wire.MsgTx) FutureSignRawTransactionResult { 432 txHex := "" 433 if tx != nil { 434 // Serialize the transaction and convert to hex string. 435 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 436 if err := tx.Serialize(buf); err != nil { 437 return newFutureError(err) 438 } 439 txHex = hex.EncodeToString(buf.Bytes()) 440 } 441 442 cmd := btcjson.NewSignRawTransactionCmd(txHex, nil, nil, nil) 443 return c.SendCmd(cmd) 444 } 445 446 // SignRawTransaction signs inputs for the passed transaction and returns the 447 // signed transaction as well as whether or not all inputs are now signed. 448 // 449 // This function assumes the RPC server already knows the input transactions and 450 // private keys for the passed transaction which needs to be signed and uses the 451 // default signature hash type. Use one of the SignRawTransaction# variants to 452 // specify that information if needed. 453 func (c *Client) SignRawTransaction(tx *wire.MsgTx) (*wire.MsgTx, bool, error) { 454 return c.SignRawTransactionAsync(tx).Receive() 455 } 456 457 // SignRawTransaction2Async returns an instance of a type that can be used to 458 // get the result of the RPC at some future time by invoking the Receive 459 // function on the returned instance. 460 // 461 // See SignRawTransaction2 for the blocking version and more details. 462 func (c *Client) SignRawTransaction2Async(tx *wire.MsgTx, inputs []btcjson.RawTxInput) FutureSignRawTransactionResult { 463 txHex := "" 464 if tx != nil { 465 // Serialize the transaction and convert to hex string. 466 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 467 if err := tx.Serialize(buf); err != nil { 468 return newFutureError(err) 469 } 470 txHex = hex.EncodeToString(buf.Bytes()) 471 } 472 473 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, nil, nil) 474 return c.SendCmd(cmd) 475 } 476 477 // SignRawTransaction2 signs inputs for the passed transaction given the list 478 // of information about the input transactions needed to perform the signing 479 // process. 480 // 481 // This only input transactions that need to be specified are ones the 482 // RPC server does not already know. Already known input transactions will be 483 // merged with the specified transactions. 484 // 485 // See SignRawTransaction if the RPC server already knows the input 486 // transactions. 487 func (c *Client) SignRawTransaction2(tx *wire.MsgTx, inputs []btcjson.RawTxInput) (*wire.MsgTx, bool, error) { 488 return c.SignRawTransaction2Async(tx, inputs).Receive() 489 } 490 491 // SignRawTransaction3Async returns an instance of a type that can be used to 492 // get the result of the RPC at some future time by invoking the Receive 493 // function on the returned instance. 494 // 495 // See SignRawTransaction3 for the blocking version and more details. 496 func (c *Client) SignRawTransaction3Async(tx *wire.MsgTx, 497 inputs []btcjson.RawTxInput, 498 privKeysWIF []string) FutureSignRawTransactionResult { 499 500 txHex := "" 501 if tx != nil { 502 // Serialize the transaction and convert to hex string. 503 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 504 if err := tx.Serialize(buf); err != nil { 505 return newFutureError(err) 506 } 507 txHex = hex.EncodeToString(buf.Bytes()) 508 } 509 510 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF, 511 nil) 512 return c.SendCmd(cmd) 513 } 514 515 // SignRawTransaction3 signs inputs for the passed transaction given the list 516 // of information about extra input transactions and a list of private keys 517 // needed to perform the signing process. The private keys must be in wallet 518 // import format (WIF). 519 // 520 // This only input transactions that need to be specified are ones the 521 // RPC server does not already know. Already known input transactions will be 522 // merged with the specified transactions. This means the list of transaction 523 // inputs can be nil if the RPC server already knows them all. 524 // 525 // NOTE: Unlike the merging functionality of the input transactions, ONLY the 526 // specified private keys will be used, so even if the server already knows some 527 // of the private keys, they will NOT be used. 528 // 529 // See SignRawTransaction if the RPC server already knows the input 530 // transactions and private keys or SignRawTransaction2 if it already knows the 531 // private keys. 532 func (c *Client) SignRawTransaction3(tx *wire.MsgTx, 533 inputs []btcjson.RawTxInput, 534 privKeysWIF []string) (*wire.MsgTx, bool, error) { 535 536 return c.SignRawTransaction3Async(tx, inputs, privKeysWIF).Receive() 537 } 538 539 // SignRawTransaction4Async returns an instance of a type that can be used to 540 // get the result of the RPC at some future time by invoking the Receive 541 // function on the returned instance. 542 // 543 // See SignRawTransaction4 for the blocking version and more details. 544 func (c *Client) SignRawTransaction4Async(tx *wire.MsgTx, 545 inputs []btcjson.RawTxInput, privKeysWIF []string, 546 hashType SigHashType) FutureSignRawTransactionResult { 547 548 txHex := "" 549 if tx != nil { 550 // Serialize the transaction and convert to hex string. 551 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 552 if err := tx.Serialize(buf); err != nil { 553 return newFutureError(err) 554 } 555 txHex = hex.EncodeToString(buf.Bytes()) 556 } 557 558 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF, 559 btcjson.String(string(hashType))) 560 return c.SendCmd(cmd) 561 } 562 563 // SignRawTransaction4 signs inputs for the passed transaction using 564 // the specified signature hash type given the list of information about extra 565 // input transactions and a potential list of private keys needed to perform 566 // the signing process. The private keys, if specified, must be in wallet 567 // import format (WIF). 568 // 569 // The only input transactions that need to be specified are ones the RPC server 570 // does not already know. This means the list of transaction inputs can be nil 571 // if the RPC server already knows them all. 572 // 573 // NOTE: Unlike the merging functionality of the input transactions, ONLY the 574 // specified private keys will be used, so even if the server already knows some 575 // of the private keys, they will NOT be used. The list of private keys can be 576 // nil in which case any private keys the RPC server knows will be used. 577 // 578 // This function should only used if a non-default signature hash type is 579 // desired. Otherwise, see SignRawTransaction if the RPC server already knows 580 // the input transactions and private keys, SignRawTransaction2 if it already 581 // knows the private keys, or SignRawTransaction3 if it does not know both. 582 func (c *Client) SignRawTransaction4(tx *wire.MsgTx, 583 inputs []btcjson.RawTxInput, privKeysWIF []string, 584 hashType SigHashType) (*wire.MsgTx, bool, error) { 585 586 return c.SignRawTransaction4Async(tx, inputs, privKeysWIF, 587 hashType).Receive() 588 } 589 590 // FutureSignRawTransactionWithWalletResult is a future promise to deliver 591 // the result of the SignRawTransactionWithWalletAsync RPC invocation (or 592 // an applicable error). 593 type FutureSignRawTransactionWithWalletResult chan *Response 594 595 // Receive waits for the Response promised by the future and returns the 596 // signed transaction as well as whether or not all inputs are now signed. 597 func (r FutureSignRawTransactionWithWalletResult) Receive() (*wire.MsgTx, bool, error) { 598 res, err := ReceiveFuture(r) 599 if err != nil { 600 return nil, false, err 601 } 602 603 // Unmarshal as a signtransactionwithwallet result. 604 var signRawTxWithWalletResult btcjson.SignRawTransactionWithWalletResult 605 err = json.Unmarshal(res, &signRawTxWithWalletResult) 606 if err != nil { 607 return nil, false, err 608 } 609 610 // Decode the serialized transaction hex to raw bytes. 611 serializedTx, err := hex.DecodeString(signRawTxWithWalletResult.Hex) 612 if err != nil { 613 return nil, false, err 614 } 615 616 // Deserialize the transaction and return it. 617 var msgTx wire.MsgTx 618 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil { 619 return nil, false, err 620 } 621 622 return &msgTx, signRawTxWithWalletResult.Complete, nil 623 } 624 625 // SignRawTransactionWithWalletAsync returns an instance of a type that can be used 626 // to get the result of the RPC at some future time by invoking the Receive function 627 // on the returned instance. 628 // 629 // See SignRawTransactionWithWallet for the blocking version and more details. 630 func (c *Client) SignRawTransactionWithWalletAsync(tx *wire.MsgTx) FutureSignRawTransactionWithWalletResult { 631 txHex := "" 632 if tx != nil { 633 // Serialize the transaction and convert to hex string. 634 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 635 if err := tx.Serialize(buf); err != nil { 636 return newFutureError(err) 637 } 638 txHex = hex.EncodeToString(buf.Bytes()) 639 } 640 641 cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, nil, nil) 642 return c.SendCmd(cmd) 643 } 644 645 // SignRawTransactionWithWallet signs inputs for the passed transaction and returns 646 // the signed transaction as well as whether or not all inputs are now signed. 647 // 648 // This function assumes the RPC server already knows the input transactions for the 649 // passed transaction which needs to be signed and uses the default signature hash 650 // type. Use one of the SignRawTransactionWithWallet# variants to specify that 651 // information if needed. 652 func (c *Client) SignRawTransactionWithWallet(tx *wire.MsgTx) (*wire.MsgTx, bool, error) { 653 return c.SignRawTransactionWithWalletAsync(tx).Receive() 654 } 655 656 // SignRawTransactionWithWallet2Async returns an instance of a type that can be 657 // used to get the result of the RPC at some future time by invoking the Receive 658 // function on the returned instance. 659 // 660 // See SignRawTransactionWithWallet2 for the blocking version and more details. 661 func (c *Client) SignRawTransactionWithWallet2Async(tx *wire.MsgTx, 662 inputs []btcjson.RawTxWitnessInput) FutureSignRawTransactionWithWalletResult { 663 664 txHex := "" 665 if tx != nil { 666 // Serialize the transaction and convert to hex string. 667 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 668 if err := tx.Serialize(buf); err != nil { 669 return newFutureError(err) 670 } 671 txHex = hex.EncodeToString(buf.Bytes()) 672 } 673 674 cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, &inputs, nil) 675 return c.SendCmd(cmd) 676 } 677 678 // SignRawTransactionWithWallet2 signs inputs for the passed transaction given the 679 // list of information about the input transactions needed to perform the signing 680 // process. 681 // 682 // This only input transactions that need to be specified are ones the 683 // RPC server does not already know. Already known input transactions will be 684 // merged with the specified transactions. 685 // 686 // See SignRawTransactionWithWallet if the RPC server already knows the input 687 // transactions. 688 func (c *Client) SignRawTransactionWithWallet2(tx *wire.MsgTx, 689 inputs []btcjson.RawTxWitnessInput) (*wire.MsgTx, bool, error) { 690 691 return c.SignRawTransactionWithWallet2Async(tx, inputs).Receive() 692 } 693 694 // SignRawTransactionWithWallet3Async returns an instance of a type that can 695 // be used to get the result of the RPC at some future time by invoking the 696 // Receive function on the returned instance. 697 // 698 // See SignRawTransactionWithWallet3 for the blocking version and more details. 699 func (c *Client) SignRawTransactionWithWallet3Async(tx *wire.MsgTx, 700 inputs []btcjson.RawTxWitnessInput, hashType SigHashType) FutureSignRawTransactionWithWalletResult { 701 702 txHex := "" 703 if tx != nil { 704 // Serialize the transaction and convert to hex string. 705 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) 706 if err := tx.Serialize(buf); err != nil { 707 return newFutureError(err) 708 } 709 txHex = hex.EncodeToString(buf.Bytes()) 710 } 711 712 cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, &inputs, btcjson.String(string(hashType))) 713 return c.SendCmd(cmd) 714 } 715 716 // SignRawTransactionWithWallet3 signs inputs for the passed transaction using 717 // the specified signature hash type given the list of information about extra 718 // input transactions. 719 // 720 // The only input transactions that need to be specified are ones the RPC server 721 // does not already know. This means the list of transaction inputs can be nil 722 // if the RPC server already knows them all. 723 // 724 // This function should only used if a non-default signature hash type is 725 // desired. Otherwise, see SignRawTransactionWithWallet if the RPC server already 726 // knows the input transactions, or SignRawTransactionWihWallet2 if it does not. 727 func (c *Client) SignRawTransactionWithWallet3(tx *wire.MsgTx, 728 inputs []btcjson.RawTxWitnessInput, hashType SigHashType) (*wire.MsgTx, bool, error) { 729 730 return c.SignRawTransactionWithWallet3Async(tx, inputs, hashType).Receive() 731 } 732 733 // FutureSearchRawTransactionsResult is a future promise to deliver the result 734 // of the SearchRawTransactionsAsync RPC invocation (or an applicable error). 735 type FutureSearchRawTransactionsResult chan *Response 736 737 // Receive waits for the Response promised by the future and returns the 738 // found raw transactions. 739 func (r FutureSearchRawTransactionsResult) Receive() ([]*wire.MsgTx, error) { 740 res, err := ReceiveFuture(r) 741 if err != nil { 742 return nil, err 743 } 744 745 // Unmarshal as an array of strings. 746 var searchRawTxnsResult []string 747 err = json.Unmarshal(res, &searchRawTxnsResult) 748 if err != nil { 749 return nil, err 750 } 751 752 // Decode and deserialize each transaction. 753 msgTxns := make([]*wire.MsgTx, 0, len(searchRawTxnsResult)) 754 for _, hexTx := range searchRawTxnsResult { 755 // Decode the serialized transaction hex to raw bytes. 756 serializedTx, err := hex.DecodeString(hexTx) 757 if err != nil { 758 return nil, err 759 } 760 761 // Deserialize the transaction and add it to the result slice. 762 var msgTx wire.MsgTx 763 err = msgTx.Deserialize(bytes.NewReader(serializedTx)) 764 if err != nil { 765 return nil, err 766 } 767 msgTxns = append(msgTxns, &msgTx) 768 } 769 770 return msgTxns, nil 771 } 772 773 // SearchRawTransactionsAsync returns an instance of a type that can be used to 774 // get the result of the RPC at some future time by invoking the Receive 775 // function on the returned instance. 776 // 777 // See SearchRawTransactions for the blocking version and more details. 778 func (c *Client) SearchRawTransactionsAsync(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult { 779 addr := address.EncodeAddress() 780 verbose := btcjson.Int(0) 781 cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count, 782 nil, &reverse, &filterAddrs) 783 return c.SendCmd(cmd) 784 } 785 786 // SearchRawTransactions returns transactions that involve the passed address. 787 // 788 // NOTE: Chain servers do not typically provide this capability unless it has 789 // specifically been enabled. 790 // 791 // See SearchRawTransactionsVerbose to retrieve a list of data structures with 792 // information about the transactions instead of the transactions themselves. 793 func (c *Client) SearchRawTransactions(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) ([]*wire.MsgTx, error) { 794 return c.SearchRawTransactionsAsync(address, skip, count, reverse, filterAddrs).Receive() 795 } 796 797 // FutureSearchRawTransactionsVerboseResult is a future promise to deliver the 798 // result of the SearchRawTransactionsVerboseAsync RPC invocation (or an 799 // applicable error). 800 type FutureSearchRawTransactionsVerboseResult chan *Response 801 802 // Receive waits for the Response promised by the future and returns the 803 // found raw transactions. 804 func (r FutureSearchRawTransactionsVerboseResult) Receive() ([]*btcjson.SearchRawTransactionsResult, error) { 805 res, err := ReceiveFuture(r) 806 if err != nil { 807 return nil, err 808 } 809 810 // Unmarshal as an array of raw transaction results. 811 var result []*btcjson.SearchRawTransactionsResult 812 err = json.Unmarshal(res, &result) 813 if err != nil { 814 return nil, err 815 } 816 817 return result, nil 818 } 819 820 // SearchRawTransactionsVerboseAsync returns an instance of a type that can be 821 // used to get the result of the RPC at some future time by invoking the Receive 822 // function on the returned instance. 823 // 824 // See SearchRawTransactionsVerbose for the blocking version and more details. 825 func (c *Client) SearchRawTransactionsVerboseAsync(address btcutil.Address, skip, 826 count int, includePrevOut, reverse bool, filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult { 827 828 addr := address.EncodeAddress() 829 verbose := btcjson.Int(1) 830 var prevOut *int 831 if includePrevOut { 832 prevOut = btcjson.Int(1) 833 } 834 cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count, 835 prevOut, &reverse, filterAddrs) 836 return c.SendCmd(cmd) 837 } 838 839 // SearchRawTransactionsVerbose returns a list of data structures that describe 840 // transactions which involve the passed address. 841 // 842 // NOTE: Chain servers do not typically provide this capability unless it has 843 // specifically been enabled. 844 // 845 // See SearchRawTransactions to retrieve a list of raw transactions instead. 846 func (c *Client) SearchRawTransactionsVerbose(address btcutil.Address, skip, 847 count int, includePrevOut, reverse bool, filterAddrs []string) ([]*btcjson.SearchRawTransactionsResult, error) { 848 849 return c.SearchRawTransactionsVerboseAsync(address, skip, count, 850 includePrevOut, reverse, &filterAddrs).Receive() 851 } 852 853 // FutureDecodeScriptResult is a future promise to deliver the result 854 // of a DecodeScriptAsync RPC invocation (or an applicable error). 855 type FutureDecodeScriptResult chan *Response 856 857 // Receive waits for the Response promised by the future and returns information 858 // about a script given its serialized bytes. 859 func (r FutureDecodeScriptResult) Receive() (*btcjson.DecodeScriptResult, error) { 860 res, err := ReceiveFuture(r) 861 if err != nil { 862 return nil, err 863 } 864 865 // Unmarshal result as a decodescript result object. 866 var decodeScriptResult btcjson.DecodeScriptResult 867 err = json.Unmarshal(res, &decodeScriptResult) 868 if err != nil { 869 return nil, err 870 } 871 872 return &decodeScriptResult, nil 873 } 874 875 // DecodeScriptAsync returns an instance of a type that can be used to 876 // get the result of the RPC at some future time by invoking the Receive 877 // function on the returned instance. 878 // 879 // See DecodeScript for the blocking version and more details. 880 func (c *Client) DecodeScriptAsync(serializedScript []byte) FutureDecodeScriptResult { 881 scriptHex := hex.EncodeToString(serializedScript) 882 cmd := btcjson.NewDecodeScriptCmd(scriptHex) 883 return c.SendCmd(cmd) 884 } 885 886 // DecodeScript returns information about a script given its serialized bytes. 887 func (c *Client) DecodeScript(serializedScript []byte) (*btcjson.DecodeScriptResult, error) { 888 return c.DecodeScriptAsync(serializedScript).Receive() 889 }