feat(adapters): persist WalletConnect sessions across page reloads - #835
Open
woahwhattheheck wants to merge 2 commits into
Open
Conversation
The adapter held the session only in memory, so every reload forced a fresh QR scan. The session is now written to localStorage on construction, restored by WalletConnectAdapter.restore when still valid, and cleared on disconnect. Storage access is guarded throughout: a missing, disabled or throwing store degrades to not persisting rather than breaking a working session.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #774.
The adapter held the WalletConnect session only in memory, so every page
reload dropped it and the user had to scan a fresh QR code.
Changes
src/adapters/walletconnect.ts:the SDK is handed a live session, so that is where the session is written.
WalletConnectAdapter.restore(client, storage?, nowSeconds?)— rebuildsan adapter from the stored session, or returns
nullwhen there is nothingusable.
disconnect()— ends the client session and clears the stored copy.persistWalletConnectSession,loadWalletConnectSession,clearWalletConnectSession, and the exportedWALLETCONNECT_SESSION_KEY, for callers managing the session themselves.expiryis a Unix timestamp in seconds, matching what WalletConnect putson a session object, and is checked on restore.
Details that shape the implementation
expiryis not persisted at all. Without one there isno way to tell a live session from a dead one on the next load, and silently
restoring a dead session is worse for the user than asking for a reconnect —
it fails later, at signing time, instead of immediately.
disconnect()clears storage in afinally. If the relay isunreachable the client's own disconnect rejects; clearing regardless means a
relay error cannot leave a dead session behind to be restored next load. The
rejection still propagates.
cannot be re-parsed on every subsequent load.
localStoragemay be absent (Node),disabled, full, or — in some privacy modes — a throwing getter rather than
simply missing. Persisting degrades to a no-op in all of those cases rather
than breaking an otherwise good session.
Storage is typed structurally (
getItem/setItem/removeItem), so a ReactNative shim or a test stub can be injected without this module depending on
DOM lib types.
Validation
npx vitest run test/walletconnect.session.test.ts— 21/21 passing.Covers round-trip, namespaced key, expiry (including exactly-at-now),
unparseable JSON, structurally wrong records, absent store, a store that
throws on every operation, construction with and without
persist,construction without
expiry, restore hit/expired/empty, restore leavingstorage untouched, disconnect clearing on success and on relay failure, and
a client exposing no
disconnect.localStorageand the sign clientis a stub — no network, no DOM.
npx tsc --noEmitreports 210 errors on this branch and 210 on unmodifiedmain— identical, with none insrc/adapters/walletconnect.ts.