Skip to content

Commit

Permalink
Add convenience methods on WitnessTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
afilini committed Nov 17, 2022
1 parent 442d9ac commit c3ba4d3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/miniscript/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sync::Arc;

use super::context::SigType;
use crate::descriptor::DescriptorType;
use crate::plan::{AssetProvider, Plan};
use crate::plan::{AssetProvider, Plan, RequiredPreimage, RequiredSig};
use crate::prelude::*;
use crate::util::witness_size;
use crate::{
Expand Down Expand Up @@ -721,6 +721,35 @@ impl<Pk: MiniscriptKey + ToPublicKey> WitnessTemplate<Placeholder<Pk>> {
.collect(),
}
}

/// Returns the list of required signatures
pub fn required_signatures(&self) -> Vec<RequiredSig<'_, Pk>> {
self.stack
.iter()
.filter_map(|item| match item {
Placeholder::EcdsaSigPk(pk) => Some(RequiredSig::Ecdsa(pk)),
Placeholder::SchnorrSig(pk, None) => Some(RequiredSig::SchnorrTapKey(pk)),
Placeholder::SchnorrSig(pk, Some(lh)) => {
Some(RequiredSig::SchnorrTapScript(pk, lh))
}
_ => None,
})
.collect()
}

/// Returns the list of required preimages
pub fn required_preimages(&self) -> Vec<RequiredPreimage<'_, Pk>> {
self.stack
.iter()
.filter_map(|item| match item {
Placeholder::Sha256Preimage(h) => Some(RequiredPreimage::Sha256(h)),
Placeholder::Hash256Preimage(h) => Some(RequiredPreimage::Hash256(h)),
Placeholder::Ripemd160Preimage(h) => Some(RequiredPreimage::Ripemd160(h)),
Placeholder::Hash160Preimage(h) => Some(RequiredPreimage::Hash160(h)),
_ => None,
})
.collect()
}
}

impl<Pk: MiniscriptKey + ToPublicKey> WitnessTemplate<PartialSatisfaction<Pk>> {
Expand Down
24 changes: 24 additions & 0 deletions src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ where
}
}

/// Enum defining the type of signature required
#[derive(Debug, Clone)]
pub enum RequiredSig<'pk, Pk: MiniscriptKey> {
/// ECDSA (legacy or Segwit-v0) signature
Ecdsa(&'pk Pk),
/// Schnorr key-spend signature (BIP-341)
SchnorrTapKey(&'pk Pk),
/// Schnorr script-spend signature (BIP-341)
SchnorrTapScript(&'pk Pk, &'pk TapLeafHash),
}

/// Enum defining the type of preimage required
#[derive(Debug, Clone)]
pub enum RequiredPreimage<'h, Pk: MiniscriptKey> {
/// HASH160 preimage
Hash160(&'h <Pk as MiniscriptKey>::Hash160),
/// RIPEMD160 preimage
Ripemd160(&'h <Pk as MiniscriptKey>::Ripemd160),
/// HASH256 preimage
Hash256(&'h <Pk as MiniscriptKey>::Hash256),
/// SHA256 preimage
Sha256(&'h <Pk as MiniscriptKey>::Sha256),
}

/// Representation of a particular spending path on a descriptor. Contains the witness template
/// and the timelocks needed for satisfying the plan.
/// Calling `get_plan` on a Descriptor will return this structure,
Expand Down

0 comments on commit c3ba4d3

Please sign in to comment.