Redeem a P2TR transaction output (keypath)

uenohiro4
Nayuta Engineering Blog
2 min readMar 3, 2024

It’s been a while since Taproot became active on the Bitcoin mainnet. I haven’t checked how much it’s being used, but browsing through mempool.space gives the impression that it’s being used fairly decently.

The type of addresses associated with Taproot is P2TR (Pay to Taproot).
I’ve been using Go language lately, so I wrote code using github.com/btcsuite/btcd to redeem UTXOs sent to a P2TR address.

btcd itself is one of the few implementations of Bitcoin full nodes. From my perspective, primarily focused on the Lightning Network, it’s recognized as a library commonly used in LND.

Previously, there was also github.com/btcsuite/btcutil, but it has been incorporated into btcd, leaving only github.com/btcsuite/btcd.

Here is the program to redeem with keypath.

P2TR is treated as segwit version 1. Previous formats like P2WPKH are version 0.

In P2TR, Schnorr signatures are used. For detailed information, it’s recommended to read BIP-340 and BIP-341.

  • The range of values, among other things, align with secp256k1.
  • The public key utilizes only the X-coordinate, hence it is 32 bytes in size. Calculate below:
  • SIGHASH_DEFAULT (0x00) was added for signature type.

I haven’t found any other libraries besides btcd for handling Bitcoin scripts in the Go language. However, I find the btcd library difficult to work with due to the lack of sample code.

--

--