From 8393283cf87e2eade62b75499a3f9dffde6948b8 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Mon, 20 Oct 2025 13:21:01 -0400 Subject: [PATCH] Persist NIP-07 permissions between sessions --- js/nostr.js | 18 +++++++++++++++++- tests/watch-history.test.mjs | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/js/nostr.js b/js/nostr.js index 04514e72..6e46abce 100644 --- a/js/nostr.js +++ b/js/nostr.js @@ -3175,7 +3175,23 @@ export class NostrClient { this.watchHistoryLastCreatedAt = 0; this.countRequestCounter = 0; this.countUnsupportedRelays = new Set(); - const storedPermissions = readStoredNip07Permissions(); + let storedPermissions = null; + const hasLocalStorage = + (typeof window !== "undefined" && + window && + typeof window.localStorage !== "undefined") || + (typeof globalThis !== "undefined" && + globalThis && + typeof globalThis.localStorage !== "undefined"); + + if (hasLocalStorage) { + try { + storedPermissions = readStoredNip07Permissions(); + } catch (error) { + storedPermissions = null; + } + } + this.extensionPermissionCache = storedPermissions instanceof Set ? storedPermissions : new Set(); } diff --git a/tests/watch-history.test.mjs b/tests/watch-history.test.mjs index 18f09c44..4dcc54d5 100644 --- a/tests/watch-history.test.mjs +++ b/tests/watch-history.test.mjs @@ -968,7 +968,18 @@ async function testEnsureExtensionPermissionCaching() { const actor = "permission-actor"; const extension = installExtensionCrypto({ actor }); const previousCache = nostrClient.extensionPermissionCache; + let previousStoredPermissions = null; + if (typeof localStorage !== "undefined" && localStorage) { + try { + previousStoredPermissions = localStorage.getItem( + NIP07_PERMISSIONS_STORAGE_KEY, + ); + } catch (error) { + previousStoredPermissions = null; + } + } nostrClient.extensionPermissionCache = new Set(); + clearStoredExtensionPermissions(); try { const first = await nostrClient.ensureExtensionPermissions([ @@ -998,6 +1009,20 @@ async function testEnsureExtensionPermissionCaching() { } finally { extension.restore(); nostrClient.extensionPermissionCache = previousCache; + if (typeof localStorage !== "undefined" && localStorage) { + try { + if (previousStoredPermissions && previousStoredPermissions.length) { + localStorage.setItem( + NIP07_PERMISSIONS_STORAGE_KEY, + previousStoredPermissions, + ); + } else { + clearStoredExtensionPermissions(); + } + } catch (error) { + // ignore restore errors in tests + } + } } }