github.com/btcsuite/btcd@v0.24.0/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/btcsuite/btcd/btcjson"
    13  	"github.com/btcsuite/btcd/btcutil"
    14  	"github.com/btcsuite/btcd/chaincfg/chainhash"
    15  	"github.com/btcsuite/btcd/wire"
    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 / 10
    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.Int(0))
   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.Int(1))
   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  	amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult {
   295  
   296  	convertedAmts := make(map[string]float64, len(amounts))
   297  	for addr, amount := range amounts {
   298  		convertedAmts[addr.String()] = amount.ToBTC()
   299  	}
   300  	cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedAmts, lockTime)
   301  	return c.SendCmd(cmd)
   302  }
   303  
   304  // CreateRawTransaction returns a new transaction spending the provided inputs
   305  // and sending to the provided addresses. If the inputs are either nil or an
   306  // empty slice, it is interpreted as an empty slice.
   307  func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput,
   308  	amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) {
   309  
   310  	return c.CreateRawTransactionAsync(inputs, amounts, lockTime).Receive()
   311  }
   312  
   313  // FutureSendRawTransactionResult is a future promise to deliver the result
   314  // of a SendRawTransactionAsync RPC invocation (or an applicable error).
   315  type FutureSendRawTransactionResult chan *Response
   316  
   317  // Receive waits for the Response promised by the future and returns the result
   318  // of submitting the encoded transaction to the server which then relays it to
   319  // the network.
   320  func (r FutureSendRawTransactionResult) Receive() (*chainhash.Hash, error) {
   321  	res, err := ReceiveFuture(r)
   322  	if err != nil {
   323  		return nil, err
   324  	}
   325  
   326  	// Unmarshal result as a string.
   327  	var txHashStr string
   328  	err = json.Unmarshal(res, &txHashStr)
   329  	if err != nil {
   330  		return nil, err
   331  	}
   332  
   333  	return chainhash.NewHashFromStr(txHashStr)
   334  }
   335  
   336  // SendRawTransactionAsync returns an instance of a type that can be used to get
   337  // the result of the RPC at some future time by invoking the Receive function on
   338  // the returned instance.
   339  //
   340  // See SendRawTransaction for the blocking version and more details.
   341  func (c *Client) SendRawTransactionAsync(tx *wire.MsgTx, allowHighFees bool) FutureSendRawTransactionResult {
   342  	txHex := ""
   343  	if tx != nil {
   344  		// Serialize the transaction and convert to hex string.
   345  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   346  		if err := tx.Serialize(buf); err != nil {
   347  			return newFutureError(err)
   348  		}
   349  		txHex = hex.EncodeToString(buf.Bytes())
   350  	}
   351  
   352  	// Due to differences in the sendrawtransaction API for different
   353  	// backends, we'll need to inspect our version and construct the
   354  	// appropriate request.
   355  	version, err := c.BackendVersion()
   356  	if err != nil {
   357  		return newFutureError(err)
   358  	}
   359  
   360  	var cmd *btcjson.SendRawTransactionCmd
   361  	switch version {
   362  	// Starting from bitcoind v0.19.0, the MaxFeeRate field should be used.
   363  	case BitcoindPost19:
   364  		// Using a 0 MaxFeeRate is interpreted as a maximum fee rate not
   365  		// being enforced by bitcoind.
   366  		var maxFeeRate int32
   367  		if !allowHighFees {
   368  			maxFeeRate = defaultMaxFeeRate
   369  		}
   370  		cmd = btcjson.NewBitcoindSendRawTransactionCmd(txHex, maxFeeRate)
   371  
   372  	// Otherwise, use the AllowHighFees field.
   373  	default:
   374  		cmd = btcjson.NewSendRawTransactionCmd(txHex, &allowHighFees)
   375  	}
   376  
   377  	return c.SendCmd(cmd)
   378  }
   379  
   380  // SendRawTransaction submits the encoded transaction to the server which will
   381  // then relay it to the network.
   382  func (c *Client) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error) {
   383  	return c.SendRawTransactionAsync(tx, allowHighFees).Receive()
   384  }
   385  
   386  // FutureSignRawTransactionResult is a future promise to deliver the result
   387  // of one of the SignRawTransactionAsync family of RPC invocations (or an
   388  // applicable error).
   389  type FutureSignRawTransactionResult chan *Response
   390  
   391  // Receive waits for the Response promised by the future and returns the
   392  // signed transaction as well as whether or not all inputs are now signed.
   393  func (r FutureSignRawTransactionResult) Receive() (*wire.MsgTx, bool, error) {
   394  	res, err := ReceiveFuture(r)
   395  	if err != nil {
   396  		return nil, false, err
   397  	}
   398  
   399  	// Unmarshal as a signrawtransaction result.
   400  	var signRawTxResult btcjson.SignRawTransactionResult
   401  	err = json.Unmarshal(res, &signRawTxResult)
   402  	if err != nil {
   403  		return nil, false, err
   404  	}
   405  
   406  	// Decode the serialized transaction hex to raw bytes.
   407  	serializedTx, err := hex.DecodeString(signRawTxResult.Hex)
   408  	if err != nil {
   409  		return nil, false, err
   410  	}
   411  
   412  	// Deserialize the transaction and return it.
   413  	var msgTx wire.MsgTx
   414  	if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil {
   415  		return nil, false, err
   416  	}
   417  
   418  	return &msgTx, signRawTxResult.Complete, nil
   419  }
   420  
   421  // SignRawTransactionAsync returns an instance of a type that can be used to get
   422  // the result of the RPC at some future time by invoking the Receive function on
   423  // the returned instance.
   424  //
   425  // See SignRawTransaction for the blocking version and more details.
   426  func (c *Client) SignRawTransactionAsync(tx *wire.MsgTx) FutureSignRawTransactionResult {
   427  	txHex := ""
   428  	if tx != nil {
   429  		// Serialize the transaction and convert to hex string.
   430  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   431  		if err := tx.Serialize(buf); err != nil {
   432  			return newFutureError(err)
   433  		}
   434  		txHex = hex.EncodeToString(buf.Bytes())
   435  	}
   436  
   437  	cmd := btcjson.NewSignRawTransactionCmd(txHex, nil, nil, nil)
   438  	return c.SendCmd(cmd)
   439  }
   440  
   441  // SignRawTransaction signs inputs for the passed transaction and returns the
   442  // signed transaction as well as whether or not all inputs are now signed.
   443  //
   444  // This function assumes the RPC server already knows the input transactions and
   445  // private keys for the passed transaction which needs to be signed and uses the
   446  // default signature hash type.  Use one of the SignRawTransaction# variants to
   447  // specify that information if needed.
   448  func (c *Client) SignRawTransaction(tx *wire.MsgTx) (*wire.MsgTx, bool, error) {
   449  	return c.SignRawTransactionAsync(tx).Receive()
   450  }
   451  
   452  // SignRawTransaction2Async returns an instance of a type that can be used to
   453  // get the result of the RPC at some future time by invoking the Receive
   454  // function on the returned instance.
   455  //
   456  // See SignRawTransaction2 for the blocking version and more details.
   457  func (c *Client) SignRawTransaction2Async(tx *wire.MsgTx, inputs []btcjson.RawTxInput) FutureSignRawTransactionResult {
   458  	txHex := ""
   459  	if tx != nil {
   460  		// Serialize the transaction and convert to hex string.
   461  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   462  		if err := tx.Serialize(buf); err != nil {
   463  			return newFutureError(err)
   464  		}
   465  		txHex = hex.EncodeToString(buf.Bytes())
   466  	}
   467  
   468  	cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, nil, nil)
   469  	return c.SendCmd(cmd)
   470  }
   471  
   472  // SignRawTransaction2 signs inputs for the passed transaction given the list
   473  // of information about the input transactions needed to perform the signing
   474  // process.
   475  //
   476  // This only input transactions that need to be specified are ones the
   477  // RPC server does not already know.  Already known input transactions will be
   478  // merged with the specified transactions.
   479  //
   480  // See SignRawTransaction if the RPC server already knows the input
   481  // transactions.
   482  func (c *Client) SignRawTransaction2(tx *wire.MsgTx, inputs []btcjson.RawTxInput) (*wire.MsgTx, bool, error) {
   483  	return c.SignRawTransaction2Async(tx, inputs).Receive()
   484  }
   485  
   486  // SignRawTransaction3Async returns an instance of a type that can be used to
   487  // get the result of the RPC at some future time by invoking the Receive
   488  // function on the returned instance.
   489  //
   490  // See SignRawTransaction3 for the blocking version and more details.
   491  func (c *Client) SignRawTransaction3Async(tx *wire.MsgTx,
   492  	inputs []btcjson.RawTxInput,
   493  	privKeysWIF []string) FutureSignRawTransactionResult {
   494  
   495  	txHex := ""
   496  	if tx != nil {
   497  		// Serialize the transaction and convert to hex string.
   498  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   499  		if err := tx.Serialize(buf); err != nil {
   500  			return newFutureError(err)
   501  		}
   502  		txHex = hex.EncodeToString(buf.Bytes())
   503  	}
   504  
   505  	cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF,
   506  		nil)
   507  	return c.SendCmd(cmd)
   508  }
   509  
   510  // SignRawTransaction3 signs inputs for the passed transaction given the list
   511  // of information about extra input transactions and a list of private keys
   512  // needed to perform the signing process.  The private keys must be in wallet
   513  // import format (WIF).
   514  //
   515  // This only input transactions that need to be specified are ones the
   516  // RPC server does not already know.  Already known input transactions will be
   517  // merged with the specified transactions.  This means the list of transaction
   518  // inputs can be nil if the RPC server already knows them all.
   519  //
   520  // NOTE: Unlike the merging functionality of the input transactions, ONLY the
   521  // specified private keys will be used, so even if the server already knows some
   522  // of the private keys, they will NOT be used.
   523  //
   524  // See SignRawTransaction if the RPC server already knows the input
   525  // transactions and private keys or SignRawTransaction2 if it already knows the
   526  // private keys.
   527  func (c *Client) SignRawTransaction3(tx *wire.MsgTx,
   528  	inputs []btcjson.RawTxInput,
   529  	privKeysWIF []string) (*wire.MsgTx, bool, error) {
   530  
   531  	return c.SignRawTransaction3Async(tx, inputs, privKeysWIF).Receive()
   532  }
   533  
   534  // SignRawTransaction4Async returns an instance of a type that can be used to
   535  // get the result of the RPC at some future time by invoking the Receive
   536  // function on the returned instance.
   537  //
   538  // See SignRawTransaction4 for the blocking version and more details.
   539  func (c *Client) SignRawTransaction4Async(tx *wire.MsgTx,
   540  	inputs []btcjson.RawTxInput, privKeysWIF []string,
   541  	hashType SigHashType) FutureSignRawTransactionResult {
   542  
   543  	txHex := ""
   544  	if tx != nil {
   545  		// Serialize the transaction and convert to hex string.
   546  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   547  		if err := tx.Serialize(buf); err != nil {
   548  			return newFutureError(err)
   549  		}
   550  		txHex = hex.EncodeToString(buf.Bytes())
   551  	}
   552  
   553  	cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF,
   554  		btcjson.String(string(hashType)))
   555  	return c.SendCmd(cmd)
   556  }
   557  
   558  // SignRawTransaction4 signs inputs for the passed transaction using
   559  // the specified signature hash type given the list of information about extra
   560  // input transactions and a potential list of private keys needed to perform
   561  // the signing process.  The private keys, if specified, must be in wallet
   562  // import format (WIF).
   563  //
   564  // The only input transactions that need to be specified are ones the RPC server
   565  // does not already know.  This means the list of transaction inputs can be nil
   566  // if the RPC server already knows them all.
   567  //
   568  // NOTE: Unlike the merging functionality of the input transactions, ONLY the
   569  // specified private keys will be used, so even if the server already knows some
   570  // of the private keys, they will NOT be used.  The list of private keys can be
   571  // nil in which case any private keys the RPC server knows will be used.
   572  //
   573  // This function should only used if a non-default signature hash type is
   574  // desired.  Otherwise, see SignRawTransaction if the RPC server already knows
   575  // the input transactions and private keys, SignRawTransaction2 if it already
   576  // knows the private keys, or SignRawTransaction3 if it does not know both.
   577  func (c *Client) SignRawTransaction4(tx *wire.MsgTx,
   578  	inputs []btcjson.RawTxInput, privKeysWIF []string,
   579  	hashType SigHashType) (*wire.MsgTx, bool, error) {
   580  
   581  	return c.SignRawTransaction4Async(tx, inputs, privKeysWIF,
   582  		hashType).Receive()
   583  }
   584  
   585  // FutureSignRawTransactionWithWalletResult is a future promise to deliver
   586  // the result of the SignRawTransactionWithWalletAsync RPC invocation (or
   587  // an applicable error).
   588  type FutureSignRawTransactionWithWalletResult chan *Response
   589  
   590  // Receive waits for the Response promised by the future and returns the
   591  // signed transaction as well as whether or not all inputs are now signed.
   592  func (r FutureSignRawTransactionWithWalletResult) Receive() (*wire.MsgTx, bool, error) {
   593  	res, err := ReceiveFuture(r)
   594  	if err != nil {
   595  		return nil, false, err
   596  	}
   597  
   598  	// Unmarshal as a signtransactionwithwallet result.
   599  	var signRawTxWithWalletResult btcjson.SignRawTransactionWithWalletResult
   600  	err = json.Unmarshal(res, &signRawTxWithWalletResult)
   601  	if err != nil {
   602  		return nil, false, err
   603  	}
   604  
   605  	// Decode the serialized transaction hex to raw bytes.
   606  	serializedTx, err := hex.DecodeString(signRawTxWithWalletResult.Hex)
   607  	if err != nil {
   608  		return nil, false, err
   609  	}
   610  
   611  	// Deserialize the transaction and return it.
   612  	var msgTx wire.MsgTx
   613  	if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil {
   614  		return nil, false, err
   615  	}
   616  
   617  	return &msgTx, signRawTxWithWalletResult.Complete, nil
   618  }
   619  
   620  // SignRawTransactionWithWalletAsync returns an instance of a type that can be used
   621  // to get the result of the RPC at some future time by invoking the Receive function
   622  // on the returned instance.
   623  //
   624  // See SignRawTransactionWithWallet for the blocking version and more details.
   625  func (c *Client) SignRawTransactionWithWalletAsync(tx *wire.MsgTx) FutureSignRawTransactionWithWalletResult {
   626  	txHex := ""
   627  	if tx != nil {
   628  		// Serialize the transaction and convert to hex string.
   629  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   630  		if err := tx.Serialize(buf); err != nil {
   631  			return newFutureError(err)
   632  		}
   633  		txHex = hex.EncodeToString(buf.Bytes())
   634  	}
   635  
   636  	cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, nil, nil)
   637  	return c.SendCmd(cmd)
   638  }
   639  
   640  // SignRawTransactionWithWallet signs inputs for the passed transaction and returns
   641  // the signed transaction as well as whether or not all inputs are now signed.
   642  //
   643  // This function assumes the RPC server already knows the input transactions for the
   644  // passed transaction which needs to be signed and uses the default signature hash
   645  // type.  Use one of the SignRawTransactionWithWallet# variants to specify that
   646  // information if needed.
   647  func (c *Client) SignRawTransactionWithWallet(tx *wire.MsgTx) (*wire.MsgTx, bool, error) {
   648  	return c.SignRawTransactionWithWalletAsync(tx).Receive()
   649  }
   650  
   651  // SignRawTransactionWithWallet2Async returns an instance of a type that can be
   652  // used to get the result of the RPC at some future time by invoking the Receive
   653  // function on the returned instance.
   654  //
   655  // See SignRawTransactionWithWallet2 for the blocking version and more details.
   656  func (c *Client) SignRawTransactionWithWallet2Async(tx *wire.MsgTx,
   657  	inputs []btcjson.RawTxWitnessInput) FutureSignRawTransactionWithWalletResult {
   658  
   659  	txHex := ""
   660  	if tx != nil {
   661  		// Serialize the transaction and convert to hex string.
   662  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   663  		if err := tx.Serialize(buf); err != nil {
   664  			return newFutureError(err)
   665  		}
   666  		txHex = hex.EncodeToString(buf.Bytes())
   667  	}
   668  
   669  	cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, &inputs, nil)
   670  	return c.SendCmd(cmd)
   671  }
   672  
   673  // SignRawTransactionWithWallet2 signs inputs for the passed transaction given the
   674  // list of information about the input transactions needed to perform the signing
   675  // process.
   676  //
   677  // This only input transactions that need to be specified are ones the
   678  // RPC server does not already know.  Already known input transactions will be
   679  // merged with the specified transactions.
   680  //
   681  // See SignRawTransactionWithWallet if the RPC server already knows the input
   682  // transactions.
   683  func (c *Client) SignRawTransactionWithWallet2(tx *wire.MsgTx,
   684  	inputs []btcjson.RawTxWitnessInput) (*wire.MsgTx, bool, error) {
   685  
   686  	return c.SignRawTransactionWithWallet2Async(tx, inputs).Receive()
   687  }
   688  
   689  // SignRawTransactionWithWallet3Async returns an instance of a type that can
   690  // be used to get the result of the RPC at some future time by invoking the
   691  // Receive function on the returned instance.
   692  //
   693  // See SignRawTransactionWithWallet3 for the blocking version and more details.
   694  func (c *Client) SignRawTransactionWithWallet3Async(tx *wire.MsgTx,
   695  	inputs []btcjson.RawTxWitnessInput, hashType SigHashType) FutureSignRawTransactionWithWalletResult {
   696  
   697  	txHex := ""
   698  	if tx != nil {
   699  		// Serialize the transaction and convert to hex string.
   700  		buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
   701  		if err := tx.Serialize(buf); err != nil {
   702  			return newFutureError(err)
   703  		}
   704  		txHex = hex.EncodeToString(buf.Bytes())
   705  	}
   706  
   707  	cmd := btcjson.NewSignRawTransactionWithWalletCmd(txHex, &inputs, btcjson.String(string(hashType)))
   708  	return c.SendCmd(cmd)
   709  }
   710  
   711  // SignRawTransactionWithWallet3 signs inputs for the passed transaction using
   712  // the specified signature hash type given the list of information about extra
   713  // input transactions.
   714  //
   715  // The only input transactions that need to be specified are ones the RPC server
   716  // does not already know.  This means the list of transaction inputs can be nil
   717  // if the RPC server already knows them all.
   718  //
   719  // This function should only used if a non-default signature hash type is
   720  // desired.  Otherwise, see SignRawTransactionWithWallet if the RPC server already
   721  // knows the input transactions, or SignRawTransactionWihWallet2 if it does not.
   722  func (c *Client) SignRawTransactionWithWallet3(tx *wire.MsgTx,
   723  	inputs []btcjson.RawTxWitnessInput, hashType SigHashType) (*wire.MsgTx, bool, error) {
   724  
   725  	return c.SignRawTransactionWithWallet3Async(tx, inputs, hashType).Receive()
   726  }
   727  
   728  // FutureSearchRawTransactionsResult is a future promise to deliver the result
   729  // of the SearchRawTransactionsAsync RPC invocation (or an applicable error).
   730  type FutureSearchRawTransactionsResult chan *Response
   731  
   732  // Receive waits for the Response promised by the future and returns the
   733  // found raw transactions.
   734  func (r FutureSearchRawTransactionsResult) Receive() ([]*wire.MsgTx, error) {
   735  	res, err := ReceiveFuture(r)
   736  	if err != nil {
   737  		return nil, err
   738  	}
   739  
   740  	// Unmarshal as an array of strings.
   741  	var searchRawTxnsResult []string
   742  	err = json.Unmarshal(res, &searchRawTxnsResult)
   743  	if err != nil {
   744  		return nil, err
   745  	}
   746  
   747  	// Decode and deserialize each transaction.
   748  	msgTxns := make([]*wire.MsgTx, 0, len(searchRawTxnsResult))
   749  	for _, hexTx := range searchRawTxnsResult {
   750  		// Decode the serialized transaction hex to raw bytes.
   751  		serializedTx, err := hex.DecodeString(hexTx)
   752  		if err != nil {
   753  			return nil, err
   754  		}
   755  
   756  		// Deserialize the transaction and add it to the result slice.
   757  		var msgTx wire.MsgTx
   758  		err = msgTx.Deserialize(bytes.NewReader(serializedTx))
   759  		if err != nil {
   760  			return nil, err
   761  		}
   762  		msgTxns = append(msgTxns, &msgTx)
   763  	}
   764  
   765  	return msgTxns, nil
   766  }
   767  
   768  // SearchRawTransactionsAsync returns an instance of a type that can be used to
   769  // get the result of the RPC at some future time by invoking the Receive
   770  // function on the returned instance.
   771  //
   772  // See SearchRawTransactions for the blocking version and more details.
   773  func (c *Client) SearchRawTransactionsAsync(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult {
   774  	addr := address.EncodeAddress()
   775  	verbose := btcjson.Int(0)
   776  	cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count,
   777  		nil, &reverse, &filterAddrs)
   778  	return c.SendCmd(cmd)
   779  }
   780  
   781  // SearchRawTransactions returns transactions that involve the passed address.
   782  //
   783  // NOTE: Chain servers do not typically provide this capability unless it has
   784  // specifically been enabled.
   785  //
   786  // See SearchRawTransactionsVerbose to retrieve a list of data structures with
   787  // information about the transactions instead of the transactions themselves.
   788  func (c *Client) SearchRawTransactions(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) ([]*wire.MsgTx, error) {
   789  	return c.SearchRawTransactionsAsync(address, skip, count, reverse, filterAddrs).Receive()
   790  }
   791  
   792  // FutureSearchRawTransactionsVerboseResult is a future promise to deliver the
   793  // result of the SearchRawTransactionsVerboseAsync RPC invocation (or an
   794  // applicable error).
   795  type FutureSearchRawTransactionsVerboseResult chan *Response
   796  
   797  // Receive waits for the Response promised by the future and returns the
   798  // found raw transactions.
   799  func (r FutureSearchRawTransactionsVerboseResult) Receive() ([]*btcjson.SearchRawTransactionsResult, error) {
   800  	res, err := ReceiveFuture(r)
   801  	if err != nil {
   802  		return nil, err
   803  	}
   804  
   805  	// Unmarshal as an array of raw transaction results.
   806  	var result []*btcjson.SearchRawTransactionsResult
   807  	err = json.Unmarshal(res, &result)
   808  	if err != nil {
   809  		return nil, err
   810  	}
   811  
   812  	return result, nil
   813  }
   814  
   815  // SearchRawTransactionsVerboseAsync returns an instance of a type that can be
   816  // used to get the result of the RPC at some future time by invoking the Receive
   817  // function on the returned instance.
   818  //
   819  // See SearchRawTransactionsVerbose for the blocking version and more details.
   820  func (c *Client) SearchRawTransactionsVerboseAsync(address btcutil.Address, skip,
   821  	count int, includePrevOut, reverse bool, filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult {
   822  
   823  	addr := address.EncodeAddress()
   824  	verbose := btcjson.Int(1)
   825  	var prevOut *int
   826  	if includePrevOut {
   827  		prevOut = btcjson.Int(1)
   828  	}
   829  	cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count,
   830  		prevOut, &reverse, filterAddrs)
   831  	return c.SendCmd(cmd)
   832  }
   833  
   834  // SearchRawTransactionsVerbose returns a list of data structures that describe
   835  // transactions which involve the passed address.
   836  //
   837  // NOTE: Chain servers do not typically provide this capability unless it has
   838  // specifically been enabled.
   839  //
   840  // See SearchRawTransactions to retrieve a list of raw transactions instead.
   841  func (c *Client) SearchRawTransactionsVerbose(address btcutil.Address, skip,
   842  	count int, includePrevOut, reverse bool, filterAddrs []string) ([]*btcjson.SearchRawTransactionsResult, error) {
   843  
   844  	return c.SearchRawTransactionsVerboseAsync(address, skip, count,
   845  		includePrevOut, reverse, &filterAddrs).Receive()
   846  }
   847  
   848  // FutureDecodeScriptResult is a future promise to deliver the result
   849  // of a DecodeScriptAsync RPC invocation (or an applicable error).
   850  type FutureDecodeScriptResult chan *Response
   851  
   852  // Receive waits for the Response promised by the future and returns information
   853  // about a script given its serialized bytes.
   854  func (r FutureDecodeScriptResult) Receive() (*btcjson.DecodeScriptResult, error) {
   855  	res, err := ReceiveFuture(r)
   856  	if err != nil {
   857  		return nil, err
   858  	}
   859  
   860  	// Unmarshal result as a decodescript result object.
   861  	var decodeScriptResult btcjson.DecodeScriptResult
   862  	err = json.Unmarshal(res, &decodeScriptResult)
   863  	if err != nil {
   864  		return nil, err
   865  	}
   866  
   867  	return &decodeScriptResult, nil
   868  }
   869  
   870  // DecodeScriptAsync returns an instance of a type that can be used to
   871  // get the result of the RPC at some future time by invoking the Receive
   872  // function on the returned instance.
   873  //
   874  // See DecodeScript for the blocking version and more details.
   875  func (c *Client) DecodeScriptAsync(serializedScript []byte) FutureDecodeScriptResult {
   876  	scriptHex := hex.EncodeToString(serializedScript)
   877  	cmd := btcjson.NewDecodeScriptCmd(scriptHex)
   878  	return c.SendCmd(cmd)
   879  }
   880  
   881  // DecodeScript returns information about a script given its serialized bytes.
   882  func (c *Client) DecodeScript(serializedScript []byte) (*btcjson.DecodeScriptResult, error) {
   883  	return c.DecodeScriptAsync(serializedScript).Receive()
   884  }