-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathclient.go
856 lines (727 loc) · 23.5 KB
/
client.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitcoin
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"strconv"
"time"
bitcoinUtils "github.com/coinbase/rosetta-bitcoin/utils"
"github.com/btcsuite/btcutil"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
)
const (
// genesisBlockIndex is the height of the block we consider to be the
// genesis block of the bitcoin blockchain for polling
genesisBlockIndex = 0
// requestID is the JSON-RPC request ID we use for making requests.
// We don't need unique request IDs because we're processing all of
// our requests synchronously.
requestID = 1
// jSONRPCVersion is the JSON-RPC version we use for making requests
jSONRPCVersion = "2.0"
// blockVerbosity represents the verbose level used when fetching blocks
// * 0 returns the hex representation
// * 1 returns the JSON representation
// * 2 returns the JSON representation with included Transaction data
blockVerbosity = 2
)
type requestMethod string
const (
// https://bitcoin.org/en/developer-reference#getblock
requestMethodGetBlock requestMethod = "getblock"
// https://bitcoin.org/en/developer-reference#getblockhash
requestMethodGetBlockHash requestMethod = "getblockhash"
// https://bitcoin.org/en/developer-reference#getblockchaininfo
requestMethodGetBlockchainInfo requestMethod = "getblockchaininfo"
// https://developer.bitcoin.org/reference/rpc/getpeerinfo.html
requestMethodGetPeerInfo requestMethod = "getpeerinfo"
// https://developer.bitcoin.org/reference/rpc/pruneblockchain.html
requestMethodPruneBlockchain requestMethod = "pruneblockchain"
// https://developer.bitcoin.org/reference/rpc/sendrawtransaction.html
requestMethodSendRawTransaction requestMethod = "sendrawtransaction"
// https://developer.bitcoin.org/reference/rpc/estimatesmartfee.html
requestMethodEstimateSmartFee requestMethod = "estimatesmartfee"
// https://developer.bitcoin.org/reference/rpc/getrawmempool.html
requestMethodRawMempool requestMethod = "getrawmempool"
// blockNotFoundErrCode is the RPC error code when a block cannot be found
blockNotFoundErrCode = -5
)
const (
defaultTimeout = 100 * time.Second
dialTimeout = 5 * time.Second
// timeMultiplier is used to multiply the time
// returned in Bitcoin blocks to be milliseconds.
timeMultiplier = 1000
// rpc credentials are fixed in rosetta-bitcoin
// because we never expose access to the raw bitcoind
// endpoints (that could be used perform an attack, like
// changing our peers).
rpcUsername = "rosetta"
rpcPassword = "rosetta"
)
var (
// ErrBlockNotFound is returned by when the requested block
// cannot be found by the node
ErrBlockNotFound = errors.New("unable to find block")
// ErrJSONRPCError is returned when receiving an error from a JSON-RPC response
ErrJSONRPCError = errors.New("JSON-RPC error")
)
// Client is used to fetch blocks from bitcoind and
// to parse Bitcoin block data into Rosetta types.
//
// We opted not to use existing Bitcoin RPC libraries
// because they don't allow providing context
// in each request.
type Client struct {
baseURL string
genesisBlockIdentifier *types.BlockIdentifier
currency *types.Currency
httpClient *http.Client
}
// LocalhostURL returns the URL to use
// for a client that is running at localhost.
func LocalhostURL(rpcPort int) string {
return fmt.Sprintf("http://localhost:%d", rpcPort)
}
// NewClient creates a new Bitcoin client.
func NewClient(
baseURL string,
genesisBlockIdentifier *types.BlockIdentifier,
currency *types.Currency,
) *Client {
return &Client{
baseURL: baseURL,
genesisBlockIdentifier: genesisBlockIdentifier,
currency: currency,
httpClient: newHTTPClient(defaultTimeout),
}
}
// newHTTPClient returns a new HTTP client
func newHTTPClient(timeout time.Duration) *http.Client {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: dialTimeout,
}).Dial,
}
httpClient := &http.Client{
Timeout: timeout,
Transport: netTransport,
}
return httpClient
}
// NetworkStatus returns the *types.NetworkStatusResponse for
// bitcoind.
func (b *Client) NetworkStatus(ctx context.Context) (*types.NetworkStatusResponse, error) {
rawBlock, err := b.getBlock(ctx, nil)
if err != nil {
return nil, fmt.Errorf("%w: unable to get current block", err)
}
currentBlock, err := b.parseBlockData(rawBlock)
if err != nil {
return nil, fmt.Errorf("%w: unable to parse current block", err)
}
peers, err := b.GetPeers(ctx)
if err != nil {
return nil, err
}
return &types.NetworkStatusResponse{
CurrentBlockIdentifier: currentBlock.BlockIdentifier,
CurrentBlockTimestamp: currentBlock.Timestamp,
GenesisBlockIdentifier: b.genesisBlockIdentifier,
Peers: peers,
}, nil
}
// GetPeers fetches the list of peer nodes
func (b *Client) GetPeers(ctx context.Context) ([]*types.Peer, error) {
info, err := b.getPeerInfo(ctx)
if err != nil {
return nil, err
}
peers := make([]*types.Peer, len(info))
for i, peerInfo := range info {
metadata, err := types.MarshalMap(peerInfo)
if err != nil {
return nil, fmt.Errorf("%w: unable to marshal peer info", err)
}
peers[i] = &types.Peer{
PeerID: peerInfo.Addr,
Metadata: metadata,
}
}
return peers, nil
}
// GetRawBlock fetches a block (block) by *types.PartialBlockIdentifier.
func (b *Client) GetRawBlock(
ctx context.Context,
identifier *types.PartialBlockIdentifier,
) (*Block, []string, error) {
block, err := b.getBlock(ctx, identifier)
if err != nil {
return nil, nil, err
}
coins := []string{}
blockTxHashes := []string{}
for txIndex, tx := range block.Txs {
blockTxHashes = append(blockTxHashes, tx.Hash)
for inputIndex, input := range tx.Inputs {
txHash, vout, ok := b.getInputTxHash(input, txIndex, inputIndex)
if !ok {
continue
}
// If any transactions spent in the same block they are created, don't include them
// in previousTxHashes to fetch.
if !utils.ContainsString(blockTxHashes, txHash) {
coins = append(coins, CoinIdentifier(txHash, vout))
}
}
}
return block, coins, nil
}
// ParseBlock returns a parsed bitcoin block given a raw bitcoin
// block and a map of transactions containing inputs.
func (b *Client) ParseBlock(
ctx context.Context,
block *Block,
coins map[string]*types.AccountCoin,
) (*types.Block, error) {
rblock, err := b.parseBlockData(block)
if err != nil {
return nil, err
}
txs, err := b.parseTransactions(ctx, block, coins)
if err != nil {
return nil, err
}
rblock.Transactions = txs
return rblock, nil
}
// SendRawTransaction submits a serialized transaction
// to bitcoind.
func (b *Client) SendRawTransaction(
ctx context.Context,
serializedTx string,
) (string, error) {
// Parameters:
// 1. hextring
// 2. maxfeerate (0 means accept any fee)
params := []interface{}{serializedTx, 0}
response := &sendRawTransactionResponse{}
if err := b.post(ctx, requestMethodSendRawTransaction, params, response); err != nil {
return "", fmt.Errorf("%w: error submitting raw transaction", err)
}
return response.Result, nil
}
// SuggestedFeeRate estimates the approximate fee per vKB needed
// to get a transaction in a block within conf_target.
func (b *Client) SuggestedFeeRate(
ctx context.Context,
confTarget int64,
) (float64, error) {
// Parameters:
// 1. conf_target (confirmation target in blocks)
params := []interface{}{confTarget}
response := &suggestedFeeRateResponse{}
if err := b.post(ctx, requestMethodEstimateSmartFee, params, response); err != nil {
return -1, fmt.Errorf("%w: error getting fee estimate", err)
}
return response.Result.FeeRate, nil
}
// PruneBlockchain prunes up to the provided height.
// https://bitcoincore.org/en/doc/0.20.0/rpc/blockchain/pruneblockchain
func (b *Client) PruneBlockchain(
ctx context.Context,
height int64,
) (int64, error) {
// Parameters:
// 1. Height
// https://developer.bitcoin.org/reference/rpc/pruneblockchain.html#argument-1-height
params := []interface{}{height}
response := &pruneBlockchainResponse{}
if err := b.post(ctx, requestMethodPruneBlockchain, params, response); err != nil {
return -1, fmt.Errorf("%w: error pruning blockchain", err)
}
return response.Result, nil
}
// RawMempool returns an array of all transaction
// hashes currently in the mempool.
func (b *Client) RawMempool(
ctx context.Context,
) ([]string, error) {
// Parameters:
// 1. verbose
params := []interface{}{false}
response := &rawMempoolResponse{}
if err := b.post(ctx, requestMethodRawMempool, params, response); err != nil {
return nil, fmt.Errorf("%w: error getting raw mempool", err)
}
return response.Result, nil
}
// getPeerInfo performs the `getpeerinfo` JSON-RPC request
func (b *Client) getPeerInfo(
ctx context.Context,
) ([]*PeerInfo, error) {
params := []interface{}{}
response := &peerInfoResponse{}
if err := b.post(ctx, requestMethodGetPeerInfo, params, response); err != nil {
return nil, fmt.Errorf("%w: error posting to JSON-RPC", err)
}
return response.Result, nil
}
// getBlock returns a Block for the specified identifier
func (b *Client) getBlock(
ctx context.Context,
identifier *types.PartialBlockIdentifier,
) (*Block, error) {
hash, err := b.getBlockHash(ctx, identifier)
if err != nil {
return nil, fmt.Errorf("%w: error getting block hash by identifier", err)
}
// Parameters:
// 1. Block hash (string, required)
// 2. Verbosity (integer, optional, default=1)
// https://bitcoin.org/en/developer-reference#getblock
params := []interface{}{hash, blockVerbosity}
response := &blockResponse{}
if err := b.post(ctx, requestMethodGetBlock, params, response); err != nil {
return nil, fmt.Errorf("%w: error fetching block by hash %s", err, hash)
}
return response.Result, nil
}
// getBlockchainInfo performs the `getblockchaininfo` JSON-RPC request
func (b *Client) getBlockchainInfo(
ctx context.Context,
) (*BlockchainInfo, error) {
params := []interface{}{}
response := &blockchainInfoResponse{}
if err := b.post(ctx, requestMethodGetBlockchainInfo, params, response); err != nil {
return nil, fmt.Errorf("%w: unbale to get blockchain info", err)
}
return response.Result, nil
}
// getBlockHash returns the hash for a specified block identifier.
// If the identifier includes a hash it will return that hash.
// If the identifier only includes an index, if will fetch the hash that corresponds to
// that block height from the node.
func (b *Client) getBlockHash(
ctx context.Context,
identifier *types.PartialBlockIdentifier,
) (string, error) {
// Lookup best block if no PartialBlockIdentifier provided.
if identifier == nil || (identifier.Hash == nil && identifier.Index == nil) {
info, err := b.getBlockchainInfo(ctx)
if err != nil {
return "", fmt.Errorf("%w: unable to get blockchain info", err)
}
return info.BestBlockHash, nil
}
if identifier.Hash != nil {
return *identifier.Hash, nil
}
return b.getHashFromIndex(ctx, *identifier.Index)
}
// parseBlock returns a *types.Block from a Block
func (b *Client) parseBlockData(block *Block) (*types.Block, error) {
if block == nil {
return nil, errors.New("error parsing nil block")
}
blockIndex := block.Height
previousBlockIndex := blockIndex - 1
previousBlockHash := block.PreviousBlockHash
// the genesis block's predecessor is itself
if blockIndex == genesisBlockIndex {
previousBlockIndex = genesisBlockIndex
previousBlockHash = block.Hash
}
metadata, err := block.Metadata()
if err != nil {
return nil, fmt.Errorf("%w: unable to create block metadata", err)
}
return &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Hash: block.Hash,
Index: blockIndex,
},
ParentBlockIdentifier: &types.BlockIdentifier{
Hash: previousBlockHash,
Index: previousBlockIndex,
},
Timestamp: block.Time * timeMultiplier,
Metadata: metadata,
}, nil
}
// getHashFromIndex performs the `getblockhash` JSON-RPC request for the specified
// block index, and returns the hash.
// https://bitcoin.org/en/developer-reference#getblockhash
func (b *Client) getHashFromIndex(
ctx context.Context,
index int64,
) (string, error) {
// Parameters:
// 1. Block height (numeric, required)
// https://bitcoin.org/en/developer-reference#getblockhash
params := []interface{}{index}
response := &blockHashResponse{}
if err := b.post(ctx, requestMethodGetBlockHash, params, response); err != nil {
return "", fmt.Errorf(
"%w: error fetching block hash by index: %d",
err,
index,
)
}
return response.Result, nil
}
// skipTransactionOperations is used to skip operations on transactions that
// contain duplicate UTXOs (which are no longer possible after BIP-30). This
// function mirrors the behavior of a similar commit in bitcoin-core.
//
// Source: https://github.com/bitcoin/bitcoin/commit/ab91bf39b7c11e9c86bb2043c24f0f377f1cf514
func skipTransactionOperations(blockNumber int64, blockHash string, transactionHash string) bool {
if blockNumber == 91842 && blockHash == "00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec" &&
transactionHash == "d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599" {
return true
}
if blockNumber == 91880 && blockHash == "00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721" &&
transactionHash == "e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468" {
return true
}
return false
}
// parseTransactions returns the transactions for a specified `Block`
func (b *Client) parseTransactions(
ctx context.Context,
block *Block,
coins map[string]*types.AccountCoin,
) ([]*types.Transaction, error) {
logger := bitcoinUtils.ExtractLogger(ctx, "client")
if block == nil {
return nil, errors.New("error parsing nil block")
}
txs := make([]*types.Transaction, len(block.Txs))
for index, transaction := range block.Txs {
txOps, err := b.parseTxOperations(transaction, index, coins)
if err != nil {
return nil, fmt.Errorf("%w: error parsing transaction operations", err)
}
if skipTransactionOperations(block.Height, block.Hash, transaction.Hash) {
logger.Warnw(
"skipping transaction",
"block index", block.Height,
"block hash", block.Hash,
"transaction hash", transaction.Hash,
)
for _, op := range txOps {
op.Status = types.String(SkippedStatus)
}
}
metadata, err := transaction.Metadata()
if err != nil {
return nil, fmt.Errorf("%w: unable to get metadata for transaction", err)
}
tx := &types.Transaction{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: transaction.Hash,
},
Operations: txOps,
Metadata: metadata,
}
txs[index] = tx
// In some cases, a transaction will spent an output
// from the same block.
for _, op := range tx.Operations {
if op.CoinChange == nil {
continue
}
if op.CoinChange.CoinAction != types.CoinCreated {
continue
}
coins[op.CoinChange.CoinIdentifier.Identifier] = &types.AccountCoin{
Coin: &types.Coin{
CoinIdentifier: op.CoinChange.CoinIdentifier,
Amount: op.Amount,
},
Account: op.Account,
}
}
}
return txs, nil
}
// parseTransactions returns the transaction operations for a specified transaction.
// It uses a map of previous transactions to properly hydrate the input operations.
func (b *Client) parseTxOperations(
tx *Transaction,
txIndex int,
coins map[string]*types.AccountCoin,
) ([]*types.Operation, error) {
txOps := []*types.Operation{}
for networkIndex, input := range tx.Inputs {
if bitcoinIsCoinbaseInput(input, txIndex, networkIndex) {
txOp, err := b.coinbaseTxOperation(input, int64(len(txOps)), int64(networkIndex))
if err != nil {
return nil, err
}
txOps = append(txOps, txOp)
break
}
// Fetch the *storage.AccountCoin the input is associated with
accountCoin, ok := coins[CoinIdentifier(input.TxHash, input.Vout)]
if !ok {
return nil, fmt.Errorf(
"error finding previous tx: %s, for tx: %s, input index: %d",
input.TxHash,
tx.Hash,
networkIndex,
)
}
// Parse the input transaction operation
txOp, err := b.parseInputTransactionOperation(
input,
int64(len(txOps)),
int64(networkIndex),
accountCoin,
)
if err != nil {
return nil, fmt.Errorf("%w: error parsing tx input", err)
}
txOps = append(txOps, txOp)
}
for networkIndex, output := range tx.Outputs {
txOp, err := b.parseOutputTransactionOperation(
output,
tx.Hash,
int64(len(txOps)),
int64(networkIndex),
)
if err != nil {
return nil, fmt.Errorf(
"%w: error parsing tx output, hash: %s, index: %d",
err,
tx.Hash,
networkIndex,
)
}
txOps = append(txOps, txOp)
}
return txOps, nil
}
// parseOutputTransactionOperation returns the types.Operation for the specified
// `bitcoinOutput` transaction output.
func (b *Client) parseOutputTransactionOperation(
output *Output,
txHash string,
index int64,
networkIndex int64,
) (*types.Operation, error) {
amount, err := b.parseAmount(output.Value)
if err != nil {
return nil, fmt.Errorf(
"%w: error parsing output value, hash: %s, index: %d",
err,
txHash,
index,
)
}
metadata, err := output.Metadata()
if err != nil {
return nil, fmt.Errorf("%w: unable to get output metadata", err)
}
coinChange := &types.CoinChange{
CoinIdentifier: &types.CoinIdentifier{
Identifier: fmt.Sprintf("%s:%d", txHash, networkIndex),
},
CoinAction: types.CoinCreated,
}
// If we are unable to parse the output account (i.e. bitcoind
// returns a blank/nonstandard ScriptPubKey), we create an address as the
// concatenation of the tx hash and index.
//
// Example: 4852fe372ff7534c16713b3146bbc1e86379c70bea4d5c02fb1fa0112980a081:1
// on testnet
account := b.parseOutputAccount(output.ScriptPubKey)
if len(account.Address) == 0 {
account.Address = fmt.Sprintf("%s:%d", txHash, networkIndex)
}
// If this is an OP_RETURN locking script,
// we don't create a coin because it is provably unspendable.
if output.ScriptPubKey.Type == NullData {
coinChange = nil
}
return &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Index: index,
NetworkIndex: &networkIndex,
},
Type: OutputOpType,
Status: types.String(SuccessStatus),
Account: account,
Amount: &types.Amount{
Value: strconv.FormatInt(int64(amount), 10),
Currency: b.currency,
},
CoinChange: coinChange,
Metadata: metadata,
}, nil
}
// getInputTxHash returns the transaction hash corresponding to an inputs previous
// output. If the input is a coinbase input, then no previous transaction is associated
// with the input.
func (b *Client) getInputTxHash(
input *Input,
txIndex int,
inputIndex int,
) (string, int64, bool) {
if bitcoinIsCoinbaseInput(input, txIndex, inputIndex) {
return "", -1, false
}
return input.TxHash, input.Vout, true
}
// bitcoinIsCoinbaseInput returns whether the specified input is
// the coinbase input. The coinbase input is always the first input in the first
// transaction, and does not contain a previous transaction hash.
func bitcoinIsCoinbaseInput(input *Input, txIndex int, inputIndex int) bool {
return txIndex == 0 && inputIndex == 0 && input.TxHash == "" && input.Coinbase != ""
}
// parseInputTransactionOperation returns the types.Operation for the specified
// Input transaction input.
func (b *Client) parseInputTransactionOperation(
input *Input,
index int64,
networkIndex int64,
accountCoin *types.AccountCoin,
) (*types.Operation, error) {
metadata, err := input.Metadata()
if err != nil {
return nil, fmt.Errorf("%w: unable to get input metadata", err)
}
newValue, err := types.NegateValue(accountCoin.Coin.Amount.Value)
if err != nil {
return nil, fmt.Errorf("%w: unable to negate previous output", err)
}
return &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Index: index,
NetworkIndex: &networkIndex,
},
Type: InputOpType,
Status: types.String(SuccessStatus),
Account: accountCoin.Account,
Amount: &types.Amount{
Value: newValue,
Currency: b.currency,
},
CoinChange: &types.CoinChange{
CoinIdentifier: &types.CoinIdentifier{
Identifier: fmt.Sprintf("%s:%d", input.TxHash, input.Vout),
},
CoinAction: types.CoinSpent,
},
Metadata: metadata,
}, nil
}
// parseAmount returns the atomic value of the specified amount.
// https://godoc.org/github.com/btcsuite/btcutil#NewAmount
func (b *Client) parseAmount(amount float64) (uint64, error) {
atomicAmount, err := btcutil.NewAmount(amount)
if err != nil {
return uint64(0), fmt.Errorf("%w: error parsing amount", err)
}
if atomicAmount < 0 {
return uint64(0), fmt.Errorf("error unexpected negative amount: %d", atomicAmount)
}
return uint64(atomicAmount), nil
}
// parseOutputAccount parses a bitcoinScriptPubKey and returns an account
// identifier. The account identifier's address corresponds to the first
// address encoded in the script.
func (b *Client) parseOutputAccount(
scriptPubKey *ScriptPubKey,
) *types.AccountIdentifier {
if len(scriptPubKey.Addresses) != 1 {
return &types.AccountIdentifier{Address: scriptPubKey.Hex}
}
return &types.AccountIdentifier{Address: scriptPubKey.Addresses[0]}
}
// coinbaseTxOperation constructs a transaction operation for the coinbase input.
// This reflects an input that does not correspond to a previous output.
func (b *Client) coinbaseTxOperation(
input *Input,
index int64,
networkIndex int64,
) (*types.Operation, error) {
metadata, err := input.Metadata()
if err != nil {
return nil, fmt.Errorf("%w: unable to get input metadata", err)
}
return &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Index: index,
NetworkIndex: &networkIndex,
},
Type: CoinbaseOpType,
Status: types.String(SuccessStatus),
Metadata: metadata,
}, nil
}
// post makes a HTTP request to a Bitcoin node
func (b *Client) post(
ctx context.Context,
method requestMethod,
params []interface{},
response jSONRPCResponse,
) error {
rpcRequest := &request{
JSONRPC: jSONRPCVersion,
ID: requestID,
Method: string(method),
Params: params,
}
requestBody, err := json.Marshal(rpcRequest)
if err != nil {
return fmt.Errorf("%w: error marshalling RPC request", err)
}
req, err := http.NewRequest(http.MethodPost, b.baseURL, bytes.NewReader(requestBody))
if err != nil {
return fmt.Errorf("%w: error constructing request", err)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(rpcUsername, rpcPassword)
// Perform the post request
res, err := b.httpClient.Do(req.WithContext(ctx))
if err != nil {
return fmt.Errorf("%w: error posting to rpc-api", err)
}
defer func() {
if cerr := res.Body.Close(); cerr != nil {
// Log or handle the error
fmt.Printf("Failed to close response body: %v", cerr)
}
}()
// We expect JSON-RPC responses to return `200 OK` statuses
if res.StatusCode != http.StatusOK {
val, _ := io.ReadAll(res.Body)
return fmt.Errorf("invalid response: %s %s", res.Status, string(val))
}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return fmt.Errorf("%w: error decoding response body", err)
}
// Handle errors that are returned in JSON-RPC responses with `200 OK` statuses
return response.Err()
}