diff options
| -rw-r--r-- | index.ts | 23 | ||||
| -rw-r--r-- | lib/lightning.ts | 33 | ||||
| -rw-r--r-- | lib/payments.ts | 12 |
3 files changed, 32 insertions, 36 deletions
@@ -3,7 +3,7 @@ Error.stackTraceLimit = Infinity; import app from "$lib/app"; import { admin, auth, optional } from "$lib/auth"; -import { listenForLightning } from "$lib/lightning"; +import { listenForLightning, replay } from "$lib/lightning"; import { getLocations } from "$lib/locations"; import nwc from "$lib/nwc"; import { catchUp, check } from "$lib/payments"; @@ -135,13 +135,11 @@ app.post("/claim", auth, ecash.claim); app.post("/mint", auth, ecash.mint); app.post("/melt", auth, ecash.melt); app.post("/ecash/:id", ecash.receive); -app.get( - "/liquid-asset-proof-c938c75d110b18aed74b45caf613ebfc1eeb9f77ab48f85016eca850489d07c2", - (_, res) => - res.send( - "Authorize linking the domain name coinos.io to the Liquid asset c938c75d110b18aed74b45caf613ebfc1eeb9f77ab48f85016eca850489d07c2", - ), -); + +app.get("/replay/:index", (req,res) => { + replay(req.params.index); + res.send({}); +}); app.post("/echo", (req, res) => { console.log("echo", req.body); @@ -153,13 +151,6 @@ const port: number = parseInt(process.env["PORT"]) || 3119; app.listen({ host, port }); -const logerr = (e: Error) => - // (e && - // e.message && - // (e.message.includes("Invalid") || - // e.message.includes("MASK") || - // e.message.includes("Rate"))) || - console.log(e); - +const logerr = (e: Error) => console.log(e); process.on("unhandledRejection", logerr); process.on("uncaughtException", logerr); 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; |
