diff options
| author | Adam Soltys <[email protected]> | 2022-09-05 21:08:53 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-05 21:08:53 -0700 |
| commit | 8a82c69419dc358fbaeab322c32d18ee84656feb (patch) | |
| tree | df71a4f11a16d241a38c30e6841fe199725bc7fb | |
| parent | a3bcd415de65a6ae14ea451d7d1c8f3f136e99d4 (diff) | |
| download | coinos-server-8a82c69419dc358fbaeab322c32d18ee84656feb.tar.gz coinos-server-8a82c69419dc358fbaeab322c32d18ee84656feb.zip | |
missing imports and fastify err responses
| -rw-r--r-- | lib/sockets.js | 6 | ||||
| -rw-r--r-- | lib/utils.js | 1 | ||||
| -rw-r--r-- | routes/assets.js | 14 | ||||
| -rw-r--r-- | routes/bitcoin/broadcast.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/fee.js | 6 | ||||
| -rw-r--r-- | routes/bitcoin/generate.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/sweep.js | 2 | ||||
| -rw-r--r-- | routes/funding.js | 10 | ||||
| -rw-r--r-- | routes/info.js | 5 | ||||
| -rw-r--r-- | routes/invoices.js | 2 | ||||
| -rw-r--r-- | routes/lightning/channel.js | 6 | ||||
| -rw-r--r-- | routes/lightning/channelRequest.js | 2 | ||||
| -rw-r--r-- | routes/lightning/invoice.js | 2 | ||||
| -rw-r--r-- | routes/lightning/query.js | 2 | ||||
| -rw-r--r-- | routes/lightning/send.js | 2 | ||||
| -rw-r--r-- | routes/liquid/broadcast.js | 2 | ||||
| -rw-r--r-- | routes/liquid/fee.js | 2 | ||||
| -rw-r--r-- | routes/liquid/generate.js | 2 | ||||
| -rw-r--r-- | routes/liquid/send.js | 2 | ||||
| -rw-r--r-- | routes/liquid/taxi.js | 2 | ||||
| -rw-r--r-- | routes/lnurl.js | 16 | ||||
| -rw-r--r-- | routes/payments.js | 2 | ||||
| -rw-r--r-- | routes/referrals.js | 4 | ||||
| -rw-r--r-- | routes/send.js | 4 | ||||
| -rw-r--r-- | routes/swaps.js | 10 | ||||
| -rw-r--r-- | routes/users.js | 40 |
27 files changed, 81 insertions, 71 deletions
diff --git a/lib/sockets.js b/lib/sockets.js index 8a5ef958..366820e0 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -1,4 +1,5 @@ import store from "./store"; +import config from "$config"; import app from "$app"; import jwt from "jsonwebtoken"; import { v4 } from "uuid"; @@ -6,6 +7,9 @@ import WebSocket from "ws"; import { differenceInSeconds, subSeconds } from "date-fns"; import fastifyWs from "@fastify/websocket"; import whitelist from "./whitelist"; +import { l, err, warn } from "$lib/logging"; +import { getUser } from "$lib/utils"; +import db from "$db"; const code = 1000; @@ -36,7 +40,7 @@ export const emit = (username, type, data) => { } if (!store.sockets[username]) return; - for (id in store.sockets[username]) { + for (let id in store.sockets[username]) { const ws = store.sockets[username][id]; ws.send(JSON.stringify({ type, data })); } diff --git a/lib/utils.js b/lib/utils.js index af50d7ec..a7db0ea3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,4 @@ +import db from "$db"; import sequelize from '@sequelize/core'; const { Op } = sequelize; diff --git a/routes/assets.js b/routes/assets.js index 4ff2577c..43098004 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -22,7 +22,7 @@ let fetchAssets; console.debug("using static assets..." + e.message); } else { err("error fetching assets", e.message); - res.status(500).send("error fetching assets"); + res.code(500).send("error fetching assets"); } } @@ -65,7 +65,7 @@ app.get( res.send(store.assets); } else { console.log("error getting blockstream assets"); - res.status(500).send("Problem fetching blockstream asset registry data"); + res.code(500).send("Problem fetching blockstream asset registry data"); } }) @@ -293,7 +293,7 @@ app.post( res.send(issuances[txid] ? issuances[txid] : { asset }); } catch (e) { err("asset issuance failed", e.message, e.stack); - res.status(500).send(e.message); + res.code(500).send(e.message); } }) @@ -323,7 +323,7 @@ app.post( res.send(result); } catch (e) { err("asset registration failed", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }) @@ -331,7 +331,7 @@ app.post( "/loadFaucet", auth, async (req, res) => { - return res.status(500).send("Faucet feature temporarily disabled"); + return res.code(500).send("Faucet feature temporarily disabled"); const { user } = req; let { asset, amount } = req.body; @@ -429,11 +429,11 @@ app.post( emit(user.username, "user", user); l("loaded faucet", asset, amount); - res.end(); + res.send({}); }); } catch (e) { err("problem loading faucet", user.username, e.message); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } }) diff --git a/routes/bitcoin/broadcast.js b/routes/bitcoin/broadcast.js index 2e8c61b7..ec733101 100644 --- a/routes/bitcoin/broadcast.js +++ b/routes/bitcoin/broadcast.js @@ -46,6 +46,6 @@ export default async (req, res) => { res.send(result); } } catch (e) { - res.status(500).send(e.message); + res.code(500).send(e.message); } }; diff --git a/routes/bitcoin/fee.js b/routes/bitcoin/fee.js index fe0c7a26..a47ac05e 100644 --- a/routes/bitcoin/fee.js +++ b/routes/bitcoin/fee.js @@ -10,7 +10,7 @@ export default async (req, res) => { let psbt = await buildTx({ address, amount, feeRate, replaceable, user }); return res.send(psbt); } catch (e) { - return res.status(500).send(e.message); + return res.code(500).send(e.message); } } @@ -27,7 +27,7 @@ export default async (req, res) => { let { ismine } = await bc.getAddressInfo(address); if (ismine) { emit(user.username, "to", invoice.user); - return res.end(); + return res.send({}); } } @@ -62,6 +62,6 @@ export default async (req, res) => { res.send({ feeRate, tx }); } catch (e) { err("bitcoin fee estimation error", e); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } }; diff --git a/routes/bitcoin/generate.js b/routes/bitcoin/generate.js index 1b6c1aa4..6285492d 100644 --- a/routes/bitcoin/generate.js +++ b/routes/bitcoin/generate.js @@ -1,5 +1,5 @@ export default async (req, res) => { let address = await bc.getNewAddress(); await bc.generateToAddress(1, address); - res.end(); + res.send({}); }; diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index a0ff3bb6..1992bbde 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -173,6 +173,6 @@ export default async (req, res) => { "The coinos server hot wallet has insufficient funds to complete the payment, try again later"; err("error sending bitcoin", e.message); console.log(e); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } }; diff --git a/routes/bitcoin/sweep.js b/routes/bitcoin/sweep.js index 8bb51f14..49fb1f04 100644 --- a/routes/bitcoin/sweep.js +++ b/routes/bitcoin/sweep.js @@ -52,7 +52,7 @@ export default async (req, res) => { if (!inputs || !outputs) return res - .status(500) + .code(500) .send("Unable to construct sweep transaction, try a lower fee rate?"); let psbt = new bitcoin.Psbt({ network }); diff --git a/routes/funding.js b/routes/funding.js index c8654a31..b2275b48 100644 --- a/routes/funding.js +++ b/routes/funding.js @@ -24,7 +24,7 @@ app.post( mailgun.messages().send(data); l("id uploaded", user.username); - res.end(); + res.send({}); }) app.post( @@ -39,7 +39,7 @@ app.post( emit(user.username, "user", user); l("id proof uploaded", user.username); - res.end(); + res.send({}); }) app.post( @@ -78,7 +78,7 @@ app.post( res.send(deposit); } catch (e) { err("funding error", e.message); - res.status(500).send("Funding request failed"); + res.code(500).send("Funding request failed"); } }) @@ -101,9 +101,9 @@ app.post( notes }); - res.end(); + res.send({}); } catch (e) { err("withdrawaerr", e.message); - res.status(500).send("Withdrawal request failed"); + res.code(500).send("Withdrawal request failed"); } }) diff --git a/routes/info.js b/routes/info.js index 06736b9a..001caf85 100644 --- a/routes/info.js +++ b/routes/info.js @@ -1,5 +1,8 @@ import app from "$app"; import sequelize from '@sequelize/core'; +import config from "$config"; +import store from "$lib/store"; + const { Op } = sequelize; /** @@ -15,7 +18,7 @@ app.get("/info", async (req, res, next) => { const { clientVersion } = config; const info = { - nodes: networks, + nodes: store.networks, clientVersion, }; diff --git a/routes/invoices.js b/routes/invoices.js index bb65f811..e1948c0b 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -95,7 +95,7 @@ app.post("/invoice", optionalAuth, async (req, res, next) => { res.send(invoice); } catch (e) { err(e.message, e.stack); - res.status(500).send(`Problem during invoice creation: ${e.message}`); + res.code(500).send(`Problem during invoice creation: ${e.message}`); } }); diff --git a/routes/lightning/channel.js b/routes/lightning/channel.js index 4dbcfd20..5e8ed441 100644 --- a/routes/lightning/channel.js +++ b/routes/lightning/channel.js @@ -14,7 +14,7 @@ export default async (req, res) => { } catch (e) { if (!e.message.includes("already connected")) { err("problem connecting to peer", e.message); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } } @@ -24,7 +24,7 @@ export default async (req, res) => { remoteid = (await lnp.getInfo({})).identity_pubkey; } catch (e) { err("problem getting lightning node info", e.message); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } let url = `${callback}?k1=${k1}&remoteid=${remoteid}&private=0`; @@ -34,6 +34,6 @@ export default async (req, res) => { res.send(response.data); } catch (e) { err("problem sending channel request", e.message); - return res.status(500).send({ status: "ERROR", reason: e.message }); + return res.code(500).send({ status: "ERROR", reason: e.message }); } }; diff --git a/routes/lightning/channelRequest.js b/routes/lightning/channelRequest.js index e8938929..447a3946 100644 --- a/routes/lightning/channelRequest.js +++ b/routes/lightning/channelRequest.js @@ -18,6 +18,6 @@ export default async (req, res) => { res.send(result); } catch (e) { err("problem generating channel request", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }; diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index f744dd22..40787e3c 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -19,6 +19,6 @@ export default async (req, res) => { } } catch (e) { err("problem creating invoice", e.message, e.stack); - res.status(500).send(e.message); + res.code(500).send(e.message); } }; diff --git a/routes/lightning/query.js b/routes/lightning/query.js index 36c297f6..d01fe442 100644 --- a/routes/lightning/query.js +++ b/routes/lightning/query.js @@ -13,5 +13,5 @@ export default async (req, res) => { }); if (invoice) emit(req.user.username, "to", invoice.user); - res.end(); + res.send({}); }; diff --git a/routes/lightning/send.js b/routes/lightning/send.js index a482091a..c5de7ece 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -11,6 +11,6 @@ export default async (req, res) => { payreq, e.message ); - res.status(500).send(e.message); + res.code(500).send(e.message); } }; diff --git a/routes/liquid/broadcast.js b/routes/liquid/broadcast.js index e9cbcaf5..5f44558e 100644 --- a/routes/liquid/broadcast.js +++ b/routes/liquid/broadcast.js @@ -48,6 +48,6 @@ export default async (req, res) => { } } catch (e) { err("problem broadcasting liquid transaction", e); - res.status(500).send(e.message); + res.code(500).send(e.message); } }; diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index e5b9ad74..7c461d70 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -79,6 +79,6 @@ export default async (req, res) => { res.send(tx); } catch (e) { err("error estimating liquid fee", e.message, e.stack); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } }; diff --git a/routes/liquid/generate.js b/routes/liquid/generate.js index 73ad9b59..423d8b6a 100644 --- a/routes/liquid/generate.js +++ b/routes/liquid/generate.js @@ -1,5 +1,5 @@ export default async (req, res) => { let address = await lq.getNewAddress(); await lq.generateToAddress(1, address); - res.end(); + res.send({}); }; diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 70d858e6..2e91f013 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -257,6 +257,6 @@ export default async (req, res) => { res.send(await sendLiquid({ ...req.body, user })); } catch (e) { err("problem sending liquid", user.username, e.message); - return res.status(500).send(e); + return res.code(500).send(e); } }; diff --git a/routes/liquid/taxi.js b/routes/liquid/taxi.js index e5baeeb9..32d0d636 100644 --- a/routes/liquid/taxi.js +++ b/routes/liquid/taxi.js @@ -80,6 +80,6 @@ export default async (req, res) => { res.send({ txid }); } catch (e) { console.log(e); - return res.status(500).send(e); + return res.code(500).send(e); } }; diff --git a/routes/lnurl.js b/routes/lnurl.js index e7bdfd71..71023ca8 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -53,7 +53,7 @@ app.get("/withdraw", auth, async (req, res, next) => { res.send(result); } catch (e) { err("problem generating withdrawl url", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -70,7 +70,7 @@ app.post("/code", async (req, res, next) => { res.send(code); } catch (e) { err(e.errors); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -99,7 +99,7 @@ app.post("/withdraw", auth, async (req, res, next) => { res.send(result); } catch (e) { err("failed to withdraw", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -136,7 +136,7 @@ app.get("/lnurlp/:username", async (req, res, next) => { res.send(result.data); } catch (e) { err("problem generating payment url", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -165,7 +165,7 @@ app.get("/pay/:username", async (req, res, next) => { res.send(result); } catch (e) { err("problem generating payment url", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -208,7 +208,7 @@ app.post("/pay", auth, async (req, res, next) => { } } catch (e) { err("failed to send payment", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -223,7 +223,7 @@ app.get("/login", optionalAuth, async (req, res, next) => { res.send(result); } catch (e) { err("problem generating login url", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -257,7 +257,7 @@ app.get("/decode", async (req, res, next) => { res.send(params); } catch (e) { err("problem decoding lnurl", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }); diff --git a/routes/payments.js b/routes/payments.js index 4d1debc0..741de6e7 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -133,6 +133,6 @@ app.get("/payment/:redeemcode", async (req, res) => { res.send(payment); } catch (e) { - res.status(500).send(e.message); + res.code(500).send(e.message); } }); diff --git a/routes/referrals.js b/routes/referrals.js index 87cef102..08059c74 100644 --- a/routes/referrals.js +++ b/routes/referrals.js @@ -194,10 +194,10 @@ router.get( return res.send({ verified: true, sponsor_id: found.sponsor_id, updated: found.updated_at}); } else { - res.status(500).send({ verified: false, message: 'Referral already ' + found[0].status }) + res.code(500).send({ verified: false, message: 'Referral already ' + found[0].status }) } } else { - res.status(500).send('Invalid referral token') + res.code(500).send('Invalid referral token') } }) diff --git a/routes/send.js b/routes/send.js index 67dbaca4..5c9203ba 100644 --- a/routes/send.js +++ b/routes/send.js @@ -21,7 +21,7 @@ export default async (req, res, next) => { if (!asset) asset = config.liquid.btcasset; if (!amount || amount < 0) - return res.status(500).send("Amount must be greater than zero"); + return res.code(500).send("Amount must be greater than zero"); try { await db.transaction(async transaction => { @@ -212,6 +212,6 @@ export default async (req, res, next) => { } catch (e) { console.log(e); err("problem sending internal payment", user.username, e.message); - return res.status(500).send(e.message); + return res.code(500).send(e.message); } }; diff --git a/routes/swaps.js b/routes/swaps.js index 9f7cfcbf..b37708d6 100644 --- a/routes/swaps.js +++ b/routes/swaps.js @@ -340,7 +340,7 @@ app.delete( const { user } = req; await cancel(user, id); - res.end(); + res.send({}); }) ); @@ -348,14 +348,14 @@ app.post( "/orders", auth, async (req, res) => { - return res.status(500).send("Trading temporarily disabled"); + return res.code(500).send("Trading temporarily disabled"); const { user } = req; try { await swap(user, req.body); - res.end(); + res.send({}); } catch (e) { err(req.user.username, e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } }) ); @@ -393,7 +393,7 @@ app.get( }) ); } catch (e) { - res.status(500).send(e.message); + res.code(500).send(e.message); } }) ); diff --git a/routes/users.js b/routes/users.js index c6a8ec6a..5e27be49 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,4 +1,5 @@ import app from "$app"; +import config from "$config"; import { auth, optionalAuth } from "$lib/passport"; import { getUser } from "$lib/utils"; import axios from "axios"; @@ -14,6 +15,7 @@ import { Mutex } from "async-mutex"; import bip32 from "bip32"; import whitelist from "$lib/whitelist"; import { l, err, warn } from "$lib/logging"; +import db from "$db"; const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); @@ -67,7 +69,7 @@ app.get("/users/:username", async (req, res) => { }); if (user) res.send(user); - else res.status(500).send("User not found"); + else res.code(500).send("User not found"); }); app.post("/register", async (req, res) => { @@ -76,7 +78,7 @@ app.post("/register", async (req, res) => { const user = await register(req.body.user, ip, false); res.send(pick(user, ...whitelist)); } catch (e) { - res.status(500).send(e.message); + res.code(500).send(e.message); } }); @@ -93,7 +95,7 @@ app.post("/disable2fa", auth, twofa, async (req, res) => { app.get("/otpsecret", auth, twofa, async (req, res) => { let { user } = req; emit(user.username, "otpsecret", user.otpsecret); - res.end(); + res.send({}); }); app.post("/2fa", auth, async (req, res) => { @@ -105,7 +107,7 @@ app.post("/2fa", auth, async (req, res) => { user.save(); emit(user.username, "user", req.user); } else { - return res.status(500).send("Invalid token"); + return res.code(500).send("Invalid token"); } } catch (e) { err("error setting up 2fa", e); @@ -149,7 +151,7 @@ app.post("/user", auth, async (req, res) => { let token; if (user.username !== username && exists) { err("username taken", username, user.username, exists.username); - return res.status(500).send("Username taken"); + return res.code(500).send("Username taken"); } else { sockets[username] = sockets[user.username]; if (user.username !== username) @@ -230,14 +232,14 @@ app.post("/updateSeeds", auth, async (req, res) => { emit(user.username, "account", account); } - res.end(); + res.send({}); }); app.post("/accounts/delete", auth, async (req, res) => { const { id } = req.body; const account = await db.Account.findOne({ where: { id } }); if (account) await account.destroy(); - res.end(); + res.send({}); }); app.post("/accounts", auth, async (req, res) => { @@ -292,7 +294,7 @@ let login = async (req, res) => { res.send(response.data); } catch (e) { err("problem calling lnurl login", e.message); - res.status(500).send(e.message); + res.code(500).send(e.message); } return; @@ -308,7 +310,7 @@ let login = async (req, res) => { !(await bcrypt.compare(req.body.password, user.password))) ) { warn("invalid username or password attempt", req.body.username); - return res.status(401).end(); + return res.status(401).send({}); } if ( @@ -334,7 +336,7 @@ let login = async (req, res) => { res.send({ user, token }); } catch (e) { err("login error", e.message, req.connection.remoteAddress); - res.status(401).end(); + res.status(401).send({}); } }; @@ -344,7 +346,7 @@ app.post("/doggin", login); app.post("/logout", optionalAuth, async (req, res) => { let { subscription } = req.body; - if (!subscription) return res.end(); + if (!subscription) return res.send({}); const { username } = req.user; @@ -362,7 +364,7 @@ app.post("/logout", optionalAuth, async (req, res) => { ); } - res.end(); + res.send({}); }); app.get("/address", async (req, res) => { @@ -382,7 +384,7 @@ app.get("/address", async (req, res) => { try { if (network === "bitcoin") { if (!config.bitcoin) - return res.status(500).send("Bitcoin not configured"); + return res.code(500).send("Bitcoin not configured"); address = await bc.getNewAddress("", type); bcAddressIndex = i + 1; @@ -520,11 +522,11 @@ app.post("/account", auth, async (req, res) => { await account.reload({ transaction }); emit(user.username, "account", account); - res.end(); + res.send({}); }); } catch (e) { err("problem updating account", e.message); - return res.status(500).send("There was a problem updating the account"); + return res.code(500).send("There was a problem updating the account"); } }); @@ -538,7 +540,7 @@ app.post("/shiftAccount", auth, async (req, res) => { }); if (account.user_id !== user.id) - return res.status(500).send("Failed to open wallet"); + return res.code(500).send("Failed to open wallet"); user.account_id = account.id; await user.save(); @@ -564,7 +566,7 @@ app.post("/shiftAccount", auth, async (req, res) => { res.send(user); } catch (e) { err("problem switching account", e.message); - return res.status(500).send("There was a problem switching accounts"); + return res.code(500).send("There was a problem switching accounts"); } }); @@ -675,7 +677,7 @@ app.post("/redeem", optionalAuth, async function(req, res) { } catch (e) { delete redeeming[redeemcode]; err("problem redeeming", e.message); - return res.status(500).send("There was a problem redeeming the voucher"); + return res.code(500).send("There was a problem redeeming the voucher"); } }); @@ -744,5 +746,5 @@ app.post("/signMessage", auth, async function(req, res) { return res.send(await bc.signMessage(address, message)); } - res.status(500).send("Address not found for user"); + res.code(500).send("Address not found for user"); }); |
