aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-17 11:42:57 -0800
committerAdam Soltys <[email protected]>2023-01-17 11:42:57 -0800
commit46913d34e77f19c3dd7c105b1deec452348dd1db (patch)
treea1cdc217ce9f2031bbb4ca6b4aabc1093024161b
parent220d6d52b8439a81b572a889d7012f826efc55c5 (diff)
downloadcoinos-server-46913d34e77f19c3dd7c105b1deec452348dd1db.tar.gz
coinos-server-46913d34e77f19c3dd7c105b1deec452348dd1db.zip
rename invoice id to hash
-rw-r--r--index.js4
-rw-r--r--lib/lightning.js82
-rw-r--r--routes/invoices.js12
3 files changed, 92 insertions, 6 deletions
diff --git a/index.js b/index.js
index 4a8fb01a..ec0e4244 100644
--- a/index.js
+++ b/index.js
@@ -1,5 +1,7 @@
import app from "$app";
import { auth, optional } from "$lib/auth";
+
+import { listenForLightning } from "$lib/lightning";
import { getLocations } from "$lib/locations";
import { getRates, sendRates } from "$lib/rates";
import { sendHeartbeat } from "$lib/sockets";
@@ -13,6 +15,8 @@ import users from "$routes/users";
import payments from "$routes/payments";
import requests from "$routes/requests";
+listenForLightning();
+
getLocations();
getRates();
diff --git a/lib/lightning.js b/lib/lightning.js
index e69de29b..2a8e8053 100644
--- a/lib/lightning.js
+++ b/lib/lightning.js
@@ -0,0 +1,82 @@
+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, db } from "$lib/db";
+
+export async function listenForLightning() {
+ let {
+ payment_hash,
+ bolt11: request,
+ pay_index,
+ status,
+ msatoshi_received,
+ payment_preimage: preimage
+ } = await ln.waitanyinvoice(await g("pay_index"));
+
+ await s("pay_index", pay_index);
+
+ let received = Math.round(msatoshi_received / 1000);
+
+ try {
+ l("incoming lightning payment", received);
+
+ if (!preimage) return;
+
+ let invoice = await g(`invoice:${payment_hash}`);
+ if (!invoice) return warn("received lightning with no invoice", request);
+
+ let { hash, currency, memo, rate, tip, uid } = invoice;
+ let amount = parseInt(received) - tip;
+ if (amount < 0) throw new Error("amount out of range");
+
+ let user = await g(`user:${uid}`);
+
+ let payment = {
+ amount,
+ created: Date.now(),
+ currency,
+ fee: 0,
+ hash,
+ memo,
+ preimage,
+ rate,
+ tip,
+ type: "lightning",
+ uid
+ };
+
+ invoice.status = "paid";
+ invoice.received += received;
+ await s(`invoice:${payment_hash}`, invoice);
+
+ let t = async () => {
+ await db.watch(`balance:${uid}`);
+ let b = await g(`balance:${uid}`);
+ b += received;
+ let m = await db.multi();
+ await s(`balance:${uid}`, b);
+ if (!(await m.exec())) t();
+ };
+ await t();
+
+ payment.invoice = invoice;
+
+ await s(`payment:${hash}`, payment);
+ await db.lPush(`${uid}:payments`, hash);
+
+ callWebhook(invoice, payment);
+
+ emit(uid, "payment", payment);
+ notify(user, `Received ${received} SAT`);
+
+ l("lightning payment received", user.username, payment.amount, payment.tip);
+ } catch (e) {
+ console.log(e);
+ err("problem receiving lightning payment", e.message);
+ }
+
+ setTimeout(listenForLightning);
+}
diff --git a/routes/invoices.js b/routes/invoices.js
index 94b0d27d..acd9457e 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -5,8 +5,8 @@ import { db, g, s } from "$lib/db";
import ln from "$lib/ln";
export default {
- async get({ query: { id } }, res) {
- let invoice = await g(`invoice:${id}`);
+ async get({ query: { hash } }, res) {
+ let invoice = await g(`invoice:${hash}`);
invoice.user = await g(`user:${invoice.uid}`);
res.send(invoice);
},
@@ -28,7 +28,7 @@ export default {
if (!rate) rate = store.rates[currency];
if (amount < 0) throw new Error("invalid amount");
- let { payment_hash: id, bolt11: text } = await ln.invoice(
+ let { payment_hash: hash, bolt11: text } = await ln.invoice(
amount ? `${amount + tip}sat` : "any",
new Date(),
"",
@@ -37,7 +37,7 @@ export default {
invoice = {
...invoice,
- id,
+ hash,
amount,
currency,
rate,
@@ -57,8 +57,8 @@ export default {
`${text.substr(0, 8)}..${text.substr(-6)}`
);
- await s(`invoice:${id}`, invoice);
- await db.lPush(`${uid}:invoices`, id);
+ await s(`invoice:${hash}`, invoice);
+ await db.lPush(`${uid}:invoices`, hash);
if (request_id) {
let request = await g(`request:${request_id}`);