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

Open
fametrano wants to merge 2 commits from psbt-taproot-sighash-absent into master AGit
fametrano commented 2026-10-19 20:49:04 +00:00 (Migrated from github.com)

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.

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 commented 2026-10-19 20:49:13 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36577 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36577.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36577#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [Yudis-bit](https://github.com/bitcoin/bitcoin/pull/36577#issuecomment-6708105111) | | Approach ACK | [Husteem](https://github.com/bitcoin/bitcoin/pull/36577#issuecomment-6346377698) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36710](https://github.com/bitcoin/bitcoin/pull/36710) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36710.svg"></sub> (test: Replace hardcoded extended keys with ExtendedKey helpers by nebula-21) * [#36387](https://github.com/bitcoin/bitcoin/pull/36387) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36387.svg"></sub> (wallet: rpc: Deprecate `encryptwallet` RPC for encrypting existing wallets. by davidgumberg) * [#36122](https://github.com/bitcoin/bitcoin/pull/36122) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36122.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Missing signature masks DEFAULT-field finalizer rejection

The finalizer assertion uses a one-signature 2-of-2 PSBT, so it stays incomplete even if the DEFAULT-field mismatch check is bypassed.

Location: test/functional/rpc_psbt.py:789 (head).

🔵 Suggestion · Fieldless non-ALL ECDSA signatures lack regression coverage

The new tests do not exercise acceptance of non-ALL ECDSA partial signatures when both the PSBT sighash field and RPC argument are absent.

Location: test/functional/rpc_psbt.py:758 (head).

🔵 Suggestion · RPC help omits the explicit-default sighash distinction

For a fieldless, unfinalized PSBT, explicitly passing the usual signing default can reject a cosigner's signature that an omitted argument accepts, but neither processing RPC's help explains the distinction.

Location: src/psbt.cpp:709 (head).

🔵 Suggestion · Fieldless unfinalized Taproot key signatures lack coverage

The key-path fixture finalizes its first signature, so reprocessing tests final-witness retention rather than the relaxed check or retention rule for an existing m_tap_key_sig.

Location: test/functional/rpc_psbt.py:805 (head).

🔵 Suggestion · Final-witness tests never exercise annex removal

Core's signer produces annex-free witnesses, so deleting the new annex-stripping branch would not affect the added retention tests.

Location: src/psbt.cpp:791 (head).

🔵 Suggestion · Second signer's DEFAULT sighash is not asserted

The mixed-sighash test would also accept signer2 copying signer1's type, including NONE, because it checks completion and mempool acceptance but not signer2's signature encoding.

Location: test/functional/rpc_psbt.py:778 (head).

Review coverage was incomplete.


Reviewed: f169a27f3adf · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:6996cf7abadd491b8636608787f8b5212cd14623 --> <!-- ralph-head:f169a27f3adfee011bc74446b9d3468a6c18c3ca --> <!-- ralph-case:dcb502c55bcde7584c8239f7cd095f2bffa707c93c59111714da33f3b7489591 --> **🔵 Suggestion · Missing signature masks DEFAULT-field finalizer rejection** The finalizer assertion uses a one-signature 2-of-2 PSBT, so it stays incomplete even if the DEFAULT-field mismatch check is bypassed. Location: [`test/functional/rpc_psbt.py:789`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/test/functional/rpc_psbt.py#L789>) (head). **🔵 Suggestion · Fieldless non-ALL ECDSA signatures lack regression coverage** The new tests do not exercise acceptance of non-ALL ECDSA partial signatures when both the PSBT sighash field and RPC argument are absent. Location: [`test/functional/rpc_psbt.py:758`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/test/functional/rpc_psbt.py#L758>) (head). **🔵 Suggestion · RPC help omits the explicit-default sighash distinction** For a fieldless, unfinalized PSBT, explicitly passing the usual signing default can reject a cosigner's signature that an omitted argument accepts, but neither processing RPC's help explains the distinction. Location: [`src/psbt.cpp:709`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/src/psbt.cpp#L709>) (head). **🔵 Suggestion · Fieldless unfinalized Taproot key signatures lack coverage** The key-path fixture finalizes its first signature, so reprocessing tests final-witness retention rather than the relaxed check or retention rule for an existing `m_tap_key_sig`. Location: [`test/functional/rpc_psbt.py:805`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/test/functional/rpc_psbt.py#L805>) (head). **🔵 Suggestion · Final-witness tests never exercise annex removal** Core's signer produces annex-free witnesses, so deleting the new annex-stripping branch would not affect the added retention tests. Location: [`src/psbt.cpp:791`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/src/psbt.cpp#L791>) (head). **🔵 Suggestion · Second signer's DEFAULT sighash is not asserted** The mixed-sighash test would also accept signer2 copying signer1's type, including NONE, because it checks completion and mempool acceptance but not signer2's signature encoding. Location: [`test/functional/rpc_psbt.py:778`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca/test/functional/rpc_psbt.py#L778>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`f169a27f3adf`](<https://git.fish.foo/bitcoin/bitcoin/commit/f169a27f3adfee011bc74446b9d3468a6c18c3ca>) · [Full review report](<https://review.fish.foo/traces/36577-f169a27f3adfee011bc74446b9d3468a6c18c3ca-2276-5.html>) · [Previous reports](<https://review.fish.foo/traces/36577/>) [permalink](<https://review.fish.foo/traces/36577-f169a27f3adfee011bc74446b9d3468a6c18c3ca-2276-5.html>)
Yudis-bit commented 2026-10-20 08:38:53 +00:00 (Migrated from github.com)

Tested ACK 1291d0e389 on Windows.

Tested ACK 1291d0e389 on Windows.
Husteem commented 2026-10-22 16:57:12 +00:00 (Migrated from github.com)

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.
Approach ACK / tACK [1291d0e](https://github.com/bitcoin/bitcoin/pull/36577/commits/1291d0e3895791bc700d4901c66d77dcbb42ed69) 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 (Migrated from github.com) reviewed 2026-11-04 22:26:20 +00:00
achow101 (Migrated from github.com) left a comment

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

Please replace your unit tests with functional tests. We prefer functional tests for PSBTs.
achow101 (Migrated from github.com) commented 2026-11-04 22:26:17 +00:00

In 1291d0e389 "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.

In 1291d0e3895791bc700d4901c66d77dcbb42ed69 "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.
fametrano (Migrated from github.com) reviewed 2026-11-05 23:20:09 +00:00
fametrano (Migrated from github.com) commented 2026-11-05 23:20:09 +00:00

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.

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.
fametrano commented 2026-11-05 23:57:09 +00:00 (Migrated from github.com)

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.

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 commented 2026-11-07 14:20:59 +00:00 (Migrated from github.com)

re-ACK f169a27f3

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

re-ACK f169a27f3 Verified range-diff against 1291d0e. The extra commit for finalized ANYONECANPAY witness signatures looks good, tests pass.
DrahtBot commented 2026-11-20 22:40:59 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36577
No description provided.