aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-12-05 05:09:09 -0500
committerAdam Soltys <[email protected]>2021-12-05 05:09:09 -0500
commit1ffed1ea00b61660ec3d2c4a62fb80f36337bb1e (patch)
treec2408a8957be354ef635113185a7a9b38911c54e
parent2993a6ccd48c18d21dd52fb08c6fdf9eeda4b8d8 (diff)
downloadcoinos-server-1ffed1ea00b61660ec3d2c4a62fb80f36337bb1e.tar.gz
coinos-server-1ffed1ea00b61660ec3d2c4a62fb80f36337bb1e.zip
lnurl invoice capture
-rw-r--r--routes/lnurl.js69
1 files changed, 24 insertions, 45 deletions
diff --git a/routes/lnurl.js b/routes/lnurl.js
index 1e6da0e7..65118108 100644
--- a/routes/lnurl.js
+++ b/routes/lnurl.js
@@ -3,6 +3,7 @@ const lnurl = require("lnurl");
const jwt = require("jsonwebtoken");
const qs = require("query-string");
const persist = require("../lib/persist");
+const bolt11 = require("bolt11");
logins = persist("data/logins.json");
recipients = persist("data/recipients.json");
@@ -280,6 +281,29 @@ app.get(
})
);
+lnurlServer.on('payRequest:action:processed', async function(event) {
+ const { secret, params, result } = event;
+ const { id, invoice } = result;
+ const recipient = recipients[secret];
+ let payreq = bolt11.decode(invoice);
+
+ try {
+ let i = await db.Invoice.create({
+ user_id: recipient.id,
+ text: invoice,
+ rate: app.get("rates")[recipient.currency],
+ currency: recipient.currency,
+ amount: payreq.satoshis,
+ tip: 0,
+ network: "lightning"
+ });
+
+ l.info("invoice created", i.text, i.amount);
+ } catch (e) {
+ l.error("problem finding lnurl invoice", e.message);
+ }
+});
+
lnurlServer.bindToHook(
"middleware:signedLnurl:afterCheckSignature",
async (req, res, next) => {
@@ -306,51 +330,6 @@ lnurlServer.bindToHook(
throw new Error("Insufficient funds");
}
}
-
- const recipient = recipients[secret];
-
- if (recipient) {
- l.info("lnurl recipient", recipient.username);
- setTimeout(async () => {
- try {
- let result = await lnp.listInvoices({
- num_max_invoices: 5,
- reversed: true
- });
- let { invoices } = result;
-
- if (invoices.length) {
- let existing = [];
- existing = await db.Invoice.findAll({
- attributes: ["text"],
- where: {
- text: invoices.map(i => i.payment_request)
- }
- });
-
- let candidates = invoices.filter(
- i => !existing.map(e => e.text).includes(i.payment_request)
- );
-
- if (candidates.length) {
- let invoice = await db.Invoice.create({
- user_id: recipient.id,
- text: candidates[candidates.length - 1].payment_request,
- rate: app.get("rates")[recipient.currency],
- currency: recipient.currency,
- amount,
- tip: 0,
- network: "lightning"
- });
-
- l.info("invoice created", invoice.text, invoice.amount);
- }
- }
- } catch (e) {
- l.error("problem finding lnurl invoice", e.message);
- }
- }, 2000);
- }
}
if (tag === "login") {