Skip to content

refactor(keys): let deriving be the mnemonic validation, and parse once - #387

Merged
grunch merged 1 commit into
mainfrom
refactor/mnemonic-single-parse
Sep 3, 2026
Merged

refactor(keys): let deriving be the mnemonic validation, and parse once#387
grunch merged 1 commit into
mainfrom
refactor/mnemonic-single-parse

Conversation

@grunch

@grunch grunch commented Sep 3, 2026

Copy link
Copy Markdown
Member

Follow-up to a review observation on #235: the mnemonic import path parses the same phrase twice.

The duplication

load_identity_from_mnemonic — behind the account screen's import user button — did:

key_ops::validate_mnemonic(&words)?;
let keys = key_ops::derive_master_key(&words)?;

Both call Mnemonic::parse on the same phrase, and both fail with the same invalid mnemonic: …. The second parse is not a safety net over the first; it is the first, run again. What it does create is a second place to answer "what counts as a valid mnemonic here", which is the kind of pair that drifts: change one of them and the two disagree, silently, on the one input where being wrong costs the user their account.

The change

  • The explicit call goes away. Deriving is the validation — it has to parse to derive — and the error the caller sees is byte-identical.
  • validate_mnemonic goes with its only caller. It was pub but not on the bridge and had no other user anywhere in the repo (checked across every remote branch, where the single hit is this same line). Three lines to bring back the day something needs validation without derivation.
  • derive_master_key's doc now says it validates, so the next person does not add the pair back.

Why the test

Removing an explicit check is only safe if the implicit one holds, so deriving_refuses_a_phrase_that_is_not_a_mnemonic pins it: a phrase of real BIP-39 words with a wrong checksum, a phrase with a word that is not in the list, and an empty list (an nsec-imported identity stores no words). Both kinds do reach Rust — the import dialog only checks the shape, 12 or 24 alphabetic words — so this is the layer that has to say no.

Without it, the failure mode is not a crash: a typo'd word whose checksum happens to pass would derive some other valid identity, and the user would be looking at an empty account rather than an error.

Test plan

  • cargo test — 366 pass
  • cargo clippy --all-targets — clean
  • cargo check --target wasm32-unknown-unknown
  • flutter analyze — no new issues
  • ./scripts/frb-generate.sh produces no diff (neither function is on the bridge)
  • Manual: Account → import user, with a valid phrase (imports), a wrong-checksum phrase and a nonsense word (both rejected with the same message as before)

🤖 Generated with Claude Code

https://claude.ai/code/session_012VCrukiZdZo2HfZczSaQ8U

`load_identity_from_mnemonic` — what the account screen's "import user" button
runs — called `validate_mnemonic` and then `derive_master_key`, and both parse
the same phrase with `Mnemonic::parse`. The second parse is not a safety net:
it is the same check, so the pair only creates a second place to answer "what
counts as a valid mnemonic", and a future edit to one of them would leave the
two disagreeing.

Deriving already rejects a bad word or a bad checksum with the very same
`invalid mnemonic: …` error, so the import path keeps exactly the behaviour it
had, and `validate_mnemonic` goes with its only caller rather than staying as a
public helper nothing calls. The Dart dialog only checks the shape (12 or 24
alphabetic words), so both kinds of bad phrase do reach this code — pinned in
`deriving_refuses_a_phrase_that_is_not_a_mnemonic`, because the whole point of
removing the explicit check is that the implicit one has to hold.

Verified: `cargo test` (366), `cargo clippy`, `cargo check --target
wasm32-unknown-unknown`, `flutter analyze`. The bridge surface is unchanged —
neither function was on it, and `frb-generate.sh` produces no diff.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012VCrukiZdZo2HfZczSaQ8U
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 8e743b82-0840-412d-92d2-759b2e2b32ee

📥 Commits

Reviewing files that changed from the base of the PR and between 398390a and 93abf4d.

📒 Files selected for processing (2)
  • rust/src/api/identity.rs
  • rust/src/crypto/keys.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-03T19:18:38.438708Z 93abf4d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@grunch
grunch merged commit a9470a4 into main Sep 3, 2026
4 checks passed
@grunch
grunch deleted the refactor/mnemonic-single-parse branch September 3, 2026 19:24
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.

1 participant