Skip to content
Merged
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
12 changes: 9 additions & 3 deletions src/seedsigner/helpers/embit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ def parse_derivation_path(derivation_path: str) -> dict:

sections = derivation_path.split("/")

if sections[1] == "48h":
# So far this helper is only meant for single sig message signing
raise Exception("Not implemented")
# The path can come verbatim from an untrusted signmessage QR. A path too short
# to name a purpose and coin type, or a multisig (48h) one -- this helper is
# only meant for single sig -- is reported as an unclean match, which callers
# already turn into "not supported", rather than raised as a System Error.
if len(sections) < 3 or sections[1] == "48h":
return dict(
script_type=None, network=None, is_change=None, index=None,
wallet_derivation_path=None, clean_match=False,
)

lookups = {
"script_types": {
Expand Down
14 changes: 14 additions & 0 deletions src/seedsigner/helpers/ur2/fountain_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
from .fountain_utils import choose_fragments, contains, is_strict_subset, set_difference
from .utils import join_lists, join_bytes, crc32_int, xor_with, take_first

# A part's seq_len comes straight off a scanned QR; the UR parser only rejects values
# below 1 or above 2**64. validate_part() builds a set that size and choose_fragments()
# runs a shuffle quadratic in it, so one frame could exhaust memory or hang a Pi Zero
# for hours. The shuffle is part of the fountain code and can't be changed without
# breaking interoperability, so the count is bounded instead. 10,000 fragments is far
# beyond any real message (100 KB even at a 10-byte fragment). Same bound as upstream
# SeedSigner #970.
MAX_SEQ_LEN = 10000


class InvalidPart(Exception):
pass

Expand Down Expand Up @@ -268,6 +278,10 @@ def process_mixed_part(self, p):
self.mixed_parts[p2.indexes] = p2

def validate_part(self, p):
# Reject implausible part counts before allocating anything sized by them
if p.seq_len < 1 or p.seq_len > MAX_SEQ_LEN:
return False

# If this is the first part we've seen
if self.expected_part_indexes == None:
# Record the things that all the other parts we see will have to match to be valid.
Expand Down
64 changes: 43 additions & 21 deletions src/seedsigner/models/decode_qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ def detect_segment_type(s, wordlist_language_code=None):
return QRType.WALLET__GENERIC

# Seed
if re.search(r'\d{48,96}', s):
# The whole payload, not merely a digit run inside something else (a
# SettingsQR with a long number in it is not a seed).
if re.fullmatch(r'(?:\d{4}){12,24}', s.strip()):
return QRType.SEED__SEEDQR

# Bitcoin Address
Expand Down Expand Up @@ -1218,7 +1220,13 @@ def add(self, segment, qr_type=QRType.SEED__SEEDQR):
if len(self.seed_phrase) > 0:
if not self.has_valid_word_count():
return DecodeQRStatus.INVALID
self.seed_type = "bip39"
# Hand-transcribed SeedQRs are exactly where a wrong digit is
# expected, and the checksum is what catches it. Unchecked, the
# bad mnemonic only failed later, as a System Error.
seed_type = self._classify_mnemonic(self.seed_phrase)
if seed_type is None:
return DecodeQRStatus.INVALID
self.seed_type = seed_type
self.complete = True
self.collected_segments = 1
return DecodeQRStatus.COMPLETE
Expand Down Expand Up @@ -1249,23 +1257,10 @@ def add(self, segment, qr_type=QRType.SEED__SEEDQR):
if not self.has_valid_word_count():
return DecodeQRStatus.INVALID

is_valid_bip39 = False
try:
Seed(seed_phrase_list, passphrase="", wordlist_language_code=self.wordlist_language_code)
is_valid_bip39 = True
except Exception:
is_valid_bip39 = False

is_valid_aezeed = len(seed_phrase_list) == 24 and aezeed_has_valid_checksum(seed_phrase_list, self.word_to_index)

if is_valid_aezeed and is_valid_bip39:
self.seed_type = "ambiguous"
elif is_valid_aezeed:
self.seed_type = "aezeed"
elif is_valid_bip39:
self.seed_type = "bip39"
else:
seed_type = self._classify_mnemonic(seed_phrase_list)
if seed_type is None:
return DecodeQRStatus.INVALID
self.seed_type = seed_type

self.seed_phrase = seed_phrase_list
self.complete = True
Expand Down Expand Up @@ -1312,6 +1307,29 @@ def get_seed_type(self):
return self.seed_type
return None

def _classify_mnemonic(self, words: list) -> str | None:
"""
"bip39", "aezeed", or "ambiguous" (both checksums pass) for a mnemonic whose
checksum holds; None when neither does.
"""
is_valid_bip39 = False
try:
Seed(words, passphrase="", wordlist_language_code=self.wordlist_language_code)
is_valid_bip39 = True
except Exception:
is_valid_bip39 = False

is_valid_aezeed = len(words) == 24 and aezeed_has_valid_checksum(words, self.word_to_index)

if is_valid_aezeed and is_valid_bip39:
return "ambiguous"
elif is_valid_aezeed:
return "aezeed"
elif is_valid_bip39:
return "bip39"
return None


def has_valid_word_count(self):
return len(self.seed_phrase) in (12, 15, 18, 21, 24)

Expand Down Expand Up @@ -1440,10 +1458,14 @@ def add(self, segment, qr_type=QRType.SIGN_MESSAGE):

signmessage {derivation_path} ascii:{message}
"""
parts = segment.split()
# Split off the header only: the message itself may contain spaces or
# another "ascii:". A malformed payload is INVALID, never an IndexError --
# this decoder runs inside the camera loop, where an exception is a crash.
parts = segment.split(maxsplit=2)
if len(parts) < 3 or ":" not in parts[2]:
return DecodeQRStatus.INVALID
self.derivation_path = parts[1].replace("h", "'")
fmt = parts[2].split(":")[0]
self.message = segment.split(f"{fmt}:")[1]
fmt, self.message = parts[2].split(":", 1)

# TODO: support formats other than ascii?
if fmt != "ascii":
Expand Down
Loading
Loading