Close relay connections after each NWC call (#1739)
This commit is contained in:
parent
d3a705d3ad
commit
4db2edb1d9
|
@ -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}
|
||||
*/
|
||||
|
@ -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')))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue