diff options
| author | Adam Soltys <[email protected]> | 2024-08-26 21:17:43 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-08-26 21:17:43 -0700 |
| commit | fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7 (patch) | |
| tree | aa8f34e1692bd2d638a2c996eeadb58671d07aea /lib/lightning.ts | |
| parent | 00e43ddc0bf3d6dd719fc9295d156567fa2fecfb (diff) | |
| download | coinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.tar.gz coinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.zip | |
typescript
Diffstat (limited to 'lib/lightning.ts')
| -rw-r--r-- | lib/lightning.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/lightning.ts b/lib/lightning.ts new file mode 100644 index 00000000..2d44581d --- /dev/null +++ b/lib/lightning.ts @@ -0,0 +1,52 @@ +import { handleZap } from "$lib/nostr"; +import config from "$config"; +import { notify } from "$lib/notifications"; +import { callWebhook } from "$lib/webhooks"; +import ln from "$lib/ln"; +import { l, err, warn } from "$lib/logging"; +import { g, s, t, db } from "$lib/db"; +import { credit, types } from "$lib/payments"; + +export async function listenForLightning() { + let inv = await ln.waitanyinvoice((await g("pay_index")) || 0); + let { + bolt11, + description, + pay_index, + status, + amount_received_msat, + payment_preimage: preimage, + } = inv; + + await s("pay_index", pay_index); + setTimeout(listenForLightning); + + let received = Math.round(amount_received_msat / 1000); + + try { + if (!preimage) return; + + let id = await g(`invoice:${bolt11}`); + let invoice = await g(`invoice:${id}`); + if (!invoice) return warn("received lightning with no invoice", bolt11); + + if (invoice && invoice.memo) { + try { + if (JSON.parse(description).kind === 9734) await handleZap(inv); + } catch (e) { + if (!e.message.startsWith("Unexpected")) + warn("failed to handle zap", e.message); + } + } + + let p = await g(`payment:${bolt11}`); + if (typeof p === "string") p = await g(`payment:${p}`); + + if (p) return warn("already processed", bolt11); + + await credit(bolt11, received, invoice.memo, preimage, types.lightning); + } catch (e) { + console.log(e); + err("problem receiving lightning payment", e.message); + } +} |
