import type {ClientOptions} from "@pomade/core" import type {Wallet} from "@welshman/util" import {nip01, nip07, nip46, nip55, pomade, toSession} from "@welshman/app" import type {Session} from "@welshman/app" import {app, login, session} from "@app/core" import {wallet} from "@app/lightning" import {kv, ss, storage} from "@app/storage" import {deactivateCurrentPomadeSession} from "@app/pomade" import {Push} from "@app/push" // Sessions used to be a Record keyed by an active pubkey, and carried their // pubkey plus method-specific fields inline. Convert one so upgrading doesn't log people out. type LegacySession = { method: string pubkey: string secret?: string signer?: string email?: string clientOptions?: ClientOptions handler?: {pubkey: string; relays: string[]} wallet?: Wallet } const toCurrentSession = (legacy: LegacySession): Session | undefined => { switch (legacy.method) { case "nip01": return legacy.secret ? toSession(nip01, {secret: legacy.secret}) : undefined case "nip07": return toSession(nip07, {}) case "nip46": return legacy.secret && legacy.handler ? toSession(nip46, { clientSecret: legacy.secret, signerPubkey: legacy.handler.pubkey, relays: legacy.handler.relays, }) : undefined case "nip55": return legacy.signer ? toSession(nip55, {pubkey: legacy.pubkey, signer: legacy.signer}) : undefined case "pomade": return legacy.clientOptions && legacy.email ? toSession(pomade, {clientOptions: legacy.clientOptions, email: legacy.email}) : undefined } } const readLegacySession = async () => { const pubkey = await kv.get("pubkey") const sessions = await ss.get>("sessions") const legacy = pubkey ? sessions?.[pubkey] : undefined if (legacy?.wallet) { wallet.set(legacy.wallet) } return legacy ? toCurrentSession(legacy) : undefined } // The session is derived from the app's user, so it can't be synced to storage directly — // read it back once at startup, then persist it whenever the identity changes. export const restoreSession = async () => { const $session = (await ss.get("session")) ?? (await readLegacySession()) if ($session) { await login($session) } return session.subscribe($session => { if ($session) { ss.set("session", $session) } }) } export const logout = async () => { await deactivateCurrentPomadeSession() await Push.disable() await kv.clear() await ss.clear() await storage.get()?.clear() app.get().cleanup() localStorage.clear() window.location.href = "/" }