Skip to content

psbt: check sig sighash only if a type is given - #36577

Open
fametrano wants to merge 2 commits into
bitcoin:masterfrom
fametrano:psbt-taproot-sighash-absent
Open

fametrano wants to merge 2 commits into
bitcoin:masterfrom
fametrano:psbt-taproot-sighash-absent

Conversation

@fametrano

@fametrano fametrano commented Oct 19, 2026 •

Copy link
Copy Markdown

SignPSBTInput() checked existing signatures against an assumed sighash type when neither PSBT_IN_SIGHASH_TYPE nor the sighashtype parameter was given: SIGHASH_DEFAULT for taproot, SIGHASH_ALL otherwise. A PSBT holding a valid 65-byte SIGHASH_ALL taproot signature could then be neither finalized nor signed further.

BIP 174 lets a signer use any sighash type when the field is absent, so the check now runs only when the field or the parameter is given, for every input type. Without the field, RemoveUnnecessaryTransactions() now also looks at the taproot signatures for SIGHASH_ANYONECANPAY, so that the non_witness_utxo is kept. A functional test in rpc_psbt.py covers a taproot input signed with ALL, NONE and ALL|ANYONECANPAY without the field. A second commit also keeps the non_witness_utxo of a finalized taproot input, whose signatures are left only in the final witness, and tests it on a key path and a script path.

Fixes #36507.

@DrahtBot DrahtBot added the PSBT label Oct 19, 2026
@DrahtBot

DrahtBot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK Yudis-bit
Approach ACK Husteem

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36710 (test: Replace hardcoded extended keys with ExtendedKey helpers by nebula-21)
  • #36387 (wallet: rpc: Deprecate encryptwallet RPC for encrypting existing wallets. by davidgumberg)
  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Yudis-bit

Copy link
Copy Markdown

Tested ACK 1291d0e on Windows.

@Husteem

Husteem commented Oct 22, 2026 •

Copy link
Copy Markdown

Approach ACK / tACK 1291d0e

Good fix for the referenced issue.
When sighash_type is unset, accepting 65-byte Taproot signatures ending in SIGHASH_ALL is safe because it commits to the identical BIP-341 digest as SIGHASH_DEFAULT.
Preserving rejection for other variants keeps Core's safety checks intact.

Tested on Ubuntu 24.04:

  • psbt_tests passed (including both new absent-field tests).
  • rpc_psbt.py passed cleanly too.

@achow101 achow101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace your unit tests with functional tests. We prefer functional tests for PSBTs.

Comment thread src/psbt.cpp Outdated
// so a taproot signature with an explicit SIGHASH_ALL is also accepted.
const auto sig_matches = [&](const std::vector<unsigned char>& sig) {
return sig.size() == 64 || (!sighash_specified && sig.size() == 65 && sig.back() == SIGHASH_ALL);
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 1291d0e "psbt: accept taproot ALL sigs with sighash unset"

I think it would be better to rewrite these checks to be dependent on the presence of the field and/or parameter, rather than assuming something about the sighash value. Allowing SIGHASH_ALL for a SIGHASH_DEFAULT seems like there is a possibility for another bug given that the sighash type is encoded into the signature message itself.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: existing signatures' sighash types are now checked only when PSBT_IN_SIGHASH_TYPE or the sighashtype parameter is given. Without either, any type is accepted, on non-taproot inputs too, as BIP 174 allows. As you suggest, this replaces the rule I argued for on #36558, which accepted only ALL besides DEFAULT. RemoveUnnecessaryTransactions also finds ANYONECANPAY in taproot signatures. The unit tests became a functional test.

Ferdinando Ametrano added 2 commits November 6, 2026 00:05
SignPSBTInput() checks that existing signatures use the input's sighash
type. When neither PSBT_IN_SIGHASH_TYPE nor the sighash_type parameter
was given, it checked them against an assumed default: SIGHASH_DEFAULT
for taproot, SIGHASH_ALL otherwise. A PSBT holding a valid 65-byte
SIGHASH_ALL taproot signature could then be neither finalized nor
signed further.

BIP 174 lets a signer use any sighash type when the field is absent,
and asks a finalizer to fail on a mismatch only when it is present. So
run the check only when the field or the parameter is given. This also
lets non-taproot partial signatures use any sighash type in that case.

RemoveUnnecessaryTransactions() looked only at the field to find
SIGHASH_ANYONECANPAY. Without the field, also look at the taproot
signatures, so that the non_witness_utxo is kept when one of them uses
it.

Fixes bitcoin#36507.
A finalized input is serialized without its sighash type field and its
taproot signatures, so RemoveUnnecessaryTransactions dropped the
non_witness_utxos once a PSBT with a finalized ANYONECANPAY taproot
input was processed again, for example by utxoupdatepsbt or
walletprocesspsbt.

Also check the signatures in the final witness, which are among the
items left once the annex and, for a script path, the script and the
control block are removed. A control block can be 65 bytes long and end
with a byte that has 0x80 set, so it is not read as a signature.
@fametrano

Copy link
Copy Markdown
Author

Once a taproot input is finalized, its sighash type field and its taproot signatures are no longer serialized, so a later utxoupdatepsbt or walletprocesspsbt drops its non_witness_utxo even when it was signed with ANYONECANPAY. The second commit also reads the signatures in the final witness, through the same check as the first commit.

@Yudis-bit

Copy link
Copy Markdown

re-ACK f169a27

Verified range-diff against 1291d0e. The extra commit for finalized ANYONECANPAY witness signatures looks good, tests pass.

@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PSBT: SignPSBTInput rejects valid 65-byte taproot script signatures when PSBT_IN_SIGHASH_TYPE is absent

5 participants