aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-16 23:40:25 -0800
committerAdam Soltys <[email protected]>2023-01-16 23:40:25 -0800
commit91e847126c832efa25ab0bf8d6f3b7e537378fa7 (patch)
tree4a42b6e3bec6eb0f640ed44533b54e6d3d6a5e0e /routes/lightning
parent6bafea9bffe24d2227d269812a73a9a513d92ddf (diff)
downloadcoinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.tar.gz
coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.zip
shuffle things around
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/conversionFee.js12
-rw-r--r--routes/lightning/index.js11
-rw-r--r--routes/lightning/invoice.js25
-rw-r--r--routes/lightning/parse.js19
-rw-r--r--routes/lightning/query.js19
-rw-r--r--routes/lightning/receive.js92
-rw-r--r--routes/lightning/send.js17
7 files changed, 0 insertions, 195 deletions
diff --git a/routes/lightning/conversionFee.js b/routes/lightning/conversionFee.js
deleted file mode 100644
index f6cf434c..00000000
--- a/routes/lightning/conversionFee.js
+++ /dev/null
@@ -1,12 +0,0 @@
-// Code for the conversion fee
-// This is in a separate file so that the same function can be used by ./send and ./receive
-
-const conversionFeeMultiplier = 0.001;
-export const conversionFeeReceiver = "coinosfees"; // account that receives the fees
-
-/**
- * Returns the fee you have to pay for withdrawing 'amount' lightning BTC.
- */
-export function computeConversionFee(amount) {
- return Math.floor(amount * conversionFeeMultiplier);
-}
diff --git a/routes/lightning/index.js b/routes/lightning/index.js
deleted file mode 100644
index ddc10bfe..00000000
--- a/routes/lightning/index.js
+++ /dev/null
@@ -1,11 +0,0 @@
-import invoice from "./invoice";
-import query from "./query";
-import send from "./send";
-import parse from "./parse";
-
-export default {
- invoice,
- query,
- send,
- parse
-};
diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js
deleted file mode 100644
index ae974120..00000000
--- a/routes/lightning/invoice.js
+++ /dev/null
@@ -1,25 +0,0 @@
-import config from "$config";
-import { err } from "$lib/logging";
-import ln from "$lib/ln";
-import { v4 } from "uuid";
-
-export default async (req, res) => {
- let { amount, memo, tip } = req.body;
- if (!tip) tip = 0;
- let value = amount + tip;
-
- try {
- if (!memo) memo = "coinos";
- const invoice = await ln.invoice(
- value ? `${value}sat` : "any",
- v4(),
- memo,
- 360
- );
- res.send({ text: invoice.bolt11 });
- } catch (e) {
- console.log(e);
- err("problem creating invoice", e.message);
- res.code(500).send(e.message);
- }
-};
diff --git a/routes/lightning/parse.js b/routes/lightning/parse.js
deleted file mode 100644
index 1b77019a..00000000
--- a/routes/lightning/parse.js
+++ /dev/null
@@ -1,19 +0,0 @@
-import ln from "$lib/ln";
-import store from "$lib/store";
-
-export default async (req, res) => {
- let { payreq } = req.body;
- let hour = 1000 * 60 * 60;
- let { last } = store.nodes;
- let { nodes } = store;
-
- if (!last || last > Date.now() - hour) ({ nodes } = await ln.listnodes());
- store.nodes = nodes;
-
- let twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14));
- let { msatoshi, payee } = await ln.decodepay(payreq);
- let node = nodes.find((n) => n.nodeid === payee);
- let alias = node ? node.alias : payee.substr(0, 12);
-
- res.send({ alias, amount: Math.round(msatoshi / 1000) });
-};
diff --git a/routes/lightning/query.js b/routes/lightning/query.js
deleted file mode 100644
index c5456a6f..00000000
--- a/routes/lightning/query.js
+++ /dev/null
@@ -1,19 +0,0 @@
-import { emit } from "$lib/sockets";
-
-export default async (req, res) => {
- const { payreq } = req.body;
-
- const invoice = await db.Invoice.findOne({
- include: {
- attributes: ["username"],
- model: db.User,
- as: "user"
- },
- where: {
- text: payreq
- }
- });
-
- if (invoice) emit(req.user.username, "to", invoice.user);
- res.send({});
-};
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
deleted file mode 100644
index ffb0ad95..00000000
--- a/routes/lightning/receive.js
+++ /dev/null
@@ -1,92 +0,0 @@
-import { v4 } from "uuid";
-import store from "$lib/store";
-import { emit } from "$lib/sockets";
-import config from "$config";
-import { notify } from "$lib/notifications";
-import { callWebhook } from "$lib/webhooks";
-import { computeConversionFee } from "./conversionFee";
-import ln from "$lib/ln";
-import { l, err, warn } from "$lib/logging";
-import { g, s, rd } from "$lib/redis";
-
-let handle = async () => {
- let {
- payment_hash,
- bolt11: request,
- pay_index,
- status,
- msatoshi_received,
- payment_preimage: secret
- } = 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 (!secret) return;
- let account, total, user;
-
- let invoice = await g(`invoice:${payment_hash}`);
- if (!invoice) return warn("received lightning with no invoice", request);
-
- let { text: hash, currency, memo, rate, tip, user_id } = invoice;
- let amount = parseInt(received) - tip;
- if (amount < 0) throw new Error("amount out of range");
-
- let id = v4();
- let payment = {
- id,
- user_id,
- hash,
- memo,
- amount,
- currency,
- secret,
- rate,
- tip,
- invoice_id: invoice.id,
- created_at: Date.now()
- };
-
- total = amount + tip;
-
- invoice.status = "paid";
- invoice.received += total;
- await s(`invoice:${payment_hash}`, invoice);
-
- let t = async () => {
- await rd.watch(`user:${user_id}`);
- user = await g(`user:${user_id}`);
- user.balance += total;
- let m = await rd.multi();
- await s(`user:${user.id}`, user);
- if (!(await m.exec())) t();
- };
- await t();
-
- payment.invoice = invoice;
-
- await s(`payment:${id}`, payment);
- await rd.lPush(`${user.id}:payments`, id);
-
- callWebhook(invoice, payment);
-
- emit(user.username, "payment", payment);
- notify(user, `Received ${total} SAT`);
-
- l("lightning payment received", user.username, payment.amount, payment.tip);
- } catch (e) {
- console.log(e);
- err("problem receiving lightning payment", e.message);
- }
-};
-
-let listen = async () => {
- await handle();
- listen();
-};
-
-listen();
diff --git a/routes/lightning/send.js b/routes/lightning/send.js
deleted file mode 100644
index b41d5ed0..00000000
--- a/routes/lightning/send.js
+++ /dev/null
@@ -1,17 +0,0 @@
-import { err } from "$lib/logging";
-import send from "$lib/send";
-import { requirePin } from "$lib/utils";
-
-export default async (req, res) => {
- let { amount, route, memo, payreq } = req.body;
- let { user } = req;
-
- try {
- await requirePin(req);
- res.send(await send(amount, memo, payreq, user));
- } catch (e) {
- console.log(e);
- err("problem sending lightning payment", user.username, payreq, e.message);
- res.code(500).send(e.message);
- }
-};