Skip to content

Commit d9fa2cb

Browse files
committed
Remove inocrrect panic from satisfy
It is true that all thresh fragments must be `d`, but in certain edge cases it is possible that we don't have the information to dissatify it. For example, the preimage to the PkH fragment. Although users should always use descriptors and DescriptorPublicKey type when dealing with this and this would not be an issue in practise we should still remove the panic
1 parent 116b912 commit d9fa2cb

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/miniscript/satisfy.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ impl Satisfaction {
627627
sat_indices.sort_by_key(|&i| {
628628
let stack_weight = match (&sats[i].stack, &ret_stack[i].stack) {
629629
(&Witness::Unavailable, _) | (&Witness::Impossible, _) => i64::MAX,
630-
(_, &Witness::Unavailable) | (_, &Witness::Impossible) => {
631-
unreachable!("Threshold fragments must be 'd'")
632-
}
630+
// This can only be the case when we have PkH without the corresponding
631+
// Pubkey.
632+
(_, &Witness::Unavailable) | (_, &Witness::Impossible) => i64::MIN,
633633
(&Witness::Stack(ref s), &Witness::Stack(ref d)) => {
634634
witness_size(s) as i64 - witness_size(d) as i64
635635
}
@@ -669,7 +669,9 @@ impl Satisfaction {
669669
// For example, the fragment thresh(2, hash, hash, 0, 0)
670670
// is uniquely satisfyiable because there is no satisfaction
671671
// for the 0 fragment
672-
else if !sats[sat_indices[k]].has_sig && sats[sat_indices[k]].stack != Witness::Impossible
672+
else if k < sat_indices.len()
673+
&& !sats[sat_indices[k]].has_sig
674+
&& sats[sat_indices[k]].stack != Witness::Impossible
673675
{
674676
// All arguments should be `d`, so dissatisfactions have no
675677
// signatures; and in this branch we assume too many weak
@@ -744,9 +746,8 @@ impl Satisfaction {
744746
sat_indices.sort_by_key(|&i| {
745747
let stack_weight = match (&sats[i].stack, &ret_stack[i].stack) {
746748
(&Witness::Unavailable, _) | (&Witness::Impossible, _) => i64::MAX,
747-
(_, &Witness::Unavailable) | (_, &Witness::Impossible) => {
748-
unreachable!("Threshold fragments must be 'd'")
749-
}
749+
// This is only possible when one of the branches has PkH
750+
(_, &Witness::Unavailable) | (_, &Witness::Impossible) => i64::MIN,
750751
(&Witness::Stack(ref s), &Witness::Stack(ref d)) => {
751752
witness_size(s) as i64 - witness_size(d) as i64
752753
}

0 commit comments

Comments
 (0)