Skip to content

Commit 121e0b4

Browse files
committed
Add an API to convert Descriptor into a pre-taproot descriptor
1 parent 9e0735e commit 121e0b4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/descriptor/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,43 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
329329
Descriptor::Tr(ref _tr) => DescriptorType::Tr,
330330
}
331331
}
332+
333+
/// .
334+
/// Convert a Descriptor into [`pretaproot::PreTaprootDescriptor`]
335+
/// # Examples
336+
///
337+
/// ```
338+
/// use std::str::FromStr;
339+
/// use miniscript::descriptor::Descriptor;
340+
/// use miniscript::{PreTaprootDescriptor, PreTaprootDescriptorTrait};
341+
/// use miniscript::bitcoin;
342+
///
343+
/// // A descriptor with a string generic
344+
/// let desc = Descriptor::<bitcoin::PublicKey>::from_str("wpkh(02e18f242c8b0b589bfffeac30e1baa80a60933a649c7fb0f1103e78fbf58aa0ed)")
345+
/// .expect("Valid segwitv0 descriptor");
346+
/// let pre_tap_desc = desc.into_pre_taproot_desc().expect("Wsh is pre taproot");
347+
///
348+
/// // Now the script code and explicit script no longer fail on longer fail
349+
/// // on PreTaprootDescriptor using PreTaprootDescriptorTrait
350+
/// let script_code = pre_tap_desc.script_code();
351+
/// assert_eq!(script_code.to_string(),
352+
/// "Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 62107d047e8818b594303fe0657388cc4fc8771f OP_EQUALVERIFY OP_CHECKSIG)"
353+
/// );
354+
/// ```
355+
///
356+
/// # Errors
357+
///
358+
/// This function will return an error if descriptor is not a pre taproot descriptor.
359+
pub fn into_pre_taproot_desc(self) -> Result<pretaproot::PreTaprootDescriptor<Pk>, Self> {
360+
match self {
361+
Descriptor::Bare(bare) => Ok(pretaproot::PreTaprootDescriptor::Bare(bare)),
362+
Descriptor::Pkh(pkh) => Ok(pretaproot::PreTaprootDescriptor::Pkh(pkh)),
363+
Descriptor::Wpkh(wpkh) => Ok(pretaproot::PreTaprootDescriptor::Wpkh(wpkh)),
364+
Descriptor::Sh(sh) => Ok(pretaproot::PreTaprootDescriptor::Sh(sh)),
365+
Descriptor::Wsh(wsh) => Ok(pretaproot::PreTaprootDescriptor::Wsh(wsh)),
366+
Descriptor::Tr(tr) => Err(Descriptor::Tr(tr)),
367+
}
368+
}
332369
}
333370

334371
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {

0 commit comments

Comments
 (0)