@@ -3,10 +3,13 @@ use std::str::FromStr;
33use std:: sync:: Arc ;
44use std:: time:: Duration ;
55
6+ #[ cfg( feature = "bitcoind" ) ]
7+ pub mod bitcoind;
8+ #[ cfg( feature = "bitcoind" ) ]
9+ pub use bitcoind:: * ;
10+
611use axum_server:: tls_rustls:: RustlsConfig ;
7- use bitcoin:: { Amount , Psbt } ;
8- pub use corepc_node; // re-export for convenience
9- use corepc_node:: AddressType ;
12+ use bitcoin:: Psbt ;
1013use http:: StatusCode ;
1114use ohttp:: hpke:: { Aead , Kdf , Kem } ;
1215use ohttp:: { KeyId , SymmetricSuite } ;
@@ -19,7 +22,6 @@ use rustls::pki_types::CertificateDer;
1922use rustls:: RootCertStore ;
2023use tempfile:: tempdir;
2124use tokio:: task:: JoinHandle ;
22- use tracing:: Level ;
2325use tracing_subscriber:: { EnvFilter , FmtSubscriber } ;
2426
2527pub type BoxError = Box < dyn std:: error:: Error + ' static > ;
@@ -173,77 +175,10 @@ pub fn local_cert_key() -> rcgen::CertifiedKey<rcgen::KeyPair> {
173175 . expect ( "Failed to generate cert" )
174176}
175177
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-
230178pub fn http_agent ( cert_der : Vec < u8 > ) -> Result < Client , BoxSendSyncError > {
231179 Ok ( http_agent_builder ( cert_der) . build ( ) ?)
232180}
233181
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-
247182fn http_agent_builder ( cert_der : Vec < u8 > ) -> ClientBuilder {
248183 ClientBuilder :: new ( ) . http1_only ( ) . use_rustls_tls ( ) . add_root_certificate (
249184 reqwest:: tls:: Certificate :: from_der ( cert_der. as_slice ( ) )
0 commit comments