@@ -4,9 +4,7 @@ use std::sync::Arc;
44use std:: time:: Duration ;
55
66use axum_server:: tls_rustls:: RustlsConfig ;
7- use bitcoin:: { Amount , Psbt } ;
8- pub use corepc_node; // re-export for convenience
9- use corepc_node:: AddressType ;
7+ use bitcoin:: Psbt ;
108use http:: StatusCode ;
119use ohttp:: hpke:: { Aead , Kdf , Kem } ;
1210use ohttp:: { KeyId , SymmetricSuite } ;
@@ -19,9 +17,15 @@ use rustls::pki_types::CertificateDer;
1917use rustls:: RootCertStore ;
2018use tempfile:: tempdir;
2119use tokio:: task:: JoinHandle ;
22- use tracing:: Level ;
2320use tracing_subscriber:: { EnvFilter , FmtSubscriber } ;
2421
22+ #[ cfg( feature = "bitcoind" ) ]
23+ mod bitcoind;
24+ #[ cfg( feature = "bitcoind" ) ]
25+ pub use bitcoind:: * ;
26+ #[ cfg( feature = "bitcoind" ) ]
27+ pub use corepc_node;
28+
2529pub type BoxError = Box < dyn std:: error:: Error + ' static > ;
2630pub type BoxSendSyncError = Box < dyn std:: error:: Error + Send + Sync > ;
2731
@@ -173,77 +177,10 @@ pub fn local_cert_key() -> rcgen::CertifiedKey<rcgen::KeyPair> {
173177 . expect ( "Failed to generate cert" )
174178}
175179
176- pub fn init_bitcoind ( ) -> Result < corepc_node:: Node , BoxError > {
177- let bitcoind_exe = corepc_node:: exe_path ( ) ?;
178- let mut conf = corepc_node:: Conf :: default ( ) ;
179- conf. view_stdout = tracing:: enabled!( target: "corepc" , Level :: TRACE ) ;
180- // conf.args.push("-txindex");
181- let bitcoind = corepc_node:: Node :: with_conf ( bitcoind_exe, & conf) ?;
182- Ok ( bitcoind)
183- }
184-
185- pub fn init_bitcoind_sender_receiver (
186- sender_address_type : Option < AddressType > ,
187- receiver_address_type : Option < AddressType > ,
188- ) -> Result < ( corepc_node:: Node , corepc_node:: Client , corepc_node:: Client ) , BoxError > {
189- let bitcoind = init_bitcoind ( ) ?;
190- let mut wallets = create_and_fund_wallets (
191- & bitcoind,
192- vec ! [ ( "receiver" , receiver_address_type) , ( "sender" , sender_address_type) ] ,
193- ) ?;
194- let receiver = wallets. pop ( ) . expect ( "receiver to exist" ) ;
195- let sender = wallets. pop ( ) . expect ( "sender to exist" ) ;
196-
197- Ok ( ( bitcoind, receiver, sender) )
198- }
199-
200- fn create_and_fund_wallets < W : AsRef < str > > (
201- bitcoind : & corepc_node:: Node ,
202- wallets : Vec < ( W , Option < AddressType > ) > ,
203- ) -> Result < Vec < corepc_node:: Client > , BoxError > {
204- let mut funded_wallets = vec ! [ ] ;
205- let funding_wallet = bitcoind. create_wallet ( "funding_wallet" ) ?;
206- let funding_address = funding_wallet. new_address ( ) ?;
207- // 100 blocks would work here, we add a extra block to cover fees between transfers
208- bitcoind. client . generate_to_address ( 101 + wallets. len ( ) , & funding_address) ?;
209- for ( wallet_name, address_type) in wallets {
210- let wallet = bitcoind. create_wallet ( wallet_name) ?;
211- let address = wallet. get_new_address ( None , address_type) ?. into_model ( ) ?. 0 . assume_checked ( ) ;
212- funding_wallet. send_to_address ( & address, Amount :: from_btc ( 50.0 ) ?) ?;
213- funded_wallets. push ( wallet) ;
214- }
215- // Mine the block which funds the different wallets
216- bitcoind. client . generate_to_address ( 1 , & funding_address) ?;
217-
218- for wallet in funded_wallets. iter ( ) {
219- let balances = wallet. get_balances ( ) ?. into_model ( ) ?;
220- assert_eq ! (
221- balances. mine. trusted,
222- Amount :: from_btc( 50.0 ) ?,
223- "wallet doesn't have expected amount of bitcoin"
224- ) ;
225- }
226-
227- Ok ( funded_wallets)
228- }
229-
230180pub fn http_agent ( cert_der : Vec < u8 > ) -> Result < Client , BoxSendSyncError > {
231181 Ok ( http_agent_builder ( cert_der) . build ( ) ?)
232182}
233183
234- pub fn init_bitcoind_multi_sender_single_reciever (
235- number_of_senders : usize ,
236- ) -> Result < ( corepc_node:: Node , Vec < corepc_node:: Client > , corepc_node:: Client ) , BoxError > {
237- let bitcoind = init_bitcoind ( ) ?;
238- let wallets_to_create =
239- ( 0 ..number_of_senders + 1 ) . map ( |i| ( format ! ( "sender_{i}" ) , None ) ) . collect :: < Vec < _ > > ( ) ;
240- let mut wallets = create_and_fund_wallets ( & bitcoind, wallets_to_create) ?;
241- let receiver = wallets. pop ( ) . expect ( "receiver to exist" ) ;
242- let senders = wallets;
243-
244- Ok ( ( bitcoind, senders, receiver) )
245- }
246-
247184fn http_agent_builder ( cert_der : Vec < u8 > ) -> ClientBuilder {
248185 ClientBuilder :: new ( ) . http1_only ( ) . use_rustls_tls ( ) . add_root_certificate (
249186 reqwest:: tls:: Certificate :: from_der ( cert_der. as_slice ( ) )
0 commit comments