aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-12-26 22:36:12 +0000
committerAdam Soltys <[email protected]>2024-12-26 22:37:03 +0000
commit703564c452f3549adc2c23c62ff11d5e713b681e (patch)
treeb369f162feceed4ad452bd10eae6f9ed0716a2fd /lib
parentbe16090d086a05ed03fc8149581c49c717826535 (diff)
downloadcoinos-server-703564c452f3549adc2c23c62ff11d5e713b681e.tar.gz
coinos-server-703564c452f3549adc2c23c62ff11d5e713b681e.zip
bolt12 replay
Diffstat (limited to 'lib')
-rw-r--r--lib/lightning.ts33
-rw-r--r--lib/payments.ts12
2 files changed, 25 insertions, 20 deletions
diff --git a/lib/lightning.ts b/lib/lightning.ts
index 974a91c3..acaa158b 100644
--- a/lib/lightning.ts
+++ b/lib/lightning.ts
@@ -10,12 +10,13 @@ export async function listenForLightning() {
const {
local_offer_id,
bolt11,
+ bolt12,
description,
pay_index,
amount_received_msat,
payment_preimage: preimage,
} = inv;
-
+
await s("pay_index", pay_index);
setTimeout(listenForLightning);
@@ -33,20 +34,20 @@ export async function listenForLightning() {
try {
if (JSON.parse(description).kind === 9734) await handleZap(inv);
} catch (e) {
- if (!e.message.startsWith("Unexpected"))
+ if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
- const p = await getPayment(bolt11 || local_offer_id);
- if (p) return warn("already processed", bolt11);
+ const p = await getPayment(bolt11 || bolt12);
+ if (p) return warn("already processed", bolt11 || bolt12);
await credit({
- hash: bolt11 || local_offer_id,
+ hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
- type: types.lightning,
+ type: bolt12 ? types.bolt12 : types.lightning,
});
} catch (e) {
console.log(e);
@@ -55,19 +56,18 @@ export async function listenForLightning() {
}
-export async function replay() {
- const inv = await ln.waitanyinvoice(440516 - 1);
+export async function replay(index) {
+ const inv = await ln.waitanyinvoice(index - 1);
const {
local_offer_id,
bolt11,
+ bolt12,
description,
- pay_index,
amount_received_msat,
payment_preimage: preimage,
} = inv;
const received = Math.round(amount_received_msat / 1000);
- console.log("replaying", pay_index);
try {
if (!preimage) return;
@@ -81,28 +81,25 @@ export async function replay() {
try {
if (JSON.parse(description).kind === 9734) await handleZap(inv);
} catch (e) {
- if (!e.message.startsWith("Unexpected"))
+ if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
- const p = await getPayment(bolt11 || local_offer_id);
+ const p = await getPayment(bolt11 || bolt12);
if (p) {
- console.log(invoice);
- return warn("already processed", bolt11, local_offer_id);
+ return warn("already processed", bolt11 || bolt12);
}
await credit({
- hash: bolt11 || local_offer_id,
+ hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
- type: types.lightning,
+ type: bolt12 ? types.bolt12 : types.lightning,
});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
-
-// replay();
diff --git a/lib/payments.ts b/lib/payments.ts
index c9d1c74c..0797721b 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -67,7 +67,7 @@ export const debit = async ({
let iid;
if (invoice) {
- if (invoice.received >= amount) fail("Invoice already paid");
+ if (invoice.received >= amount && invoice.type !== types.bolt12) fail("Invoice already paid");
({ id: iid } = invoice);
ref = invoice.uid;
@@ -151,7 +151,15 @@ export const credit = async ({
}) => {
amount = parseInt(amount) || 0;
- const inv = await getInvoice(hash);
+ let inv;
+ if (type === types.bolt12) {
+ let { invoices } = await ln.listinvoices({ invstring: hash });
+ let { local_offer_id } = invoices[0];
+ inv = await getInvoice(local_offer_id);
+ } else {
+ inv = await getInvoice(hash);
+ }
+
if (!inv) {
await db.sAdd("missing", ref.split(":")[0]);
return;