From 703564c452f3549adc2c23c62ff11d5e713b681e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 26 Dec 2024 22:36:12 +0000 Subject: bolt12 replay --- lib/lightning.ts | 33 +++++++++++++++------------------ lib/payments.ts | 12 ++++++++++-- 2 files changed, 25 insertions(+), 20 deletions(-) (limited to 'lib') 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; -- cgit v1.2.3