2024-12-13 19:28:36 +00:00
|
|
|
import { getNwc, supportedMethods, nwcTryRun } from '@/wallets/nwc'
|
2024-11-16 00:38:14 +00:00
|
|
|
export * from '@/wallets/nwc'
|
2024-08-21 15:13:27 +00:00
|
|
|
|
2024-12-16 20:05:31 +00:00
|
|
|
export async function testCreateInvoice ({ nwcUrlRecv }, { signal }) {
|
|
|
|
const supported = await supportedMethods(nwcUrlRecv, { signal })
|
2024-08-21 15:13:27 +00:00
|
|
|
|
|
|
|
const supports = (method) => supported.includes(method)
|
|
|
|
|
|
|
|
if (!supports('make_invoice')) {
|
|
|
|
throw new Error('make_invoice not supported')
|
|
|
|
}
|
|
|
|
|
|
|
|
const mustNotSupport = ['pay_invoice', 'multi_pay_invoice', 'pay_keysend', 'multi_pay_keysend']
|
|
|
|
for (const method of mustNotSupport) {
|
|
|
|
if (supports(method)) {
|
|
|
|
throw new Error(`${method} must not be supported`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-16 20:05:31 +00:00
|
|
|
return await createInvoice({ msats: 1000, expiry: 1 }, { nwcUrlRecv }, { signal })
|
2024-08-21 15:13:27 +00:00
|
|
|
}
|
|
|
|
|
2024-12-16 20:05:31 +00:00
|
|
|
export async function createInvoice ({ msats, description, expiry }, { nwcUrlRecv }, { signal }) {
|
|
|
|
const nwc = await getNwc(nwcUrlRecv, { signal })
|
|
|
|
// TODO: support AbortSignal
|
2024-12-13 19:28:36 +00:00
|
|
|
const result = await nwcTryRun(() => nwc.sendReq('make_invoice', { amount: msats, description, expiry }))
|
2024-08-21 15:13:27 +00:00
|
|
|
return result.invoice
|
|
|
|
}
|