@@ -43,8 +43,18 @@ use bdk::database::any::SledDbConfiguration;
4343#[ cfg( feature = "sqlite-db" ) ]
4444use bdk:: database:: any:: SqliteDbConfiguration ;
4545use bdk:: database:: { AnyDatabase , AnyDatabaseConfig , BatchDatabase , ConfigurableDatabase } ;
46+ #[ cfg( feature = "hardware-signer" ) ]
47+ use bdk:: hwi:: { interface:: HWIClient , types:: HWIChain } ;
48+ #[ cfg( feature = "hardware-signer" ) ]
49+ use bdk:: wallet:: hardwaresigner:: HWISigner ;
50+ #[ cfg( feature = "hardware-signer" ) ]
51+ use bdk:: wallet:: signer:: { SignerError , SignerOrdering } ;
4652use bdk:: wallet:: wallet_name_from_descriptor;
53+ #[ cfg( feature = "hardware-signer" ) ]
54+ use bdk:: KeychainKind ;
4755use bdk:: { Error , Wallet } ;
56+ #[ cfg( feature = "hardware-signer" ) ]
57+ use std:: sync:: Arc ;
4858
4959/// Create a randomized wallet name from the descriptor checksum.
5060/// If wallet options already includes a name, use that instead.
@@ -497,5 +507,41 @@ where
497507 let descriptor = wallet_opts. descriptor . as_str ( ) ;
498508 let change_descriptor = wallet_opts. change_descriptor . as_deref ( ) ;
499509 let wallet = Wallet :: new ( descriptor, change_descriptor, network, database) ?;
510+
511+ #[ cfg( feature = "hardware-signer" ) ]
512+ let wallet = add_hardware_signers ( wallet, network) ?;
513+
514+ Ok ( wallet)
515+ }
516+
517+ /// Add hardware wallets as signers to the wallet
518+ #[ cfg( feature = "hardware-signer" ) ]
519+ fn add_hardware_signers < D > ( wallet : Wallet < D > , network : Network ) -> Result < Wallet < D > , Error >
520+ where
521+ D : BatchDatabase ,
522+ {
523+ let mut wallet = wallet;
524+ let chain = match network {
525+ Network :: Bitcoin => HWIChain :: Main ,
526+ Network :: Testnet => HWIChain :: Test ,
527+ Network :: Regtest => HWIChain :: Regtest ,
528+ Network :: Signet => HWIChain :: Signet ,
529+ } ;
530+ let devices = HWIClient :: enumerate ( ) . map_err ( |e| SignerError :: from ( e) ) ?;
531+ for device in devices {
532+ let device = device. map_err ( |e| SignerError :: from ( e) ) ?;
533+ // Creating a custom signer from the device
534+ let custom_signer =
535+ HWISigner :: from_device ( & device, chain. clone ( ) ) . map_err ( |e| SignerError :: from ( e) ) ?;
536+
537+ // Adding the hardware signer to the BDK wallet
538+ wallet. add_signer (
539+ KeychainKind :: External ,
540+ SignerOrdering ( 200 ) ,
541+ Arc :: new ( custom_signer) ,
542+ ) ;
543+ println ! ( "Added {} as a signer to the wallet." , device. model) ;
544+ }
545+
500546 Ok ( wallet)
501547}
0 commit comments