aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lightning.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-02-06 16:48:13 -0800
committerAdam Soltys <[email protected]>2023-02-06 16:48:13 -0800
commit9c667055c6367e1b2e239686e72f52995679a47e (patch)
tree0b1d4aee51ec2d4e92d08a9d1404ba4e2ecd462a /lib/lightning.js
parent15294844dbc8e73b4d1e92008a7eebc6b86b9ec5 (diff)
parent6f5602d112b68adb7452abf3047f20c32e7725b6 (diff)
downloadcoinos-server-9c667055c6367e1b2e239686e72f52995679a47e.tar.gz
coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.zip
Merge branch 'master' into test
Diffstat (limited to 'lib/lightning.js')
-rw-r--r--lib/lightning.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/lightning.js b/lib/lightning.js
new file mode 100644
index 00000000..2915b7c7
--- /dev/null
+++ b/lib/lightning.js
@@ -0,0 +1,39 @@
+import { emit } from "$lib/sockets";
+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 event = await ln.waitanyinvoice(await g("pay_index"));
+ let {
+ payment_hash: hash,
+ bolt11,
+ pay_index,
+ status,
+ msatoshi_received,
+ payment_preimage: preimage
+ } = event;
+
+ await s("pay_index", pay_index);
+ setTimeout(listenForLightning);
+
+ let received = Math.round(msatoshi_received / 1000);
+
+ try {
+ l("incoming lightning payment", received);
+
+ if (!preimage) return;
+
+ let invoice = await g(`invoice:${hash}`);
+ if (!invoice) return warn("received lightning with no invoice", bolt11);
+
+ await credit(hash, received, invoice.memo, preimage, types.lightning);
+ } catch (e) {
+ console.log(e);
+ err("problem receiving lightning payment", e.message);
+ }
+}