From e4ca2d6e07c8f1ccf89ef352f66d214e465f3294 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 19 Dec 2024 15:27:45 +0100 Subject: [PATCH] Refine #1739 and fix regression causing nostr crossposts and login to not work (#1740) * Refine #1739 and fix regression causing crossposts and login to not work * use temp nostr instance for signing --- components/nostr-auth.js | 7 +++++-- components/use-crossposter.js | 5 ++++- lib/nostr.js | 22 +++++++++++++++++++++- wallets/nwc/index.js | 20 +++++--------------- worker/nostr.js | 5 +++-- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/components/nostr-auth.js b/components/nostr-auth.js index a1bcd186..b655327c 100644 --- a/components/nostr-auth.js +++ b/components/nostr-auth.js @@ -117,6 +117,8 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) { error: false, loading: true }) + + const nostr = new Nostr() try { const { data, error } = await createAuth() if (error) throw error @@ -125,7 +127,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) { if (!k1) throw new Error('Error generating challenge') // should never happen const useExtension = !nip46token - const signer = Nostr.getSigner({ nip46token, supportNip07: useExtension }) + const signer = nostr.getSigner({ nip46token, supportNip07: useExtension }) if (!signer && useExtension) throw new Error('No extension found') if (signer instanceof NDKNip46Signer) { @@ -142,7 +144,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) { loading: true }) - const signedEvent = await Nostr.sign({ + const signedEvent = await nostr.sign({ kind: 27235, created_at: Math.floor(Date.now() / 1000), tags: [ @@ -161,6 +163,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) { } catch (e) { setError(e) } finally { + nostr.close() clearSuggestionTimer() } }, []) diff --git a/components/use-crossposter.js b/components/use-crossposter.js index e4ee529d..560887ff 100644 --- a/components/use-crossposter.js +++ b/components/use-crossposter.js @@ -202,8 +202,9 @@ export default function useCrossposter () { if (!event) return { allSuccessful, noteId } do { + const nostr = new Nostr() try { - const result = await Nostr.crosspost(event, { relays: failedRelays || relays }) + const result = await nostr.crosspost(event, { relays: failedRelays || relays }) if (result.error) { failedRelays = [] @@ -231,6 +232,8 @@ export default function useCrossposter () { // wait 2 seconds to show error then break await new Promise(resolve => setTimeout(resolve, 2000)) return { allSuccessful, noteId } + } finally { + nostr.close() } } while (failedRelays.length > 0) diff --git a/lib/nostr.js b/lib/nostr.js index 077e658b..349d2efe 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -34,7 +34,7 @@ export default class Nostr { * @type {NDK} */ _ndk = null - + static globalInstance = null constructor ({ privKey, defaultSigner, relays, nip46token, supportNip07 = false, ...ndkOptions } = {}) { this._ndk = new NDK({ explicitRelayUrls: relays, @@ -47,6 +47,16 @@ export default class Nostr { }) } + /** + * @type {NDK} + */ + static get () { + if (!Nostr.globalInstance) { + Nostr.globalInstance = new Nostr() + } + return Nostr.globalInstance + } + /** * @type {NDK} */ @@ -151,6 +161,16 @@ export default class Nostr { return { error } } } + + /** + * Close all relay connections + */ + close () { + const pool = this.ndk.pool + for (const relay of pool.urls()) { + pool.removeRelay(relay) + } + } } export function hexToBech32 (hex, prefix = 'npub') { diff --git a/wallets/nwc/index.js b/wallets/nwc/index.js index 3d0e212c..1597d419 100644 --- a/wallets/nwc/index.js +++ b/wallets/nwc/index.js @@ -36,8 +36,8 @@ export const card = { subtitle: 'use Nostr Wallet Connect for payments' } -async function getNwc (nwcUrl, { signal }) { - const ndk = new Nostr().ndk +async function getNwc (nostr, nwcUrl, { signal }) { + const ndk = nostr.ndk const { walletPubkey, secret, relayUrls } = parseNwcUrl(nwcUrl) const nwc = new NDKNwc({ ndk, @@ -66,9 +66,9 @@ async function getNwc (nwcUrl, { signal }) { * @returns - the result of the nwc function */ export async function nwcTryRun (fun, { nwcUrl }, { signal }) { - let nwc + const nostr = new Nostr() try { - nwc = await getNwc(nwcUrl, { signal }) + const nwc = await getNwc(nostr, nwcUrl, { signal }) const { error, result } = await fun(nwc) if (error) throw new Error(error.message || error.code) return result @@ -76,17 +76,7 @@ export async function nwcTryRun (fun, { nwcUrl }, { signal }) { if (e.error) throw new Error(e.error.message || e.error.code) throw e } finally { - if (nwc) close(nwc) - } -} - -/** - * Close all relay connections of the NDKNwc instance - * @param {NDKNwc} nwc - */ -async function close (nwc) { - for (const relay of nwc.relaySet.relays) { - nwc.ndk.pool.removeRelay(relay.url) + nostr.close() } } diff --git a/worker/nostr.js b/worker/nostr.js index fe220126..cc59c98b 100644 --- a/worker/nostr.js +++ b/worker/nostr.js @@ -45,8 +45,9 @@ export async function nip57 ({ data: { hash }, boss, lnd, models }) { } console.log('zap note', e, relays) - const signer = Nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY }) - await Nostr.publish(e, { + const nostr = Nostr.get() + const signer = nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY }) + await nostr.publish(e, { relays, signer, timeout: 1000