Skip to content

descriptor: reject uncompressed keys in tr() (closes #180) - #183

Open
Yudis-bit wants to merge 1 commit into
diybitcoinhardware:masterfrom
Yudis-bit:fix/tr-uncompressed-key
Open

Yudis-bit wants to merge 1 commit into
diybitcoinhardware:masterfrom
Yudis-bit:fix/tr-uncompressed-key

Conversation

@Yudis-bit

Copy link
Copy Markdown

BIP 386 requires all key expressions under tr() to produce x-only public keys. Uncompressed 65-byte public keys under tr(), whether used as the internal key or inside script leaves like pk() and multi_a(), are now refused with DescriptorError.

Closes #180

Copilot AI balanced review requested due to automatic review settings November 13, 2026 17:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1ee84ab

All 15 tr() descriptors I tried that hold an uncompressed key are now refused: hex or WIF, as internal key or inside pk(), pkh(), multi_a(), sortedmulti_a(), and_v() or or_d(). The compressed, x-only and xpub controls still parse. getdescriptorinfo of Bitcoin Core v32.1.0 refuses and accepts exactly the same strings. Each case and the outputs at base, at head and from Core are in https://gist.github.com/fametrano/42b8419bf887fc7d256ad77379245709/c3a475203e560c186e44f82689f0e76e3a5e0fa1.

Comment thread src/embit/descriptor/descriptor.py Outdated
Comment on lines +339 to +341
# BIP 386: tr() only accepts compressed or x-only public keys
if not getattr(key.key, "compressed", True):
raise DescriptorError("Uncompressed keys are not allowed in tr()")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check added to __init__ already refuses these keys, in parsed strings and in objects made with Descriptor(...), so this one and the taptree loop below can go.

Suggested change
# BIP 386: tr() only accepts compressed or x-only public keys
if not getattr(key.key, "compressed", True):
raise DescriptorError("Uncompressed keys are not allowed in tr()")

Comment thread src/embit/descriptor/descriptor.py Outdated
Comment on lines +350 to +354
for k in taptree.keys:
if not getattr(k.key, "compressed", True):
raise DescriptorError(
"Uncompressed keys are not allowed in tr()"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for k in taptree.keys:
if not getattr(k.key, "compressed", True):
raise DescriptorError(
"Uncompressed keys are not allowed in tr()"
)

@Yudis-bit
Yudis-bit force-pushed the fix/tr-uncompressed-key branch from 1ee84ab to 1accfe9 Compare November 15, 2026 05:24
Copilot AI balanced review requested due to automatic review settings November 15, 2026 05:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Yudis-bit

Copy link
Copy Markdown
Author

Dropped the redundant read_from checks and added uncompressed WIF coverage in 1accfe9. Range-diff against 1ee84ab:

1:  1ee84ab ! 1:  1accfe9 descriptor: reject uncompressed keys in tr() (closes #180)
    @@ src/embit/descriptor/descriptor.py: class Descriptor(DescriptorBase):
      
          @property
          def script_len(self):
     
      ## tests/tests/test_descriptor.py ##
     @@ tests/tests/test_descriptor.py: class DescriptorTest(TestCase):
     +            "04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae"
     +            "168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a"
     +        )
    ++        UWIF = "5HpjE2Hs7vjU4SN3YyPQCdhzCu92WoEeuE6PWNuiPyTu3ESGnzn"
     +        C = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
     +        X = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
     +        # BIP 386: uncompressed keys are not allowed in tr()
     +        for d in [
     +            "tr(%s)" % U,
    ++            "tr(%s)" % UWIF,
     +            "tr(%s,pk(%s))" % (C, U),
     +            "tr(%s,multi_a(1,%s))" % (C, U),
     +            "tr(%s,{pk(%s),pk(%s)})" % (C, C, U),
    @@ tests/tests/test_descriptor.py: class DescriptorTest(TestCase):
     +        ]:
     +            self.assertRaises(DescriptorError, Descriptor.from_string, d)
     +
    ++        # direct construction with uncompressed key must also fail
    ++        ukey = Key(ec.PublicKey.parse(bytes.fromhex(U)))
    ++        self.assertRaises(DescriptorError, Descriptor, key=ukey, taproot=True)
    ++
     +        # compressed and x-only keys are valid
     +        for d in [
     +            "tr(%s)" % C,

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK 1accfe9

Thanks. With only the __init__ check left, the probe in my earlier gist gives the same output. The two assertions the test gains, on tr(UWIF) and Descriptor(key=ukey, taproot=True), each fail on master.

Comment thread src/embit/descriptor/descriptor.py Resolved
Comment thread tests/tests/test_descriptor.py Resolved
@jdlcdl

jdlcdl commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

BIP-386, in the section before Test Vectors, clearly states:

Modified Key Expression

Key Expressions within a tr() expression must only create x-only public keys. Uncompressed public keys are not allowed, but compressed public keys would be implicitly converted to x-only public keys.

...and I believe this PR implements and proves it in its tests. (w/ one request to use unhexlify) resolved


I've left a blocking comment about use of bytes.fromhex() which needs to be binascii.unhexlify() to pass tests in older micropythons used by downstream projects. resolved

I have tested this rebased onto "hardened" with conflicts resolved as noted in the changed files (non-blocking comments for repo-maintainer) as well as use of unhexlify() in tests... Adds 1 more passing test for Cpython and micropython.


tACK at 96bc89b below, against "hardened" with 2 conflicts -- each resolved by taking both. Awaiting more ACKs approvals, then a near-term merge to "hardened"

Comment thread tests/tests/test_descriptor.py Resolved
…re#180)

BIP 386 requires all key expressions under tr() to produce x-only
public keys. Uncompressed public keys and uncompressed WIF private
keys under tr()—whether parsed from strings or constructed directly
via Descriptor(..., taproot=True)—are refused with DescriptorError.

Signed-off-by: Yudis-bit <Yudis-bit@proton.me>
@Yudis-bit
Yudis-bit force-pushed the fix/tr-uncompressed-key branch from 1accfe9 to 96bc89b Compare November 16, 2026 14:04
Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Yudis-bit

Copy link
Copy Markdown
Author

Switched to binascii.unhexlify for MicroPython compatibility and added the comment in 96bc89b. Range-diff against 1accfe9:

1:  1accfe9 ! 1:  96bc89b descriptor: reject uncompressed keys in tr() (closes #180)
    @@ src/embit/descriptor/descriptor.py: class Descriptor(DescriptorBase):
              # make sure all keys are either taproot or not
              for k in self.keys:
                  k.taproot = taproot
    ++            # BIP-386: uncompressed keys are not allowed in tr()
     +            if self.taproot and not getattr(k.key, "compressed", True):
     +                raise DescriptorError("Uncompressed keys are not allowed in tr()")
      
    @@ src/embit/descriptor/descriptor.py: class Descriptor(DescriptorBase):
          def script_len(self):
     
      ## tests/tests/test_descriptor.py ##
    +@@
    + from unittest import TestCase
    +-from binascii import hexlify
    ++from binascii import hexlify, unhexlify
    + from embit.descriptor import Descriptor, Key
    + from embit.descriptor.arguments import KeyHash, Number
    + from embit.descriptor.miniscript import OPERATORS, WRAPPERS
     @@ tests/tests/test_descriptor.py: class DescriptorTest(TestCase):
              with self.assertRaises(DescriptorError):
                  Descriptor.from_string(d)
    @@ tests/tests/test_descriptor.py: class DescriptorTest(TestCase):
     +            self.assertRaises(DescriptorError, Descriptor.from_string, d)
     +
     +        # direct construction with uncompressed key must also fail
    -+        ukey = Key(ec.PublicKey.parse(bytes.fromhex(U)))
    ++        ukey = Key(ec.PublicKey.parse(unhexlify(U)))
     +        self.assertRaises(DescriptorError, Descriptor, key=ukey, taproot=True)
     +
     +        # compressed and x-only keys are valid

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Descriptor.from_string() accepts uncompressed keys in tr()

4 participants