Skip to content

Commit

Permalink
Refactor satisfy* functions
Browse files Browse the repository at this point in the history
Reduce duplicate code by factoring out a helper function.

Refactor only, no logic changes.
  • Loading branch information
tcharding committed Jul 27, 2023
1 parent 0e96460 commit 6775430
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
{
// Only satisfactions for default versions (0xc0) are allowed.
let leaf_hash = TapLeafHash::from_script(&self.encode(), LeafVersion::TapScript);
match satisfy::Satisfaction::satisfy(&self.node, &satisfier, self.ty.mall.safe, &leaf_hash)
.stack
{
satisfy::Witness::Stack(stack) => {
Ctx::check_witness::<Pk>(&stack)?;
Ok(stack)
}
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => {
Err(Error::CouldNotSatisfy)
}
}
let satisfaction = satisfy::Satisfaction::satisfy(&self.node, &satisfier, self.ty.mall.safe, &leaf_hash);
self._satisfy(satisfaction)
}

/// Attempt to produce a malleable satisfying witness for the
Expand All @@ -225,14 +216,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
Pk: ToPublicKey,
{
let leaf_hash = TapLeafHash::from_script(&self.encode(), LeafVersion::TapScript);
match satisfy::Satisfaction::satisfy_mall(
&self.node,
&satisfier,
self.ty.mall.safe,
&leaf_hash,
)
.stack
{
let satisfaction = satisfy::Satisfaction::satisfy_mall(&self.node, &satisfier, self.ty.mall.safe, &leaf_hash);
self._satisfy(satisfaction)
}

fn _satisfy(&self, satisfaction: satisfy::Satisfaction) -> Result<Vec<Vec<u8>>, Error>
where
Pk: ToPublicKey,
{
match satisfaction.stack {
satisfy::Witness::Stack(stack) => {
Ctx::check_witness::<Pk>(&stack)?;
Ok(stack)
Expand Down

0 comments on commit 6775430

Please sign in to comment.