Skip to content

Commit

Permalink
Close relay connections after each NWC call (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis authored Dec 19, 2024
1 parent d3a705d commit 4db2edb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
7 changes: 1 addition & 6 deletions lib/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const RELAYS_BLACKLIST = []
* @property {function(Object, {privKey: string, signer: NDKSigner}): Promise<NDKEvent>} sign
* @property {function(Object, {relays: Array<string>, privKey: string, signer: NDKSigner}): Promise<NDKEvent>} publish
*/
export class Nostr {
export default class Nostr {
/**
* @type {NDK}
*/
Expand Down Expand Up @@ -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')))
}
Expand Down
6 changes: 2 additions & 4 deletions wallets/nwc/client.js
Original file line number Diff line number Diff line change
@@ -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 }) {
Expand All @@ -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
}
26 changes: 19 additions & 7 deletions wallets/nwc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions wallets/nwc/server.js
Original file line number Diff line number Diff line change
@@ -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 }) {
Expand All @@ -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
}

0 comments on commit 4db2edb

Please sign in to comment.