aboutsummaryrefslogtreecommitdiffstats
path: root/lib/invoices.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-09-18 14:42:13 -0700
committerAdam Soltys <[email protected]>2025-09-18 14:42:13 -0700
commit8c7fa95d44aabd38fadc3ef955d835ad3379ec6c (patch)
treec5c3b9f5490acd944656438d31355081835098d3 /lib/invoices.ts
parentd2db386e53296babe4e4068ca8d68b3c052e0efb (diff)
downloadcoinos-server-8c7fa95d44aabd38fadc3ef955d835ad3379ec6c.tar.gz
coinos-server-8c7fa95d44aabd38fadc3ef955d835ad3379ec6c.zip
sendinvoice support
Diffstat (limited to 'lib/invoices.ts')
-rw-r--r--lib/invoices.ts29
1 files changed, 19 insertions, 10 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index 45987ae1..1800b859 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -16,6 +16,7 @@ export const generate = async ({ invoice, user }) => {
let {
address_type,
bolt11,
+ bolt12,
aid,
currency,
expiry,
@@ -35,8 +36,8 @@ export const generate = async ({ invoice, user }) => {
secret,
} = invoice;
- amount = parseInt(amount || 0);
- tip = tip == null ? null : parseInt(tip);
+ amount = Number.parseInt(amount || 0);
+ tip = tip == null ? null : Number.parseInt(tip);
if (user) user = await getUser(user.username);
if (!user) fail("user not provided");
@@ -95,18 +96,26 @@ export const generate = async ({ invoice, user }) => {
text = r.bolt11;
paymentHash = r.payment_hash;
} else if (type === PaymentType.bolt12) {
- const r = await ln.offer({
- amount: amount ? `${amount + tip}sat` : "any",
- label: `${id} ${user.username} ${new Date()}`,
- description: memo || id,
- });
+ let r;
+ if (bolt12) {
+ const { id: nodeid } = await ln.getinfo();
+ r = await ln.decode(bolt12);
+ if (r.invoice_node_id !== nodeid) fail("invalid invoice");
+ amount = Math.round(r.invoice_amount_msat / 1000);
+ r.bolt12 = bolt12;
+ } else {
+ const r = await ln.offer({
+ amount: amount ? `${amount + tip}sat` : "any",
+ label: `${id} ${user.username} ${new Date()}`,
+ description: memo || id,
+ });
- if (await getInvoice(r.offer_id)) fail("Duplicate offer exists");
+ if (await getInvoice(r.offer_id)) fail("Duplicate offer exists");
+ await s(`invoice:${r.offer_id}`, id);
+ }
hash = r.bolt12;
text = r.bolt12;
-
- await s(`invoice:${r.offer_id}`, id);
} else if (type === PaymentType.bitcoin) {
address_type ||= "bech32";
hash = await bc.getNewAddress({ address_type });