Skip to content

feat: add BIP-360 P2MR address encoding and Merkle tree support#246

Open
lcovar wants to merge 2 commits intomasterfrom
BTC-3241-bip360-p2mr-support
Open

feat: add BIP-360 P2MR address encoding and Merkle tree support#246
lcovar wants to merge 2 commits intomasterfrom
BTC-3241-bip360-p2mr-support

Conversation

@lcovar
Copy link
Copy Markdown
Contributor

@lcovar lcovar commented Apr 7, 2026

Add Pay-to-Merkle-Root (BIP-360) support to wasm-utxo covering address
encoding/decoding (witness v2, bech32m bc1z/tb1z) and internal Merkle
tree construction with control block generation.

Address encoding (commit 1):

  • Generic bech32 segwit::encode() for all witness versions (v0-v2+)
  • P2MR script detection (34 bytes, witness v2, OP_PUSHBYTES_32)
  • OutputScriptSupport.p2mr flag on all Bitcoin networks
  • P2MR address test vectors (bc1z mainnet, tb1z testnet)

Merkle tree core (commit 2):

  • Internal p2mr module using bitcoin crate's TapLeafHash/TapNodeHash
    (not exposed to JS, used internally by fixed-script wallet)
  • Tree building, control block generation/verification
  • BIP-360 spec committed to bips/bip-0360/
  • Fixture-driven Rust tests loading from p2mr_construction.json,
    covering all 6 construction vectors + mainnet address validation

BTC-3241

@lcovar lcovar requested a review from a team as a code owner April 7, 2026 21:43
@lcovar lcovar force-pushed the BTC-3241-bip360-p2mr-support branch from 95389bd to 5a099bf Compare April 7, 2026 22:20
@lcovar lcovar marked this pull request as draft April 7, 2026 22:32
Copy link
Copy Markdown
Contributor

@OttoAllmendinger OttoAllmendinger left a comment

Choose a reason for hiding this comment

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

do we really need to expose the JS api? if we add this to fixed-script wallets it will be just another chain code with the script internals hidden from the user

@lcovar lcovar force-pushed the BTC-3241-bip360-p2mr-support branch from 5a099bf to 3bf0bc7 Compare April 8, 2026 19:24
@lcovar
Copy link
Copy Markdown
Contributor Author

lcovar commented Apr 8, 2026

do we really need to expose the JS api? if we add this to fixed-script wallets it will be just another chain code with the script internals hidden from the user

removed

@lcovar lcovar requested a review from OttoAllmendinger April 8, 2026 19:31
Copy link
Copy Markdown
Contributor

@OttoAllmendinger OttoAllmendinger left a comment

Choose a reason for hiding this comment

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

almost there

@lcovar lcovar force-pushed the BTC-3241-bip360-p2mr-support branch 2 times, most recently from b19bd0e to eeae358 Compare April 9, 2026 16:38
@lcovar lcovar requested a review from OttoAllmendinger April 9, 2026 16:43
lcovar added 2 commits April 9, 2026 11:36
Add witness v2 (P2MR) support to the address encoding layer:
- Generic bech32 segwit::encode() for all witness versions (v0-v2+)
- P2MR script detection (34 bytes, witness v2, OP_PUSHBYTES_32)
- OutputScriptSupport.p2mr flag on all Bitcoin networks
- P2MR address test vectors (bc1z mainnet, tb1z testnet) validated
  against BIP-360 spec fixtures

BTC-3241
Add internal P2MR tree construction module (not exposed to JS):
- Tagged hash computation (TapLeaf, TapBranch per BIP-341)
- Script tree building with DFS traversal and per-leaf control blocks
- Control block generation (leaf_version | 0x01 parity) and verification
- Merkle proof verification against expected root

Rust tests cover all 6 BIP-360 construction vectors (single leaf,
two-leaf same/different versions, lightning contract, two three-leaf
variants). TS tests validate mainnet bc1z address encoding against
BIP-360 fixture addresses.

BTC-3241
@lcovar lcovar force-pushed the BTC-3241-bip360-p2mr-support branch from eeae358 to ac5e692 Compare April 9, 2026 18:36
@lcovar lcovar marked this pull request as ready for review April 10, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants