Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- `tr()` accepted tapscript leaves that are not valid miniscript, such as
`and_v(pk(A),1)`. A leaf is now type-checked and must be of type `B`, as a
`wsh()` script is.

## [0.8.2] - 2026-08-08

### Changed
Expand Down
7 changes: 6 additions & 1 deletion src/embit/descriptor/taptree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .errors import MiniscriptError
from .errors import DescriptorError, MiniscriptError
from .base import DescriptorBase
from .miniscript import Miniscript
from ..hashes import tagged_hash
Expand All @@ -7,6 +7,11 @@

class TapLeaf(DescriptorBase):
def __init__(self, miniscript=None, version=0xC0):
if miniscript is not None:
# will raise if can't verify
miniscript.verify()
if miniscript.type != "B":
raise DescriptorError("Top level miniscript should be 'B'")
self.miniscript = miniscript
self.version = version

Expand Down
38 changes: 37 additions & 1 deletion tests/tests/test_taptree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from unittest import TestCase
from embit.descriptor import Descriptor
from embit.networks import NETWORKS
from embit.descriptor.errors import MiniscriptError
from embit.descriptor.errors import DescriptorError, MiniscriptError
from embit.descriptor.miniscript import Miniscript
from embit.descriptor.taptree import TapLeaf

NETWORK = NETWORKS["test"]

Expand Down Expand Up @@ -144,3 +146,37 @@ def test_multi_variants(self):
Descriptor.from_string("wsh(sortedmulti(1,%s,%s,%s))" % keys)
Descriptor.from_string("tr(%s,multi_a(1,%s,%s))" % keys)
Descriptor.from_string("tr(%s,sortedmulti_a(1,%s,%s))" % keys)

def test_leaf_miniscript_is_type_checked(self):
"""Tapscript leaves go through the same type rules as wsh scripts"""
root, a, b = PUBKEYS[:3]
invalid = [
# and_v needs a V as its first argument
"and_v(pk(%s),1)" % a,
# a leaf must be a B
"v:pk(%s)" % a,
]
for leaf in invalid:
for tree in (leaf, "{%s,pk(%s)}" % (leaf, b), "{pk(%s),%s}" % (b, leaf)):
with self.assertRaises(DescriptorError):
Descriptor.from_string("tr(%s,%s)" % (root, tree))
# a leaf that is not a B raises the same error as a wsh script
for desc in ("wsh(v:pk(%s))" % a, "tr(%s,v:pk(%s))" % (root, a)):
with self.assertRaises(DescriptorError) as exc:
Descriptor.from_string(desc)
self.assertIs(type(exc.exception), DescriptorError)
# a leaf built in code is checked too
with self.assertRaises(MiniscriptError):
TapLeaf(Miniscript.from_string("and_v(pk(%s),1)" % a, taproot=True))
TapLeaf(None)
valid = [
"pk(%s)" % a,
"multi_a(2,%s,%s)" % (a, b),
"and_v(v:pk(%s),older(10))" % a,
# d: is u in tapscript, so dv:older(10) is allowed as a condition
"andor(dv:older(10),pk(%s),pk(%s))" % (a, b),
"and_n(pk(%s),pk(%s))" % (a, b),
]
for leaf in valid:
desc = Descriptor.from_string("tr(%s,%s)" % (root, leaf))
self.assertEqual(str(desc), "tr(%s,%s)" % (root, leaf))
Loading