From 5a07879ff382b6738dcf30734086083bc1754728 Mon Sep 17 00:00:00 2001 From: Greg Sanders Date: Mon, 19 Jan 2026 16:06:28 -0500 Subject: [PATCH] Fix BIP118 ANYPREVOUTANYSCRIPT sighash In the current code anytime sh_anyprevout_anyscript is true, so will sh_anyonecanpay will be true. Therefore it will fall through and hash the prevout erroneously. Fix this by checking for APOAS first. --- src/tx_io.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tx_io.c b/src/tx_io.c index 77b49594c..09a21bc1d 100644 --- a/src/tx_io.c +++ b/src/tx_io.c @@ -910,7 +910,10 @@ static int bip341_signature_hash( } /* Input data */ hash_u8(&io.ctx, (tapleaf_script ? 1 : 0) * 2 + (annex ? 1 : 0)); /* spend_type */ - if (sh_anyonecanpay || sh_anyprevout) { + if (sh_anyprevout_anyscript) { + // Note that this means sh_anyonecanpay is set so we check this first + hash_le32(&io.ctx, tx->inputs[index].sequence); /* nSequence */ + } else if (sh_anyonecanpay || sh_anyprevout) { if (sh_anyonecanpay) { #ifdef BUILD_ELEMENTS if (is_elements) @@ -925,8 +928,6 @@ static int bip341_signature_hash( else #endif txio_hash_input(&io, tx, index, scripts, values, NULL, 0, WALLY_SIGTYPE_SW_V1); - } else if (sh_anyprevout_anyscript) { - hash_le32(&io.ctx, tx->inputs[index].sequence); /* nSequence */ } else { hash_le32(&io.ctx, index); /* input_index */ }