Keychain recovery: orphaned non-settings data is scanned but unreachable; restoreSettings is superseded
Found while reviewing #4694, which adds the first test file for utils/KeychainRecoveryUtils.ts. Three related loose ends, none of them introduced by that PR.
1. Orphaned non-settings data is scanned and then discarded
scanForRecoverableData walks the 20 keys in OTHER_KEYS (contacts, notes, POS categories and products, lightning address hashes and activation flag, unit preferences, hidden accounts, currency codes, activity filters, backup status, LSP orders, swaps, reverse swaps, swap rescue data) across all five sources, filters out anything current storage can already read, and returns the remainder as otherDataFound.
Nothing reads it. views/Tools/NodeConfigExportImport.tsx uses scanResult.settingsFound only (line 1134); otherDataFound appears nowhere in the tree. restoreDataKey (KeychainRecoveryUtils.ts:351), the function that would write one of those entries back, has no callers either.
So on a device where the desync migration stranded data, the scan establishes that the user's contacts and notes are sitting in the legacy keychain, logs a count, and drops the result on the floor with no way to act on it. This is the half worth building rather than deleting: the scan work is already done and the write path already exists.
2. restoreSettings looks superseded, not merely unwired
restoreSettings (KeychainRecoveryUtils.ts:287) also has no callers, but its semantics are the reason. It overwrites STORAGE_KEY wholesale with the recovered blob. The live path does something safer: handleRecoveryPreview shows the recovered nodes, the user ticks the ones they want, and handleRecoveryRestore appends just those through saveNodeConfigs (NodeConfigExportImport.tsx:1017), the same call the import flow uses. That cannot clobber wallets that are currently working; a whole-blob overwrite can.
If we agree the append-selected behaviour is the one we want, restoreSettings should be deleted rather than wired up.
3. Both verifications are truthiness-only
Whichever way the above goes, note that the post-write check in restoreSettings and restoreDataKey is:
const verifyData = await Storage.getItem(key);
if (!verifyData) return { success: false, error: 'Verification failed after write' };
It never compares verifyData against what was written, so a stale value left behind by a failed write passes verification and the caller is told the data is safe. If either function survives, compare against the payload.
Note for whoever picks this up
#4694 adds utils/KeychainRecoveryUtils.test.ts, six of whose tests cover restoreSettings (valid payload, malformed JSON, JSON null, bare number, rejected write, unreadable after write). Deleting the function means deleting those tests; wiring it up means the coverage is already in place.
I'd like to take this, if it's not spoken for.
Part 3 I can do straight away and it's correct whichever way parts 1 and 2 go. Both sites read back and then only check truthiness:
const verifyData = await Storage.getItem(STORAGE_KEY);
if (!verifyData) { ... 'Verification failed after write' }
Comparing against the payload instead of !verifyData also closes a second gap: Storage.getItem
resolves false for a missing key rather than throwing, so the current check cannot
distinguish "nothing there" from "something stale there" — both pass as long as the stale
value is non-empty.
Part 1 is the one with user-visible consequences and I'd rather agree the shape before
writing it. restoreDataKey already exists and works; what's missing is a caller. As I
read it the smallest honest version is: surface otherDataFound in
NodeConfigExportImport beside the existing settingsFound list, grouped by the
description already attached to each result, with per-entry restore calling
restoreDataKey. No new recovery logic — the scan has been finding this data all along.
Two things I'd want your call on before starting:
Does restoring a non-settings key need the same care as settings? Some of those 20 are
harmless preferences (UNIT_KEY, ACTIVITY_FILTERS_KEY), but SWAPS_RESCUE_KEY and
LSPS_ORDERS_KEY are not, and restoring a stale copy over a live one could be worse than
leaving it orphaned. The scan already filters out anything current storage can read, so
the offered entries are genuinely orphaned — but "orphaned" and "safe to write" aren't the
same claim.
Part 2 — delete or wire? You lean towards superseded. If it goes, six of my tests in
KeychainRecoveryUtils.test.ts go with it and that's fine; I'd rather delete them than
keep tests pinning a function nothing calls. If it stays, the verification fix applies to
it too.
My usual limitation, stated up front: I can't run the app — no device or simulator here —
so anything touching NodeConfigExportImport would be unit-tested and read carefully, and
would need someone to actually click through the recovery screen before it merits trust.
If that makes part 1 a bad fit for me, I'm happy to do part 3 alone and leave the UI to
someone who can see it.
Taking this. Thanks for the write-up — the three parts are separable and I'd like to do them in a different order than they're numbered, for one reason I should be straight about first.
The constraint
Part 1 ends in a modal: a new section in renderRecoveryModal, items rendered like renderRecoveryItem, a restore action per item. I cannot test that. No device, no simulator — Windows machine. You already said as much on #4712 ("that PR carries the device testing"), and I'm not going to hand you a React Native view I have only read.
What I can do is make sure the part that can be wrong silently isn't in the view.
Something the UI cannot decide on its own
scanKey returns one RecoveryResult per source, and scanForRecoverableData pushes all of them:
const orphanedResults = keyResults.filter((r) => r.source !== 'current');
result.otherDataFound.push(...orphanedResults);
So otherDataFound can hold up to four entries for a single key — contacts from unprefixed-local and encrypted-storage, with different payloads, from different points in time. A modal that lists every entry and calls restoreDataKey on each selected one writes that key several times and the last array element wins, silently.
There's already precedence reasoning in scanKey for one case:
The prefixed-cloud copy is only meaningful when the current read misses ... offering the stale synchronizable copy as a restore source would invite overwriting live data with it.
That logic stops at prefixed-cloud. The same question — which copy of contacts is the one you want — is unanswered for the rest, and it is not a view's job to answer it.
Proposed split
1a — KeychainRecoveryUtils, unit-testable, I can start now. Group otherDataFound by key, apply an explicit source precedence, and expose a restore entry point that takes the user's selection and returns a per-key result. Tests cover the duplicate-key case, precedence, partial failure (one key writes, another doesn't), and the empty selection. No view involved, so jest is real evidence rather than a claim.
1b — the modal. Small once 1a exists, because it becomes "render this list, call this function". Needs a device. Either you take it, or I write it and it does not merge until someone has run it — your call, and I'd rather you said which.
2 — delete restoreSettings. I agree with your reading: handleRecoveryRestore → saveNodeConfigs appends only the ticked nodes and cannot clobber a working wallet; a whole-blob overwrite of STORAGE_KEY can. But you framed it as "if we agree", so I'll treat it as your call rather than assume. Say yes and it goes with its six tests in KeychainRecoveryUtils.test.ts in one PR.
3 — already done. #4712 changes both verifications to compare against the payload, and after your review also pins the read-back key in both success tests. That PR is green and waiting on you.
One question that gates 1a
What should the precedence be when the same key exists in more than one legacy source? My instinct is encrypted-storage > unprefixed-local > unprefixed-cloud > prefixed-cloud, on the theory that the encrypted store was the newest writer before the migration and the cloud copies are the most likely to be stale — but that is inference from reading getStorageSources, not knowledge of the migration history, and you know which of these actually held live data on real devices.
If you'd rather not pick, the alternative is to show the user the source and the size of each copy and let them choose, which is more honest but a bigger modal. I lean to precedence-with-an-override.
Happy to start on 1a as soon as you answer that, or to start with 2 if you'd rather clear the dead code first.
Requirement for part 1b, from the second review of #4774.
#4774 now skips a key in scanForRecoverableData whenever current storage can read it, so restoreOtherData never overwrites live data. That rule also hides keys the app rewrites after losing them. In the stuck-desync case (current read misses, data only in prefixed-cloud):
SWAPS_RESCUE_KEY:checkAndShowModalinviews/Swaps/index.tsxasks the user to create a new rescue key when the read misses. Once they do, the old mnemonic drops out of the scan.derivePreimageFromRescueKeyneeds that old mnemonic for pending reverse swaps.swaps(holds arefundPrivateKeyper swap),reverse-swaps, contacts and LSPS orders behave the same way after the first new write.
1b should show legacy copies whose data differs from the live value, for the user to review, and never restore them automatically. The scan could return them in a separate list or flag each result. The rescue key and swap lists matter most because they can hold the only key for funds in a pending swap.