From ad4f56a1d48ea4e90b3ad7b3be0e713ecbc0550d Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Sun, 5 Oct 2025 08:44:23 -0400 Subject: [PATCH] Ensure NostrTools nip04 helper persists --- index.html | 70 +++++++++++++++++++++++++++++----- js/payments/nwcClient.js | 17 ++++++++- js/payments/platformAddress.js | 13 ++++++- js/payments/zapSplit.js | 22 ++++++++++- 4 files changed, 108 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index e8c49bcc..8e86ca16 100644 --- a/index.html +++ b/index.html @@ -268,25 +268,27 @@ if (nostrTools && typeof nostrTools === "object") { toolSources.push(nostrTools); } - const globalTools = - typeof window !== "undefined" && window?.NostrTools - ? window.NostrTools + const globalScope = + typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : null; + const existingTools = + globalScope && typeof globalScope.NostrTools === "object" + ? globalScope.NostrTools : null; - if (globalTools && globalTools !== nostrTools) { - toolSources.push(globalTools); + if (existingTools && existingTools !== nostrTools) { + toolSources.push(existingTools); } - const toolsBundle = Object.assign({}, ...toolSources); + const canonicalTools = Object.assign({}, ...toolSources); if (normalizedGeneratePrivateKey) { - toolsBundle.generatePrivateKey = normalizedGeneratePrivateKey; + canonicalTools.generatePrivateKey = normalizedGeneratePrivateKey; } if (normalizedGenerateSecretKey) { - toolsBundle.generateSecretKey = normalizedGenerateSecretKey; + canonicalTools.generateSecretKey = normalizedGenerateSecretKey; } if (resolvedNip04) { - toolsBundle.nip04 = resolvedNip04; + canonicalTools.nip04 = resolvedNip04; console.info( `[bitvid] Initialized nostr nip04 helpers using ${nip04Source}.`, ); @@ -296,7 +298,55 @@ ); } - window.NostrTools = toolsBundle; + if (globalScope) { + try { + Object.defineProperty(globalScope, "__BITVID_CANONICAL_NOSTR_TOOLS__", { + value: Object.freeze({ ...canonicalTools }), + writable: false, + enumerable: false, + configurable: true, + }); + } catch (error) { + globalScope.__BITVID_CANONICAL_NOSTR_TOOLS__ = Object.freeze({ + ...canonicalTools, + }); + } + + let activeTools = canonicalTools; + const mergeWithCanonical = (candidate) => { + if (!candidate || typeof candidate !== "object") { + return canonicalTools; + } + const merged = Object.assign({}, canonicalTools, candidate); + if (!merged.nip04 && canonicalTools.nip04) { + merged.nip04 = canonicalTools.nip04; + } + return merged; + }; + + const applyActive = (candidate) => { + activeTools = mergeWithCanonical(candidate); + return activeTools; + }; + + try { + Object.defineProperty(globalScope, "NostrTools", { + configurable: true, + enumerable: true, + get() { + return activeTools; + }, + set(value) { + applyActive(value); + }, + }); + } catch (error) { + console.warn("[bitvid] Failed to install NostrTools guard.", error); + } + + applyActive(existingTools); + globalScope.NostrTools = canonicalTools; + } diff --git a/js/payments/nwcClient.js b/js/payments/nwcClient.js index ff5a5ff2..76f3e682 100644 --- a/js/payments/nwcClient.js +++ b/js/payments/nwcClient.js @@ -29,7 +29,22 @@ function getGlobalWindow() { function getNostrTools() { const win = getGlobalWindow(); - return win?.NostrTools || null; + const tools = win?.NostrTools || null; + const canonical = win?.__BITVID_CANONICAL_NOSTR_TOOLS__ || null; + + if (tools && canonical && !tools.nip04 && canonical.nip04) { + try { + tools.nip04 = canonical.nip04; + } catch (error) { + return { ...canonical, ...tools, nip04: canonical.nip04 }; + } + } + + if (tools) { + return tools; + } + + return canonical || null; } function assertNostrTools(methods = []) { diff --git a/js/payments/platformAddress.js b/js/payments/platformAddress.js index bbed4a48..349b19b5 100644 --- a/js/payments/platformAddress.js +++ b/js/payments/platformAddress.js @@ -21,7 +21,18 @@ function getGlobalWindow() { function getNostrTools() { const win = getGlobalWindow(); - return win?.NostrTools || null; + const tools = win?.NostrTools || null; + const canonical = win?.__BITVID_CANONICAL_NOSTR_TOOLS__ || null; + + if (tools && canonical && !tools.nip04 && canonical.nip04) { + try { + tools.nip04 = canonical.nip04; + } catch (error) { + return { ...canonical, ...tools, nip04: canonical.nip04 }; + } + } + + return tools || canonical || null; } function decodeAdminPubkey() { diff --git a/js/payments/zapSplit.js b/js/payments/zapSplit.js index 77ad714d..a42ba18d 100644 --- a/js/payments/zapSplit.js +++ b/js/payments/zapSplit.js @@ -81,12 +81,30 @@ function mergeDependencies(overrides = {}) { } function getNostrTools() { - if (typeof window !== "undefined" && window?.NostrTools) { - return window.NostrTools; + const scope = typeof window !== "undefined" ? window : globalThis; + const tools = scope?.NostrTools || null; + const canonical = scope?.__BITVID_CANONICAL_NOSTR_TOOLS__ || null; + + if (tools && canonical && !tools.nip04 && canonical.nip04) { + try { + tools.nip04 = canonical.nip04; + } catch (error) { + return { ...canonical, ...tools, nip04: canonical.nip04 }; + } } + + if (tools) { + return tools; + } + + if (canonical) { + return canonical; + } + if (typeof globalThis !== "undefined" && globalThis?.NostrTools) { return globalThis.NostrTools; } + return null; }