aboutsummaryrefslogtreecommitdiffstats
path: root/routes/payments.js
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/payments.js
parent6bafea9bffe24d2227d269812a73a9a513d92ddf (diff)
downloadcoinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.tar.gz
coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.zip
shuffle things around
Diffstat (limited to 'routes/payments.js')
-rw-r--r--routes/payments.js300
1 files changed, 140 insertions, 160 deletions
diff --git a/routes/payments.js b/routes/payments.js
index b328e763..13c0cdb7 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -1,29 +1,19 @@
-import app from "$app";
-import config from "$config";
-import store from "$lib/store";
-import { g, rd } from "$lib/redis";
-
-import { auth, adminAuth, optionalAuth } from "$lib/passport";
-import fs from "fs";
-import { join } from "path";
-import send from "./send";
-import { err, l, warn } from "$lib/logging";
-import { fail } from "$lib/utils";
import { emit } from "$lib/sockets";
+import { db, g, s } from "$lib/db";
+import { l, err, warn } from "$lib/logging";
+import { fail } from "$lib/utils";
+import ln from "$lib/ln";
+import sendInternal from "$lib/sendInternal";
+import { requirePin } from "$lib/auth";
-import lnRoutes from "./lightning/index";
-import "./lightning/receive";
-
-app.post("/send", auth, send);
-
-app.post("/lightning/parse", lnRoutes.parse);
-app.post("/lightning/query", auth, lnRoutes.query);
-app.post("/lightning/send", auth, lnRoutes.send);
+export default {
+ async send(req, res) {
+ await requirePin(req);
+ let payment = await sendInternal(req.body, req.user);
+ res.send(payment);
+ },
-app.get(
- "/payments",
- auth,
- async ({ user: { id }, query: { start, end, limit, offset } }, res) => {
+ async list({ user: { id }, query: { start, end, limit, offset } }, res) {
if (limit) limit = parseInt(limit);
if (offset) offset = parseInt(offset);
@@ -31,144 +21,134 @@ app.get(
// if (start) where.createdAt[Op.gte] = new Date(parseInt(start));
// if (end) where.createdAt[Op.lte] = new Date(parseInt(end));
- let payments = (await rd.lRange(`${id}:payments`, 0, -1)) || [];
- payments = await Promise.all(payments.map(id => g(`payment:${id}`)));
+ let payments = (await db.lRange(`${id}:payments`, 0, -1)) || [];
+ payments = await Promise.all(payments.map(hash => g(`payment:${hash}`)));
payments = payments.filter(p => p);
res.send({ payments, total: payments.length });
- }
-);
+ },
+
+ async voucher(req, res) {
+ try {
+ const { redeemcode } = req.params;
+ let payment = await g(`voucher:${id}`);
+
+ payment = payment.get({ plain: true });
+ payment.redeemer = payment["with"];
+
+ if (!payment) fail("invalid code");
+
+ res.send(payment);
+ } catch (e) {
+ res.code(500).send(e.message);
+ }
+ },
+
+ async get({ params: { hash } }, res) {
+ res.send(await g(`payment:${hash}`));
+ },
+
+ async redeem({ body: { redeemcode } }, res) {
+ try {
+ await db.transaction(async transaction => {
+ if (!redeemcode) fail("no code provided");
+
+ let { user } = req;
+
+ const source = await db.Payment.findOne({
+ where: {
+ redeemcode: req.body.redeemcode
+ },
+ include: {
+ model: db.Account,
+ as: "account"
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+
+ l("redeeming", redeemcode);
-// app.get("/voucher/:redeemcode", async (req, res) => {
-// try {
-// const { redeemcode } = req.params;
-// let payment = await db.Payment.findOne({
-// where: {
-// redeemcode
-// },
-// include: { all: true }
-// });
-//
-// payment = payment.get({ plain: true });
-// payment.redeemer = payment["with"];
-//
-// if (!payment) fail("invalid code");
-//
-// res.send(payment);
-// } catch (e) {
-// res.code(500).send(e.message);
-// }
-// });
+ if (!source) fail("Invalid code");
+ if (source.redeemed) fail("Voucher has already been redeemed");
+ let { amount } = source;
+ amount = -amount;
-// let redeeming = {};
-// app.post("/redeem", optionalAuth, async function(req, res) {
-// const { redeemcode } = req.body;
-// try {
-// await db.transaction(async transaction => {
-// if (redeeming[redeemcode]) fail("redemption in progress");
-// redeeming[redeemcode] = true;
-// if (!redeemcode) fail("no code provided");
-//
-// let { user } = req;
-//
-// const source = await db.Payment.findOne({
-// where: {
-// redeemcode: req.body.redeemcode
-// },
-// include: {
-// model: db.Account,
-// as: "account"
-// },
-// lock: transaction.LOCK.UPDATE,
-// transaction
-// });
-//
-// l("redeeming", redeemcode);
-//
-// if (!source) fail("Invalid code");
-// if (source.redeemed) fail("Voucher has already been redeemed");
-// let { amount } = source;
-// amount = -amount;
-//
-// if (!user) {
-// const ip =
-// req.headers["x-forwarded-for"] || req.connection.remoteAddress;
-//
-// user = await register(
-// {
-// username: redeemcode.substr(0, 8),
-// password: ""
-// },
-// ip,
-// false
-// );
-//
-// let payload = { username: user.username };
-// let token = jwt.sign(payload, config.jwt);
-// res.cookie("token", token, {
-// expires: new Date(Date.now() + 432000000)
-// });
-//
-// delete redeeming[redeemcode];
-// return res.send({ user });
-// }
-//
-// let account = await getAccount(source.account.asset, user, transaction);
-// let { hash, memo, confirmed, fee, network } = source;
-//
-// source.redeemed = true;
-// (source.with_id = user.id), await source.save({ transaction });
-//
-// let payment = await db.Payment.create(
-// {
-// amount,
-// account_id: account.id,
-// user_id: user.id,
-// hash: "Voucher " + redeemcode,
-// memo,
-// rate: store.rates[user.currency],
-// currency: user.currency,
-// confirmed,
-// network,
-// received: true,
-// fee,
-// with_id: source.user_id
-// },
-// { transaction }
-// );
-//
-// await account.increment({ balance: amount }, { transaction });
-// await account.reload({ transaction });
-//
-// payment = payment.get({ plain: true });
-// payment.account = account.get({ plain: true });
-// emit(user.username, "payment", payment);
-// emit(user.username, "account", account);
-//
-// res.send({ payment });
-// });
-// } catch (e) {
-// delete redeeming[redeemcode];
-// console.log(e);
-// err("problem redeeming", e.message);
-// return res.code(500).send("There was a problem redeeming the voucher");
-// }
-// });
-//
-// app.post("/checkRedeemCode", auth, async function(req, res) {
-// const { redeemcode } = req.body;
-//
-// const payment = await db.Payment.findOne({ where: { redeemcode } });
-// res.send(payment);
-// });
-//
-// app.get("/payments/:hash", auth, async function(req, res) {
-// try {
-// let payment = await db.Payment.findOne({
-// where: { user_id: req.user.id, hash: req.params.hash }
-// });
-//
-// return payment.get({ plain: true });
-// } catch (e) {
-// console.log(e);
-// }
-// });
+ if (!user) {
+ const ip =
+ req.headers["x-forwarded-for"] || req.connection.remoteAddress;
+
+ user = await register(
+ {
+ username: redeemcode.substr(0, 8),
+ password: ""
+ },
+ ip,
+ false
+ );
+
+ let payload = { username: user.username };
+ let token = jwt.sign(payload, config.jwt);
+ res.cookie("token", token, {
+ expires: new Date(Date.now() + 432000000)
+ });
+
+ return res.send({ user });
+ }
+
+ let account = await getAccount(source.account.asset, user, transaction);
+ let { hash, memo, confirmed, fee, network } = source;
+
+ source.redeemed = true;
+ (source.with_id = user.id), await source.save({ transaction });
+
+ let payment = await db.Payment.create(
+ {
+ amount,
+ account_id: account.id,
+ user_id: user.id,
+ hash: "Voucher " + redeemcode,
+ memo,
+ rate: store.rates[user.currency],
+ currency: user.currency,
+ confirmed,
+ network,
+ received: true,
+ fee,
+ with_id: source.user_id
+ },
+ { transaction }
+ );
+
+ await account.increment({ balance: amount }, { transaction });
+ await account.reload({ transaction });
+
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
+ emit(user.username, "payment", payment);
+ emit(user.username, "account", account);
+
+ res.send({ payment });
+ });
+ } catch (e) {
+ console.log(e);
+ err("problem redeeming", e.message);
+ return res.code(500).send("There was a problem redeeming the voucher");
+ }
+ },
+
+ async sendLightning({ body: { payreq } }, res) {
+ 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) });
+ }
+};