-
Notifications
You must be signed in to change notification settings - Fork 84
/
rpcclient.go
680 lines (612 loc) · 21.7 KB
/
rpcclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package btc
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"sync"
"time"
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/dex"
dexbtc "decred.org/dcrdex/dex/networks/btc"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
const (
methodGetBalances = "getbalances"
methodListUnspent = "listunspent"
methodLockUnspent = "lockunspent"
methodListLockUnspent = "listlockunspent"
methodChangeAddress = "getrawchangeaddress"
methodNewAddress = "getnewaddress"
methodSignTx = "signrawtransactionwithwallet"
methodUnlock = "walletpassphrase"
methodLock = "walletlock"
methodPrivKeyForAddress = "dumpprivkey"
methodSignMessage = "signmessagewithprivkey"
methodGetTransaction = "gettransaction"
methodSendToAddress = "sendtoaddress"
methodSetTxFee = "settxfee"
methodGetWalletInfo = "getwalletinfo"
methodGetAddressInfo = "getaddressinfo"
methodEstimateSmartFee = "estimatesmartfee"
methodSendRawTransaction = "sendrawtransaction"
methodGetTxOut = "gettxout"
methodGetBlock = "getblock"
methodGetBlockHash = "getblockhash"
methodGetBestBlockHash = "getbestblockhash"
methodGetRawMempool = "getrawmempool"
methodGetRawTransaction = "getrawtransaction"
)
// RawRequester defines decred's rpcclient RawRequest func where all RPC
// requests sent through. For testing, it can be satisfied by a stub.
type RawRequester interface {
RawRequest(string, []json.RawMessage) (json.RawMessage, error)
}
// RawRequesterWithContext defines decred's rpcclient RawRequest func where all
// RPC requests sent through. For testing, it can be satisfied by a stub.
type RawRequesterWithContext interface {
RawRequest(context.Context, string, []json.RawMessage) (json.RawMessage, error)
}
// anylist is a list of RPC parameters to be converted to []json.RawMessage and
// sent via RawRequest.
type anylist []interface{}
// rpcClient is a bitcoind JSON RPC client that uses rpcclient.Client's
// RawRequest for wallet-related calls.
type rpcClient struct {
ctx context.Context
log dex.Logger
requester RawRequesterWithContext
chainParams *chaincfg.Params
segwit bool
decodeAddr dexbtc.AddressDecoder
minNetworkVersion uint64
arglessChangeAddrRPC bool
legacyRawSends bool
}
// newRPCClient is the constructor for a rpcClient.
func newRPCClient(requester RawRequesterWithContext, segwit bool, addrDecoder dexbtc.AddressDecoder, arglessChangeAddrRPC bool,
legacyRawSends bool, minNetworkVersion uint64, logger dex.Logger, chainParams *chaincfg.Params) *rpcClient {
return &rpcClient{
requester: requester,
chainParams: chainParams,
segwit: segwit,
log: logger,
decodeAddr: addrDecoder,
minNetworkVersion: minNetworkVersion,
arglessChangeAddrRPC: arglessChangeAddrRPC,
legacyRawSends: legacyRawSends,
}
}
func (wc *rpcClient) connect(ctx context.Context, _ *sync.WaitGroup) error {
wc.ctx = ctx
// Check the version. Do it here, so we can also diagnose a bad connection.
netVer, codeVer, err := wc.getVersion()
if err != nil {
return fmt.Errorf("error getting version: %w", err)
}
if netVer < wc.minNetworkVersion {
return fmt.Errorf("reported node version %d is less than minimum %d", netVer, wc.minNetworkVersion)
}
if codeVer < minProtocolVersion {
return fmt.Errorf("node software out of date. version %d is less than minimum %d", codeVer, minProtocolVersion)
}
return nil
}
// RawRequest passes the reqeuest to the wallet's RawRequester.
func (wc *rpcClient) RawRequest(method string, params []json.RawMessage) (json.RawMessage, error) {
return wc.requester.RawRequest(wc.ctx, method, params)
}
// estimateSmartFee requests the server to estimate a fee level based on the
// given parameters.
func (wc *rpcClient) estimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) {
res := new(btcjson.EstimateSmartFeeResult)
return res, wc.call(methodEstimateSmartFee, anylist{confTarget, mode}, res)
}
// SendRawTransactionLegacy broadcasts the transaction with an additional legacy
// boolean `allowhighfees` argument set to false.
func (wc *rpcClient) SendRawTransactionLegacy(tx *wire.MsgTx) (*chainhash.Hash, error) {
txBytes, err := serializeMsgTx(tx)
if err != nil {
return nil, err
}
return wc.callHashGetter(methodSendRawTransaction, anylist{
hex.EncodeToString(txBytes), false})
}
// sendRawTransaction sends the MsgTx.
func (wc *rpcClient) sendRawTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
if wc.legacyRawSends {
return wc.SendRawTransactionLegacy(tx)
}
return wc.SendRawTransaction(tx)
}
// getTxOut returns the transaction output info if it's unspent and
// nil, otherwise.
func (wc *rpcClient) getTxOut(txHash *chainhash.Hash, index uint32, _ []byte, _ time.Time) (*wire.TxOut, uint32, error) {
txOut, err := wc.getTxOutput(txHash, index)
if err != nil {
return nil, 0, fmt.Errorf("getTxOut error: %w", err)
}
if txOut == nil {
return nil, 0, nil
}
outputScript, _ := hex.DecodeString(txOut.ScriptPubKey.Hex)
// Check equivalence of pkScript and outputScript?
return wire.NewTxOut(int64(toSatoshi(txOut.Value)), outputScript), uint32(txOut.Confirmations), nil
}
// getTxOut returns the transaction output info if it's unspent and
// nil, otherwise.
func (wc *rpcClient) getTxOutput(txHash *chainhash.Hash, index uint32) (*btcjson.GetTxOutResult, error) {
// Note that we pass to call pointer to a pointer (&res) so that
// json.Unmarshal can nil the pointer if the method returns the JSON null.
var res *btcjson.GetTxOutResult
return res, wc.call(methodGetTxOut, anylist{txHash.String(), index, true},
&res)
}
// locked returns the wallet's lock state.
func (wc *rpcClient) locked() bool {
walletInfo, err := wc.GetWalletInfo()
if err != nil {
wc.log.Errorf("GetWalletInfo error: %w", err)
return false
}
if walletInfo.UnlockedUntil == nil {
// This wallet is not encrypted.
return false
}
return time.Unix(*walletInfo.UnlockedUntil, 0).Before(time.Now())
}
func (wc *rpcClient) callHashGetter(method string, args anylist) (*chainhash.Hash, error) {
var txid string
err := wc.call(method, args, &txid)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txid)
}
// getBlock fetches the MsgBlock.
func (wc *rpcClient) getBlock(h chainhash.Hash) (*wire.MsgBlock, error) {
var txB dex.Bytes
err := wc.call(methodGetBlock, anylist{h.String(), 0}, &txB)
if err != nil {
return nil, err
}
msgBlock := &wire.MsgBlock{}
return msgBlock, msgBlock.Deserialize(bytes.NewReader(txB))
}
// getBlockHash returns the hash of the block in the best block chain at the
// given height.
func (wc *rpcClient) getBlockHash(blockHeight int64) (*chainhash.Hash, error) {
return wc.callHashGetter(methodGetBlockHash, anylist{blockHeight})
}
// getBestBlockHash returns the hash of the best block in the longest block
// chain.
func (wc *rpcClient) getBestBlockHash() (*chainhash.Hash, error) {
return wc.callHashGetter(methodGetBestBlockHash, nil)
}
// getBestBlockHeight returns the height of the top mainchain block.
func (wc *rpcClient) getBestBlockHeight() (int32, error) {
tipHash, err := wc.getBestBlockHash()
if err != nil {
return -1, err
}
header, err := wc.getBlockHeader(tipHash)
if err != nil {
return -1, err
}
return int32(header.Height), nil
}
// GetRawMempool returns the hashes of all transactions in the memory pool.
func (wc *rpcClient) GetRawMempool() ([]*chainhash.Hash, error) {
var mempool []string
err := wc.call(methodGetRawMempool, nil, &mempool)
if err != nil {
return nil, err
}
// Convert received hex hashes to chainhash.Hash
hashes := make([]*chainhash.Hash, 0, len(mempool))
for _, h := range mempool {
hash, err := chainhash.NewHashFromStr(h)
if err != nil {
return nil, err
}
hashes = append(hashes, hash)
}
return hashes, nil
}
// GetRawTransaction retrieves the MsgTx.
func (wc *rpcClient) GetRawTransaction(txHash *chainhash.Hash) (*wire.MsgTx, error) {
var txB dex.Bytes
err := wc.call(methodGetRawTransaction, anylist{txHash.String(), false}, &txB)
if err != nil {
return nil, err
}
tx := wire.NewMsgTx(wire.TxVersion)
return tx, tx.Deserialize(bytes.NewReader(txB))
}
// balances retrieves a wallet's balance details.
func (wc *rpcClient) balances() (*GetBalancesResult, error) {
var balances GetBalancesResult
return &balances, wc.call(methodGetBalances, nil, &balances)
}
// listUnspent retrieves a list of the wallet's UTXOs.
func (wc *rpcClient) listUnspent() ([]*ListUnspentResult, error) {
unspents := make([]*ListUnspentResult, 0)
// TODO: listunspent 0 9999999 []string{}, include_unsafe=false
return unspents, wc.call(methodListUnspent, anylist{uint8(0)}, &unspents)
}
// lockUnspent locks and unlocks outputs for spending. An output that is part of
// an order, but not yet spent, should be locked until spent or until the order
// is canceled or fails.
func (wc *rpcClient) lockUnspent(unlock bool, ops []*output) error {
var rpcops []*RPCOutpoint // To clear all, this must be nil, not empty slice.
for _, op := range ops {
rpcops = append(rpcops, &RPCOutpoint{
TxID: op.txHash().String(),
Vout: op.vout(),
})
}
var success bool
err := wc.call(methodLockUnspent, anylist{unlock, rpcops}, &success)
if err == nil && !success {
return fmt.Errorf("lockunspent unsuccessful")
}
return err
}
// listLockUnspent returns a slice of outpoints for all unspent outputs marked
// as locked by a wallet.
func (wc *rpcClient) listLockUnspent() ([]*RPCOutpoint, error) {
var unspents []*RPCOutpoint
err := wc.call(methodListLockUnspent, nil, &unspents)
return unspents, err
}
// changeAddress gets a new internal address from the wallet. The address will
// be bech32-encoded (P2WPKH).
func (wc *rpcClient) changeAddress() (btcutil.Address, error) {
var addrStr string
var err error
switch {
case wc.arglessChangeAddrRPC:
err = wc.call(methodChangeAddress, nil, &addrStr)
case wc.segwit:
err = wc.call(methodChangeAddress, anylist{"bech32"}, &addrStr)
default:
err = wc.call(methodChangeAddress, anylist{"legacy"}, &addrStr)
}
if err != nil {
return nil, err
}
return wc.decodeAddr(addrStr, wc.chainParams)
}
// AddressPKH gets a new base58-encoded (P2PKH) external address from the
// wallet.
func (wc *rpcClient) addressPKH() (btcutil.Address, error) {
return wc.address("legacy")
}
// addressWPKH gets a new bech32-encoded (P2WPKH) external address from the
// wallet.
func (wc *rpcClient) addressWPKH() (btcutil.Address, error) {
return wc.address("bech32")
}
// address is used internally for fetching addresses of various types from the
// wallet.
func (wc *rpcClient) address(aType string) (btcutil.Address, error) {
var addrStr string
err := wc.call(methodNewAddress, anylist{"", aType}, &addrStr)
if err != nil {
return nil, err
}
return wc.decodeAddr(addrStr, wc.chainParams)
}
// signTx attempts to have the wallet sign the transaction inputs.
func (wc *rpcClient) signTx(inTx *wire.MsgTx) (*wire.MsgTx, error) {
txBytes, err := serializeMsgTx(inTx)
if err != nil {
return nil, fmt.Errorf("tx serialization error: %w", err)
}
res := new(SignTxResult)
err = wc.call(methodSignTx, anylist{hex.EncodeToString(txBytes)}, res)
if err != nil {
return nil, fmt.Errorf("tx signing error: %w", err)
}
if !res.Complete {
sep := ""
errMsg := ""
for _, e := range res.Errors {
errMsg += e.Error + sep
sep = ";"
}
return nil, fmt.Errorf("signing incomplete. %d signing errors encountered: %s", len(res.Errors), errMsg)
}
outTx, err := msgTxFromBytes(res.Hex)
if err != nil {
return nil, fmt.Errorf("error deserializing transaction response: %w", err)
}
return outTx, nil
}
// privKeyForAddress retrieves the private key associated with the specified
// address.
func (wc *rpcClient) privKeyForAddress(addr string) (*btcec.PrivateKey, error) {
var keyHex string
err := wc.call(methodPrivKeyForAddress, anylist{addr}, &keyHex)
if err != nil {
return nil, err
}
wif, err := btcutil.DecodeWIF(keyHex)
if err != nil {
return nil, err
}
return wif.PrivKey, nil
}
// getWalletTransaction retrieves the JSON-RPC gettransaction result.
func (wc *rpcClient) getWalletTransaction(txHash *chainhash.Hash) (*GetTransactionResult, error) {
tx := new(GetTransactionResult)
err := wc.call(methodGetTransaction, anylist{txHash.String()}, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// walletUnlock unlocks the wallet.
func (wc *rpcClient) walletUnlock(pw []byte) error {
// 100000000 comes from bitcoin-cli help walletpassphrase
return wc.call(methodUnlock, anylist{string(pw), 100000000}, nil)
}
// walletLock locks the wallet.
func (wc *rpcClient) walletLock() error {
return wc.call(methodLock, nil, nil)
}
// sendToAddress sends the amount to the address. feeRate is in units of
// sats/byte.
func (wc *rpcClient) sendToAddress(address string, value, feeRate uint64, subtract bool) (*chainhash.Hash, error) {
var success bool
// 1e-5 = 1e-8 for satoshis * 1000 for kB.
err := wc.call(methodSetTxFee, anylist{float64(feeRate) / 1e5}, &success)
if err != nil {
return nil, fmt.Errorf("error setting transaction fee: %w", err)
}
if !success {
return nil, fmt.Errorf("failed to set transaction fee")
}
var txid string
// Last boolean argument is to subtract the fee from the amount.
coinValue := btcutil.Amount(value).ToBTC()
err = wc.call(methodSendToAddress, anylist{address, coinValue, "dcrdex", "", subtract}, &txid)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txid)
}
// GetWalletInfo gets the getwalletinfo RPC result.
func (wc *rpcClient) GetWalletInfo() (*GetWalletInfoResult, error) {
wi := new(GetWalletInfoResult)
return wi, wc.call(methodGetWalletInfo, nil, wi)
}
// GetAddressInfo gets information about the given address by calling
// getaddressinfo RPC command.
func (wc *rpcClient) getAddressInfo(addr btcutil.Address) (*GetAddressInfoResult, error) {
ai := new(GetAddressInfoResult)
return ai, wc.call(methodGetAddressInfo, anylist{addr.String()}, ai)
}
// ownsAddress indicates if an address belongs to the wallet.
func (wc *rpcClient) ownsAddress(addr btcutil.Address) (bool, error) {
ai, err := wc.getAddressInfo(addr)
if err != nil {
return false, err
}
return ai.IsMine, nil
}
// syncStatus is the current synchronization state of the node.
type syncStatus struct {
Target int32 `json:"target"`
Height int32 `json:"height"`
Syncing bool `json:"syncing"`
}
// syncStatus is information about the blockchain sync status.
func (wc *rpcClient) syncStatus() (*syncStatus, error) {
chainInfo, err := wc.getBlockchainInfo()
if err != nil {
return nil, fmt.Errorf("getblockchaininfo error: %w", err)
}
return &syncStatus{
Target: int32(chainInfo.Headers),
Height: int32(chainInfo.Blocks),
Syncing: chainInfo.InitialBlockDownload || chainInfo.Headers-chainInfo.Blocks > 1,
}, nil
}
// SendRawTransaction broadcasts the transaction.
func (wc *rpcClient) SendRawTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
b, err := serializeMsgTx(tx)
if err != nil {
return nil, err
}
var txid string
err = wc.call(methodSendRawTransaction, anylist{hex.EncodeToString(b)}, &txid)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txid)
}
// swapConfirmations gets the number of confirmations for the specified coin ID
// by first checking for a unspent output, and if not found, searching indexed
// wallet transactions.
func (wc *rpcClient) swapConfirmations(txHash *chainhash.Hash, vout uint32, _ []byte, _ time.Time) (confs uint32, spent bool, err error) {
// Check for an unspent output.
txOut, err := wc.getTxOutput(txHash, vout)
if err == nil && txOut != nil {
return uint32(txOut.Confirmations), false, nil
}
// Check wallet transactions.
tx, err := wc.getWalletTransaction(txHash)
if err != nil {
if isTxNotFoundErr(err) {
return 0, false, asset.CoinNotFoundError
}
return 0, false, err
}
return uint32(tx.Confirmations), true, nil
}
// getBlockHeader gets the block header for the specified block hash.
func (wc *rpcClient) getBlockHeader(blockHash *chainhash.Hash) (*blockHeader, error) {
blkHeader := new(blockHeader)
err := wc.call(methodGetBlockHeader,
anylist{blockHash.String(), true}, blkHeader)
if err != nil {
return nil, err
}
return blkHeader, nil
}
// getBlockHeight gets the mainchain height for the specified block.
func (wc *rpcClient) getBlockHeight(blockHash *chainhash.Hash) (int32, error) {
hdr, err := wc.getBlockHeader(blockHash)
if err != nil {
return -1, err
}
if hdr.Height < 0 {
return -1, fmt.Errorf("block is not a mainchain block")
}
return int32(hdr.Height), nil
}
// getBlockchainInfo sends the getblockchaininfo request and returns the result.
func (wc *rpcClient) getBlockchainInfo() (*getBlockchainInfoResult, error) {
chainInfo := new(getBlockchainInfoResult)
err := wc.call(methodGetBlockchainInfo, nil, chainInfo)
if err != nil {
return nil, err
}
return chainInfo, nil
}
// getVersion gets the current BTC network and protocol versions.
func (wc *rpcClient) getVersion() (uint64, uint64, error) {
r := &struct {
Version uint64 `json:"version"`
ProtocolVersion uint64 `json:"protocolversion"`
}{}
err := wc.call(methodGetNetworkInfo, nil, r)
if err != nil {
return 0, 0, err
}
return r.Version, r.ProtocolVersion, nil
}
// findRedemptionsInMempool attempts to find spending info for the specified
// contracts by searching every input of all txs in the mempool.
func (wc *rpcClient) findRedemptionsInMempool(ctx context.Context, reqs map[outPoint]*findRedemptionReq) (discovered map[outPoint]*findRedemptionResult) {
contractsCount := len(reqs)
wc.log.Debugf("finding redemptions for %d contracts in mempool", contractsCount)
discovered = make(map[outPoint]*findRedemptionResult, len(reqs))
var totalFound, totalCanceled int
logAbandon := func(reason string) {
// Do not remove the contracts from the findRedemptionQueue
// as they could be subsequently redeemed in some mined tx(s),
// which would be captured when a new tip is reported.
if totalFound+totalCanceled > 0 {
wc.log.Debugf("%d redemptions found, %d canceled out of %d contracts in mempool",
totalFound, totalCanceled, contractsCount)
}
wc.log.Errorf("abandoning mempool redemption search for %d contracts because of %s",
contractsCount-totalFound-totalCanceled, reason)
}
mempoolTxs, err := wc.GetRawMempool()
if err != nil {
logAbandon(fmt.Sprintf("error retrieving transactions: %v", err))
return
}
for _, txHash := range mempoolTxs {
if ctx.Err() != nil {
return nil
}
tx, err := wc.GetRawTransaction(txHash)
if err != nil {
logAbandon(fmt.Sprintf("getrawtransaction error for tx hash %v: %v", txHash, err))
return
}
newlyDiscovered := findRedemptionsInTx(ctx, wc.segwit, reqs, tx, wc.chainParams)
for outPt, res := range newlyDiscovered {
discovered[outPt] = res
}
}
return
}
// searchBlockForRedemptions attempts to find spending info for the specified
// contracts by searching every input of all txs in the provided block range.
func (wc *rpcClient) searchBlockForRedemptions(ctx context.Context, reqs map[outPoint]*findRedemptionReq, blockHash chainhash.Hash) (discovered map[outPoint]*findRedemptionResult) {
msgBlock, err := wc.getBlock(blockHash)
if err != nil {
wc.log.Errorf("RPC GetBlock error: %v", err)
return
}
discovered = make(map[outPoint]*findRedemptionResult, len(reqs))
for _, msgTx := range msgBlock.Transactions {
newlyDiscovered := findRedemptionsInTx(ctx, wc.segwit, reqs, msgTx, wc.chainParams)
for outPt, res := range newlyDiscovered {
discovered[outPt] = res
}
}
return
}
// call is used internally to marshal parmeters and send requests to the RPC
// server via (*rpcclient.Client).RawRequest. If `thing` is non-nil, the result
// will be marshaled into `thing`.
func (wc *rpcClient) call(method string, args anylist, thing interface{}) error {
params := make([]json.RawMessage, 0, len(args))
for i := range args {
p, err := json.Marshal(args[i])
if err != nil {
return err
}
params = append(params, p)
}
b, err := wc.requester.RawRequest(wc.ctx, method, params)
if err != nil {
return fmt.Errorf("rawrequest error: %w", err)
}
if thing != nil {
return json.Unmarshal(b, thing)
}
return nil
}
// serializeMsgTx serializes the wire.MsgTx.
func serializeMsgTx(msgTx *wire.MsgTx) ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, msgTx.SerializeSize()))
err := msgTx.Serialize(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// msgTxFromHex creates a wire.MsgTx by deserializing the hex-encoded
// transaction.
func msgTxFromHex(txHex string) (*wire.MsgTx, error) {
b, err := hex.DecodeString(txHex)
if err != nil {
return nil, err
}
return msgTxFromBytes(b)
}
// msgTxFromBytes creates a wire.MsgTx by deserializing the transaction.
func msgTxFromBytes(txB []byte) (*wire.MsgTx, error) {
msgTx := wire.NewMsgTx(wire.TxVersion)
if err := msgTx.Deserialize(bytes.NewReader(txB)); err != nil {
return nil, err
}
return msgTx, nil
}
// txOutFromTxBytes parses the specified *wire.TxOut from the serialized
// transaction.
func txOutFromTxBytes(txB []byte, vout uint32) (*wire.TxOut, error) {
msgTx, err := msgTxFromBytes(txB)
if err != nil {
return nil, fmt.Errorf("error decoding transaction bytes: %v", err)
}
if len(msgTx.TxOut) <= int(vout) {
return nil, fmt.Errorf("no vout %d in tx %s", vout, msgTx.TxHash())
}
return msgTx.TxOut[vout], nil
}