Skip to content

txscript: derive P2TR pkScript in ComputePkScript - #2632

Open
fametrano wants to merge 1 commit into
btcsuite:masterfrom
fametrano:computepkscript-taproot
Open

fametrano wants to merge 1 commit into
btcsuite:masterfrom
fametrano:computepkscript-taproot

Conversation

@fametrano

@fametrano fametrano commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Change Description

I changed ComputePkScript so that it no longer returns a made-up P2WSH
script for a taproot witness (#1817, #1767).

A script path spend reveals the leaf script and the control block, so the
output key is computed from them as BIP 341 does and the P2TR script is
returned. A key path spend reveals only a signature, so it returns
ErrUnsupportedScriptType, as suggested in #1767.

A witness is taken as taproot if it ends in an annex (first byte 0x50)
or in a well-formed control block with a leaf version of at least 0xc0.
A spendable P2WSH or P2WPKH witness cannot end that way. A lone 64 or 65
byte item is taken as a key path signature. These rules follow sipa's
comment linked in #1817.

Not covered: a P2WSH witness made of a single 64 or 65 byte script now
returns ErrUnsupportedScriptType instead of its P2WSH script, and a
P2WSH witness can still be mistaken for P2WPKH, as noted in #1767.

Fixes #1817.

Steps to Test

cd txscript
go test -run 'ComputePkScript' .

TestComputePkScriptTaproot uses the 12 script path leaves of the BIP 341
wallet test vectors (with and without an annex) and fails on master.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Assisted-by: my usual tools (LLM, Internet, computer). Mistakes are mine.

@fametrano
fametrano force-pushed the computepkscript-taproot branch from 7e29426 to 49c0e4a Compare October 13, 2026 11:27

@lucasdbr05 lucasdbr05 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

This correctly extends ComputePkScript to derive a P2TR pkScript for script path spends. It removes an annex when present, parses the control block, reconstructs the Taproot merkle root, and derives the output key according to BIP-341.

I also ran go test and go test -race successfully.

@fametrano

Copy link
Copy Markdown
Contributor Author

@yyforyongyu @Roasbeef, could you approve the Build and Test runs waiting on this PR and on #2630 before they expire at 11:27 UTC on 12 November, and the one on #2651 before 22:17 UTC on 16 November? A review of any of the three would also help.

ComputePkScript treated every witness it could not match to P2WPKH as
a P2WSH one. A taproot witness was therefore given a made-up P2WSH
script, which neutrino's block filter check used as if it were real.

A script path spend reveals what the output key commits to: the leaf
script and a control block. Compute the output key from them as BIP 341
does and return the P2TR script. A witness ends in a control block if
its last item has a valid length and a leaf version of at least 0xc0,
or in an annex if it starts with 0x50. Both are opcodes that fail when
a witness script starts with them, so no spendable P2WSH or P2WPKH
witness ends that way.

A key path spend reveals only a signature, so the output key is not in
the witness. A lone 64 or 65 byte item, or a lone item followed by an
annex, returns ErrUnsupportedScriptType, as btcsuite#1767 suggests. A P2WSH
witness made of a single 64 or 65 byte script, which takes no input,
now returns the same error.

The tests use the 12 script path leaves of the BIP 341 wallet test
vectors.

Fixes btcsuite#1817. Refs btcsuite#1767.
@fametrano
fametrano force-pushed the computepkscript-taproot branch from 49c0e4a to 6d32d6d Compare November 13, 2026 01:15
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.

txscript: update ComputePkScript for a post taproot environment

2 participants