From af4810e2ec8e454e4bb616c685e907eaee04c874 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 10 Sep 2026 07:18:09 -0700 Subject: Ark state moves to IndexedDB: raw bytes on disk, memory-fronted, write-behind. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit localStorage held a deep wallet's ark state as ~700KB of JSON (UTF-16 on disk), parsed on every boot and stringified on every save, and the wallet cache blob carried a second ~600KB copy. src/ark/store.js keeps the state in IndexedDB (db coinos-ark) with each coin's signed bytes as a Uint8Array, fronted by an in-memory map so loadArkState/saveArkState stay synchronous: reads return clones, writes coalesce (200ms) and flush immediately when the page hides. Existing localStorage copies migrate on first open (newest _rev wins) and are removed only after the IndexedDB write completed; no IndexedDB means localStorage exactly as before. Records keep their btc-wallet-cache::ark… keys, and a wallet wipe clears both stores via the new per-feature wipeCache hook. The ark cache extension is now syncOnly: it still rides the sync snapshot (and the Blossom blob) but the local wallet cache no longer embeds it — measured on a live wallet the cache blob went from 655KB to 10KB. Verified in the live tab: all wallets migrated, bytes raw on disk, balance and coin set unchanged, localStorage ark keys gone. tools/ark-store-test.js covers migration by _rev, clone-on-read, write-behind, wipe and fallback. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01Nw1Dijik1tLVKRM1WkU7xE --- src/wallet.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/wallet.js') diff --git a/src/wallet.js b/src/wallet.js index cce0a45..ae9ac1b 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -1809,7 +1809,9 @@ export class Wallet { // The serializable wallet state, shared by the localStorage cache and the // Nostr sync. savedAt lets us pick the newest copy across devices. - _snapshot() { + // forSync: include extensions that keep their own local store (syncOnly) + // and only ride the snapshot to reach other devices. + _snapshot(forSync = false) { return { v: 1, savedAt: Date.now(), @@ -1821,7 +1823,7 @@ export class Wallet { nextReceiveIndex: this.nextReceiveIndex, nextChangeIndex: this.nextChangeIndex, feeRates: this.feeRates, - ...Object.assign({}, ...this._cacheExtensions.map((e) => { try { return e.save(); } catch { return {}; } })), + ...Object.assign({}, ...this._cacheExtensions.filter((e) => forSync || !e.syncOnly).map((e) => { try { return e.save(); } catch { return {}; } })), }; } @@ -1861,7 +1863,10 @@ export class Wallet { const xk = this._xpubCacheKey(); if (xk !== this._cacheKey()) localStorage.setItem(xk, JSON.stringify(snap)); // watch-only mirror } catch {} - for (const fn of this._cacheSavedHooks) { try { fn(snap); } catch {} } + // the sync hooks see the full picture (same savedAt, so echoes still match) + const syncOnly = this._cacheExtensions.filter((e) => e.syncOnly); + const forSync = syncOnly.length ? Object.assign(snap, ...syncOnly.map((e) => { try { return e.save(); } catch { return {}; } })) : snap; + for (const fn of this._cacheSavedHooks) { try { fn(forSync); } catch {} } } restoreCache() { -- cgit v1.2.3