diff --git a/lib/nostr.js b/lib/nostr.js index 7e17340e..077e658b 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -29,7 +29,7 @@ export const RELAYS_BLACKLIST = [] * @property {function(Object, {privKey: string, signer: NDKSigner}): Promise} sign * @property {function(Object, {relays: Array, privKey: string, signer: NDKSigner}): Promise} publish */ -export class Nostr { +export default class Nostr { /** * @type {NDK} */ @@ -153,11 +153,6 @@ export class Nostr { } } -/** - * @type {Nostr} - */ -export default new Nostr() - export function hexToBech32 (hex, prefix = 'npub') { return bech32.encode(prefix, bech32.toWords(Buffer.from(hex, 'hex'))) } diff --git a/wallets/nwc/client.js b/wallets/nwc/client.js index 74691edc..f6fc3829 100644 --- a/wallets/nwc/client.js +++ b/wallets/nwc/client.js @@ -1,4 +1,4 @@ -import { getNwc, supportedMethods, nwcTryRun } from '@/wallets/nwc' +import { supportedMethods, nwcTryRun } from '@/wallets/nwc' export * from '@/wallets/nwc' export async function testSendPayment ({ nwcUrl }, { signal }) { @@ -9,8 +9,6 @@ export async function testSendPayment ({ nwcUrl }, { signal }) { } export async function sendPayment (bolt11, { nwcUrl }, { signal }) { - const nwc = await getNwc(nwcUrl, { signal }) - // TODO: support AbortSignal - const result = await nwcTryRun(() => nwc.payInvoice(bolt11)) + const result = await nwcTryRun(nwc => nwc.payInvoice(bolt11), { nwcUrl }, { signal }) return result.preimage } diff --git a/wallets/nwc/index.js b/wallets/nwc/index.js index 7ec94241..3d0e212c 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' } -export async function getNwc (nwcUrl, { signal }) { - const ndk = Nostr.ndk +async function getNwc (nwcUrl, { signal }) { + const ndk = new Nostr().ndk const { walletPubkey, secret, relayUrls } = parseNwcUrl(nwcUrl) const nwc = new NDKNwc({ ndk, @@ -65,20 +65,32 @@ export async function getNwc (nwcUrl, { signal }) { * @param {function} fun - the nwc function to run * @returns - the result of the nwc function */ -export async function nwcTryRun (fun) { +export async function nwcTryRun (fun, { nwcUrl }, { signal }) { + let nwc try { - const { error, result } = await fun() + nwc = await getNwc(nwcUrl, { signal }) + const { error, result } = await fun(nwc) if (error) throw new Error(error.message || error.code) return result } catch (e) { 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) } } export async function supportedMethods (nwcUrl, { signal }) { - const nwc = await getNwc(nwcUrl, { signal }) - // TODO: support AbortSignal - const result = await nwcTryRun(() => nwc.getInfo()) + const result = await nwcTryRun(nwc => nwc.getInfo(), { nwcUrl }, { signal }) return result.methods } diff --git a/wallets/nwc/server.js b/wallets/nwc/server.js index b9cc8fd5..6fb4c82c 100644 --- a/wallets/nwc/server.js +++ b/wallets/nwc/server.js @@ -1,4 +1,4 @@ -import { getNwc, supportedMethods, nwcTryRun } from '@/wallets/nwc' +import { supportedMethods, nwcTryRun } from '@/wallets/nwc' export * from '@/wallets/nwc' export async function testCreateInvoice ({ nwcUrlRecv }, { signal }) { @@ -21,8 +21,9 @@ export async function testCreateInvoice ({ nwcUrlRecv }, { signal }) { } export async function createInvoice ({ msats, description, expiry }, { nwcUrlRecv }, { signal }) { - const nwc = await getNwc(nwcUrlRecv, { signal }) - // TODO: support AbortSignal - const result = await nwcTryRun(() => nwc.sendReq('make_invoice', { amount: msats, description, expiry })) + const result = await nwcTryRun( + nwc => nwc.sendReq('make_invoice', { amount: msats, description, expiry }), + { nwcUrl: nwcUrlRecv }, { signal } + ) return result.invoice }