Skip to content

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

Open
Sjors wants to merge 9 commits into
bitcoin:masterfrom
Sjors:2026/08/external-signer-mock
Open

Sjors wants to merge 9 commits into
bitcoin:masterfrom
Sjors:2026/08/external-signer-mock

Conversation

@Sjors

@Sjors Sjors commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

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 #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

DrahtBot commented Aug 28, 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 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

@Sjors

Sjors commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

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

@Sjors Sjors mentioned this pull request Aug 28, 2026
7 tasks
dangervslash

This comment was marked as spam.

@dangervslash

This comment was marked as resolved.

Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 3, 2026
…nd revamp mock

# Conflicts:
#	test/functional/wallet_signer.py
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 3, 2026
bitcoin#33112 tests walletdisplayaddress for an imported
external-signer descriptor using the same xpub as a wallet exercised earlier
in wallet_signer.py. bitcoin#36114 then revamps the shared external
signer mock used by the test. With #127, the imported descriptor's
known HD keys are filled immediately, so its first address is recognized as
used and the wallet advances to the next index, which the mock intentionally
maps to wrong_address for a separate error case.

Let the test configure the address echoed by the mock so it remains
independent of the derived index while preserving the mismatched-address
coverage.
@Sjors
Sjors force-pushed the 2026/08/external-signer-mock branch from 79fb761 to 0567fa6 Compare September 8, 2026 10:57
@Sjors

Sjors commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

Rebased after #36113, still based on #36076.

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Comment thread src/external_signer.cpp
}

if (!psbtx.Merge(*signer_psbtx)) {
error = "Signer returned a PSBT for a different transaction";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a constraint that the version must match.

Comment thread test/functional/wallet_signer.py Outdated
self.log.info('The signer must not use unsafe sighash types')
# The first mode declares the sighash type in the PSBT, the second
# leaves it out, so only the signatures themselves reveal it
for mode in ["sighash_none", "sighash_none_hidden"]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a test case.

@Sjors

Sjors commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

Rebased after #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
Sjors marked this pull request as ready for review September 28, 2026 08:52
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 28, 2026
…nd revamp mock

# Conflicts:
#	test/functional/wallet_signer.py
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 28, 2026
bitcoin#33112 tests walletdisplayaddress for an imported
external-signer descriptor using the same xpub as a wallet exercised earlier
in wallet_signer.py. bitcoin#36114 then revamps the shared external
signer mock used by the test. With #127, the imported descriptor's
known HD keys are filled immediately, so its first address is recognized as
used and the wallet advances to the next index, which the mock intentionally
maps to wrong_address for a separate error case.

Let the test configure the address echoed by the mock so it remains
independent of the derived index while preserving the mismatched-address
coverage.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 28, 2026
…nd revamp mock

# Conflicts:
#	test/functional/wallet_signer.py
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 28, 2026
bitcoin#33112 tests walletdisplayaddress for an imported
external-signer descriptor using the same xpub as a wallet exercised earlier
in wallet_signer.py. bitcoin#36114 then revamps the shared external
signer mock used by the test. With #127, the imported descriptor's
known HD keys are filled immediately, so its first address is recognized as
used and the wallet advances to the next index, which the mock intentionally
maps to wrong_address for a separate error case.

Let the test configure the address echoed by the mock so it remains
independent of the derived index while preserving the mismatched-address
coverage.
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 23, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
@enessubass

Copy link
Copy Markdown

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.)

johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 24, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 24, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 24, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 24, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 25, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 25, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 26, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of the R42 series, which builds only at its final commit.

Refs bitcoin-core#1122
johnny9 added a commit to johnny9/gui-qml that referenced this pull request Nov 28, 2026
Check that the transaction returned by an external signer has exactly the
reviewed terms; signing may only add signatures. Refuse a substituted
transaction. These are the app-side
checks; the full fix needs bitcoin/bitcoin#36114 in Core.

Part of a series that builds only at its final commit.

Refs bitcoin-core#1122
Sjors and others added 9 commits December 1, 2026 17:23
The external signer mock previously replayed a PSBT that the test
prepared in advance. This makes it difficult to test more complicated
scenarios like a misbehaving wallet and (MuSig2) multisig.

Instead, give the mock its own descriptor wallet. The test provides a
dedicated node for this wallet and keeps it offline, so the mock can't
cheat by e.g. inspecting the UTXO set. The mock creates the wallet on
first use and signs with walletprocesspsbt.

wallet_signer.py now funds all four descriptor types and spends them
in a single transaction, exercising every signing code path.
Reject a signer response with a different PSBT version and report the expected and returned versions. Document the requirement and test a v0 response to a v2 request.
Return the txid of the unsigned transaction. Unlike GetUniqueID(), it
commits to PSBTv2 input sequences. Also document GetUniqueID().
Previously the PSBT returned by the external signer replaced the
original wholesale, trusting the signer not to modify the transaction.
Merge it instead, and reject the response if it describes a different
transaction or if merging would change the unsigned transaction. This
also supports signers that strip fields they don't need from their
response.

Test a stripped response, and signers that change an output amount or
script, remove an output, or add a PSBT v2 input sequence that the
original omits. A signer that changes an existing sequence or corrupts
a signature must leave the original transaction unchanged and the PSBT
incomplete. Test ECDSA and Schnorr signatures independently.

Suggested in: bitcoin#36114 (comment)
and bitcoin#36114 (comment)

Co-authored-by: brunoerg <[email protected]>
Require signer responses to keep signatures in non-final fields and leave finalization to Bitcoin Core. Document the requirement in the signer API.

Test final scriptSig and witness fields independently, including responses that finalize only one input.
A signature with SIGHASH_NONE or SIGHASH_SINGLE doesn't commit to all
outputs, letting anyone alter them after signing. Reject a PSBT from
an external signer that declares such a sighash type or contains
signatures made with one.

SIGHASH_ANYONECANPAY is still accepted: it only permits adding inputs,
which does not affect us.

The mock signer produces real signatures for these scenarios by
letting its wallet sign with the requested sighash type, optionally
hiding the declared sighash type field so that only the signatures
themselves reveal it.

Co-authored-by: brunoerg <[email protected]>
@Sjors
Sjors force-pushed the 2026/08/external-signer-mock branch from aee9e7a to 70f1357 Compare December 1, 2026 18:08
@Sjors

Sjors commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

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

Added a check based on @enessubass's feedback: #36114 (comment)

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

@enessubass

Copy link
Copy Markdown

ACK 70f1357

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants