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
This commit is contained in:
Riccardo Balbo 2024-12-19 15:27:45 +01:00 committed by GitHub
parent 4db2edb1d9
commit e4ca2d6e07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 21 deletions

View File

@ -117,6 +117,8 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
error: false, error: false,
loading: true loading: true
}) })
const nostr = new Nostr()
try { try {
const { data, error } = await createAuth() const { data, error } = await createAuth()
if (error) throw error 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 if (!k1) throw new Error('Error generating challenge') // should never happen
const useExtension = !nip46token 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 && useExtension) throw new Error('No extension found')
if (signer instanceof NDKNip46Signer) { if (signer instanceof NDKNip46Signer) {
@ -142,7 +144,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
loading: true loading: true
}) })
const signedEvent = await Nostr.sign({ const signedEvent = await nostr.sign({
kind: 27235, kind: 27235,
created_at: Math.floor(Date.now() / 1000), created_at: Math.floor(Date.now() / 1000),
tags: [ tags: [
@ -161,6 +163,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
} catch (e) { } catch (e) {
setError(e) setError(e)
} finally { } finally {
nostr.close()
clearSuggestionTimer() clearSuggestionTimer()
} }
}, []) }, [])

View File

@ -202,8 +202,9 @@ export default function useCrossposter () {
if (!event) return { allSuccessful, noteId } if (!event) return { allSuccessful, noteId }
do { do {
const nostr = new Nostr()
try { try {
const result = await Nostr.crosspost(event, { relays: failedRelays || relays }) const result = await nostr.crosspost(event, { relays: failedRelays || relays })
if (result.error) { if (result.error) {
failedRelays = [] failedRelays = []
@ -231,6 +232,8 @@ export default function useCrossposter () {
// wait 2 seconds to show error then break // wait 2 seconds to show error then break
await new Promise(resolve => setTimeout(resolve, 2000)) await new Promise(resolve => setTimeout(resolve, 2000))
return { allSuccessful, noteId } return { allSuccessful, noteId }
} finally {
nostr.close()
} }
} while (failedRelays.length > 0) } while (failedRelays.length > 0)

View File

@ -34,7 +34,7 @@ export default class Nostr {
* @type {NDK} * @type {NDK}
*/ */
_ndk = null _ndk = null
static globalInstance = null
constructor ({ privKey, defaultSigner, relays, nip46token, supportNip07 = false, ...ndkOptions } = {}) { constructor ({ privKey, defaultSigner, relays, nip46token, supportNip07 = false, ...ndkOptions } = {}) {
this._ndk = new NDK({ this._ndk = new NDK({
explicitRelayUrls: relays, 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} * @type {NDK}
*/ */
@ -151,6 +161,16 @@ export default class Nostr {
return { error } 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') { export function hexToBech32 (hex, prefix = 'npub') {

View File

@ -36,8 +36,8 @@ export const card = {
subtitle: 'use Nostr Wallet Connect for payments' subtitle: 'use Nostr Wallet Connect for payments'
} }
async function getNwc (nwcUrl, { signal }) { async function getNwc (nostr, nwcUrl, { signal }) {
const ndk = new Nostr().ndk const ndk = nostr.ndk
const { walletPubkey, secret, relayUrls } = parseNwcUrl(nwcUrl) const { walletPubkey, secret, relayUrls } = parseNwcUrl(nwcUrl)
const nwc = new NDKNwc({ const nwc = new NDKNwc({
ndk, ndk,
@ -66,9 +66,9 @@ async function getNwc (nwcUrl, { signal }) {
* @returns - the result of the nwc function * @returns - the result of the nwc function
*/ */
export async function nwcTryRun (fun, { nwcUrl }, { signal }) { export async function nwcTryRun (fun, { nwcUrl }, { signal }) {
let nwc const nostr = new Nostr()
try { try {
nwc = await getNwc(nwcUrl, { signal }) const nwc = await getNwc(nostr, nwcUrl, { signal })
const { error, result } = await fun(nwc) const { error, result } = await fun(nwc)
if (error) throw new Error(error.message || error.code) if (error) throw new Error(error.message || error.code)
return result 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) if (e.error) throw new Error(e.error.message || e.error.code)
throw e throw e
} finally { } finally {
if (nwc) close(nwc) nostr.close()
}
}
/**
* 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)
} }
} }

View File

@ -45,8 +45,9 @@ export async function nip57 ({ data: { hash }, boss, lnd, models }) {
} }
console.log('zap note', e, relays) console.log('zap note', e, relays)
const signer = Nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY }) const nostr = Nostr.get()
await Nostr.publish(e, { const signer = nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY })
await nostr.publish(e, {
relays, relays,
signer, signer,
timeout: 1000 timeout: 1000