1- package com .danubetech .btc .connection ;
2-
3- import com .danubetech .btc .records .Block ;
4- import com .danubetech .btc .records .Tx ;
5- import com .danubetech .btc .records .TxIn ;
6- import com .danubetech .btc .records .TxOut ;
7- import com .fasterxml .jackson .databind .DeserializationFeature ;
8- import com .fasterxml .jackson .databind .SerializationFeature ;
9- import com .fasterxml .jackson .databind .json .JsonMapper ;
1+ package com .danubetech .btc .connection .impl ;
2+
3+ import com .danubetech .btc .connection .*;
104import org .slf4j .Logger ;
115import org .slf4j .LoggerFactory ;
126import wf .bitcoin .javabitcoindrpcclient .BitcoinJSONRPCClient ;
137
148import java .net .URL ;
159import java .util .List ;
1610import java .util .Map ;
17- import java .util .stream .Collectors ;
1811
1912public class BitcoindRPCBitcoinConnection extends AbstractBitcoinConnection implements BitcoinConnection {
2013
2114 private static final Logger log = LoggerFactory .getLogger (BitcoindRPCBitcoinConnection .class );
2215
23- private static final JsonMapper mapper = JsonMapper .builder ()
24- .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false )
25- .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
26- .build ();
27-
2816 private final BitcoinJSONRPCClient bitcoindRpcClient ;
2917
3018 private BitcoindRPCBitcoinConnection (BitcoinJSONRPCClient bitcoindRpcClient ) {
@@ -40,17 +28,17 @@ public static BitcoindRPCBitcoinConnection create(URL rpcUrl) {
4028 @ Override
4129 public Block getBlockByBlockHeight (Integer blockHeight ) {
4230 BitcoinJSONRPCClient bitcoinJSONRPCClient = this .getBitcoinJsonRpcClient ();
43- wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
44- List <Tx > txs = bitcoinBlock .tx ().stream ().map (tx -> txFromBitcoinRawTransaction (bitcoinJSONRPCClient , tx )).collect ( Collectors . toList () );
45- Block block = new Block ( bitcoinBlock . height (), bitcoinBlock . hash (), bitcoinBlock . time (). getTime (), txs );
31+ wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinjBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
32+ List <Tx > txs = bitcoinjBlock .tx ().stream ().map (tx -> txFromBitcoinjRawTransaction (bitcoinJSONRPCClient , tx )).toList ();
33+ Block block = blockFromBitcoinjBlock ( bitcoinjBlock );
4634 if (log .isDebugEnabled ()) log .debug ("getBlockByBlockHeight for {}: {}" , blockHeight , block );
4735 return block ;
4836 }
4937
5038 @ Override
5139 public Tx getTransactionById (String txId ) {
5240 BitcoinJSONRPCClient bitcoinJSONRPCClient = this .getBitcoinJsonRpcClient ();
53- Tx tx = txFromBitcoinRawTransaction (bitcoinJSONRPCClient , txId );
41+ Tx tx = txFromBitcoinjRawTransaction (bitcoinJSONRPCClient , txId );
5442 if (log .isDebugEnabled ()) log .debug ("getTransactionById for {}: {}" , txId , tx );
5543 return tx ;
5644 }
@@ -61,9 +49,9 @@ public Block getBlockByTargetTime(Long targetTime) {
6149 Integer blocks = bitcoinJSONRPCClient .getBlockChainInfo ().blocks ();
6250 Block block = null ;
6351 for (int blockHeight =blocks -1 ; blockHeight >=0 ; blockHeight --) {
64- wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
65- if (bitcoinBlock .time ().getTime () < targetTime ) {
66- block = new Block ( bitcoinBlock . height (), bitcoinBlock . hash (), bitcoinBlock . time (). getTime (), null );
52+ wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinjBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
53+ if (bitcoinjBlock .time ().getTime () < targetTime ) {
54+ block = blockFromBitcoinjBlock ( bitcoinjBlock );
6755 break ;
6856 }
6957 }
@@ -77,9 +65,9 @@ public Block getBlockByMinConfirmations(Integer minConfirmations) {
7765 Integer blocks = bitcoinJSONRPCClient .getBlockChainInfo ().blocks ();
7866 Block block = null ;
7967 for (int blockHeight =blocks -1 ; blockHeight >=0 ; blockHeight --) {
80- wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
81- if (bitcoinBlock .confirmations () >= minConfirmations ) {
82- block = new Block ( bitcoinBlock . height (), bitcoinBlock . hash (), bitcoinBlock . time (). getTime (), null );
68+ wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinjBlock = bitcoinJSONRPCClient .getBlock (blockHeight );
69+ if (bitcoinjBlock .confirmations () >= minConfirmations ) {
70+ block = blockFromBitcoinjBlock ( bitcoinjBlock );
8371 break ;
8472 }
8573 }
@@ -97,24 +85,27 @@ public Map<String, Object> getMetadata() {
9785 * Helper methods
9886 */
9987
100- private static Tx txFromBitcoinRawTransaction (BitcoinJSONRPCClient bitcoinJSONRPCClient , String txId ) {
88+ private static Block blockFromBitcoinjBlock (wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .Block bitcoinjBlock ) {
89+ return new Block (bitcoinjBlock .height (), bitcoinjBlock .hash (), bitcoinjBlock .time ().getTime (), bitcoinjBlock .confirmations ());
90+ }
91+
92+ private static Tx txFromBitcoinjRawTransaction (BitcoinJSONRPCClient bitcoinJSONRPCClient , String txId ) {
10193 if (Tx .COINBASE_TX_IDENTIFIER .equals (txId ) || Tx .GENESIS_TX_IDENTIFIER .equals (txId )) {
102- return new Tx (txId , null , null , null );
94+ return new Tx (txId , null , null );
10395 }
104- wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction bitcoinRawTransaction = bitcoinJSONRPCClient .getRawTransaction (txId );
105- String txHex = bitcoinRawTransaction .hex ();
106- List <TxIn > txIns = bitcoinRawTransaction .vIn ().stream ().map (BitcoindRPCBitcoinConnection ::txInFromBitcoinIn ).toList ();
107- List <TxOut > txOuts = bitcoinRawTransaction .vOut ().stream ().map (BitcoindRPCBitcoinConnection ::txOutFromBitcoinOut ).toList ();
108- return new Tx (txId , txHex , txIns , txOuts );
96+ wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction bitcoinjRawTransaction = bitcoinJSONRPCClient .getRawTransaction (txId );
97+ List <TxIn > txIns = bitcoinjRawTransaction .vIn ().stream ().map (BitcoindRPCBitcoinConnection ::txInFromBitcoinjIn ).toList ();
98+ List <TxOut > txOuts = bitcoinjRawTransaction .vOut ().stream ().map (BitcoindRPCBitcoinConnection ::txOutFromBitcoinjOut ).toList ();
99+ return new Tx (txId , txIns , txOuts );
109100 }
110101
111- private static TxIn txInFromBitcoinIn (wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction .In in ) {
112- String txId = in .txid ();
102+ private static TxIn txInFromBitcoinjIn (wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction .In in ) {
103+ var txId = in .txid ();
113104 Integer vout = txId == null ? null : in .getTransactionOutput ().n ();
114105 return new TxIn (txId , vout );
115106 }
116107
117- private static TxOut txOutFromBitcoinOut (wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction .Out out ) {
108+ private static TxOut txOutFromBitcoinjOut (wf .bitcoin .javabitcoindrpcclient .BitcoindRpcClient .RawTransaction .Out out ) {
118109 String txId = out .transaction ().txId ();
119110 String scriptPubKeyAddress = out .scriptPubKey ().mapStr ("address" );
120111 String asm = out .scriptPubKey ().asm ();
0 commit comments