From b8a34966a2c1f456498de37440fdfe33e5647530 Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Thu, 22 Oct 2026 08:29:55 +0200 Subject: [PATCH 1/5] descriptor: type-check tapscript leaves TapLeaf never called verify(), so tr() accepted leaves that Bitcoin Core rejects, such as and_v(pk(B),1), and so did a TapLeaf built in code. Verify the miniscript when the leaf is built and require its type to be B, as Descriptor.__init__ does for wsh. --- src/embit/descriptor/taptree.py | 5 +++++ tests/tests/test_taptree.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/embit/descriptor/taptree.py b/src/embit/descriptor/taptree.py index 7f611e5e..47b9589b 100644 --- a/src/embit/descriptor/taptree.py +++ b/src/embit/descriptor/taptree.py @@ -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 MiniscriptError("Top level miniscript should be 'B'") self.miniscript = miniscript self.version = version diff --git a/tests/tests/test_taptree.py b/tests/tests/test_taptree.py index 56645db7..7a95bfbd 100644 --- a/tests/tests/test_taptree.py +++ b/tests/tests/test_taptree.py @@ -2,6 +2,8 @@ from embit.descriptor import Descriptor from embit.networks import NETWORKS from embit.descriptor.errors import MiniscriptError +from embit.descriptor.miniscript import Miniscript +from embit.descriptor.taptree import TapLeaf NETWORK = NETWORKS["test"] @@ -144,3 +146,36 @@ 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, + # multi is for wsh, multi_a for tapscript + "multi(1,%s,%s)" % (a, b), + # 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.subTest(tree=tree): + with self.assertRaises(MiniscriptError): + Descriptor.from_string("tr(%s,%s)" % (root, tree)) + # 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: + with self.subTest(leaf=leaf): + desc = Descriptor.from_string("tr(%s,%s)" % (root, leaf)) + self.assertEqual(str(desc), "tr(%s,%s)" % (root, leaf)) From 879922eb36095b2a570ae513053e93147aff363b Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Fri, 13 Nov 2026 01:34:35 +0100 Subject: [PATCH 2/5] descriptor: raise DescriptorError for a non-B tapscript leaf Descriptor.__init__ raises DescriptorError when a wsh script is not of type B. Raise the same error for a tapscript leaf. --- src/embit/descriptor/taptree.py | 4 ++-- tests/tests/test_taptree.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/embit/descriptor/taptree.py b/src/embit/descriptor/taptree.py index 47b9589b..c110cd06 100644 --- a/src/embit/descriptor/taptree.py +++ b/src/embit/descriptor/taptree.py @@ -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 @@ -11,7 +11,7 @@ def __init__(self, miniscript=None, version=0xC0): # will raise if can't verify miniscript.verify() if miniscript.type != "B": - raise MiniscriptError("Top level miniscript should be 'B'") + raise DescriptorError("Top level miniscript should be 'B'") self.miniscript = miniscript self.version = version diff --git a/tests/tests/test_taptree.py b/tests/tests/test_taptree.py index 7a95bfbd..b245fb76 100644 --- a/tests/tests/test_taptree.py +++ b/tests/tests/test_taptree.py @@ -1,7 +1,7 @@ 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 @@ -161,8 +161,14 @@ def test_leaf_miniscript_is_type_checked(self): for leaf in invalid: for tree in (leaf, "{%s,pk(%s)}" % (leaf, b), "{pk(%s),%s}" % (b, leaf)): with self.subTest(tree=tree): - with self.assertRaises(MiniscriptError): + 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.subTest(desc=desc): + 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)) From 158e82c6c63b4a736a088060d381eb8d241eb27d Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Fri, 13 Nov 2026 01:34:51 +0100 Subject: [PATCH 3/5] tests: drop the multi case from the tapscript leaf check Multi.__init__ already rejects multi in tr(), before the leaf is type-checked, and test_multi_variants covers it. Keep only the cases the leaf check catches. --- tests/tests/test_taptree.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/tests/test_taptree.py b/tests/tests/test_taptree.py index b245fb76..5d6d511e 100644 --- a/tests/tests/test_taptree.py +++ b/tests/tests/test_taptree.py @@ -153,8 +153,6 @@ def test_leaf_miniscript_is_type_checked(self): invalid = [ # and_v needs a V as its first argument "and_v(pk(%s),1)" % a, - # multi is for wsh, multi_a for tapscript - "multi(1,%s,%s)" % (a, b), # a leaf must be a B "v:pk(%s)" % a, ] From 6255583e009d308b8b739e706bd2b93b9a502a15 Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Fri, 13 Nov 2026 01:35:05 +0100 Subject: [PATCH 4/5] Record the tapscript leaf type check in the changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b11788d..baeaeced 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 508c05b03027ca8ac111401f951a19b1ff09126a Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Fri, 13 Nov 2026 14:04:34 +0100 Subject: [PATCH 5/5] tests: drop subTest from the tapscript leaf check The tests/unittest.py shim, which run_tests.py uses, has no subTest. Use plain loops instead. --- tests/tests/test_taptree.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/tests/test_taptree.py b/tests/tests/test_taptree.py index 5d6d511e..773a762a 100644 --- a/tests/tests/test_taptree.py +++ b/tests/tests/test_taptree.py @@ -158,15 +158,13 @@ def test_leaf_miniscript_is_type_checked(self): ] for leaf in invalid: for tree in (leaf, "{%s,pk(%s)}" % (leaf, b), "{pk(%s),%s}" % (b, leaf)): - with self.subTest(tree=tree): - with self.assertRaises(DescriptorError): - Descriptor.from_string("tr(%s,%s)" % (root, tree)) + 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.subTest(desc=desc): - with self.assertRaises(DescriptorError) as exc: - Descriptor.from_string(desc) - self.assertIs(type(exc.exception), DescriptorError) + 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)) @@ -180,6 +178,5 @@ def test_leaf_miniscript_is_type_checked(self): "and_n(pk(%s),pk(%s))" % (a, b), ] for leaf in valid: - with self.subTest(leaf=leaf): - desc = Descriptor.from_string("tr(%s,%s)" % (root, leaf)) - self.assertEqual(str(desc), "tr(%s,%s)" % (root, leaf)) + desc = Descriptor.from_string("tr(%s,%s)" % (root, leaf)) + self.assertEqual(str(desc), "tr(%s,%s)" % (root, leaf))