Skip to content

fix: validate taproot control block conversion - #1083

Merged
reez merged 1 commit into
bitcoindevkit:masterfrom
reez:tap
Nov 11, 2026
Merged

reez merged 1 commit into
bitcoindevkit:masterfrom
reez:tap

Conversation

@reez

@reez reez commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Reject invalid Taproot output key parity and mismatched leaf versions during PSBT input conversion (which prevents silent control block corruption).

Notes to the reviewers

Documentation

Changelog

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing
  • I've added exactly one changelog:* label
  • I've linked the relevant upstream docs or specs above

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@thunderbiscuit thunderbiscuit added this to the 3.2.0 milestone Sep 11, 2026
@reez
reez marked this pull request as ready for review September 22, 2026 18:29
@reez
reez requested a review from thunderbiscuit September 22, 2026 18:29

@thunderbiscuit thunderbiscuit left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks like it solves the problem. Trying to review it alongside with Claude I have 1 piece of feedback I would keep, and it's the fact that the leaf-version equality check is reject PSBTs that rust-bitcoin itself accepts?

rust-bitcoin apparently doesn't check that the two leaf versions match. This allows creating a PSBT that cannot be finalized because it would create an invalid transaction. The check/guard here doesn't really hurt, but also is adding a layer of check not done elsewhere.

The other 2 parts of the PR are good and worth adding for sure.

@reez

reez commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

This looks like it solves the problem. Trying to review it alongside with Claude I have 1 piece of feedback I would keep, and it's the fact that the leaf-version equality check is reject PSBTs that rust-bitcoin itself accepts?

rust-bitcoin apparently doesn't check that the two leaf versions match. This allows creating a PSBT that cannot be finalized because it would create an invalid transaction. The check/guard here doesn't really hurt, but also is adding a layer of check not done elsewhere.

The other 2 parts of the PR are good and worth adding for sure.

Yeah, I think that rust-bitcoin accepts PSBTs where the control block and script value have different leaf versions.

I added the check because our current conversion otherwise uses the control block’s version for both fields. Removing only the check would silently change the script value’s version.

We could keep the check and reject mismatches, or convert each version separately and preserve both values (matching rust-bitcoin’s behavior?)

Any preference?

@reez reez modified the milestones: 3.1.1, 3.2.0 Sep 28, 2026
@j-kon

j-kon commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I reproduced this case and confirmed current master is lossy here: when the two versions differ, the tap-script leaf version is silently replaced by the control-block version.

One nuance though: rust-bitcoin can preserve and round-trip both values independently, and BIP341 consensus derives the actual spend leaf version from the control block itself. The mismatch does break normal Miniscript finalization, but the separate BIP371 leaf-version field is PSBT metadata rather than part of the final witness.

Since our FFI types already carry both values independently, would preserving both here be closer to upstream rust-bitcoin semantics than rejecting the mismatch?

@thunderbiscuit thunderbiscuit left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK c5f7f2b. Sorry it took me so long to get to this! A quick rebase and this is ready for merge.

@reez
reez requested a review from a team as a code owner November 11, 2026 17:23
@reez
reez merged commit 7c41144 into bitcoindevkit:master Nov 11, 2026
8 checks passed
@reez
reez deleted the tap branch November 11, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants