From 3ae8bf7c50f7a1a845de0ae1387b8fec8ae77b4f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 9 Jun 2022 01:06:06 -0400 Subject: tweaks to sync routine --- index.js | 2 +- lib/sync.js | 183 +++++++++++++++++++++++++++++++------------------------- routes/users.js | 69 ++------------------- 3 files changed, 108 insertions(+), 146 deletions(-) diff --git a/index.js b/index.js index 5dd6c93c..d98e0df0 100644 --- a/index.js +++ b/index.js @@ -47,7 +47,7 @@ app.use((err, req, res, next) => { if (req.user) details.username = req.user.username; - l.error("JSON Error: ", details); + l.error("JSON Error: ", JSON.stringify(details)); res.status(500); res.set({ "Cache-Control": "no-cache" diff --git a/lib/sync.js b/lib/sync.js index e217dcd0..18ca5186 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -44,22 +44,23 @@ init(); const sync = async () => { const twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); - const payments = await db.Payment.findAll({ - attributes: ["hash", "confirmed", "preimage"], - where: { createdAt: { [Op.gt]: twoWeeksAgo } }, - include: [ - { - model: db.Account, - as: "account" - }, - { - model: db.User, - as: "user" - } - ] - }); try { + const payments = await db.Payment.findAll({ + attributes: ["hash", "confirmed", "preimage"], + where: { createdAt: { [Op.gt]: twoWeeksAgo } }, + include: [ + { + model: db.Account, + as: "account" + }, + { + model: db.User, + as: "user" + } + ] + }); + let { invoices } = await lnp.listInvoices({ reversed: true, num_max_invoices: 1000 @@ -89,45 +90,50 @@ const sync = async () => { let missed = recent.filter(i => !settled.includes(i.preimage)); for (let i = 0; i < missed.length; i++) { let { amount, preimage, pr: text, createdAt } = missed[i]; - let invoice = await db.Invoice.findOne({ - where: { text }, - include: { - model: db.User, - as: "user" - } - }); - - if (invoice && invoice.user_id) { - let account = await db.Account.findOne({ - where: { - user_id: invoice.user_id, - asset: config.liquid.btcasset, - pubkey: null + + try { + let invoice = await db.Invoice.findOne({ + where: { text }, + include: { + model: db.User, + as: "user" } }); - if (account) { - let payment = await db.Payment.create({ - account_id: account.id, - user_id: invoice.user_id, - hash: text, - amount, - currency: invoice.currency, - preimage, - rate: invoice.rate, - received: true, - confirmed: true, - network: "lightning", - tip: invoice.tip, - invoice_id: invoice.id, - createdAt, - updatedAt: createdAt + if (invoice && invoice.user_id) { + let account = await db.Account.findOne({ + where: { + user_id: invoice.user_id, + asset: config.liquid.btcasset, + pubkey: null + } }); - await account.increment({ balance: amount }); + if (account) { + let payment = await db.Payment.create({ + account_id: account.id, + user_id: invoice.user_id, + hash: text, + amount, + currency: invoice.currency, + preimage, + rate: invoice.rate, + received: true, + confirmed: true, + network: "lightning", + tip: invoice.tip, + invoice_id: invoice.id, + createdAt, + updatedAt: createdAt + }); - l.info("lightning missing from account", account.id, amount); + await account.increment({ balance: amount }); + + l.info("lightning missing from account", account.id, amount); + } } + } catch (e) { + l.error("problem syncing lightning payment", JSON.stringify(missed[i]), e.message, e.stack); } } @@ -152,7 +158,11 @@ const sync = async () => { const transactions = [ ...(await bc.listTransactions("*", 500)), ...(await lq.listTransactions("*", 500)) - ].filter(tx => ["send", "receive"].includes(tx.category) && new Date(tx.time * 1000) > twoWeeksAgo); + ].filter( + tx => + ["send", "receive"].includes(tx.category) && + new Date(tx.time * 1000) > twoWeeksAgo + ); missed = transactions.filter( tx => @@ -165,17 +175,21 @@ const sync = async () => { let p = unconfirmed.find(p => p.hash === missed[i].txid); p.confirmed = 1; - await db.transaction(async transaction => { - await p.save({ transaction }); + try { + await db.transaction(async transaction => { + await p.save({ transaction }); - await p.account.increment({ balance: p.amount }, { transaction }); - await p.account.decrement({ pending: p.amount }, { transaction }); - }); + await p.account.increment({ balance: p.amount }, { transaction }); + await p.account.decrement({ pending: p.amount }, { transaction }); + }); - emit(p.user.username, "account", p.account); - emit(p.user.username, "payment", p); + emit(p.user.username, "account", p.account); + emit(p.user.username, "payment", p); - l.info("unconfirmed tx", p.user_id, p.hash, p.address); + l.info("unconfirmed tx", p.user_id, p.hash, p.address); + } catch (e) { + l.error("problem confirming payment", p, e.message, e.stack); + } } unaccounted = []; @@ -250,32 +264,41 @@ const sync = async () => { amount = toSats(amount); - await db.transaction(async transaction => { - l.info("found pending transaction", user.username, amount, hash); - payments.push(hash); - let p = await db.Payment.create( - { - account_id, - address: address || unconfidential, - amount, - confirmed: false, - currency, - hash, - invoice_id, - network, - rate, - received: true, - tip, - user_id - }, - { transaction } + try { + await db.transaction(async transaction => { + l.info("found pending transaction", user.username, amount, hash); + payments.push(hash); + let p = await db.Payment.create( + { + account_id, + address: address || unconfidential, + amount, + confirmed: false, + currency, + hash, + invoice_id, + network, + rate, + received: true, + tip, + user_id + }, + { transaction } + ); + + await account.increment({ pending: amount }, { transaction }); + + emit(user.username, "account", account); + emit(user.username, "payment", p); + }); + } catch (e) { + l.error( + "problem updating pending transaction", + txid, + e.message, + e.stack ); - - await account.increment({ pending: amount }, { transaction }); - - emit(user.username, "account", account); - emit(user.username, "payment", p); - }); + } } } } catch (e) { diff --git a/routes/users.js b/routes/users.js index 6bf9a063..78b3adf7 100644 --- a/routes/users.js +++ b/routes/users.js @@ -302,9 +302,7 @@ app.post( }) ); -app.post( - "/login", - ah(async (req, res) => { +let login = ah(async (req, res) => { try { const { params, sig, key } = req.body; @@ -362,69 +360,10 @@ app.post( res.status(401).end(); } }) -); - -app.post( - "/doggin", - ah(async (req, res) => { - try { - const { params, sig, key } = req.body; - - if (sig) { - const { callback } = params; - - try { - const url = `${callback}&sig=${sig}&key=${key}`; - const response = await axios.get(url); - res.send(response.data); - } catch (e) { - l.error("problem calling lnurl login", e.message); - res.status(500).send(e.message); - } - - return; - } - - let twofa = req.body.token; - - let user = await getUser(req.body.username); - if ( - !user || - (user.password && - !(await bcrypt.compare(req.body.password, user.password))) - ) { - l.warn("invalid username or password attempt", req.body.username); - return res.status(401).end(); - } - - if ( - user.twofa && - (typeof twofa === "undefined" || - !authenticator.check(twofa, user.otpsecret)) - ) { - return res.status(401).send("2fa required"); - } - - l.info( - "login", - req.body.username, - req.headers["x-forwarded-for"] || req.connection.remoteAddress - ); - - let payload = { username: user.username }; - let token = jwt.sign(payload, config.jwt); - res.cookie("token", token, { expires: new Date(Date.now() + 432000000) }); - user.accounts = await user.getAccounts(); - user.keys = await user.getKeys(); - user = pick(user, ...whitelist); - res.send({ user, token }); - } catch (e) { - l.error("login error", e.message, req.connection.remoteAddress); - res.status(401).end(); - } - }) -); +app.post("/login", login); +app.post("/taboggan", login); +app.post("/doggin", login); app.post( "/logout", -- cgit v1.2.3