Redeem a P2TR transaction output (scriptpath)

uenohiro4
Nayuta Engineering Blog
2 min readMar 9, 2024

I will write about the program to redeem from scriptpath to golang.

With P2SH and P2WSH, all scripts were included, so once redeemed, you could confirm what kind of script it was. However, in the case of P2TR, it is not necessary to include all scripts when redeeming.

I referred to this site and create a sample JavaScript code.

The first thing I understood was that I had to break down the script. This is the documentation for the script debugger btcdeb, and it includes script rewriting early on.

In the case of P2SH and P2WSH, it is sometimes necessary to push meaningless data onto the stack to make the OP_ELSEbranch. However, indicating which leaf script to redeem is sufficient.

Create a tree from the dissected script.

The sample code redeems the left script (referred to as script1 in the code). In the case of P2TR scriptpath, the control block is placed at the end of the witness stack.

The sample code created in Go language is here.

I haven’t redeemed the complex script yet, but I believe the implementation direction is not wrong.

--

--