wallet: harden external signer psbt processing, revamp mock #36114

Open
Sjors wants to merge 9 commits from 2026/08/external-signer-mock into master AGit
Sjors commented 2026-08-28 11:27:33 +00:00 (Migrated from github.com)

The current external signer functional test mock can only echo a prepared PSBT. This gets in the way of testing more complicated scenarios, i.e. misbehaving signers and (MuSig2) multisig.

  • test: have external signer mock use a wallet: gives the mock its own offline node and wallet with private keys, while the test uses the watch-only version.
  • external_signer: require matching PSBT versions
  • psbt: add GetUnsignedTxid(): unlike GetUniqueID(), this commits to PSBTv2 input sequences, which BIP 370 lets Updaters and Combiners change.
  • external_signer: merge PSBT response instead of replacing: tests signers that drop outputs, reduce their value or change the script. Merging rejects these changes while allowing signers to omit unnecessary PSBT fields. PSBTv2 merging also fills in fields missing from the original, such as an input sequence, so the response is rejected if merging changes the unsigned transaction.
  • external_signer: reject unsafe sighash types
  • external_signer: reject finalized PSBT inputs: keeps the above check simple. Documents this new requirement. HWI does not finalize.

The remaining commits prepare the test helpers and enforce IWYU for src/external_signer.cpp.

This PR absorbs the scenarios from bitcoin/bitcoin#35358, but is better able to test them thanks to the new mock signer implementation.

This PR does not intend to fully harden against a malicious external signer, and can't protect against a malicious HWI (equivalent) process running as the same user.

The current external signer functional test mock can only echo a prepared PSBT. This gets in the way of testing more complicated scenarios, i.e. misbehaving signers and (MuSig2) multisig. - *test: have external signer mock use a wallet*: gives the mock its own offline node and wallet with private keys, while the test uses the watch-only version. - *external_signer: require matching PSBT versions* - *psbt: add `GetUnsignedTxid()`*: unlike `GetUniqueID()`, this commits to PSBTv2 input sequences, which BIP 370 lets Updaters and Combiners change. - *external_signer: merge PSBT response instead of replacing*: tests signers that drop outputs, reduce their value or change the script. Merging rejects these changes while allowing signers to omit unnecessary PSBT fields. PSBTv2 merging also fills in fields missing from the original, such as an input sequence, so the response is rejected if merging changes the unsigned transaction. - *external_signer: reject unsafe sighash types* - *external_signer: **reject finalized PSBT inputs***: keeps the above check simple. Documents this new requirement. HWI does not finalize. The remaining commits prepare the test helpers and enforce IWYU for `src/external_signer.cpp`. This PR absorbs the scenarios from bitcoin/bitcoin#35358, but is better able to test them thanks to the new mock signer implementation. This PR does not intend to fully harden against a malicious external signer, and can't protect against a malicious HWI (equivalent) process running as the same user.
DrahtBot commented 2026-08-28 11:27:42 +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 enessubass
Concept ACK jeanpablojp, vicjuma
Stale ACK fametrano

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:

  • #37046 (wallet: Support quoted external signer arguments by hamdyaea)
  • #36990 (test: replace hard-coded RPC error codes by l0rinc)
  • #36844 (iwyu: Fix warnings in more modules in src/ and treat them as errors by hebasto)
  • #35433 (wallet: deprecate replaceable argument from transaction (and psbt) creation (and modification) RPCs by rkrux)
  • #33112 (wallet: relax external_signer flag constraints by Sjors)

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.

LLM Linter (✨ experimental)

Possible places where comparison-specific test macros should replace generic comparisons:

  • test/functional/mocks/signer.py: assert len(signed.i) > 1 → Prefer assert_greater_than(len(signed.i), 1).

2026-12-01 18:09:13

<!--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/36114 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36114.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36114#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 | [enessubass](https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-7325393776) | | Concept ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/36114#pullrequestreview-5141340050), [vicjuma](https://github.com/bitcoin/bitcoin/pull/36114#pullrequestreview-5801213649) | | Stale ACK | [fametrano](https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-6397202873) | 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: * [#37046](https://github.com/bitcoin/bitcoin/pull/37046) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37046.svg"></sub> (wallet: Support quoted external signer arguments by hamdyaea) * [#36990](https://github.com/bitcoin/bitcoin/pull/36990) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36990.svg"></sub> (test: replace hard-coded RPC error codes by l0rinc) * [#36844](https://github.com/bitcoin/bitcoin/pull/36844) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36844.svg"></sub> (iwyu: Fix warnings in more modules in `src/` and treat them as errors by hebasto) * [#35433](https://github.com/bitcoin/bitcoin/pull/35433) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35433.svg"></sub> (wallet: deprecate replaceable argument from transaction (and psbt) creation (and modification) RPCs by rkrux) * [#33112](https://github.com/bitcoin/bitcoin/pull/33112) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33112.svg"></sub> (wallet: relax external_signer flag constraints by Sjors) 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--> ### LLM Linter (✨ experimental) Possible places where comparison-specific test macros should replace generic comparisons: - `test/functional/mocks/signer.py`: `assert len(signed.i) > 1` → Prefer `assert_greater_than(len(signed.i), 1)`. <sup>2026-12-01 18:09:13</sup>
Sjors commented 2026-08-28 11:30:00 +00:00 (Migrated from github.com)

cc @bigspider and other hardware wallet folks, any other hardware wallet shenanigans we should check against (perhaps in a followup)?

cc @bigspider and other hardware wallet folks, any other hardware wallet shenanigans we should check against (perhaps in a followup)?
dangervslash (Migrated from github.com) reviewed 2026-08-28 22:00:16 +00:00
dangervslash (Migrated from github.com) left a comment

Approved flame

Approved flame
dangervslash commented 2026-08-28 22:00:35 +00:00 (Migrated from github.com)

MarkUp

MarkUp
DrahtBot commented 2026-09-07 22:31:20 +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).
Sjors commented 2026-09-08 10:57:13 +00:00 (Migrated from github.com)

Rebased after #36113, still based on #36076.

Rebased after #36113, still based on #36076.
jeanpablojp (Migrated from github.com) reviewed 2026-09-08 11:54:39 +00:00
jeanpablojp (Migrated from github.com) left a comment

Concept ACK

I made the new mock return a finalized response and FindUnsafeSighashType doesn't catch it. The fields it reads are the ones PSBTInput::Serialize writes only when the input isn't finalized, and BIP 174 has a finalizer clear them, so the signatures arrive in final_script_sig and final_script_witness alone.

With SIGHASH_NONE the send comes back complete, both signatures in the transaction end in 0x02, and testmempoolaccept allows it.

The commit message says the change rejects a response containing signatures made with an unsafe sighash type, and a finalized one does. The different-transaction check still rejects a finalized response that changes an output.

Is skipping the finalized case deliberate, or worth covering?

Concept ACK I made the new mock return a finalized response and `FindUnsafeSighashType` doesn't catch it. The fields it reads are the ones `PSBTInput::Serialize` writes only when the input isn't finalized, and BIP 174 has a finalizer clear them, so the signatures arrive in `final_script_sig` and `final_script_witness` alone. With `SIGHASH_NONE` the `send` comes back complete, both signatures in the transaction end in `0x02`, and `testmempoolaccept` allows it. The commit message says the change rejects a response containing signatures made with an unsafe sighash type, and a finalized one does. The different-transaction check still rejects a finalized response that changes an output. Is skipping the finalized case deliberate, or worth covering?
jeanpablojp (Migrated from github.com) commented 2026-09-08 11:54:39 +00:00

nit: Merge only returns false for two reasons, GetUniqueID and the version comparison, since neither PSBTInput::Merge nor PSBTOutput::Merge has a failure path.

I returned the same transaction as a v0 PSBT and send failed with this message. GetUniqueID is what stops it, since it zeroes nSequence only from version 2 on, and before this commit that response was accepted.

Worth a separate message, or does the signer have to return the version it was given?

nit: `Merge` only returns false for two reasons, `GetUniqueID` and the version comparison, since neither `PSBTInput::Merge` nor `PSBTOutput::Merge` has a failure path. I returned the same transaction as a v0 PSBT and `send` failed with this message. `GetUniqueID` is what stops it, since it zeroes `nSequence` only from version 2 on, and before this commit that response was accepted. Worth a separate message, or does the signer have to return the version it was given?
jeanpablojp (Migrated from github.com) commented 2026-09-08 11:54:39 +00:00

nit: this transaction has an ECDSA and a taproot input, and FindUnsafeSighashType collects from both before it scans, so either signature alone triggers the rejection. Disabling the m_tap_key_sig collection leaves wallet_signer.py green, and disabling the partial_sigs one does too. Only removing both makes sighash_none_hidden fail.

A single-input transaction of each type would make each branch carry its own case. Worth adding?

nit: this transaction has an ECDSA and a taproot input, and `FindUnsafeSighashType` collects from both before it scans, so either signature alone triggers the rejection. Disabling the `m_tap_key_sig` collection leaves `wallet_signer.py` green, and disabling the `partial_sigs` one does too. Only removing both makes `sighash_none_hidden` fail. A single-input transaction of each type would make each branch carry its own case. Worth adding?
Sjors commented 2026-09-28 08:45:59 +00:00 (Migrated from github.com)

Rebased after https://github.com/bitcoin/bitcoin/pull/36076 and addressed @jeanpablojp's feedback.

Is skipping the finalized case deliberate, or worth covering?

It now rejects finalized PSBTs, which avoids complicating FindUnsafeSighashType. HWI doesn't finalize.

I simplified test setup by using create_outpoints.

Also enforced IWYU.

Rebased after https://github.com/bitcoin/bitcoin/pull/36076 and addressed @jeanpablojp's feedback. > Is skipping the finalized case deliberate, or worth covering? It now rejects finalized PSBTs, which avoids complicating `FindUnsafeSighashType`. HWI doesn't finalize. I simplified test setup by using `create_outpoints`. Also enforced IWYU.
Sjors (Migrated from github.com) reviewed 2026-09-28 08:46:01 +00:00
Sjors (Migrated from github.com) commented 2026-09-28 08:46:01 +00:00

Added a test case.

Added a test case.
Sjors (Migrated from github.com) reviewed 2026-09-28 08:46:03 +00:00
Sjors (Migrated from github.com) commented 2026-09-28 08:46:03 +00:00

I added a constraint that the version must match.

I added a constraint that the version must match.
First-time contributor

Earlier concerns neither republished nor resolved: 1 major finding · 1 minor finding · 1 suggestion. See the Run archive linked from the Review report for their assessments.

🟠 Minor · Finalized-input check rejects inputs already finalized in the request

When a signer echoes a pre-finalized foreign input, mixed-wallet PSBT signing now fails with EXTERNAL_SIGNER_FAILED; the in-tree mock preserves these fields, but HWI behavior is unconfirmed.

Location: src/external_signer.cpp:178 (head).

🔵 Suggestion · Unsafe-sighash rejection attributes request data to the signer

A signer that honors an existing NONE or SINGLE declaration can prompt for approval before Core rejects the echoed request data as signer misbehavior.

Location: src/external_signer.cpp:185 (head).

🔵 Suggestion · Signer tests miss the Taproot script-path sighash check

Removing the m_tap_script_sigs scan would leave the new external-signer tests passing because their fixtures produce only ECDSA and key-path Schnorr signatures.

Location: src/external_signer.cpp:111 (head).

🔵 Suggestion · The signtx contract omits the accepted sighash types

A same-version, unfinalized signer response can satisfy the documented requirements yet be rejected for using SIGHASH_NONE or SIGHASH_SINGLE.

Location: src/external_signer.cpp:183 (head).

🔵 Suggestion · External-signer compatibility changes lack a user-facing release note

The new response checks can reject previously accepted signer tools with only a generic RPC error; the number of affected deployed signers is unknown.

Location: doc/external-signer.md:153 (head).

Review coverage was incomplete.


Reviewed: 70f13577ba61 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:5c4e8374074f1c247e88c4fc12fc5b8858e6ff6f --> <!-- ralph-head:70f13577ba61c1bd62d73329c2eb386788767887 --> <!-- ralph-case:3dfcbcd65a1bef1dde0ef005e87649d6f82f803096c260b5754c8c03c7701881 --> Earlier concerns neither republished nor resolved: 1 major finding · 1 minor finding · 1 suggestion. See the Run archive linked from the Review report for their assessments. **🟠 Minor · Finalized-input check rejects inputs already finalized in the request** When a signer echoes a pre-finalized foreign input, mixed-wallet PSBT signing now fails with `EXTERNAL_SIGNER_FAILED`; the in-tree mock preserves these fields, but HWI behavior is unconfirmed. Location: [`src/external_signer.cpp:178`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/70f13577ba61c1bd62d73329c2eb386788767887/src/external_signer.cpp#L178>) (head). **🔵 Suggestion · Unsafe-sighash rejection attributes request data to the signer** A signer that honors an existing `NONE` or `SINGLE` declaration can prompt for approval before Core rejects the echoed request data as signer misbehavior. Location: [`src/external_signer.cpp:185`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/70f13577ba61c1bd62d73329c2eb386788767887/src/external_signer.cpp#L185>) (head). **🔵 Suggestion · Signer tests miss the Taproot script-path sighash check** Removing the `m_tap_script_sigs` scan would leave the new external-signer tests passing because their fixtures produce only ECDSA and key-path Schnorr signatures. Location: [`src/external_signer.cpp:111`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/70f13577ba61c1bd62d73329c2eb386788767887/src/external_signer.cpp#L111>) (head). **🔵 Suggestion · The `signtx` contract omits the accepted sighash types** A same-version, unfinalized signer response can satisfy the documented requirements yet be rejected for using `SIGHASH_NONE` or `SIGHASH_SINGLE`. Location: [`src/external_signer.cpp:183`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/70f13577ba61c1bd62d73329c2eb386788767887/src/external_signer.cpp#L183>) (head). **🔵 Suggestion · External-signer compatibility changes lack a user-facing release note** The new response checks can reject previously accepted signer tools with only a generic RPC error; the number of affected deployed signers is unknown. Location: [`doc/external-signer.md:153`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/70f13577ba61c1bd62d73329c2eb386788767887/doc/external-signer.md#L153>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`70f13577ba61`](<https://git.fish.foo/bitcoin/bitcoin/commit/70f13577ba61c1bd62d73329c2eb386788767887>) · [Review report](<https://review.fish.foo/report/36114-70f13577ba61c1bd62d73329c2eb386788767887-3405-10.html>) · [Previous reports](<https://review.fish.foo/traces/36114/>) [permalink](<https://review.fish.foo/report/36114-70f13577ba61c1bd62d73329c2eb386788767887-3405-10.html>)
DrahtBot commented 2026-10-13 02:18:41 +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).
Sjors commented 2026-10-20 04:18:24 +00:00 (Migrated from github.com)

Trivial rebase after #36440.

Trivial rebase after #36440.
fametrano commented 2026-10-21 20:58:59 +00:00 (Migrated from github.com)

tACK 26f84e2fdb

I built the head and compared its signer path with two Combiners: BIP174's, as combinepsbt runs it, and btclib-wallet's combine. The new checks stop what a Combiner lets through.

A replaying mock returned responses built from the wallet's real request, and wallet_signer.py passes. The signer path and both Combiners reject a removed output, a reduced value and a changed script. Only the signer path rejects NONE and SINGLE signatures, a PSBT_IN_SIGHASH_TYPE of NONE, and one or all inputs finalized. ALL|ANYONECANPAY passes and gives the honest txid. A changed v2 sequence or a corrupted signature leaves the wallet's transaction as it was and returns an incomplete PSBT. I can share the probe and its output.

tACK 26f84e2fdb62b474fb97ced6d81cff4f08d311d3 I built the head and compared its signer path with two Combiners: BIP174's, as `combinepsbt` runs it, and btclib-wallet's `combine`. The new checks stop what a Combiner lets through. A replaying mock returned responses built from the wallet's real request, and `wallet_signer.py` passes. The signer path and both Combiners reject a removed output, a reduced value and a changed script. Only the signer path rejects NONE and SINGLE signatures, a `PSBT_IN_SIGHASH_TYPE` of NONE, and one or all inputs finalized. ALL|ANYONECANPAY passes and gives the honest txid. A changed v2 sequence or a corrupted signature leaves the wallet's transaction as it was and returns an incomplete PSBT. I can share the probe and its output.
DrahtBot commented 2026-10-23 23:10:11 +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).
Sjors commented 2026-10-24 06:12:09 +00:00 (Migrated from github.com)

Rebased after #36624.

@fametrano I expanded the tests based on your comment. If anything is still missing, feel free to suggest additional test coverage (or publish your probe in a gist, linking to that).

Rebased after #36624. @fametrano I expanded the tests based on your comment. If anything is still missing, feel free to suggest additional test coverage (or publish your probe in a gist, linking to that).
fametrano commented 2026-10-24 22:00:00 +00:00 (Migrated from github.com)

@Sjors the new tests cover the cases in my tACK except two: a changed v2 input sequence and a corrupted signature. In both the wallet's transaction stays as it was and walletprocesspsbt returns an incomplete PSBT, which a test could pin.

The probe and its output: https://gist.github.com/fametrano/e808f936324f16573f74e5f0f3aadf02

I re-ran it on the new head and wallet_signer.py passes. tACK dc30c41f9b

@Sjors the new tests cover the cases in my tACK except two: a changed v2 input sequence and a corrupted signature. In both the wallet's transaction stays as it was and `walletprocesspsbt` returns an incomplete PSBT, which a test could pin. The probe and its output: https://gist.github.com/fametrano/e808f936324f16573f74e5f0f3aadf02 I re-ran it on the new head and `wallet_signer.py` passes. tACK dc30c41f9b29bbae1eed172155e23ade03717849
Sjors commented 2026-10-25 02:20:35 +00:00 (Migrated from github.com)

Thanks, added two more cases.

Thanks, added two more cases.
fametrano commented 2026-10-25 11:43:40 +00:00 (Migrated from github.com)

tACK 42298793aa

The two new cases are the ones I suggested, and each fails when its protection is removed. Copying the signer's sequence into the merged PSBT fails the sequence case. Skipping signature verification fails the corrupted-signature case, for ECDSA and Schnorr. The wallet verifies in two places, ProduceSignature and CWallet::FillPSBT, so both had to go. wallet_signer.py passes, and my probe gives the same outcomes as in the gist.

tACK 42298793aa1813d805ae49b963a91df7ba49bd26 The two new cases are the ones I suggested, and each fails when its protection is removed. Copying the signer's sequence into the merged PSBT fails the sequence case. Skipping signature verification fails the corrupted-signature case, for ECDSA and Schnorr. The wallet verifies in two places, `ProduceSignature` and `CWallet::FillPSBT`, so both had to go. `wallet_signer.py` passes, and my probe gives the same outcomes as in the gist.
vicjuma (Migrated from github.com) reviewed 2026-11-06 14:32:17 +00:00
vicjuma (Migrated from github.com) left a comment

Concept ACK, left an inline suggestion

Concept ACK, left an inline suggestion
vicjuma (Migrated from github.com) commented 2026-11-06 14:07:37 +00:00

Was following an issue and imo it feels a bit stringent to REJECT this. Is leaning towards a warning possible, and let the signer determine how to handle it? So that the function FindUnsafeSighashType is only used to detect the unsafe sighash for the sake of emitting a warning

if (const std::optional<int32_t> sighash{FindUnsafeSighashType(*signer_psbtx)}) {
         const std::string sighash_str{SighashToStr(*sighash)};
-        error = strprintf("Signer used an unsafe sighash type: %s", sighash_str.empty() ? "unknown" : sighash_str);
-        return false;
+        LogWarning("Warning: signer used an unsafe sighash type: %s\n", sighash_str.empty() ? "unknown" : sighash_str);
        // some other action to emit the warning maybe
     }
Was following an [issue](https://github.com/spesmilo/electrum/issues/8687) and imo it feels a bit stringent to REJECT this. Is leaning towards a warning possible, and let the signer determine how to handle it? So that the function `FindUnsafeSighashType` is only used to detect the unsafe sighash for the sake of emitting a warning ```diff if (const std::optional<int32_t> sighash{FindUnsafeSighashType(*signer_psbtx)}) { const std::string sighash_str{SighashToStr(*sighash)}; - error = strprintf("Signer used an unsafe sighash type: %s", sighash_str.empty() ? "unknown" : sighash_str); - return false; + LogWarning("Warning: signer used an unsafe sighash type: %s\n", sighash_str.empty() ? "unknown" : sighash_str); // some other action to emit the warning maybe } ```
vicjuma (Migrated from github.com) commented 2026-11-06 14:07:49 +00:00

Added tests work as expected

Image

Introduced a few changes to the test/functional/mocks/signer.py, and they were successfully caught

  1. Commenting out if mode == "psbt_v0"
  2. Skipping the psbt.o[0].map[PSBT_OUT_SCRIPT] = bytes([0x51]) branch
  3. Skipping the if mode in ("sighash_none", "sighash_none_hidden"): branch
  4. Removing the elif mode in ("finalized", "partially_finalized"): branch
  5. Removing the elif mode == "strip": branch
Added tests work as expected <img width="2042" height="954" alt="Image" src="https://git.fish.foo/attachments/3e0de14a-87cb-4720-9d90-d4bd9054717e" /> Introduced a few changes to the test/functional/mocks/signer.py, and they were successfully caught 1. Commenting out `if mode == "psbt_v0"` 2. Skipping the `psbt.o[0].map[PSBT_OUT_SCRIPT] = bytes([0x51])` branch 3. Skipping the `if mode in ("sighash_none", "sighash_none_hidden"):` branch 4. Removing the `elif mode in ("finalized", "partially_finalized"):` branch 5. Removing the `elif mode == "strip":` branch
Sjors (Migrated from github.com) reviewed 2026-11-10 14:28:54 +00:00
Sjors (Migrated from github.com) commented 2026-11-10 14:28:54 +00:00

Very few users are going to see that warning, especially in the GUI. We could add a warning mechanism to the signer flow (currently it either succeeds or errors), but can you explain what a use case would be for an unsafe sighash type?

Very few users are going to see that warning, especially in the GUI. We could add a warning mechanism to the signer flow (currently it either succeeds or errors), but can you explain what a use case would be for an unsafe sighash type?
vicjuma (Migrated from github.com) reviewed 2026-11-10 20:34:00 +00:00
vicjuma (Migrated from github.com) commented 2026-11-10 20:34:00 +00:00

Very few users are going to see that warning, especially in the GUI.

An enum class could probably work instead of a boolean. No strong opinion on this, looks great as is

but can you explain what a use case would be for an unsafe sighash type?

Collaboration between two parties, each with their inputs and outputs (saying this loosely though and only for SINGLE). If security outweighs flexibility, we can go with this. One that I haven't found any use case is NONE. Again looking great as is, no strong opinion

> Very few users are going to see that warning, especially in the GUI. An enum class could probably work instead of a boolean. No strong opinion on this, looks great as is > but can you explain what a use case would be for an unsafe sighash type? Collaboration between two parties, each with their inputs and outputs (saying this loosely though and only for SINGLE). If security outweighs flexibility, we can go with this. One that I haven't found any use case is NONE. Again looking great as is, no strong opinion
DrahtBot commented 2026-11-12 11:56:57 +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).
Sjors commented 2026-11-12 14:03:46 +00:00 (Migrated from github.com)

Trivial rebase after #36807.

Trivial rebase after #36807.
enessubass commented 2026-11-24 19:43:27 +00:00 (Migrated from github.com)

Concept ACK aee9e7a4da26cb47319095f4820b4849672fa3ae

Built on macOS arm64 and tested an edge case around PSBTv2 input sequences in psbtx.Merge(*signer_psbtx) (src/external_signer.cpp:181):

In test_invalid_signer_signatures (test/functional/wallet_signer.py:319), walletcreatefundedpsbt populates PSBT_IN_SEQUENCE, so PSBTInput::Merge preserves the original sequence. However, PSBT_IN_SEQUENCE is optional in BIP 370 — when omitted (sequence == std::nullopt), GetUnsignedTx() (src/psbt.cpp:134) defaults to CTxIn::SEQUENCE_FINAL (0xffffffff).

If a caller passes a v2 PSBT with an omitted PSBT_IN_SEQUENCE to walletprocesspsbt and the signer injects e.g. PSBT_IN_SEQUENCE = 0xfffffffd and signs that modified transaction:

  1. GetUniqueID() zeroes nSequence for v2, so psbtx.Merge(*signer_psbtx) returns true.
  2. PSBTInput::Merge (src/psbt.cpp:464) copies input.sequence into psbtx because sequence == std::nullopt.
  3. On regtest, walletprocesspsbt returns complete=True directly for a legacy input, and for bech32m res["psbt"] retains the injected 0xfffffffd + valid signature so finalizepsbt returns complete=True (testmempoolaccept allowed).

Would it make sense to snapshot psbtx.GetUnsignedTx() before Merge and verify the unsigned tx hash did not change afterwards? That still permits the strip mode (where signer_psbtx omits PSBT_IN_SEQUENCE) while catching sequence injection when PSBT_IN_SEQUENCE was omitted on the original input.

(Disclosure per AI policy: used an LLM while auditing PSBTInput::Merge callers, then built aee9e7a locally and verified the regtest case manually.)

Concept ACK `aee9e7a4da26cb47319095f4820b4849672fa3ae` Built on macOS arm64 and tested an edge case around PSBTv2 input sequences in `psbtx.Merge(*signer_psbtx)` (`src/external_signer.cpp:181`): In `test_invalid_signer_signatures` (`test/functional/wallet_signer.py:319`), `walletcreatefundedpsbt` populates `PSBT_IN_SEQUENCE`, so `PSBTInput::Merge` preserves the original sequence. However, `PSBT_IN_SEQUENCE` is optional in BIP 370 — when omitted (`sequence == std::nullopt`), `GetUnsignedTx()` (`src/psbt.cpp:134`) defaults to `CTxIn::SEQUENCE_FINAL` (`0xffffffff`). If a caller passes a v2 PSBT with an omitted `PSBT_IN_SEQUENCE` to `walletprocesspsbt` and the signer injects e.g. `PSBT_IN_SEQUENCE = 0xfffffffd` and signs that modified transaction: 1. `GetUniqueID()` zeroes `nSequence` for v2, so `psbtx.Merge(*signer_psbtx)` returns `true`. 2. `PSBTInput::Merge` (`src/psbt.cpp:464`) copies `input.sequence` into `psbtx` because `sequence == std::nullopt`. 3. On regtest, `walletprocesspsbt` returns `complete=True` directly for a `legacy` input, and for `bech32m` `res["psbt"]` retains the injected `0xfffffffd` + valid signature so `finalizepsbt` returns `complete=True` (`testmempoolaccept` allowed). Would it make sense to snapshot `psbtx.GetUnsignedTx()` before `Merge` and verify the unsigned tx hash did not change afterwards? That still permits the `strip` mode (where `signer_psbtx` omits `PSBT_IN_SEQUENCE`) while catching sequence injection when `PSBT_IN_SEQUENCE` was omitted on the original input. *(Disclosure per AI policy: used an LLM while auditing `PSBTInput::Merge` callers, then built `aee9e7a` locally and verified the regtest case manually.)*
Sjors commented 2026-12-01 18:09:44 +00:00 (Migrated from github.com)

@johnny9 that's quite a lot of back-pings :-)

Added a check based on @enessubass's feedback: https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-7142498603

I ended up adding a GetUnsignedTxid() helper to psbt.h for this.

@johnny9 that's quite a lot of back-pings :-) Added a check based on @enessubass's feedback: https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-7142498603 I ended up adding a `GetUnsignedTxid()` helper to `psbt.h` for this.
enessubass commented 2026-12-01 20:52:04 +00:00 (Migrated from github.com)

ACK 70f13577ba

GetUnsignedTxid() cleanly closes the omitted-PSBT_IN_SEQUENCE case. Reviewed all 9 commits and manually tested a couple of extra regtest cases not in the test suite: an omitted PSBT_IN_SEQUENCE where the signer explicitly fills 0xffffffff (SEQUENCE_FINAL, accepted since the unsigned txid does not change, whereas 144 CSV is rejected), and a 65-byte PSBT_IN_TAP_SCRIPT_SIG carrying a hidden SIGHASH_NONE / SINGLE|ANYONECANPAY byte.

(AI policy disclosure: used an LLM to help script the local regtest checks; reviewed the commits directly.)

ACK 70f13577ba61c1bd62d73329c2eb386788767887 `GetUnsignedTxid()` cleanly closes the omitted-`PSBT_IN_SEQUENCE` case. Reviewed all 9 commits and manually tested a couple of extra regtest cases not in the test suite: an omitted `PSBT_IN_SEQUENCE` where the signer explicitly fills `0xffffffff` (`SEQUENCE_FINAL`, accepted since the unsigned txid does not change, whereas `144` CSV is rejected), and a 65-byte `PSBT_IN_TAP_SCRIPT_SIG` carrying a hidden `SIGHASH_NONE` / `SINGLE|ANYONECANPAY` byte. *(AI policy disclosure: used an LLM to help script the local regtest checks; reviewed the commits directly.)*
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!36114
No description provided.