From 50cbc19a500e2b324cc144f85b4c8bbdfc03a567 Mon Sep 17 00:00:00 2001 From: Ben Westgate Date: Thu, 22 Oct 2026 04:16:07 +0000 Subject: [PATCH] cli: Remove duplicated messages Drop checks whose messages an earlier check already gives or makes unreachable: create's ms-only and --existing checks, the wallet commands' MasterSeed check, and create's HeaderCollision suffix, which repeated the library message. Ask the blank-wallet question in one place instead of three. Refs #156 Claude-Session: https://claude.ai/code/session_01Q1TNm1YeYW8K9W2eBvxmCD --- src/codex32/_bitcoin_core.py | 18 ++++++------------ src/codex32/cli.py | 17 +++++------------ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/codex32/_bitcoin_core.py b/src/codex32/_bitcoin_core.py index 606594f6..e3c5be51 100644 --- a/src/codex32/_bitcoin_core.py +++ b/src/codex32/_bitcoin_core.py @@ -312,11 +312,11 @@ def _select( ask: Callable[[str], str], tell: Callable[[str], None], ) -> str: + def use(name: str) -> bool: + return ask(f"Use blank wallet {json.dumps(name)}? [y/N]").lower() in ("y", "yes") + choices = tuple(name for name in sorted(self._names()) if self._target(name) is not None) - if len(choices) == 1 and ask(f"Use blank wallet {json.dumps(choices[0])}? [y/N]").lower() in ( - "y", - "yes", - ): + if len(choices) == 1 and use(choices[0]): return choices[0] while True: if choices: @@ -327,10 +327,7 @@ def _select( answer = ask("Choose a wallet number") if answer.isdecimal() and 1 <= int(answer) <= len(choices): name = choices[int(answer) - 1] - if ask(f"Use blank wallet {json.dumps(name)}? [y/N]").lower() in ( - "y", - "yes", - ): + if use(name): return name continue if answer != str(len(choices) + 1): @@ -350,10 +347,7 @@ def _select( if appeared: break choices = tuple(name for name in sorted(names) if self._target(name) is not None) - if len(appeared) == 1 and ask(f"Use blank wallet {json.dumps(appeared[0])}? [y/N]").lower() in ( - "y", - "yes", - ): + if len(appeared) == 1 and use(appeared[0]): return appeared[0] def initialize( diff --git a/src/codex32/cli.py b/src/codex32/cli.py index 2c7c7782..89789058 100644 --- a/src/codex32/cli.py +++ b/src/codex32/cli.py @@ -50,7 +50,7 @@ _residue_low_discrimination, correct_worksheet_residue, ) -from codex32.errors import CodexError, HeaderCollision, InvalidCorrectionInput +from codex32.errors import CodexError, InvalidCorrectionInput from codex32.generation import ( ConfirmationResult, CreationCeremony, @@ -108,12 +108,11 @@ def _secret(artifacts: list[Artifact]) -> Secret: def _master_seed(fingerprint: Callable[[MasterSeed], bytes] | None = None) -> MasterSeed: - if isinstance( - value := _secret(_artifacts(profiles=(Profile.MS,), initial_prefix="MS1", fingerprint=fingerprint)), + # Only ms input is accepted, and an ms secret is always a MasterSeed. + return cast( MasterSeed, - ): - return value - raise _UsageError("Wallet commands accept only Bitcoin master-seed secrets.") + _secret(_artifacts(profiles=(Profile.MS,), initial_prefix="MS1", fingerprint=fingerprint)), + ) def _summary(artifact: Artifact, *, valid: bool = False) -> list[str]: @@ -470,8 +469,6 @@ def _create( ) -> int: profile, selected_threshold, identifier = _creation_header(header) threshold = 0 if selected_threshold is None else selected_threshold - if profile is not Profile.MS: - raise _UsageError("Only Bitcoin master-seed backups can be created.") if shares is not None and indices is not None: raise _UsageError("Choose either --shares or --indices, not both.") if byte_length is not None and existing: @@ -485,8 +482,6 @@ def _create( raise _UsageError("For thresholds 4 through 9, choose --shares or --indices.") core = _connected_core() source = _creation_source(profile) if existing else None - if not existing and not sys.stdin.isatty() and _text("", optional=True): - raise _UsageError("Use --existing when supplying a seed or secret.") if isinstance(source, (Share, Secret)) and not isinstance(source, MasterSeed): raise _UsageError(f"Enter one {_profile_rules(profile).label}, not a share or another backup type.") try: @@ -534,8 +529,6 @@ def _create( share_count=shares, indices=indices, ) - except HeaderCollision as error: - raise _CommandError(f"{error}; choose another set header") from error except CodexError as error: raise _CommandError(str(error)) from error output_count = shares if shares is not None else len(indices or "")