1+ use crate :: bitcoin:: Address ;
12use crate :: bitcoin:: DescriptorId ;
23use crate :: bitcoin:: DescriptorType ;
4+ use crate :: bitcoin:: Script ;
35use crate :: error:: DescriptorError ;
46use crate :: error:: MiniscriptError ;
57use crate :: keys:: DescriptorPublicKey ;
@@ -12,6 +14,7 @@ use bdk_wallet::chain::DescriptorExt;
1214use bdk_wallet:: descriptor:: { ExtendedDescriptor , IntoWalletDescriptor } ;
1315use bdk_wallet:: keys:: DescriptorPublicKey as BdkDescriptorPublicKey ;
1416use bdk_wallet:: keys:: { DescriptorSecretKey as BdkDescriptorSecretKey , KeyMap } ;
17+ use bdk_wallet:: miniscript:: descriptor:: ConversionError ;
1518use bdk_wallet:: template:: {
1619 Bip44 , Bip44Public , Bip49 , Bip49Public , Bip84 , Bip84Public , Bip86 , Bip86Public ,
1720 DescriptorTemplate ,
@@ -355,6 +358,35 @@ impl Descriptor {
355358 pub fn desc_type ( & self ) -> DescriptorType {
356359 self . extended_descriptor . desc_type ( )
357360 }
361+
362+ pub fn derived_address (
363+ & self ,
364+ index : u32 ,
365+ network : Network ,
366+ ) -> Result < Arc < Address > , DescriptorError > {
367+ if self . extended_descriptor . is_multipath ( ) {
368+ return Err ( DescriptorError :: MultiPath ) ;
369+ }
370+
371+ let secp = Secp256k1 :: verification_only ( ) ;
372+ let derived_descriptor = self
373+ . extended_descriptor
374+ . at_derivation_index ( index)
375+ . and_then ( |desc| desc. derived_descriptor ( & secp) )
376+ . map_err ( |error| match error {
377+ ConversionError :: HardenedChild => DescriptorError :: HardenedDerivationXpub ,
378+ ConversionError :: MultiKey => DescriptorError :: MultiPath ,
379+ } ) ?;
380+ let script_pubkey = derived_descriptor. script_pubkey ( ) ;
381+ let address =
382+ Address :: from_script ( Arc :: new ( Script ( script_pubkey) ) , network) . map_err ( |error| {
383+ DescriptorError :: Miniscript {
384+ error_message : error. to_string ( ) ,
385+ }
386+ } ) ?;
387+
388+ Ok ( Arc :: new ( address) )
389+ }
358390}
359391
360392impl Display for Descriptor {
0 commit comments