From 1962a6dcf1bb37bb8beb3fc4c0231d5a504a75fe Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 17 Jul 2020 20:55:47 -0700 Subject: memos --- db/models/invoices.js | 9 +++++++++ db/models/payments.js | 9 +++++++++ db/schema.sql | 10 ++++++---- lib/send.js | 3 ++- lib/sockets.js | 39 +++++++++++++++++++++++++++++---------- routes/bitcoin/receive.js | 2 ++ routes/bitcoin/send.js | 3 ++- routes/lightning/invoice.js | 7 ++++--- routes/lightning/receive.js | 3 ++- routes/lightning/send.js | 5 ++--- routes/liquid/receive.js | 2 ++ routes/liquid/send.js | 3 ++- routes/send.js | 4 +++- 13 files changed, 74 insertions(+), 25 deletions(-) diff --git a/db/models/invoices.js b/db/models/invoices.js index 6fb68c31..231ed6f6 100644 --- a/db/models/invoices.js +++ b/db/models/invoices.js @@ -64,6 +64,15 @@ const attributes = { comment: null, field: "uuid" }, + memo: { + type: DataTypes.TEXT, + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "memo" + }, network: { type: DataTypes.STRING(255), allowNull: true, diff --git a/db/models/payments.js b/db/models/payments.js index 51ef2550..f5d970bb 100644 --- a/db/models/payments.js +++ b/db/models/payments.js @@ -28,6 +28,15 @@ const attributes = { comment: null, field: "user_id" }, + memo: { + type: DataTypes.TEXT, + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "memo" + }, hash: { type: DataTypes.TEXT, allowNull: true, diff --git a/db/schema.sql b/db/schema.sql index e6a3c4b8..940a3490 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -57,7 +57,7 @@ CREATE TABLE `accounts` ( `ticker` varchar(255) DEFAULT NULL, `precision` int(11) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3729 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=3730 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -96,8 +96,9 @@ CREATE TABLE `invoices` ( `network` varchar(255) DEFAULT NULL, `unconfidential` varchar(255) DEFAULT NULL, `uuid` varchar(255) DEFAULT NULL, + `memo` text DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4600 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4626 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -141,8 +142,9 @@ CREATE TABLE `payments` ( `network` varchar(255) DEFAULT NULL, `account_id` int(11) DEFAULT NULL, `preimage` varchar(255) DEFAULT NULL, + `memo` text DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4437 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4458 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -202,4 +204,4 @@ CREATE TABLE `users` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2020-07-16 13:52:58 +-- Dump completed on 2020-07-17 20:55:55 diff --git a/lib/send.js b/lib/send.js index 71209456..7850da08 100644 --- a/lib/send.js +++ b/lib/send.js @@ -1,6 +1,6 @@ const bolt11 = require("bolt11"); -send = async (amount, hash, user) => { +send = async (amount, memo, hash, user) => { let payreq = bolt11.decode(hash); if (!amount) amount = payreq.satoshis; @@ -93,6 +93,7 @@ send = async (amount, hash, user) => { account_id: account.id, user_id: user.id, hash, + memo, preimage, rate: app.get("rates")[user.currency], currency: user.currency, diff --git a/lib/sockets.js b/lib/sockets.js index d0f00771..2dae9e93 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -26,19 +26,19 @@ broadcast = (type, data) => { } }; -server.on("upgrade", function(req, socket, head) { - wss.handleUpgrade(req, socket, head, function(ws) { +server.on("upgrade", function (req, socket, head) { + wss.handleUpgrade(req, socket, head, function (ws) { wss.emit("connection", ws, req); }); }); const names = {}; -wss.on("connection", function(ws, req) { +wss.on("connection", function (ws, req) { const id = uuidv4(); ws.send(JSON.stringify({ type: "connected" })); - ws.on("message", async message => { + ws.on("message", async (message) => { let type, data; try { ({ type, data } = JSON.parse(message)); @@ -57,22 +57,24 @@ wss.on("connection", function(ws, req) { const existing = Object.keys(sockets[username]); if (existing.length > 5) { l.warn("too many sockets, logging out", existing[0]); - sockets[username][existing[0]].send(JSON.stringify({ type: "login", data: false })); - } + sockets[username][existing[0]].send( + JSON.stringify({ type: "login", data: false }) + ); + } sockets[username][id] = ws; names[id] = username; let user = await getUser(username); let payments = await user.getPayments({ where: { - account_id: user.account_id + account_id: user.account_id, }, order: [["id", "DESC"]], limit: 12, include: { model: db.Account, - as: "account" - } + as: "account", + }, }); let accounts = await user.getAccounts(); let keys = await user.getKeys(); @@ -106,12 +108,29 @@ wss.on("connection", function(ws, req) { case "lnurl": sessions[data] = ws; break; + case "updateMemo": + const payment = await db.Payment.findOne({ + where: { + user_id: ws.user.id, + id: data.id, + }, + include: { + model: db.Account, + as: "account", + }, + }); + if (payment) { + payment.memo = data.memo; + await payment.save(); + emit(ws.user.username, "payment", payment); + } + break; default: l.warn("received socket message of unknown type", type, data); } }); - ws.on("close", function() { + ws.on("close", function () { try { const username = names[id]; if (sockets[username] && sockets[username][id]) diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 131166bc..f4fd4294 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -55,6 +55,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { const currency = invoice ? invoice.currency : user.currency; const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; const tip = invoice ? invoice.tip : 0; + const memo = invoice ? invoice.memo : ''; let confirmed = false; @@ -92,6 +93,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { user_id: user.id, hash, fee, + memo, amount: value - tip, currency, rate, diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 2ab28054..fcdcb298 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -3,7 +3,7 @@ const reverse = require("buffer-reverse"); module.exports = async (req, res) => { let { user } = req; - let { address, tx } = req.body; + let { address, memo, tx } = req.body; let { hex } = tx; let fee = toSats(tx.fee); @@ -56,6 +56,7 @@ module.exports = async (req, res) => { params = { amount: -total, fee, + memo, account_id: account.id, user_id: user.id, rate: app.get("rates")[user.currency], diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index 3b453a73..682fe542 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -1,14 +1,15 @@ module.exports = async (req, res) => { - const { amount, tip } = req.body; + const { amount, memo, tip } = req.body; l.info("adding lightning invoice", req.user.username, amount, tip); try { if (config.lna.clightning) { - const invoice = await lnb.invoice(`${amount + tip}sat` || "any", new Date(), "", 360); + const invoice = await lnb.invoice(`${amount + tip}sat` || "any", new Date(), memo, 360); res.send(invoice.bolt11); } else { - const invoice = await lnb.addInvoice({ value: amount + tip }); + const invoice = await lnb.addInvoice({ value: amount + tip, memo }); + l.info("invoice", invoice); res.send(invoice.payment_request); } } catch (e) { diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 292f19e2..34ac1fc3 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -13,7 +13,7 @@ const handlePayment = async msg => { if (!invoice) return l.warn("received lightning with no invoice", msg.payment_request); - const { text: hash, currency, rate, tip, user_id } = invoice; + const { text: hash, currency, memo, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; const account = await db.Account.findOne({ @@ -37,6 +37,7 @@ const handlePayment = async msg => { account_id: account.id, user_id, hash, + memo, amount, currency, preimage, diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 9aa4ddaf..be68143e 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -1,10 +1,9 @@ module.exports = async (req, res) => { - let { payreq } = req.body; - let { amount, route } = req.body; + let { amount, route, memo, payreq } = req.body; let { user } = req; try { - res.send(await send(amount, payreq, user)); + res.send(await send(amount, memo, payreq, user)); } catch (e) { l.error("problem sending lightning payment", user.username, e.message); res.status(500).send(e.message); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index f6783eee..45419ecb 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -100,6 +100,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { const currency = invoice ? invoice.currency : user.currency; const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; const tip = invoice ? invoice.tip : 0; + const memo = invoice ? invoice.memo : ''; let payment = await db.Payment.create({ account_id: account.id, @@ -107,6 +108,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { hash: blinded.txid, amount: value - tip, currency, + memo, rate, received: true, tip, diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 35a155ec..a443edf3 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -2,7 +2,7 @@ const reverse = require("buffer-reverse"); module.exports = async (req, res) => { let { user } = req; - let { address, tx } = req.body; + let { address, memo, tx } = req.body; const isChange = async (address) => (await lq.getAddressInfo(address)).ismine && @@ -72,6 +72,7 @@ module.exports = async (req, res) => { amount: -total, account_id: account.id, fee, + memo, user_id: user.id, rate: app.get("rates")[user.currency], currency: user.currency, diff --git a/routes/send.js b/routes/send.js index 8b4137d8..d2ba3e5d 100644 --- a/routes/send.js +++ b/routes/send.js @@ -1,7 +1,7 @@ const axios = require("axios"); module.exports = async (req, res) => { - let { amount, asset, username } = req.body; + let { amount, asset, memo, username } = req.body; let { user } = req; l.info("attempting internal payment", user.username, amount); @@ -32,6 +32,7 @@ module.exports = async (req, res) => { { amount: -amount, account_id: account.id, + memo, user_id: user.id, rate: app.get("rates")[user.currency], currency: user.currency, @@ -111,6 +112,7 @@ module.exports = async (req, res) => { currency: user.currency, confirmed: true, hash: "Internal Transfer", + memo, network: "COINOS", received: true }, -- cgit v1.2.3