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.
12 "github.com/btcsuite/btcd/btcjson"
13 "github.com/btcsuite/btcd/chaincfg/chainhash"
14 "github.com/btcsuite/btcd/wire"
15 "github.com/btcsuite/btcutil"
18 // SigHashType enumerates the available signature hashing types that the
19 // SignRawTransaction function accepts.
20 type SigHashType string
22 // Constants used to indicate the signature hash type for SignRawTransaction.
24 // SigHashAll indicates ALL of the outputs should be signed.
25 SigHashAll SigHashType = "ALL"
27 // SigHashNone indicates NONE of the outputs should be signed. This
28 // can be thought of as specifying the signer does not care where the
30 SigHashNone SigHashType = "NONE"
32 // SigHashSingle indicates that a SINGLE output should be signed. This
33 // can be thought of specifying the signer only cares about where ONE of
34 // the outputs goes, but not any of the others.
35 SigHashSingle SigHashType = "SINGLE"
37 // SigHashAllAnyoneCanPay indicates that signer does not care where the
38 // other inputs to the transaction come from, so it allows other people
39 // to add inputs. In addition, it uses the SigHashAll signing method
41 SigHashAllAnyoneCanPay SigHashType = "ALL|ANYONECANPAY"
43 // SigHashNoneAnyoneCanPay 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 SigHashNone signing method
47 SigHashNoneAnyoneCanPay SigHashType = "NONE|ANYONECANPAY"
49 // SigHashSingleAnyoneCanPay indicates that signer does not care where
50 // the other inputs to the transaction come from, so it allows other
51 // people to add inputs. In addition, it uses the SigHashSingle signing
52 // method for outputs.
53 SigHashSingleAnyoneCanPay SigHashType = "SINGLE|ANYONECANPAY"
56 // String returns the SighHashType in human-readable form.
57 func (s SigHashType) String() string {
61 // FutureGetRawTransactionResult is a future promise to deliver the result of a
62 // GetRawTransactionAsync RPC invocation (or an applicable error).
63 type FutureGetRawTransactionResult chan *response
65 // Receive waits for the response promised by the future and returns a
66 // transaction given its hash.
67 func (r FutureGetRawTransactionResult) Receive() (*btcutil.Tx, error) {
68 res, err := receiveFuture(r)
73 // Unmarshal result as a string.
75 err = json.Unmarshal(res, &txHex)
80 // Decode the serialized transaction hex to raw bytes.
81 serializedTx, err := hex.DecodeString(txHex)
86 // Deserialize the transaction and return it.
88 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil {
91 return btcutil.NewTx(&msgTx), nil
94 // GetRawTransactionAsync returns an instance of a type that can be used to get
95 // the result of the RPC at some future time by invoking the Receive function on
96 // the returned instance.
98 // See GetRawTransaction for the blocking version and more details.
99 func (c *Client) GetRawTransactionAsync(txHash *chainhash.Hash) FutureGetRawTransactionResult {
102 hash = txHash.String()
105 cmd := btcjson.NewGetRawTransactionCmd(hash, btcjson.Int(0))
106 return c.sendCmd(cmd)
109 // GetRawTransaction returns a transaction given its hash.
111 // See GetRawTransactionVerbose to obtain additional information about the
113 func (c *Client) GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error) {
114 return c.GetRawTransactionAsync(txHash).Receive()
117 // FutureGetRawTransactionVerboseResult is a future promise to deliver the
118 // result of a GetRawTransactionVerboseAsync RPC invocation (or an applicable
120 type FutureGetRawTransactionVerboseResult chan *response
122 // Receive waits for the response promised by the future and returns information
123 // about a transaction given its hash.
124 func (r FutureGetRawTransactionVerboseResult) Receive() (*btcjson.TxRawResult, error) {
125 res, err := receiveFuture(r)
130 // Unmarshal result as a gettrawtransaction result object.
131 var rawTxResult btcjson.TxRawResult
132 err = json.Unmarshal(res, &rawTxResult)
137 return &rawTxResult, nil
140 // GetRawTransactionVerboseAsync returns an instance of a type that can be used
141 // to get the result of the RPC at some future time by invoking the Receive
142 // function on the returned instance.
144 // See GetRawTransactionVerbose for the blocking version and more details.
145 func (c *Client) GetRawTransactionVerboseAsync(txHash *chainhash.Hash) FutureGetRawTransactionVerboseResult {
148 hash = txHash.String()
151 cmd := btcjson.NewGetRawTransactionCmd(hash, btcjson.Int(1))
152 return c.sendCmd(cmd)
155 // GetRawTransactionVerbose returns information about a transaction given
158 // See GetRawTransaction to obtain only the transaction already deserialized.
159 func (c *Client) GetRawTransactionVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error) {
160 return c.GetRawTransactionVerboseAsync(txHash).Receive()
163 // FutureDecodeRawTransactionResult is a future promise to deliver the result
164 // of a DecodeRawTransactionAsync RPC invocation (or an applicable error).
165 type FutureDecodeRawTransactionResult chan *response
167 // Receive waits for the response promised by the future and returns information
168 // about a transaction given its serialized bytes.
169 func (r FutureDecodeRawTransactionResult) Receive() (*btcjson.TxRawResult, error) {
170 res, err := receiveFuture(r)
175 // Unmarshal result as a decoderawtransaction result object.
176 var rawTxResult btcjson.TxRawResult
177 err = json.Unmarshal(res, &rawTxResult)
182 return &rawTxResult, nil
185 // DecodeRawTransactionAsync returns an instance of a type that can be used to
186 // get the result of the RPC at some future time by invoking the Receive
187 // function on the returned instance.
189 // See DecodeRawTransaction for the blocking version and more details.
190 func (c *Client) DecodeRawTransactionAsync(serializedTx []byte) FutureDecodeRawTransactionResult {
191 txHex := hex.EncodeToString(serializedTx)
192 cmd := btcjson.NewDecodeRawTransactionCmd(txHex)
193 return c.sendCmd(cmd)
196 // DecodeRawTransaction returns information about a transaction given its
198 func (c *Client) DecodeRawTransaction(serializedTx []byte) (*btcjson.TxRawResult, error) {
199 return c.DecodeRawTransactionAsync(serializedTx).Receive()
202 // FutureCreateRawTransactionResult is a future promise to deliver the result
203 // of a CreateRawTransactionAsync RPC invocation (or an applicable error).
204 type FutureCreateRawTransactionResult chan *response
206 // Receive waits for the response promised by the future and returns a new
207 // transaction spending the provided inputs and sending to the provided
209 func (r FutureCreateRawTransactionResult) Receive() (*wire.MsgTx, error) {
210 res, err := receiveFuture(r)
215 // Unmarshal result as a string.
217 err = json.Unmarshal(res, &txHex)
222 // Decode the serialized transaction hex to raw bytes.
223 serializedTx, err := hex.DecodeString(txHex)
228 // Deserialize the transaction and return it.
230 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil {
236 // CreateRawTransactionAsync returns an instance of a type that can be used to
237 // get the result of the RPC at some future time by invoking the Receive
238 // function on the returned instance.
240 // See CreateRawTransaction for the blocking version and more details.
241 func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput,
242 amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult {
244 convertedAmts := make(map[string]float64, len(amounts))
245 for addr, amount := range amounts {
246 convertedAmts[addr.String()] = amount.ToBTC()
248 cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedAmts, lockTime)
249 return c.sendCmd(cmd)
252 // CreateRawTransaction returns a new transaction spending the provided inputs
253 // and sending to the provided addresses.
254 func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput,
255 amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) {
257 return c.CreateRawTransactionAsync(inputs, amounts, lockTime).Receive()
260 // FutureSendRawTransactionResult is a future promise to deliver the result
261 // of a SendRawTransactionAsync RPC invocation (or an applicable error).
262 type FutureSendRawTransactionResult chan *response
264 // Receive waits for the response promised by the future and returns the result
265 // of submitting the encoded transaction to the server which then relays it to
267 func (r FutureSendRawTransactionResult) Receive() (*chainhash.Hash, error) {
268 res, err := receiveFuture(r)
273 // Unmarshal result as a string.
275 err = json.Unmarshal(res, &txHashStr)
280 return chainhash.NewHashFromStr(txHashStr)
283 // SendRawTransactionAsync returns an instance of a type that can be used to get
284 // the result of the RPC at some future time by invoking the Receive function on
285 // the returned instance.
287 // See SendRawTransaction for the blocking version and more details.
288 func (c *Client) SendRawTransactionAsync(tx *wire.MsgTx, allowHighFees bool) FutureSendRawTransactionResult {
291 // Serialize the transaction and convert to hex string.
292 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
293 if err := tx.Serialize(buf); err != nil {
294 return newFutureError(err)
296 txHex = hex.EncodeToString(buf.Bytes())
299 cmd := btcjson.NewSendRawTransactionCmd(txHex, &allowHighFees)
300 return c.sendCmd(cmd)
303 // SendRawTransaction submits the encoded transaction to the server which will
304 // then relay it to the network.
305 func (c *Client) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error) {
306 return c.SendRawTransactionAsync(tx, allowHighFees).Receive()
309 // FutureSignRawTransactionResult is a future promise to deliver the result
310 // of one of the SignRawTransactionAsync family of RPC invocations (or an
311 // applicable error).
312 type FutureSignRawTransactionResult chan *response
314 // Receive waits for the response promised by the future and returns the
315 // signed transaction as well as whether or not all inputs are now signed.
316 func (r FutureSignRawTransactionResult) Receive() (*wire.MsgTx, bool, error) {
317 res, err := receiveFuture(r)
319 return nil, false, err
322 // Unmarshal as a signrawtransaction result.
323 var signRawTxResult btcjson.SignRawTransactionResult
324 err = json.Unmarshal(res, &signRawTxResult)
326 return nil, false, err
329 // Decode the serialized transaction hex to raw bytes.
330 serializedTx, err := hex.DecodeString(signRawTxResult.Hex)
332 return nil, false, err
335 // Deserialize the transaction and return it.
337 if err := msgTx.Deserialize(bytes.NewReader(serializedTx)); err != nil {
338 return nil, false, err
341 return &msgTx, signRawTxResult.Complete, nil
344 // SignRawTransactionAsync returns an instance of a type that can be used to get
345 // the result of the RPC at some future time by invoking the Receive function on
346 // the returned instance.
348 // See SignRawTransaction for the blocking version and more details.
349 func (c *Client) SignRawTransactionAsync(tx *wire.MsgTx) FutureSignRawTransactionResult {
352 // Serialize the transaction and convert to hex string.
353 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
354 if err := tx.Serialize(buf); err != nil {
355 return newFutureError(err)
357 txHex = hex.EncodeToString(buf.Bytes())
360 cmd := btcjson.NewSignRawTransactionCmd(txHex, nil, nil, nil)
361 return c.sendCmd(cmd)
364 // SignRawTransaction signs inputs for the passed transaction and returns the
365 // signed transaction as well as whether or not all inputs are now signed.
367 // This function assumes the RPC server already knows the input transactions and
368 // private keys for the passed transaction which needs to be signed and uses the
369 // default signature hash type. Use one of the SignRawTransaction# variants to
370 // specify that information if needed.
371 func (c *Client) SignRawTransaction(tx *wire.MsgTx) (*wire.MsgTx, bool, error) {
372 return c.SignRawTransactionAsync(tx).Receive()
375 // SignRawTransaction2Async returns an instance of a type that can be used to
376 // get the result of the RPC at some future time by invoking the Receive
377 // function on the returned instance.
379 // See SignRawTransaction2 for the blocking version and more details.
380 func (c *Client) SignRawTransaction2Async(tx *wire.MsgTx, inputs []btcjson.RawTxInput) FutureSignRawTransactionResult {
383 // Serialize the transaction and convert to hex string.
384 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
385 if err := tx.Serialize(buf); err != nil {
386 return newFutureError(err)
388 txHex = hex.EncodeToString(buf.Bytes())
391 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, nil, nil)
392 return c.sendCmd(cmd)
395 // SignRawTransaction2 signs inputs for the passed transaction given the list
396 // of information about the input transactions needed to perform the signing
399 // This only input transactions that need to be specified are ones the
400 // RPC server does not already know. Already known input transactions will be
401 // merged with the specified transactions.
403 // See SignRawTransaction if the RPC server already knows the input
405 func (c *Client) SignRawTransaction2(tx *wire.MsgTx, inputs []btcjson.RawTxInput) (*wire.MsgTx, bool, error) {
406 return c.SignRawTransaction2Async(tx, inputs).Receive()
409 // SignRawTransaction3Async returns an instance of a type that can be used to
410 // get the result of the RPC at some future time by invoking the Receive
411 // function on the returned instance.
413 // See SignRawTransaction3 for the blocking version and more details.
414 func (c *Client) SignRawTransaction3Async(tx *wire.MsgTx,
415 inputs []btcjson.RawTxInput,
416 privKeysWIF []string) FutureSignRawTransactionResult {
420 // Serialize the transaction and convert to hex string.
421 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
422 if err := tx.Serialize(buf); err != nil {
423 return newFutureError(err)
425 txHex = hex.EncodeToString(buf.Bytes())
428 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF,
430 return c.sendCmd(cmd)
433 // SignRawTransaction3 signs inputs for the passed transaction given the list
434 // of information about extra input transactions and a list of private keys
435 // needed to perform the signing process. The private keys must be in wallet
436 // import format (WIF).
438 // This only input transactions that need to be specified are ones the
439 // RPC server does not already know. Already known input transactions will be
440 // merged with the specified transactions. This means the list of transaction
441 // inputs can be nil if the RPC server already knows them all.
443 // NOTE: Unlike the merging functionality of the input transactions, ONLY the
444 // specified private keys will be used, so even if the server already knows some
445 // of the private keys, they will NOT be used.
447 // See SignRawTransaction if the RPC server already knows the input
448 // transactions and private keys or SignRawTransaction2 if it already knows the
450 func (c *Client) SignRawTransaction3(tx *wire.MsgTx,
451 inputs []btcjson.RawTxInput,
452 privKeysWIF []string) (*wire.MsgTx, bool, error) {
454 return c.SignRawTransaction3Async(tx, inputs, privKeysWIF).Receive()
457 // SignRawTransaction4Async 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.
461 // See SignRawTransaction4 for the blocking version and more details.
462 func (c *Client) SignRawTransaction4Async(tx *wire.MsgTx,
463 inputs []btcjson.RawTxInput, privKeysWIF []string,
464 hashType SigHashType) FutureSignRawTransactionResult {
468 // Serialize the transaction and convert to hex string.
469 buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
470 if err := tx.Serialize(buf); err != nil {
471 return newFutureError(err)
473 txHex = hex.EncodeToString(buf.Bytes())
476 cmd := btcjson.NewSignRawTransactionCmd(txHex, &inputs, &privKeysWIF,
477 btcjson.String(string(hashType)))
478 return c.sendCmd(cmd)
481 // SignRawTransaction4 signs inputs for the passed transaction using the
482 // the specified signature hash type given the list of information about extra
483 // input transactions and a potential list of private keys needed to perform
484 // the signing process. The private keys, if specified, must be in wallet
485 // import format (WIF).
487 // The only input transactions that need to be specified are ones the RPC server
488 // does not already know. This means the list of transaction inputs can be nil
489 // if the RPC server already knows them all.
491 // NOTE: Unlike the merging functionality of the input transactions, ONLY the
492 // specified private keys will be used, so even if the server already knows some
493 // of the private keys, they will NOT be used. The list of private keys can be
494 // nil in which case any private keys the RPC server knows will be used.
496 // This function should only used if a non-default signature hash type is
497 // desired. Otherwise, see SignRawTransaction if the RPC server already knows
498 // the input transactions and private keys, SignRawTransaction2 if it already
499 // knows the private keys, or SignRawTransaction3 if it does not know both.
500 func (c *Client) SignRawTransaction4(tx *wire.MsgTx,
501 inputs []btcjson.RawTxInput, privKeysWIF []string,
502 hashType SigHashType) (*wire.MsgTx, bool, error) {
504 return c.SignRawTransaction4Async(tx, inputs, privKeysWIF,
508 // FutureSearchRawTransactionsResult is a future promise to deliver the result
509 // of the SearchRawTransactionsAsync RPC invocation (or an applicable error).
510 type FutureSearchRawTransactionsResult chan *response
512 // Receive waits for the response promised by the future and returns the
513 // found raw transactions.
514 func (r FutureSearchRawTransactionsResult) Receive() ([]*wire.MsgTx, error) {
515 res, err := receiveFuture(r)
520 // Unmarshal as an array of strings.
521 var searchRawTxnsResult []string
522 err = json.Unmarshal(res, &searchRawTxnsResult)
527 // Decode and deserialize each transaction.
528 msgTxns := make([]*wire.MsgTx, 0, len(searchRawTxnsResult))
529 for _, hexTx := range searchRawTxnsResult {
530 // Decode the serialized transaction hex to raw bytes.
531 serializedTx, err := hex.DecodeString(hexTx)
536 // Deserialize the transaction and add it to the result slice.
538 err = msgTx.Deserialize(bytes.NewReader(serializedTx))
542 msgTxns = append(msgTxns, &msgTx)
548 // SearchRawTransactionsAsync returns an instance of a type that can be used to
549 // get the result of the RPC at some future time by invoking the Receive
550 // function on the returned instance.
552 // See SearchRawTransactions for the blocking version and more details.
553 func (c *Client) SearchRawTransactionsAsync(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult {
554 addr := address.EncodeAddress()
555 verbose := btcjson.Int(0)
556 cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count,
557 nil, &reverse, &filterAddrs)
558 return c.sendCmd(cmd)
561 // SearchRawTransactions returns transactions that involve the passed address.
563 // NOTE: Chain servers do not typically provide this capability unless it has
564 // specifically been enabled.
566 // See SearchRawTransactionsVerbose to retrieve a list of data structures with
567 // information about the transactions instead of the transactions themselves.
568 func (c *Client) SearchRawTransactions(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) ([]*wire.MsgTx, error) {
569 return c.SearchRawTransactionsAsync(address, skip, count, reverse, filterAddrs).Receive()
572 // FutureSearchRawTransactionsVerboseResult is a future promise to deliver the
573 // result of the SearchRawTransactionsVerboseAsync RPC invocation (or an
574 // applicable error).
575 type FutureSearchRawTransactionsVerboseResult chan *response
577 // Receive waits for the response promised by the future and returns the
578 // found raw transactions.
579 func (r FutureSearchRawTransactionsVerboseResult) Receive() ([]*btcjson.SearchRawTransactionsResult, error) {
580 res, err := receiveFuture(r)
585 // Unmarshal as an array of raw transaction results.
586 var result []*btcjson.SearchRawTransactionsResult
587 err = json.Unmarshal(res, &result)
595 // SearchRawTransactionsVerboseAsync returns an instance of a type that can be
596 // used to get the result of the RPC at some future time by invoking the Receive
597 // function on the returned instance.
599 // See SearchRawTransactionsVerbose for the blocking version and more details.
600 func (c *Client) SearchRawTransactionsVerboseAsync(address btcutil.Address, skip,
601 count int, includePrevOut, reverse bool, filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult {
603 addr := address.EncodeAddress()
604 verbose := btcjson.Int(1)
607 prevOut = btcjson.Int(1)
609 cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count,
610 prevOut, &reverse, filterAddrs)
611 return c.sendCmd(cmd)
614 // SearchRawTransactionsVerbose returns a list of data structures that describe
615 // transactions which involve the passed address.
617 // NOTE: Chain servers do not typically provide this capability unless it has
618 // specifically been enabled.
620 // See SearchRawTransactions to retrieve a list of raw transactions instead.
621 func (c *Client) SearchRawTransactionsVerbose(address btcutil.Address, skip,
622 count int, includePrevOut, reverse bool, filterAddrs []string) ([]*btcjson.SearchRawTransactionsResult, error) {
624 return c.SearchRawTransactionsVerboseAsync(address, skip, count,
625 includePrevOut, reverse, &filterAddrs).Receive()