Skip to content

Commit fbb351d

Browse files
committed
Change satisfy API to return detailed error
1 parent 7c08aae commit fbb351d

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
@@ -986,10 +986,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
986986
{
987987
match *self {
988988
Descriptor::Bare(ref d) => {
989-
let wit = match d.satisfy(satisfier, to_pk_ctx) {
990-
Some(wit) => wit,
991-
None => return Err(Error::CouldNotSatisfy),
992-
};
989+
let wit = d.satisfy(satisfier, to_pk_ctx)?;
993990
let script_sig = witness_to_scriptsig(&wit);
994991
let witness = vec![];
995992
Ok((witness, script_sig))
@@ -1053,20 +1050,14 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
10531050
}
10541051
}
10551052
Descriptor::Sh(ref d) => {
1056-
let mut script_witness = match d.satisfy(satisfier, to_pk_ctx) {
1057-
Some(wit) => wit,
1058-
None => return Err(Error::CouldNotSatisfy),
1059-
};
1053+
let mut script_witness = d.satisfy(satisfier, to_pk_ctx)?;
10601054
script_witness.push(d.encode(to_pk_ctx).into_bytes());
10611055
let script_sig = witness_to_scriptsig(&script_witness);
10621056
let witness = vec![];
10631057
Ok((witness, script_sig))
10641058
}
10651059
Descriptor::Wsh(ref d) => {
1066-
let mut witness = match d.satisfy(satisfier, to_pk_ctx) {
1067-
Some(wit) => wit,
1068-
None => return Err(Error::CouldNotSatisfy),
1069-
};
1060+
let mut witness = d.satisfy(satisfier, to_pk_ctx)?;
10701061
witness.push(d.encode(to_pk_ctx).into_bytes());
10711062
let script_sig = Script::new();
10721063
Ok((witness, script_sig))
@@ -1077,28 +1068,19 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
10771068
.push_slice(&witness_script.to_v0_p2wsh()[..])
10781069
.into_script();
10791070

1080-
let mut witness = match d.satisfy(satisfier, to_pk_ctx) {
1081-
Some(wit) => wit,
1082-
None => return Err(Error::CouldNotSatisfy),
1083-
};
1071+
let mut witness = d.satisfy(satisfier, to_pk_ctx)?;
10841072
witness.push(witness_script.into_bytes());
10851073
Ok((witness, script_sig))
10861074
}
10871075
Descriptor::ShSortedMulti(ref smv) => {
1088-
let mut script_witness = match smv.satisfy(satisfier, to_pk_ctx) {
1089-
Some(wit) => wit,
1090-
None => return Err(Error::CouldNotSatisfy),
1091-
};
1076+
let mut script_witness = smv.satisfy(satisfier, to_pk_ctx)?;
10921077
script_witness.push(smv.encode(to_pk_ctx).into_bytes());
10931078
let script_sig = witness_to_scriptsig(&script_witness);
10941079
let witness = vec![];
10951080
Ok((witness, script_sig))
10961081
}
10971082
Descriptor::WshSortedMulti(ref smv) => {
1098-
let mut witness = match smv.satisfy(satisfier, to_pk_ctx) {
1099-
Some(wit) => wit,
1100-
None => return Err(Error::CouldNotSatisfy),
1101-
};
1083+
let mut witness = smv.satisfy(satisfier, to_pk_ctx)?;
11021084
witness.push(smv.encode(to_pk_ctx).into_bytes());
11031085
let script_sig = Script::new();
11041086
Ok((witness, script_sig))
@@ -1109,10 +1091,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
11091091
.push_slice(&witness_script.to_v0_p2wsh()[..])
11101092
.into_script();
11111093

1112-
let mut witness = match smv.satisfy(satisfier, to_pk_ctx) {
1113-
Some(wit) => wit,
1114-
None => return Err(Error::CouldNotSatisfy),
1115-
};
1094+
let mut witness = smv.satisfy(satisfier, to_pk_ctx)?;
11161095
witness.push(witness_script.into_bytes());
11171096
Ok((witness, script_sig))
11181097
}
@@ -1554,7 +1533,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
15541533

15551534
/// Attempt to produce a satisfying witness for the
15561535
/// witness script represented by the parse tree
1557-
pub fn satisfy<ToPkCtx, S>(&self, satisfier: S, to_pk_ctx: ToPkCtx) -> Option<Vec<Vec<u8>>>
1536+
pub fn satisfy<ToPkCtx, S>(
1537+
&self,
1538+
satisfier: S,
1539+
to_pk_ctx: ToPkCtx,
1540+
) -> Result<Vec<Vec<u8>>, Error>
15581541
where
15591542
ToPkCtx: Copy,
15601543
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)