Skip to content

fix(descriptor): handle pk() descriptors in derive_from_psbt_input - #596

Open
henrypeters wants to merge 2 commits into
bitcoindevkit:masterfrom
henrypeters:fix/derive-from-psbt-input-pk-descriptor
Open

henrypeters wants to merge 2 commits into
bitcoindevkit:masterfrom
henrypeters:fix/derive-from-psbt-input-pk-descriptor

Conversation

@henrypeters

@henrypeters henrypeters commented Oct 9, 2026 •

Copy link
Copy Markdown

Summary

derive_from_psbt_input falls back to a script comparison strategy when
a PSBT input carries no key origin data and the descriptor has no wildcard.
This fallback uses a match on DescriptorType with three arms:

  1. Direct script_pubkey comparison — for Pkh, Wpkh, ShWpkh, Tr
  2. redeem_script comparison — for Sh, ShSortedMulti
  3. witness_script comparison — for Wsh, ShWsh, etc.

DescriptorType::Bare — which covers bare pk() descriptors — was
incorrectly placed in arm 2, which checks against psbt_input.redeem_script.
A bare pk() descriptor does not use a redeem script — it produces a
script_pubkey directly. This meant psbt_input.redeem_script was always
None for these inputs, the guard condition always failed, and the function
always returned None for any pk() descriptor without key origin data.

This silently broke signing and finalization for wallets using bare pk()
descriptors whenever the PSBT lacked BIP32 derivation metadata.

Fix

I moved DescriptorType::Bare from arm 2 to arm 1, where it is matched by
comparing descriptor.script_pubkey() against the UTXO's script_pubkey
— the correct approach for bare pk() descriptors.

Also removes the // TODO: add pk() here comment that identified this gap.

Testing

Added a test using a bare pk() descriptor confirming that
derive_from_psbt_input returns Some(descriptor) instead of None
when the UTXO's script_pubkey matches.

@henrypeters
henrypeters requested a review from a team as a code owner October 9, 2026 16:54

@busayo-od busayo-od 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.

Thanks for working on this! I noticed that #457 introduces validation that rejects Bare descriptors during wallet creation and loading. Given that derive_from_psbt_input is currently only called during wallet finalization using descriptors from the wallet's keychain index, would this case still be reachable through supported Wallet usage once 457 is applied?

@henrypeters

henrypeters commented Oct 19, 2026 •

Copy link
Copy Markdown
Author

Thanks for the pointer to #457 . Since that PR is still open and not yet merged into master, Bare descriptors are currently accepted by the wallet, meaning this fix addresses a real reachable bug today, but if #457 does gets merged it means this fix becomes redundant and could be removed. This fix just serves as a safety net at the signing layer. Happy to discuss a preferred approach with the maintainers

This branch has not been deployed

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

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

2 participants