Skip to content

Commit 50a49c2

Browse files
authored
Merge branch 'master' into zbd
2 parents 83a9907 + e4ca2d6 commit 50a49c2

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

components/nostr-auth.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
117117
error: false,
118118
loading: true
119119
})
120+
121+
const nostr = new Nostr()
120122
try {
121123
const { data, error } = await createAuth()
122124
if (error) throw error
@@ -125,7 +127,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
125127
if (!k1) throw new Error('Error generating challenge') // should never happen
126128

127129
const useExtension = !nip46token
128-
const signer = Nostr.getSigner({ nip46token, supportNip07: useExtension })
130+
const signer = nostr.getSigner({ nip46token, supportNip07: useExtension })
129131
if (!signer && useExtension) throw new Error('No extension found')
130132

131133
if (signer instanceof NDKNip46Signer) {
@@ -142,7 +144,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
142144
loading: true
143145
})
144146

145-
const signedEvent = await Nostr.sign({
147+
const signedEvent = await nostr.sign({
146148
kind: 27235,
147149
created_at: Math.floor(Date.now() / 1000),
148150
tags: [
@@ -161,6 +163,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
161163
} catch (e) {
162164
setError(e)
163165
} finally {
166+
nostr.close()
164167
clearSuggestionTimer()
165168
}
166169
}, [])

components/use-crossposter.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ export default function useCrossposter () {
202202
if (!event) return { allSuccessful, noteId }
203203

204204
do {
205+
const nostr = new Nostr()
205206
try {
206-
const result = await Nostr.crosspost(event, { relays: failedRelays || relays })
207+
const result = await nostr.crosspost(event, { relays: failedRelays || relays })
207208

208209
if (result.error) {
209210
failedRelays = []
@@ -231,6 +232,8 @@ export default function useCrossposter () {
231232
// wait 2 seconds to show error then break
232233
await new Promise(resolve => setTimeout(resolve, 2000))
233234
return { allSuccessful, noteId }
235+
} finally {
236+
nostr.close()
234237
}
235238
} while (failedRelays.length > 0)
236239

lib/nostr.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class Nostr {
3434
* @type {NDK}
3535
*/
3636
_ndk = null
37-
37+
static globalInstance = null
3838
constructor ({ privKey, defaultSigner, relays, nip46token, supportNip07 = false, ...ndkOptions } = {}) {
3939
this._ndk = new NDK({
4040
explicitRelayUrls: relays,
@@ -47,6 +47,16 @@ export default class Nostr {
4747
})
4848
}
4949

50+
/**
51+
* @type {NDK}
52+
*/
53+
static get () {
54+
if (!Nostr.globalInstance) {
55+
Nostr.globalInstance = new Nostr()
56+
}
57+
return Nostr.globalInstance
58+
}
59+
5060
/**
5161
* @type {NDK}
5262
*/
@@ -151,6 +161,16 @@ export default class Nostr {
151161
return { error }
152162
}
153163
}
164+
165+
/**
166+
* Close all relay connections
167+
*/
168+
close () {
169+
const pool = this.ndk.pool
170+
for (const relay of pool.urls()) {
171+
pool.removeRelay(relay)
172+
}
173+
}
154174
}
155175

156176
export function hexToBech32 (hex, prefix = 'npub') {

wallets/nwc/index.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export const card = {
3636
subtitle: 'use Nostr Wallet Connect for payments'
3737
}
3838

39-
async function getNwc (nwcUrl, { signal }) {
40-
const ndk = new Nostr().ndk
39+
async function getNwc (nostr, nwcUrl, { signal }) {
40+
const ndk = nostr.ndk
4141
const { walletPubkey, secret, relayUrls } = parseNwcUrl(nwcUrl)
4242
const nwc = new NDKNwc({
4343
ndk,
@@ -66,27 +66,17 @@ async function getNwc (nwcUrl, { signal }) {
6666
* @returns - the result of the nwc function
6767
*/
6868
export async function nwcTryRun (fun, { nwcUrl }, { signal }) {
69-
let nwc
69+
const nostr = new Nostr()
7070
try {
71-
nwc = await getNwc(nwcUrl, { signal })
71+
const nwc = await getNwc(nostr, nwcUrl, { signal })
7272
const { error, result } = await fun(nwc)
7373
if (error) throw new Error(error.message || error.code)
7474
return result
7575
} catch (e) {
7676
if (e.error) throw new Error(e.error.message || e.error.code)
7777
throw e
7878
} finally {
79-
if (nwc) close(nwc)
80-
}
81-
}
82-
83-
/**
84-
* Close all relay connections of the NDKNwc instance
85-
* @param {NDKNwc} nwc
86-
*/
87-
async function close (nwc) {
88-
for (const relay of nwc.relaySet.relays) {
89-
nwc.ndk.pool.removeRelay(relay.url)
79+
nostr.close()
9080
}
9181
}
9282

worker/nostr.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export async function nip57 ({ data: { hash }, boss, lnd, models }) {
4545
}
4646

4747
console.log('zap note', e, relays)
48-
const signer = Nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY })
49-
await Nostr.publish(e, {
48+
const nostr = Nostr.get()
49+
const signer = nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY })
50+
await nostr.publish(e, {
5051
relays,
5152
signer,
5253
timeout: 1000

0 commit comments

Comments
 (0)