fix(lite): require exactly 24 words on first-run restore (crash on valid seed)

The lite first-run restore wizard enabled Restore for {12,15,18,21,24}-word
phrases, but the SDXL backend only accepts 24-word / 32-byte-entropy seeds:
LightWallet::new does copy_from_slice(&phrase.entropy()) into a [u8;32]
(lightwallet.rs:231), which panics on 16/20/24/28-byte entropy. Mnemonic::
from_phrase accepts the shorter valid phrase, and the restore FFI
litelib_initialize_new_from_phrase (lib.rs:127) has no catch_unwind (unlike
litelib_execute), so the panic unwinds across extern "C" -> process abort
(UB on the pinned rustc 1.63). A user restoring a legitimate 12-word seed
from another wallet crashed the app.

The Settings restore gate was already tightened to == 24 (6ff1fda) but the
first-run wizard gate (df14533) was never updated — same restore path, two
verdicts, crash only via the more-common first-run path.

Add shared util/seed_phrase.{h,cpp} as the single source of truth:
- normalizeSeedPhrase: fold NBSP/en/em/ideographic/narrow spaces to ASCII,
  strip zero-width marks, collapse+trim (word bytes untouched)
- seedPhraseWordCount
- isCompleteRecoveryPhrase(int) == 24  (the sole SDXL contract)

Both restore gates now count via the normalizer and gate via
isCompleteRecoveryPhrase, and both submit the normalized phrase. This closes
the crash, reconciles the two gates so they can't drift again, and — because
tiny-bip39 splits on literal ASCII space with no NFKD — makes an NBSP-pasted
24-word seed (common from PDFs/note apps) restore correctly instead of being
undercounted and rejected.

Adds testSeedPhraseHelpers. Suite green (1/1).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-09 01:32:40 -05:00
parent 5b6ba5094b
commit a1d3964e34
6 changed files with 181 additions and 27 deletions

View File

@@ -74,6 +74,7 @@
#include "util/platform.h"
#include "util/text_format.h"
#include "util/payment_uri.h"
#include "util/seed_phrase.h"
#include "util/texture_loader.h"
#include "util/svg_texture.h"
#include "ui/material/colors.h"
@@ -3074,22 +3075,16 @@ void App::renderLiteFirstRunPrompt()
}
ImGui::Spacing(); ImGui::Spacing();
// Trim surrounding whitespace from the entered seed.
std::string seedTrim(restoreSeed);
while (!seedTrim.empty() && std::isspace((unsigned char)seedTrim.front())) seedTrim.erase(seedTrim.begin());
while (!seedTrim.empty() && std::isspace((unsigned char)seedTrim.back())) seedTrim.pop_back();
// Normalize the entered seed (trim, fold exotic Unicode whitespace like NBSP to plain
// spaces) so the word count and the phrase we submit agree regardless of paste source.
std::string seedTrim = util::normalizeSeedPhrase(restoreSeed);
// Require a valid BIP39 word count before enabling Restore — otherwise a truncated or
// garbage phrase (previously any non-empty text passed) is submitted and fails opaquely.
int seedWords = 0;
{ bool inWord = false;
for (char c : seedTrim) {
bool sp = (c == ' ' || c == '\t' || c == '\n' || c == '\r');
if (!sp && !inWord) { seedWords++; inWord = true; }
else if (sp) inWord = false;
} }
bool seedLenOk = (seedWords == 12 || seedWords == 15 || seedWords == 18 ||
seedWords == 21 || seedWords == 24);
// Require a COMPLETE 24-word phrase before enabling Restore. The SDXL backend only
// accepts 24-word / 32-byte-entropy seeds; a shorter valid-BIP39 phrase (12/15/18/21)
// panics it uncaught across the restore FFI, so it must be refused here (matches the
// Settings restore gate — both go through util::isCompleteRecoveryPhrase).
int seedWords = util::seedPhraseWordCount(seedTrim);
bool seedLenOk = util::isCompleteRecoveryPhrase(seedWords);
if (!seedTrim.empty() && !seedLenOk) {
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ColorConvertU32ToFloat4(ui::material::Warning()));
ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + 380.0f);
@@ -3103,7 +3098,7 @@ void App::renderLiteFirstRunPrompt()
ImGui::BeginDisabled(!seedLenOk);
if (ui::material::TactileButton(TR("lite_restore_btn"), ImVec2(btnW, 0))) {
wallet::LiteWalletRestoreRequest req;
req.seedPhrase = seedTrim;
req.seedPhrase = seedTrim; // normalized: NBSP-glued pastes restore correctly
req.birthday = static_cast<unsigned long long>(std::max(0, restoreBirthday));
req.overwrite = lite_wallet_->walletExists(); // replace any existing wallet file
if (lite_wallet_->beginRestoreWalletAsync(std::move(req))) {