ekzyis 97317d4c0c
Fix lnaddr attach fails if minSendable > 1000 (#2138)
* Fix lnaddr attach fails if minSendable > 1000

* Don't fetch lnAddr options twice for test invoice
2025-04-26 11:15:47 -05:00

47 lines
1.3 KiB
JavaScript

import { fetchWithTimeout } from '@/lib/fetch'
import { msatsSatsFloor } from '@/lib/format'
import { lnAddrOptions } from '@/lib/lnurl'
import { assertContentTypeJson, assertResponseOk } from '@/lib/url'
export * from '@/wallets/lightning-address'
export const testCreateInvoice = async ({ address }, { signal }) => {
return await createInvoice({ msats: undefined }, { address }, { signal })
}
export const createInvoice = async (
{ msats, description },
{ address },
{ signal }
) => {
const { min, callback, commentAllowed } = await lnAddrOptions(address, { signal })
const callbackUrl = new URL(callback)
// most lnurl providers suck nards so we have to floor to nearest sat
if (!msats) {
// use min sendable amount by default
msats = 1_000 * min
}
msats = msatsSatsFloor(msats)
callbackUrl.searchParams.append('amount', msats)
if (commentAllowed >= description?.length) {
callbackUrl.searchParams.append('comment', description)
}
// call callback with amount and conditionally comment
const method = 'GET'
const res = await fetchWithTimeout(callbackUrl.toString(), { method, signal })
assertResponseOk(res, { method })
assertContentTypeJson(res, { method })
const body = await res.json()
if (body.status === 'ERROR') {
throw new Error(body.reason)
}
return body.pr
}