Skip to content

Taproot

tr()

tr(internal_key: PubKey) -> Descriptor<Tr>

Constructs a P2TR Descriptor with an explicit internal_key and no script paths (keypath-only).

For example:

  • tr($alice)
  • tr(tpubD6NzVbkrYhZ4YJ35iWbEDLBCNsd3BeY2EhB8Za6yLGqf9nVg3Aaz8z6Ts6eXgh1v72SESgRpKv9jRFkR7qh65hzW549vaTZg7ozhgAcEV1r/0/9)
  • tr(b2123025f45648c3f31fd4b7d3e1ec3344769ab3f53dec5af9b8a9a95385cbd5)
tr(internal_key: PubKey, tree: Policy|Array<Policy>) -> Descriptor<Tr>

Constructs a P2TR Descriptor with an explicit internal_key and a Taproot script tree expressed as Miniscript Policy.

If the policy has top-level OR branches or a 1-of-N threshold, the sub-policies will be split into separate Taproot script leaves (structured based on the @ assigned execution probabilities, if provided).

Also accepts an array of policies, which is equivalent to a 1-of-N threshold policy between the array elements.

For example:

tr(tree: Policy|Array<Policy>) -> Descriptor<Tr>

Constructs a P2TR Descriptor with a Miniscript Policy tree and no explicit internal key.

If the policy includes an unencumbered key (may spend with no additional restrictions), it will implicitly be promoted to the internal key and removed from the script tree. If multiple exist, the most likely one will be chosen (based on the @ assigned execution probability).

If no unencumbered keys exists, NUMS will be used as the internal key. The default NUMS used by tr() may be changed by setting TR_UNSPENDABLE.

For example:

  • tr(pk($alice) || (pk($bob) && older(1))) will use $alice as the internal key (the only possible one)
  • tr(pk($alice) || 5@pk($bob)) will use $bob as the internal key (the most likely one)
  • tr(pk($alice) && older(1)) will use NUMS as the internal key (no unencumbered keys)
tr(internal_key: PubKey, tree: Script|Array<Script>) -> TapInfo
tr(tree: Script|Array<Script>) -> TapInfo

Constructs a TapInfo with the given raw Script tree and optionally with an internal_key.

When called without an internal_key, NUMS will be used by default.

For example:

tr(internal_key: PubKey, merkle_root: Hash) -> TapInfo

Constructs a TapInfo with the given merkle_root hash and internal_key.

Because the full script tree isn't provided, the resulting TapInfo supports key-path spends only and cannot be used to construct control blocks for script-path spends.

For example:
tr($alice, 0x4352c5df4ab2266ab1db28b4295b9508f798802bb6005a91a654c1e41ef71d10)

Rust source code: src/stdlib/taproot.rs:N/A

Descriptor<Tr> vs TapInfo

Descriptors can only represent Taproot trees expressed as Miniscript. To represent trees with raw Script or trees known only by their merkle root hash, the TapInfo structure is used instead.

See the Taproot guide for more information.

tr::ctrl()

tr::ctrl(
    tap: Descriptor<Tr>|TapInfo|PsbtInput,
    script_leaf: Script|Policy,
    leaf_ver: LeafVersion = TapScript
) -> Bytes

Construct the Taproot Control Block for a script path spend through the given script_leaf.

If there are multiple possible control blocks, the shortest one is chosen.

When a PsbtInput is given, the control block is extracted from its tap_scripts field.

Throws: If the script_leaf is not contained in the tap tree.

Rust source code: src/stdlib/taproot.rs:N/A

tr::script_witness()

tr::script_witness(
    tap: Descriptor<Tr>|TapInfo|PsbtInput,
    script_leaf: Script|Policy,
    witness_stack: Array<Bytes> = [],
    annex: Bytes = None,
) -> Array<Bytes>

Construct an input witness spending through the script_leaf path with the given witness_stack, and optionally with an annex.

The leaf version is assumed to be TapScript.

Throws: If the script_leaf is not contained in the tap tree.

Minsc source code: src/stdlib/btc.minsc:N/A

tr::tapinfo()

tr::tapinfo(Descriptor<Tr>) -> TapInfo

Convert a Descriptor<Tr> into a TapInfo structure. Should not typically be necessary.

Rust source code: src/stdlib/taproot.rs:N/A

TapInfo

Miniscript-compatible Taproot trees are represented as a Descriptor<Tr> and not as a TapInfo, which is only used with raw Script or trees known only by their merkle root hash.

Construction Examples:

Variations: (None)

Unlike the Descriptor<Tr> type, TapInfos are always Definite and contain public keys that are always Definite and SingleKey. (The underlying TaprootSpendInfo cannot represent Xpubs, multi-path or wildcard keys.)

Coerces from:

Underlying type: bitcoin::TaprootSpendInfo


Similar fields are mirrored on Taproot P2TR Descriptors.

script_pubkey Script

Also see:* scriptPubKey()

witness_program Int:Bytes

internal_key PubKey<SingleKey>

Always definite.

output_key PubKey<SingleKey>

Always definite.

output_key_parity Int

Get the parity of the output key (0 for even, 1 for odd)

merkle_root Hash opt

Only available when there are script paths

script_tree TapNode opt

Only available when there are script paths

scripts Array<Script>

Returns an empty array when there are no script paths

is_definite Bool

Always true

address_type String

Always "p2tr"

TR_UNSPENDABLE PubKey

TR_UNSPENDABLE = NUMS

The default unspendable NUMS key used by tr() when there are no internal key candidates. Can be overridden (shadowed over) in user-land code.

If set to false, Taproot compilation with no internal key candidates will fail with No viable internal key found instead of using a default NUMS.

TR_ANNEX_BYTE Bytes

TR_ANNEX_BYTE = 0x50

TapNode

TapLeafHash

LeafVersion