Skip to content

Commit f4599d6

Browse files
committed
Extract into_definite() and reuse in find_derivation_index_for_spk
1 parent 96f5f08 commit f4599d6

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

src/descriptor/mod.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,38 @@ impl Descriptor<DescriptorPublicKey> {
654654
/// Whether or not the descriptor has any wildcards i.e. `/*`.
655655
pub fn has_wildcard(&self) -> bool { self.for_any_key(|key| key.has_wildcard()) }
656656

657+
/// Converts a non-wildcard descriptor into a *definite* descriptor.
658+
///
659+
/// This is the correct way to obtain a `Descriptor<DefiniteDescriptorKey>` from a descriptor
660+
/// that has no wildcards. For descriptors with wildcards, use [`at_derivation_index`] instead.
661+
///
662+
/// [`at_derivation_index`]: Self::at_derivation_index
663+
///
664+
/// # Errors
665+
/// - If the descriptor contains wildcards
666+
/// - If the descriptor contains multi-path derivations
667+
pub fn into_definite(
668+
&self,
669+
) -> Result<Descriptor<DefiniteDescriptorKey>, NonDefiniteKeyError> {
670+
if self.has_wildcard() {
671+
return Err(NonDefiniteKeyError::Wildcard);
672+
}
673+
struct Definitor;
674+
675+
impl Translator<DescriptorPublicKey> for Definitor {
676+
type TargetPk = DefiniteDescriptorKey;
677+
type Error = NonDefiniteKeyError;
678+
679+
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
680+
DefiniteDescriptorKey::new(pk.clone())
681+
}
682+
683+
translate_hash_clone!(DescriptorPublicKey);
684+
}
685+
self.translate_pk(&mut Definitor)
686+
.map_err(|e| e.expect_translator_err("No Context errors while translating"))
687+
}
688+
657689
/// Replaces all wildcards (i.e. `/*`) in the descriptor with a particular derivation index,
658690
/// turning it into a *definite* descriptor.
659691
///
@@ -854,28 +886,7 @@ impl Descriptor<DescriptorPublicKey> {
854886
range: Range<u32>,
855887
) -> Result<Option<(u32, Descriptor<bitcoin::PublicKey>)>, NonDefiniteKeyError> {
856888
if !self.has_wildcard() {
857-
// Non-wildcard descriptor: translate directly to a definite descriptor without
858-
// needing a derivation index.
859-
struct Definitor;
860-
861-
impl Translator<DescriptorPublicKey> for Definitor {
862-
type TargetPk = DefiniteDescriptorKey;
863-
type Error = NonDefiniteKeyError;
864-
865-
fn pk(
866-
&mut self,
867-
pk: &DescriptorPublicKey,
868-
) -> Result<Self::TargetPk, Self::Error> {
869-
DefiniteDescriptorKey::new(pk.clone())
870-
}
871-
872-
translate_hash_clone!(DescriptorPublicKey);
873-
}
874-
875-
let definite = self
876-
.translate_pk(&mut Definitor)
877-
.map_err(|e| e.expect_translator_err("No Context errors while translating"))?;
878-
let concrete = definite.derived_descriptor(secp);
889+
let concrete = self.into_definite()?.derived_descriptor(secp);
879890
if &concrete.script_pubkey() == script_pubkey {
880891
return Ok(Some((0, concrete)));
881892
}

0 commit comments

Comments
 (0)