Skip to content

descriptor: reject sh() scripts over 520 bytes - #168

Open
fametrano wants to merge 2 commits into
diybitcoinhardware:masterfrom
fametrano:sh-redeem-script-size
Open

fametrano wants to merge 2 commits into
diybitcoinhardware:masterfrom
fametrano:sh-redeem-script-size

Conversation

@fametrano

Copy link
Copy Markdown

Descriptor accepts sh(multi(...)) and sh(sortedmulti(...)) with 16 or more compressed keys. The redeem script is then 547 bytes or more, over the 520-byte push limit of the scriptSig, so the output cannot be spent. Bitcoin Core rejects it ("P2SH script is too large"); 15 compressed keys (513 bytes) are still accepted.

This checks the compiled script of any sh() descriptor without wsh, with the same limit. The length comes from compile(), since len() counts every key as 34 bytes and underestimates uncompressed ones. Tests cover multi and sortedmulti at the boundary, with 15 and 16 compressed keys and 7 and 8 uncompressed ones, and check that wsh() and sh(wsh()) still accept the compressed cases. I compared the sh() cases with Bitcoin Core v31.1.0 getdescriptorinfo, before and after.

A P2SH redeem script is pushed in the scriptSig, where a push is limited
to 520 bytes, so an output paying to a longer one cannot be spent.
Bitcoin Core rejects sh(multi()) and sh(sortedmulti()) beyond it: 15
compressed keys (513 bytes) are accepted, 16 (547 bytes) are not.

Check the compiled script of every sh() descriptor without wsh. The
length is taken from compile(), because len() counts every key as 34
bytes and so underestimates uncompressed ones.
@odudex

odudex commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Code looks good at 169caad: sh(multi) and sh(sortedmulti) are rejected from 16 compressed keys (547 bytes), 15 still pass, and wsh() and sh(wsh()) are unaffected. Multipath and xpub sh(multi(...)) descriptors parse and derive the same addresses as before.

One thing to fix before merging: test_sh_script_size won't run under MicroPython. test_descriptor.py is imported by tests/tests/__init__.py, so it is part of micropython ./run_tests.py, and:

  • self.subTest and self.assertLessEqual are not defined in tests/unittest.py (the MicroPython shim), so the test raises AttributeError there.
  • .sec().hex(): the tests in the MicroPython list use hexlify rather than .hex(), which is not available on older MicroPython builds. hexlify(...).decode() is the safe form, and hexlify is already imported in this file.

CI only runs pytest, so this doesn't show up there. A plain loop and assertTrue(len(...) <= 520) would do it.

The tests/unittest.py shim has no subTest or assertLessEqual. Use a
plain loop and assertTrue instead, and hexlify in place of bytes.hex(),
as the rest of the code that runs on MicroPython does.
@fametrano

Copy link
Copy Markdown
Author

Fixed in 1778faa as suggested. run_tests.py on the tests/unittest.py shim now passes under CPython.

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.

2 participants