aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/user.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
committerAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
commitc0888fab024cc9f7c99812bfabf9d7774312dbf7 (patch)
treecad95683af97432f6092a2c2bae10fbb7a588db5 /routes/lightning/user.js
parentbf2b51f0c2ae9b8f0f330dde6411e45fc0da8a25 (diff)
downloadcoinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.tar.gz
coinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.zip
refactor
Diffstat (limited to 'routes/lightning/user.js')
-rw-r--r--routes/lightning/user.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/routes/lightning/user.js b/routes/lightning/user.js
new file mode 100644
index 00000000..c361600c
--- /dev/null
+++ b/routes/lightning/user.js
@@ -0,0 +1,46 @@
+const bolt11 = require("bolt11");
+
+module.exports = async (req, res) => {
+ let { payuser, amount } = req.body;
+
+ let user = await db.User.findOne({
+ where: {
+ username: payuser
+ }
+ });
+
+ if (!user) {
+ return res.status(500).send("Couldn't find the user you're trying to pay");
+ }
+ let err = m => res.status(500).send(m);
+
+ let invoice;
+ try {
+ invoice = await lnb.addInvoice({ value: amount });
+ } catch (e) {
+ return err(e.message);
+ }
+
+ let hash = invoice.payment_request;
+
+ await db.Payment.create({
+ user_id: user.id,
+ hash,
+ amount,
+ rate: app.get("rates")[req.user.currency],
+ currency: req.user.currency,
+ tip: 0,
+ confirmed: true,
+ received: false,
+ asset: 'LNBTC',
+ });
+
+ req.url = "/sendPayment";
+ let payreq = bolt11.decode(hash);
+ const { routes } = await lna.queryRoutes({ "pub_key": payreq.payeeNodeKey, "amt": payreq.satoshis });
+ if (routes.length) req.body.route = routes[0];
+ else return res.status(500).send("No route available");
+ req.body.payreq = hash;
+
+ return app._router.handle(req, res);
+};