Skip to content

Add P2MR (BIP360) support to PSBT handling#27

Merged
BlobMaster41 merged 1 commit intomainfrom
feat/implement-p2mr
Feb 16, 2026
Merged

Add P2MR (BIP360) support to PSBT handling#27
BlobMaster41 merged 1 commit intomainfrom
feat/implement-p2mr

Conversation

@BlobMaster41
Copy link
Contributor

@BlobMaster41 BlobMaster41 commented Feb 16, 2026

Description

Detect and handle P2MR (Pay-to-Merkle-Root) outputs throughout PSBT logic. Highlights:

  • Add isP2MRInput helper and treat P2MR as taproot-style input in isTaprootInput.
  • Avoid key-path finalization for P2MR inputs in Psbt.finalizeInput (always use script-path).
  • Import rootHashFromPathP2MR and isP2MR, and extend tap leaf verification to support P2MR control-block format. isTapLeafInTree now checks P2TR and P2MR control-block shapes and validates against the merkle root.
  • Update checks for mixed taproot/non-taproot outputs to account for P2MR.
  • Extensive tests added for P2MR: detection, single- and two-leaf signing/finalization, signature validation, and rejecting key-path finalization.

These changes enable BIP-360 P2MR script-path spends to be signed, validated, finalized, and extracted via PSBT.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Performance improvement
  • Refactoring (no functional changes)
  • Documentation update
  • CI/CD changes
  • Dependencies update

Checklist

Build & Tests

  • npm install completes without errors
  • npm test passes all tests
  • npm run browserBuild completes without errors (if applicable)

Code Quality

  • Code follows the project's coding standards
  • No new compiler/linter warnings introduced
  • Error handling is appropriate
  • TypeScript types are properly defined

Documentation

  • Code comments added for complex logic
  • Public APIs are documented
  • README updated (if applicable)

Security

  • No sensitive data (keys, credentials) committed
  • No new security vulnerabilities introduced
  • Cryptographic operations reviewed (if applicable)

Bitcoin Specific

  • Transaction serialization/deserialization is correct
  • Script operations are properly validated
  • Network parameters are handled correctly
  • PSBT operations maintain compatibility

Testing

Related Issues


By submitting this PR, I confirm that my contribution is made under the terms of the project's license.

Detect and handle P2MR (Pay-to-Merkle-Root) outputs throughout PSBT logic. Highlights:

- Add isP2MRInput helper and treat P2MR as taproot-style input in isTaprootInput.
- Avoid key-path finalization for P2MR inputs in Psbt.finalizeInput (always use script-path).
- Import rootHashFromPathP2MR and isP2MR, and extend tap leaf verification to support P2MR control-block format. isTapLeafInTree now checks P2TR and P2MR control-block shapes and validates against the merkle root.
- Update checks for mixed taproot/non-taproot outputs to account for P2MR.
- Extensive tests added for P2MR: detection, single- and two-leaf signing/finalization, signature validation, and rejecting key-path finalization.

These changes enable BIP-360 P2MR script-path spends to be signed, validated, finalized, and extracted via PSBT.
@BlobMaster41 BlobMaster41 added enhancement New feature or request 7.x labels Feb 16, 2026
@BlobMaster41 BlobMaster41 merged commit ff531c2 into main Feb 16, 2026
12 checks passed
@BlobMaster41 BlobMaster41 requested a review from Copilot February 16, 2026 07:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds comprehensive support for P2MR (Pay-to-Merkle-Root, BIP360) to the PSBT handling logic. P2MR is a SegWit v2 output type that commits directly to a Merkle root without an internal public key, enabling script-path spending only (no key-path). The implementation extends the existing taproot infrastructure to handle P2MR alongside P2TR throughout the PSBT signing, validation, and finalization workflows.

Changes:

  • Adds P2MR detection and handling throughout PSBT input/output validation using isP2MRInput helper
  • Prevents key-path finalization for P2MR inputs (always uses script-path)
  • Extends tap leaf verification with P2MR control-block format support via rootHashFromPathP2MR
  • Adds comprehensive test coverage for P2MR detection, signing, finalization, and validation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/psbt/bip371.ts Adds isP2MRInput helper; extends isTaprootInput and isTaprootOutput to detect P2MR; updates isTapLeafInTree to verify P2MR control blocks using rootHashFromPathP2MR with fallback logic for ambiguous cases
src/psbt.ts Modifies #finalizeTaprootInput to prevent key-path finalization for P2MR inputs by checking !isP2MRInput(input) before using tapKeySig
test/psbt.spec.ts Adds comprehensive P2MR test suite covering detection via isTaprootInput, single/multi-leaf script-path signing and finalization, signature validation, and rejection of key-path finalization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

7.x enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments