Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] DecodeQR: handle Compact SeedQR byte data that can be decoded to str #657

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/seedsigner/models/decode_qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ def detect_segment_type(s, wordlist_language_code=None):
pass

# Is it byte data?
if not isinstance(s, bytes):
try:
# TODO: remove this check & conversion once above cast to str is removed
s = s.encode()
except UnicodeError:
# Couldn't convert back to bytes; shouldn't happen
raise Exception("Conversion to bytes failed")

# 32 bytes for 24-word CompactSeedQR; 16 bytes for 12-word CompactSeedQR
if len(s) == 32 or len(s) == 16:
try:
Expand Down
26 changes: 25 additions & 1 deletion tests/test_seedqr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from seedsigner.models.decode_qr import DecodeQR, DecodeQRStatus
from seedsigner.models.encode_qr import SeedQrEncoder, CompactSeedQrEncoder
from seedsigner.models.qr_type import QRType
from seedsigner.models.seed import Seed



Expand Down Expand Up @@ -104,3 +103,28 @@ def test_compact_seedqr_handles_null_bytes():
# 12-word seed, multiple null bytes in a row
entropy = os.urandom(10) + b'\x00\x00' + os.urandom(4)
run_encode_decode_test(entropy, mnemonic_length=12, qr_type=QRType.SEED__COMPACTSEEDQR)


def test_compact_seedqr_bytes_interpretable_as_str():
"""
Should successfully decode a Compact SeedQR whose bytes can be interpreted as a valid
string. Most Compact SeedQR byte data will raise a UnicodeDecodeError when attempting to
interpret it as a string, but edge cases are possible.

see: Issue #656
"""
# Randomly generated to pass the str.decode() step; 12- and 24-word entropy.
entropy_bytes_tests = [
b'\x00' * 16, # abandon * 11 + about
b'\x12\x15\\1j`3\x0bkL}f\x00ZYK',
b'tv\x1bZjmqN@t\x13\x1aK\\v)',
b'|9\x05\x1aHF9j\xda\xb6v\x05\x08#\x12=',
b"iHK`4\x1a5\xd3\xaf\xd3\xb47htJ.}<\xea\xbf\x88Xh\x01.?R2^\xc2\xb1'",
b'|Z\x11\x1dt\xdd\x97~t&f &G$H|^[\xd3\x9d<q]z\x14.\x11`!\xd1\x91',
b'0\xd4\xb3\\,\xcd\x8d7c/Rp\x0e\xc2\xbb\xe4\x99\xa3=j5,\xcc\x9a[>\x19Z{\ng^',
]

for entropy_bytes in entropy_bytes_tests:
entropy_bytes.decode() # should not raise an exception
mnemonic_length = 12 if len(entropy_bytes) == 16 else 24
run_encode_decode_test(entropy_bytes, mnemonic_length=mnemonic_length, qr_type=QRType.SEED__COMPACTSEEDQR)
Loading