bugfix: show segwit v1+ outputs of any program length as addresses
What changed, and why it matters
This commit fixes a display bug in the COLDCARD hardware wallet. Previously, newer-style Bitcoin addresses (SegWit version 1 and above) were only shown as readable bech32m addresses if they were exactly 32 bytes long. For any other allowed length, the wallet displayed the raw script data instead of a normal address. The fix makes the wallet correctly display all allowed lengths as addresses, which helps users verify where their coins are going.
Review whether the previous behavior could have caused users to approve transactions without proper address verification. Consider whether any downstream code or UI assumes 32-byte Taproot outputs and needs similar updates. No urgent security patch appears required, but the fix should be included in the next release.
Security signals we found
UI display bug in address rendering
Incorrect handling of valid BIP-141 witness program lengths
Potential user confusion when verifying transaction outputs
No memory corruption or cryptographic weakness evident
Evidence from the diff
The change in shared/chains.py updates render_address() to accept SegWit v1+ witness programs of any BIP-141 allowed length (2 to 40 bytes). The previous check required ll == 34 and script[1] == 0x20. The new check accepts 4 <= ll <= 42 and validates script[1] == ll - 2, matching the push-byte length. It then encodes the address via ngu.codecs.segwit_encode() using the witness version and program. Tests are added covering valid lengths, invalid lengths, and malformed scripts.
Changed components
shared/chains.py:render_address()Bitcoin address display for SegWit v1+ outputsPSBT output verification UIInspect captured patch +24 / −1
### releases/Next-ChangeLog.md
@@ -10,6 +10,9 @@ your addition and anything else already in this file.**
- Change: Restrict the Codex32 checksum calculator to MS1 shares and warn that
completing a checksum locks in transcription errors.
+- Bugfix: Show outputs to segwit v1+ witness programs other than 32 bytes long
+ as their bech32m address, rather than as a raw script.
+
# Mk Specific Changes
### shared/chains.py
@@ -258,7 +258,8 @@ def render_address(cls, script):
return ngu.codecs.segwit_encode(cls.bech32_hrp, script[0], script[2:])
# segwit v1 (P2TR) and later segwit version
- if ll == 34 and (OP_1 <= script[0] <= OP_16) and script[1] == 0x20:
+ # - BIP-141 allows a 2 to 40 byte program for any of them
+ if 4 <= ll <= 42 and (OP_1 <= script[0] <= OP_16) and script[1] == ll - 2:
return ngu.codecs.segwit_encode(cls.bech32_hrp, script[0] - 80, script[2:])
raise ValueError('Unknown payment script', repr(script))
### testing/test_unit.py
@@ -292,6 +292,25 @@ def test_op_return_decode(script_hex, expect, sim_exec):
assert rv == repr(expect)
+@pytest.mark.parametrize('script_hex, ok', [
+ ('5120' + '11'*32, True), # v1, 32 bytes (P2TR)
+ ('5210' + '22'*16, True), # v2, 16 bytes
+ ('6002' + '3333', True), # v16, 2 bytes
+ ('5128' + '44'*40, True), # v1, 40 bytes
+ ('5101' + '55', False), # program too short
+ ('5129' + '66'*41, False), # program too long
+ ('5220' + '77'*31, False), # push longer than the script
+])
+def test_render_future_segwit(script_hex, ok, sim_exec):
+ import bech32
+ spk = bytes.fromhex(script_hex)
+ cmd = ('from chains import BitcoinMain; from ubinascii import unhexlify\n'
+ 'try: RV.write(BitcoinMain.render_address(unhexlify(%r)))\n'
+ 'except ValueError: RV.write("ValueError")' % script_hex)
+ rv = sim_exec(cmd)
+ assert rv == (bech32.encode('bc', spk[0] - 0x50, spk[2:]) if ok else 'ValueError')
+
+
@pytest.mark.parametrize('patterns, paths, answers', [
(["m"], ("m", "m/2", "*", "any"), [True, False, False, False]),
(["any"], ("m", "m/2", "*", "1/2/3/4/5/6h/55h"), [True]*4),Why this scored 49/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.