Skip to content

Commit c68de8c

Browse files
committed
Add convenience methods on WitnessTemplate
1 parent 68dcbe2 commit c68de8c

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/miniscript/satisfy.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use sync::Arc;
2828

2929
use super::context::SigType;
3030
use crate::descriptor::DescriptorType;
31-
use crate::plan::{AssetProvider, Plan};
31+
use crate::plan::{AssetProvider, Plan, RequiredPreimage, RequiredSig};
3232
use crate::prelude::*;
3333
use crate::util::witness_size;
3434
use crate::{
@@ -721,6 +721,35 @@ impl<Pk: MiniscriptKey + ToPublicKey> WitnessTemplate<Placeholder<Pk>> {
721721
.collect(),
722722
}
723723
}
724+
725+
/// Returns the list of required signatures
726+
pub fn required_signatures(&self) -> Vec<RequiredSig<'_, Pk>> {
727+
self.stack
728+
.iter()
729+
.filter_map(|item| match item {
730+
Placeholder::EcdsaSigPk(pk) => Some(RequiredSig::Ecdsa(pk)),
731+
Placeholder::SchnorrSig(pk, None) => Some(RequiredSig::SchnorrTapKey(pk)),
732+
Placeholder::SchnorrSig(pk, Some(lh)) => {
733+
Some(RequiredSig::SchnorrTapScript(pk, lh))
734+
}
735+
_ => None,
736+
})
737+
.collect()
738+
}
739+
740+
/// Returns the list of required preimages
741+
pub fn required_preimages(&self) -> Vec<RequiredPreimage<'_, Pk>> {
742+
self.stack
743+
.iter()
744+
.filter_map(|item| match item {
745+
Placeholder::Sha256Preimage(h) => Some(RequiredPreimage::Sha256(h)),
746+
Placeholder::Hash256Preimage(h) => Some(RequiredPreimage::Hash256(h)),
747+
Placeholder::Ripemd160Preimage(h) => Some(RequiredPreimage::Ripemd160(h)),
748+
Placeholder::Hash160Preimage(h) => Some(RequiredPreimage::Hash160(h)),
749+
_ => None,
750+
})
751+
.collect()
752+
}
724753
}
725754

726755
impl<Pk: MiniscriptKey + ToPublicKey> WitnessTemplate<PartialSatisfaction<Pk>> {

src/plan.rs

+24
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ where
215215
}
216216
}
217217

218+
/// Enum defining the type of signature required
219+
#[derive(Debug, Clone)]
220+
pub enum RequiredSig<'pk, Pk: MiniscriptKey> {
221+
/// ECDSA (legacy or Segwit-v0) signature
222+
Ecdsa(&'pk Pk),
223+
/// Schnorr key-spend signature (BIP-341)
224+
SchnorrTapKey(&'pk Pk),
225+
/// Schnorr script-spend signature (BIP-341)
226+
SchnorrTapScript(&'pk Pk, &'pk TapLeafHash),
227+
}
228+
229+
/// Enum defining the type of preimage required
230+
#[derive(Debug, Clone)]
231+
pub enum RequiredPreimage<'h, Pk: MiniscriptKey> {
232+
/// HASH160 preimage
233+
Hash160(&'h <Pk as MiniscriptKey>::Hash160),
234+
/// RIPEMD160 preimage
235+
Ripemd160(&'h <Pk as MiniscriptKey>::Ripemd160),
236+
/// HASH256 preimage
237+
Hash256(&'h <Pk as MiniscriptKey>::Hash256),
238+
/// SHA256 preimage
239+
Sha256(&'h <Pk as MiniscriptKey>::Sha256),
240+
}
241+
218242
/// Representation of a particular spending path on a descriptor. Contains the witness template
219243
/// and the timelocks needed for satisfying the plan.
220244
/// Calling `get_plan` on a Descriptor will return this structure,

0 commit comments

Comments
 (0)