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:
@@ -16,6 +16,7 @@
|
||||
#include "../windows/console_tab.h"
|
||||
#include "../../util/i18n.h"
|
||||
#include "../../util/platform.h"
|
||||
#include "../../util/seed_phrase.h"
|
||||
#include "../../resources/embedded_resources.h"
|
||||
#include <ctime>
|
||||
#include "../../rpc/rpc_client.h"
|
||||
@@ -231,16 +232,12 @@ static void exitLowSpec(bool applyEffects) {
|
||||
s_settingsState.low_spec_snapshot.valid = false;
|
||||
}
|
||||
|
||||
// Count whitespace-separated words in a (seed) buffer — used to validate/guide restore input.
|
||||
// Count words in a (seed) buffer — used to validate/guide restore input. Normalizes exotic Unicode
|
||||
// whitespace (NBSP etc.) first so the count matches the phrase actually submitted (shared with the
|
||||
// first-run restore gate via util::seed_phrase).
|
||||
static int liteSeedWordCount(const char* s) {
|
||||
int words = 0;
|
||||
bool inWord = false;
|
||||
for (; s && *s; ++s) {
|
||||
const bool space = std::isspace(static_cast<unsigned char>(*s)) != 0;
|
||||
if (space) inWord = false;
|
||||
else if (!inWord) { inWord = true; ++words; }
|
||||
}
|
||||
return words;
|
||||
return dragonx::util::seedPhraseWordCount(
|
||||
dragonx::util::normalizeSeedPhrase(s ? std::string(s) : std::string()));
|
||||
}
|
||||
|
||||
static wallet::LiteWalletLifecycleOperation liteLifecycleOperationFromPageState() {
|
||||
@@ -277,7 +274,9 @@ static void evaluateLiteLifecycleRequestFromPageState(App* app) {
|
||||
break;
|
||||
case wallet::LiteWalletLifecycleOperation::RestoreFromSeed:
|
||||
input.request.restoreRequest.walletPath = s_settingsState.lite_wallet_path;
|
||||
input.request.restoreRequest.seedPhrase = s_settingsState.lite_restore_seed;
|
||||
// Normalize (fold NBSP/exotic whitespace to plain spaces) so an NBSP-pasted phrase the
|
||||
// gate counted as 24 words also restores correctly at the backend.
|
||||
input.request.restoreRequest.seedPhrase = dragonx::util::normalizeSeedPhrase(s_settingsState.lite_restore_seed);
|
||||
input.request.restoreRequest.passphrase = s_settingsState.lite_lifecycle_passphrase;
|
||||
input.request.restoreRequest.birthday = static_cast<unsigned long long>(std::max(0, s_settingsState.lite_restore_birthday));
|
||||
input.request.restoreRequest.account = static_cast<unsigned long long>(std::max(0, s_settingsState.lite_restore_account));
|
||||
@@ -320,7 +319,7 @@ static void evaluateLiteLifecycleRequestFromPageState(App* app) {
|
||||
// entered secret on this return path).
|
||||
if (input.request.operation == wallet::LiteWalletLifecycleOperation::RestoreFromSeed) {
|
||||
const int words = liteSeedWordCount(s_settingsState.lite_restore_seed);
|
||||
if (words != 24) {
|
||||
if (!dragonx::util::isCompleteRecoveryPhrase(words)) {
|
||||
s_settingsState.lite_lifecycle_status =
|
||||
"Enter all 24 seed words to restore (got " + std::to_string(words) + ")";
|
||||
s_settingsState.lite_lifecycle_summary.clear();
|
||||
|
||||
Reference in New Issue
Block a user