Skip to content

Conversation

@brunocapelao
Copy link
Contributor

Summary

Fix a bug in the hex_or_base64() function where digits (0-9) were not recognized as valid lowercase hex characters.

Problem

The original code used is_ascii_lowercase() to check for lowercase hex characters:

s.bytes().all(|b| b.is_ascii_hexdigit() && b.is_ascii_lowercase())

However, is_ascii_lowercase() returns false for digits 0-9, causing valid lowercase hex strings like "0a1b2c3d" to be incorrectly parsed as base64 instead of hex.

Solution

Use explicit pattern matching for valid lowercase hex characters (0-9, a-f):

s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))

Testing

This bug was discovered while using hal-simplicity for Simplicity transactions on Liquid Testnet. After the fix, hex strings containing digits are correctly parsed.

The is_ascii_lowercase() method returns false for digits (0-9), causing
valid lowercase hex strings like '0a1b2c3d' to be incorrectly parsed as
base64 instead of hex.

This fix uses explicit pattern matching for valid lowercase hex
characters (0-9, a-f) to correctly identify hex-encoded strings.
@apoelstra
Copy link
Contributor

Neat! Good find. I had to read this twice to see the problem with my original logic. An alternate solution BTW would've been to change is_ascii_lowercase to !is_ascii_uppercase.

Copy link
Contributor

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK f7ef6b3; successfully ran local tests

apoelstra

This comment was marked as duplicate.

apoelstra

This comment was marked as duplicate.

@apoelstra apoelstra merged commit a9475a2 into BlockstreamResearch:master Jan 19, 2026
11 checks passed
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