66
77// We depend upon and import directly from bitcoin because this module is not concerned with PSBT
88// i.e., it is lower down the stack than the psbt_v2 crate.
9- use bitcoin:: { consensus, Address , Amount , Network , Transaction , Txid } ;
10- use bitcoind:: bitcoincore_rpc:: bitcoincore_rpc_json:: { AddressType , GetBlockchainInfoResult } ;
11- use bitcoind:: bitcoincore_rpc:: RpcApi ;
12- use bitcoind:: BitcoinD ;
9+ use bitcoin:: { Address , Amount , Transaction , Txid } ;
10+ use bitcoind:: { AddressType , Node , vtype:: GetBlockchainInfo } ;
1311
14- const NETWORK : Network = Network :: Regtest ;
1512const FIFTY_BTC : Amount = Amount :: from_int_btc ( 50 ) ;
1613
1714/// A custom bitcoind client.
1815pub struct Client {
1916 /// Handle for the regtest `bitcoind` instance.
20- bitcoind : BitcoinD ,
17+ bitcoind : Node ,
2118 /// This is public so we don't have to handle the complexity of know if send/receives are
2219 /// to/from the Core controlled wallet or somewhere else. User is required to manage this.
2320 pub balance : BalanceTracker ,
@@ -26,7 +23,8 @@ pub struct Client {
2623impl Client {
2724 /// Creates a new [`Client`].
2825 pub fn new ( ) -> anyhow:: Result < Self > {
29- let bitcoind = BitcoinD :: from_downloaded ( ) ?;
26+ let exe_path = bitcoind:: exe_path ( ) ?;
27+ let bitcoind = Node :: new ( exe_path) ?;
3028 let balance = BalanceTracker :: zero ( ) ;
3129
3230 let client = Client { bitcoind, balance } ;
@@ -60,29 +58,28 @@ impl Client {
6058 }
6159
6260 /// Calls through to bitcoincore_rpc client.
63- pub fn get_blockchain_info ( & self ) -> anyhow:: Result < GetBlockchainInfoResult > {
61+ pub fn get_blockchain_info ( & self ) -> anyhow:: Result < GetBlockchainInfo > {
6462 let client = & self . bitcoind . client ;
6563 Ok ( client. get_blockchain_info ( ) ?)
6664 }
6765
6866 /// Gets an address controlled by the currently loaded Bitcoin Core wallet (via `bitcoind`).
6967 pub fn core_wallet_controlled_address ( & self ) -> anyhow:: Result < Address > {
7068 let client = & self . bitcoind . client ;
71- let label = None ;
72- let address_type = Some ( AddressType :: Bech32m ) ;
73- let address = client. get_new_address ( label , address_type ) ? . require_network ( NETWORK ) ?;
69+ // Use Bech32 (segwit v0) for compatibility with all Bitcoin Core versions.
70+ // Bech32m (taproot) is only available from Bitcoin Core 22.0+.
71+ let address = client. new_address_with_type ( AddressType :: Bech32 ) ?;
7472 Ok ( address)
7573 }
7674
7775 pub fn balance ( & self ) -> anyhow:: Result < Amount > {
7876 let client = & self . bitcoind . client ;
79- let minconf = None ; // What is this?
80- let include_watchonly = None ;
81- Ok ( client. get_balance ( minconf, include_watchonly) ?)
77+ let balance = client. get_balance ( ) ?. balance ( ) ?;
78+ Ok ( balance)
8279 }
8380
8481 /// Mines `n` blocks to a new address controlled by the currently loaded Bitcoin Core wallet.
85- fn mine_blocks ( & self , n : u64 ) -> anyhow:: Result < ( ) > {
82+ fn mine_blocks ( & self , n : usize ) -> anyhow:: Result < ( ) > {
8683 let client = & self . bitcoind . client ;
8784 // Generate to an address controlled by the bitcoind wallet and wait for funds to mature.
8885 let address = self . core_wallet_controlled_address ( ) ?;
@@ -91,45 +88,25 @@ impl Client {
9188 Ok ( ( ) )
9289 }
9390
94- /// Send `amount` to `address` setting all other `bitcoincore_prc::send_to_address` args to `None` .
91+ /// Send `amount` to `address`.
9592 ///
9693 /// Caller required to update balance (ie, call self.balance.send()).
9794 pub fn send ( & self , amount : Amount , address : & Address ) -> anyhow:: Result < Txid > {
9895 let client = & self . bitcoind . client ;
99-
100- let comment = None ;
101- let comment_to = None ;
102- let subtract_fee = None ;
103- let replacable = None ;
104- let confirmation_target = None ;
105- let estimate_mode = None ;
106-
107- let txid = client. send_to_address (
108- address,
109- amount,
110- comment,
111- comment_to,
112- subtract_fee,
113- replacable,
114- confirmation_target,
115- estimate_mode,
116- ) ?;
117-
96+ let txid = client. send_to_address ( address, amount) ?. txid ( ) ?;
11897 Ok ( txid)
11998 }
12099
121100 pub fn get_transaction ( & self , txid : & Txid ) -> anyhow:: Result < Transaction > {
122101 let client = & self . bitcoind . client ;
123- let include_watchonly = None ;
124- let res = client. get_transaction ( txid, include_watchonly) ?;
125- let tx: Transaction = consensus:: encode:: deserialize ( & res. hex ) ?;
102+ let res = client. get_transaction ( * txid) ?;
103+ let tx = res. into_model ( ) ?. tx ;
126104 Ok ( tx)
127105 }
128106
129107 pub fn send_raw_transaction ( & self , tx : & Transaction ) -> anyhow:: Result < Txid > {
130108 let client = & self . bitcoind . client ;
131- let hex = consensus:: encode:: serialize_hex ( & tx) ;
132- let txid = client. send_raw_transaction ( hex) ?;
109+ let txid = client. send_raw_transaction ( tx) ?. txid ( ) ?;
133110 Ok ( txid)
134111 }
135112}
0 commit comments