Skip to content

wallet: select FillPSBT SigningProvider via GetUTXO - #35747

Open
nervana21 wants to merge 3 commits into
bitcoin:masterfrom
nervana21:fix-fillpsbt
Open

nervana21 wants to merge 3 commits into
bitcoin:masterfrom
nervana21:fix-fillpsbt

Conversation

@nervana21

@nervana21 nervana21 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A PSBT input can carry both a non_witness_utxo and a witness_utxo. GetUTXO prefers a verified non_witness_utxo and does not fall back to witness_utxo when the non_witness_utxo is present but unusable.

Previously, FillPSBT selected the SigningProvider from the witness_utxo whenever it was set. When the two disagreed, FillPSBT loaded keys for the witness_utxo script while SignPSBTInput signed the non_witness_utxo spend. This could lead to an incomplete input even though the wallet owns the appropriate non_witness_utxo script.

Fix

Route FillPSBT through GetUTXO so it shares SignPSBTInput's prefer/reject rule.

Behavior change

FillPSBT now uses GetUTXO for script selection, so an input whose non_witness_utxo is present but unusable (out-of-range or txid-mismatched) returns MISSING_INPUTS instead of first selecting a script from witness_utxo.

SignPSBTInput rejects the same input with MISSING_INPUTS, and FillPSBT already propagates any non-INCOMPLETE error for the remaining ScriptPubKeyMans and inputs. The change is that the failure is detected at script selection rather than after a mistaken key lookup.

Well-formed PSBTs are unaffected, since witness_utxo and non_witness_utxo resolve to the same script. Txid handling is also tightened in the selection path. A txid-mismatched non_witness_utxo is now rejected by GetUTXO rather than used for script selection when no witness_utxo is present.

Testing

fillpsbt_signs_despite_conflicting_witness_utxo covers the regression. psbt2_getutxo pins the GetUTXO prefer/reject rules the fix relies on.

Follow-up

SignPSBTInput, PSBTInputSignedAndVerified, and decodepsbt fee totaling still reimplement GetUTXO's logic and risk the same divergence. Refactoring them is left for a follow-up.

@DrahtBot

DrahtBot commented Jul 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
Concept ACK Bicaru20
Stale ACK 8144225309

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

Conflicts

No conflicts as of last run.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/29695490423/job/88215409202
LLM reason (✨ experimental): CI failed because clang-tidy reported a bugprone-argument-comment error (commented argument name mismatch in wallet/test/psbt_wallet_tests.cpp), treated as warnings-as-errors.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

if (!input.witness_utxo.IsNull()) {
script = input.witness_utxo.scriptPubKey;
} else if (input.non_witness_utxo) {
if (input.prev_out >= input.non_witness_utxo->vout.size()) {

@vicjuma vicjuma Aug 3, 2026 •

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.

Why do I feel as if these checks are enough? Is there any motivation for someone intending to disagree non_witness_utxo and witness_utxo? Any risk involved? I have explored some possible paths and saw that at no particular point can this be internal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi vicjuma, thanks for your review!

Why do I feel as if these checks are enough?

For SignPSBTInput they are but FillPSBT wasn't using that same rule for key selection though, which is the gap this PR closes.

Is there any motivation for someone intending to disagree non_witness_utxo and witness_utxo?

IIUC, a malicious or otherwise buggy PSBT coordinator could add a faulty witness_utxo to an honest peer's already-existing non_witness_utxo. That would cause FillPSBT to choose the faulty witness_utxo for key selection instead of the desired non_witness_utxo, so signing could fail even though we owned the right input.

Any risk involved?

As I see it, the risk with the old FillPSBT behavior is just that signing fails or stays incomplete. We'd never sign the wrong utxo.

I have explored some possible paths and saw that at no particular point can this be internal.

That's my understanding as well. I think this only corrects the external/mutated PSBT case.

@nervana21

Copy link
Copy Markdown
Contributor Author

Resolved merge conflicts and rebased off master.

@8144225309

Copy link
Copy Markdown
Contributor

ACK 7aed7f8

Reproduced on master first: fillpsbt_signs_despite_conflicting_witness_utxo fails there and passes with the fix. Reverting just the scriptpubkeyman.cpp change brings the failure back. Full unit and functional suites pass.

Routing FillPSBT through GetUTXO matches SignPSBTInput's selection, so when the witness_utxo disagrees with the non_witness_utxo, the wallet no longer loads keys for the wrong script. Nice fix, thanks.

@Bicaru20 Bicaru20 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 agree with the approach taken. Before, If witness_utxo and non_witness_utxo. had conflicting scripts, FillPSBT could select a signing provider
different than SignPSBTInput (sinc ethe first one is looking at witness_utxo and the second one at non_witness_utxo) leaving the input as incomplete despite having the needed key. With this fix, FillPSBT will use GetUTXO sharing the same logic as SignPSBTInput and avoiding any disagreements.

I left some comments on the tests to try to make them more strong.

Comment thread src/test/psbt_tests.cpp Resolved
Comment thread src/test/psbt_tests.cpp Resolved
Comment thread src/wallet/test/psbt_wallet_tests.cpp Resolved
@fanquake

Copy link
Copy Markdown
Member

@nervana21 can you rebase here?

@nervana21 nervana21 changed the title wallet: Fix FillPSBT failing to sign owned inputs when UTXOs disagree wallet: select FillPSBT SigningProvider via GetUTXO Dec 1, 2026
Document and test prefer-non_witness, reject OOB/txid mismatch, and no
witness fallthrough when fields disagree, before migrating callers.
Document that FillPSBT selects SigningProvider from witness_utxo
even when a verified non_witness_utxo disagrees, before aligning
it with GetUTXO.
@nervana21

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing!

Updates:

@DrahtBot

DrahtBot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/46673188214/job/142733738530
LLM reason (✨ experimental): intermittent_network: Dependency installation failed because Ubuntu package repositories repeatedly returned 503s and connection errors, leaving required packages unavailable.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening.

  • A functional or integration test failure may be intermittent or unrelated to this pull request.
    If no dedicated issue thread exists, create a new one to track it.

Leave a comment here, if you need help tracking down a confusing failure.

Match SignPSBTInput by preferring a verified non_witness_utxo and
rejecting a bad non_witness_utxo instead of falling back to a
witness_utxo.
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.

6 participants