Skip to content

Commit 70b987f

Browse files
committed
Change satisfy API to return detailed error
1 parent 0713cc2 commit 70b987f

File tree

3 files changed

+28
-47
lines changed

3 files changed

+28
-47
lines changed

src/descriptor/mod.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
977977
{
978978
match *self {
979979
Descriptor::Bare(ref d) => {
980-
let wit = match d.satisfy(satisfier, to_pk_ctx) {
981-
Some(wit) => wit,
982-
None => return Err(Error::CouldNotSatisfy),
983-
};
980+
let wit = d.satisfy(satisfier, to_pk_ctx)?;
984981
let script_sig = witness_to_scriptsig(&wit);
985982
let witness = vec![];
986983
Ok((witness, script_sig))
@@ -1044,20 +1041,14 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
10441041
}
10451042
}
10461043
Descriptor::Sh(ref d) => {
1047-
let mut script_witness = match d.satisfy(satisfier, to_pk_ctx) {
1048-
Some(wit) => wit,
1049-
None => return Err(Error::CouldNotSatisfy),
1050-
};
1044+
let mut script_witness = d.satisfy(satisfier, to_pk_ctx)?;
10511045
script_witness.push(d.encode(to_pk_ctx).into_bytes());
10521046
let script_sig = witness_to_scriptsig(&script_witness);
10531047
let witness = vec![];
10541048
Ok((witness, script_sig))
10551049
}
10561050
Descriptor::Wsh(ref d) => {
1057-
let mut witness = match d.satisfy(satisfier, to_pk_ctx) {
1058-
Some(wit) => wit,
1059-
None => return Err(Error::CouldNotSatisfy),
1060-
};
1051+
let mut witness = d.satisfy(satisfier, to_pk_ctx)?;
10611052
witness.push(d.encode(to_pk_ctx).into_bytes());
10621053
let script_sig = Script::new();
10631054
Ok((witness, script_sig))
@@ -1068,28 +1059,19 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
10681059
.push_slice(&witness_script.to_v0_p2wsh()[..])
10691060
.into_script();
10701061

1071-
let mut witness = match d.satisfy(satisfier, to_pk_ctx) {
1072-
Some(wit) => wit,
1073-
None => return Err(Error::CouldNotSatisfy),
1074-
};
1062+
let mut witness = d.satisfy(satisfier, to_pk_ctx)?;
10751063
witness.push(witness_script.into_bytes());
10761064
Ok((witness, script_sig))
10771065
}
10781066
Descriptor::ShSortedMulti(ref smv) => {
1079-
let mut script_witness = match smv.satisfy(satisfier, to_pk_ctx) {
1080-
Some(wit) => wit,
1081-
None => return Err(Error::CouldNotSatisfy),
1082-
};
1067+
let mut script_witness = smv.satisfy(satisfier, to_pk_ctx)?;
10831068
script_witness.push(smv.encode(to_pk_ctx).into_bytes());
10841069
let script_sig = witness_to_scriptsig(&script_witness);
10851070
let witness = vec![];
10861071
Ok((witness, script_sig))
10871072
}
10881073
Descriptor::WshSortedMulti(ref smv) => {
1089-
let mut witness = match smv.satisfy(satisfier, to_pk_ctx) {
1090-
Some(wit) => wit,
1091-
None => return Err(Error::CouldNotSatisfy),
1092-
};
1074+
let mut witness = smv.satisfy(satisfier, to_pk_ctx)?;
10931075
witness.push(smv.encode(to_pk_ctx).into_bytes());
10941076
let script_sig = Script::new();
10951077
Ok((witness, script_sig))
@@ -1100,10 +1082,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
11001082
.push_slice(&witness_script.to_v0_p2wsh()[..])
11011083
.into_script();
11021084

1103-
let mut witness = match smv.satisfy(satisfier, to_pk_ctx) {
1104-
Some(wit) => wit,
1105-
None => return Err(Error::CouldNotSatisfy),
1106-
};
1085+
let mut witness = smv.satisfy(satisfier, to_pk_ctx)?;
11071086
witness.push(witness_script.into_bytes());
11081087
Ok((witness, script_sig))
11091088
}
@@ -1545,7 +1524,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
15451524

15461525
/// Attempt to produce a satisfying witness for the
15471526
/// witness script represented by the parse tree
1548-
pub fn satisfy<ToPkCtx, S>(&self, satisfier: S, to_pk_ctx: ToPkCtx) -> Option<Vec<Vec<u8>>>
1527+
pub fn satisfy<ToPkCtx, S>(
1528+
&self,
1529+
satisfier: S,
1530+
to_pk_ctx: ToPkCtx,
1531+
) -> Result<Vec<Vec<u8>>, Error>
15491532
where
15501533
ToPkCtx: Copy,
15511534
Pk: ToPublicKey<ToPkCtx>,

src/miniscript/mod.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,20 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
296296
&self,
297297
satisfier: S,
298298
to_pk_ctx: ToPkCtx,
299-
) -> Option<Vec<Vec<u8>>>
299+
) -> Result<Vec<Vec<u8>>, Error>
300300
where
301301
Pk: ToPublicKey<ToPkCtx>,
302302
{
303303
match satisfy::Satisfaction::satisfy(&self.node, &satisfier, self.ty.mall.safe, to_pk_ctx)
304304
.stack
305305
{
306306
satisfy::Witness::Stack(stack) => {
307-
if Ctx::check_witness::<Pk, Ctx>(&stack).is_err() {
308-
None
309-
} else {
310-
Some(stack)
311-
}
307+
Ctx::check_witness::<Pk, Ctx>(&stack)?;
308+
Ok(stack)
309+
}
310+
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => {
311+
Err(Error::CouldNotSatisfy)
312312
}
313-
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => None,
314313
}
315314
}
316315

@@ -320,7 +319,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
320319
&self,
321320
satisfier: S,
322321
to_pk_ctx: ToPkCtx,
323-
) -> Option<Vec<Vec<u8>>>
322+
) -> Result<Vec<Vec<u8>>, Error>
324323
where
325324
Pk: ToPublicKey<ToPkCtx>,
326325
{
@@ -333,13 +332,12 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
333332
.stack
334333
{
335334
satisfy::Witness::Stack(stack) => {
336-
if Ctx::check_witness::<Pk, Ctx>(&stack).is_err() {
337-
None
338-
} else {
339-
Some(stack)
340-
}
335+
Ctx::check_witness::<Pk, Ctx>(&stack)?;
336+
Ok(stack)
337+
}
338+
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => {
339+
Err(Error::CouldNotSatisfy)
341340
}
342-
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => None,
343341
}
344342
}
345343
}

src/policy/compiler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,15 +1388,15 @@ mod tests {
13881388
right_sat.insert(keys[i].to_pubkeyhash(), (keys[i], bitcoinsig));
13891389
}
13901390

1391-
assert!(ms.satisfy(no_sat, NullCtx).is_none());
1392-
assert!(ms.satisfy(&left_sat, NullCtx).is_some());
1391+
assert!(ms.satisfy(no_sat, NullCtx).is_err());
1392+
assert!(ms.satisfy(&left_sat, NullCtx).is_ok());
13931393
assert!(ms
13941394
.satisfy((&right_sat, satisfy::Older(10001)), NullCtx)
1395-
.is_some());
1395+
.is_ok());
13961396
//timelock not met
13971397
assert!(ms
13981398
.satisfy((&right_sat, satisfy::Older(9999)), NullCtx)
1399-
.is_none());
1399+
.is_err());
14001400

14011401
assert_eq!(
14021402
ms.satisfy((left_sat, satisfy::Older(9999)), NullCtx)

0 commit comments

Comments
 (0)