diff options
| author | Adam Soltys <[email protected]> | 2022-10-05 12:15:48 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-10-05 12:15:48 -0700 |
| commit | fb91aa048617f0894e01c1e890ec957a3170539f (patch) | |
| tree | 5630fbe3bb5eb67fc0fba2bba79b8b763609d93a /routes | |
| parent | 160d95f693d7d8a17e864313b06790a841d4e160 (diff) | |
| download | coinos-server-fb91aa048617f0894e01c1e890ec957a3170539f.tar.gz coinos-server-fb91aa048617f0894e01c1e890ec957a3170539f.zip | |
prettier
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/bitcoin/receive.js | 3 | ||||
| -rw-r--r-- | routes/info.js | 102 | ||||
| -rw-r--r-- | routes/invoices.js | 25 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 4 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 3 | ||||
| -rw-r--r-- | routes/lnurl.js | 99 | ||||
| -rw-r--r-- | routes/payments.js | 2 | ||||
| -rw-r--r-- | routes/users.js | 114 |
8 files changed, 193 insertions, 159 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 3bc1750d..665cc2bc 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -231,9 +231,12 @@ setInterval(async () => { { transaction } ); await account.reload({ transaction }); + await invoice.reload({ transaction }); await p.save({ transaction }); p = p.get({ plain: true }); + p.account = account.get({ plain: true }); + p.invoice = invoice.get({ plain: true }); emit(user.username, "account", account); emit(user.username, "payment", p); diff --git a/routes/info.js b/routes/info.js index fd77c5d1..6c2c601d 100644 --- a/routes/info.js +++ b/routes/info.js @@ -37,63 +37,63 @@ app.get("/info", async (req, res, next) => { app.get("/balances", async (req, res, next) => { try { - const accounts = await db.Account.findAll({ - attributes: [ - "asset", - "pubkey", - [sequelize.fn("sum", sequelize.col("balance")), "total"] - ], - group: ["asset", "pubkey"] - }); + const accounts = await db.Account.findAll({ + attributes: [ + "asset", + "pubkey", + [sequelize.fn("sum", sequelize.col("balance")), "total"] + ], + group: ["asset", "pubkey"] + }); - let lnchannel; - let lnwallet; + let lnchannel; + let lnwallet; - if (config.lna) { - if (config.lna.clightning) { - const funds = await ln.listfunds(); - lnchannel = parseInt( - funds.channels.reduce((a, b) => a + b.channel_sat, 0) - ); - lnwallet = parseInt(funds.outputs.reduce((a, b) => a + b.value, 0)); - } else { - lnchannel = parseInt(await getChannelBalance({ lnd }).channel_balance); - lnwallet = parseInt(await getChainBalance({ lnd }).chain_balance); + if (config.lna) { + if (config.lna.clightning) { + const funds = await ln.listfunds(); + lnchannel = parseInt( + funds.channels.reduce((a, b) => a + b.channel_sat, 0) + ); + lnwallet = parseInt(funds.outputs.reduce((a, b) => a + b.value, 0)); + } else { + lnchannel = parseInt(await getChannelBalance({ lnd }).channel_balance); + lnwallet = parseInt(await getChainBalance({ lnd }).chain_balance); + } } - } - let bitcoin, bitcoind; - if (config.bitcoin) { - bitcoin = parseInt((await bc.getBalance()) * SATS); - bitcoind = await bc.getNetworkInfo(); - } + let bitcoin, bitcoind; + if (config.bitcoin) { + bitcoin = parseInt((await bc.getBalance()) * SATS); + bitcoind = await bc.getNetworkInfo(); + } - let assets, liquid, elementsd; - if (config.liquid) { - assets = await lq.getBalance(); - liquid = parseInt(assets.bitcoin * SATS); - elementsd = await lq.getNetworkInfo(); - } + let assets, liquid, elementsd; + if (config.liquid) { + assets = await lq.getBalance(); + liquid = parseInt(assets.bitcoin * SATS); + elementsd = await lq.getNetworkInfo(); + } - const info = { - bitcoind, - elementsd, - accounts, - assets, - bitcoin, - liquid, - lnchannel, - lnwallet - }; + const info = { + bitcoind, + elementsd, + accounts, + assets, + bitcoin, + liquid, + lnchannel, + lnwallet + }; - info.total = - parseInt(bitcoin) + - parseInt(liquid) + - parseInt(lnchannel) + - parseInt(lnwallet); + info.total = + parseInt(bitcoin) + + parseInt(liquid) + + parseInt(lnchannel) + + parseInt(lnwallet); - res.send(info); - } catch(e) { - console.log("problem getting balances", e) - } + res.send(info); + } catch (e) { + console.log("problem getting balances", e); + } }); diff --git a/routes/invoices.js b/routes/invoices.js index 895fb778..f843d663 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -24,7 +24,29 @@ app.get("/invoice", async (req, res, next) => { res.send(invoice); } catch (e) { - err("couldn't find invoice", e); + err("invoice not found", e); + res.code(500).send("Invoice not found"); + } +}); + +app.get("/invoice/:text", async (req, res, next) => { + try { + let { text } = req.params; + const invoice = await db.Invoice.findOne({ + where: { + [Op.or]: [{ unconfidential: text }, { address: text }, { text }] + }, + include: { + model: db.User, + as: "user", + attributes: ["username", "currency"] + } + }); + + res.send(invoice); + } catch (e) { + err("invoice not found", e); + res.code(500).send("Invoice not found"); } }); @@ -73,7 +95,6 @@ app.post("/invoice", optionalAuth, async (req, res, next) => { address = address; } } else if (network !== "lightning") { - console.log(network, { bitcoin: bc, liquid: lq }[network]) address = await { bitcoin: bc, liquid: lq }[network].getNewAddress(); if (network === "liquid") ({ unconfidential } = await lq.getAddressInfo(address)); diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 898152fe..2ca6f960 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -82,7 +82,7 @@ const handlePayment = async msg => { { transaction } ); await account.reload({ transaction }); - await invoice.save({ transaction }); + await invoice.reload({ transaction }); await payment.save({ transaction }); store.payments.push(msg.request); @@ -92,6 +92,8 @@ const handlePayment = async msg => { callWebhook(invoice, payment); + console.log("INVOICE", payment.invoice.received); + emit(user.username, "payment", payment); emit(user.username, "account", payment.account); notify(user, `Received ${total} SAT`); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index cb5c5444..2cd0e957 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -355,10 +355,13 @@ setInterval(async () => { { transaction } ); await p.account.reload({ transaction }); + await p.invoice.reload({ transaction }); await p.save({ transaction }); p = p.get({ plain: true }); + p.account = p.account.get({ plain: true }); + p.invoice = p.invoice.get({ plain: true }); emit(user.username, "account", p.account); emit(user.username, "payment", p); diff --git a/routes/lnurl.js b/routes/lnurl.js index d5c03bf1..6ca61c93 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -19,7 +19,7 @@ import { createInvoice, decodePaymentRequest, getPayments } from "lightning"; import { computeConversionFee, - conversionFeeReceiver, + conversionFeeReceiver } from "./lightning/conversionFee.js"; import send from "$lib/send"; @@ -36,8 +36,8 @@ app.get("/url", async (req, res, next) => { try { const code = await db.Code.findOne({ where: { - code: req.query.code, - }, + code: req.query.code + } }); if (code) { @@ -56,7 +56,7 @@ app.get("/withdraw", auth, async (req, res, next) => { const result = await lnurlServer.generateNewUrl("withdrawRequest", { minWithdrawable: min * 1000, maxWithdrawable: max * 1000, - defaultDescription: "coinos voucher", + defaultDescription: "coinos voucher" }); withdrawals[result.secret] = req.user; @@ -74,8 +74,8 @@ app.post("/code", async (req, res, next) => { const code = await db.Code.findOrCreate({ where: { code: `lnurl:${encoded.substr(-32)}`, - text: encoded, - }, + text: encoded + } }); res.send(code); @@ -89,7 +89,7 @@ app.post("/withdraw", auth, async (req, res, next) => { const { user } = req; const { amount: value, - params: { callback, k1 }, + params: { callback, k1 } } = req.body; let invoice; @@ -112,7 +112,7 @@ app.post("/withdraw", auth, async (req, res, next) => { currency: user.currency, amount: value, tip: 0, - network: "bitcoin", + network: "bitcoin" }); try { @@ -129,8 +129,8 @@ app.get("/lnurlp/:username", async (req, res, next) => { const { username } = req.params; let user = await db.User.findOne({ where: { - username, - }, + username + } }); if (!user) throw new Error("user not found"); @@ -145,14 +145,14 @@ app.get("/lnurlp/:username", async (req, res, next) => { maxSendable, metadata: JSON.stringify([ ["text/plain", `paying ${user.username}`], - ["text/identifier", `${user.username}@coinos.io`], - ]), + ["text/identifier", `${user.username}@coinos.io`] + ]) }); recipients[result.secret] = { id: user.id, currency: user.currency, - username, + username }; l("recipient", user.username, result.secret); @@ -170,8 +170,8 @@ app.get("/pay/:username", async (req, res, next) => { const { username } = req.params; let user = await db.User.findOne({ where: { - username, - }, + username + } }); let { amount, minSendable, maxSendable } = req.query; @@ -185,14 +185,14 @@ app.get("/pay/:username", async (req, res, next) => { maxSendable, metadata: JSON.stringify([ ["text/plain", `paying ${user.username}`], - ["text/identifier", `${user.username}@coinos.io`], - ]), + ["text/identifier", `${user.username}@coinos.io`] + ]) }); recipients[result.secret] = { id: user.id, currency: user.currency, - username, + username }; l("recipient", user.username, result.secret); @@ -208,12 +208,11 @@ app.post("/pay", auth, async (req, res, next) => { const { amount, comment, - params: { callback, k1 }, + params: { callback, k1 } } = req.body; - let url = `${callback}${callback.includes("?") ? "&" : "?"}amount=${ - amount * 1000 - }${comment ? "&comment=" + comment : ""}`; + let url = `${callback}${callback.includes("?") ? "&" : "?"}amount=${amount * + 1000}${comment ? "&comment=" + comment : ""}`; try { const parts = callback.split("/"); @@ -225,7 +224,7 @@ app.post("/pay", auth, async (req, res, next) => { { amount, memo: comment, - username: recipients[secret].username, + username: recipients[secret].username }, req.hostname, user @@ -279,7 +278,7 @@ app.get("/decode", async (req, res, next) => { .split("/")[0] .split("@") .slice(-1)[0] - .split(":")[0], + .split(":")[0] }); } @@ -292,7 +291,7 @@ app.get("/decode", async (req, res, next) => { } }); -lnurlServer.on("payRequest:action:processed", async function (event) { +lnurlServer.on("payRequest:action:processed", async function(event) { const { secret, params, result } = event; const { id, invoice } = result; const recipient = recipients[secret]; @@ -306,7 +305,7 @@ lnurlServer.on("payRequest:action:processed", async function (event) { currency: recipient.currency, amount: payreq.satoshis, tip: 0, - network: "lightning", + network: "lightning" }); l("invoice created", i.text, i.amount); @@ -333,8 +332,8 @@ lnurlServer.bindToHook( where: { user_id, asset: config.liquid.btcasset, - pubkey: null, - }, + pubkey: null + } }); if (account.balance < amount) { @@ -352,9 +351,9 @@ lnurlServer.bindToHook( include: [ { model: db.User, - as: "user", - }, - ], + as: "user" + } + ] }); if (keyObj) ({ user } = keyObj); @@ -370,7 +369,7 @@ lnurlServer.bindToHook( user = await register( { username, - password: key, + password: key }, ip ); @@ -399,34 +398,34 @@ lnurlServer.bindToHook( } else { ({ num_satoshis: amount } = await decodePaymentRequest({ lnd, - request: pr, + request: pr })); } let conversionFee = computeConversionFee(amount); - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { let account = await db.Account.findOne({ where: { user_id: user.id, - asset: config.liquid.btcasset, + asset: config.liquid.btcasset }, lock: transaction.LOCK.UPDATE, - transaction, + transaction }); // account that receives conversion fees let receiverAccount = await db.Account.findOne({ where: { - "$user.username$": conversionFeeReceiver, + "$user.username$": conversionFeeReceiver }, include: [ { model: db.User, - as: "user", - }, + as: "user" + } ], lock: transaction.LOCK.UPDATE, - transaction, + transaction }); let conversionFeeDeduction = Math.min( @@ -466,7 +465,7 @@ lnurlServer.bindToHook( currency: receiverAccount.user.currency, confirmed: true, received: true, - network: "COINOS", + network: "COINOS" }, { transaction } ); @@ -483,7 +482,7 @@ lnurlServer.bindToHook( currency: user.currency, confirmed: true, network: "lightning", - fee_payment_id, + fee_payment_id }, { transaction } ); @@ -492,10 +491,10 @@ lnurlServer.bindToHook( try { let { payments } = await ln.getPayments({ lnd, - limit: 5, + limit: 5 }); - let p = payments.find((p) => p.request === pr); + let p = payments.find(p => p.request === pr); if (p) { l("found payment", pr); payment.fee = p.fee; @@ -539,28 +538,28 @@ lnurlServer.bindToHook( } ); -lnurlServer.bindToHook("login", async (key) => { +lnurlServer.bindToHook("login", async key => { try { if (!key) throw new Error("login key not defined"); const exists = await db.Key.findOne({ where: { hex: key }, - include: [{ model: db.User, as: "user" }], + include: [{ model: db.User, as: "user" }] }); let user; if (logins[key] && logins[key] !== "undefined") { const { username } = logins[key]; user = await db.User.findOne({ - where: { username }, + where: { username } }); if (user) { const [k, created] = await db.Key.findOrCreate({ where: { user_id: user.id, - hex: key, - }, + hex: key + } }); if (created) { @@ -571,7 +570,7 @@ lnurlServer.bindToHook("login", async (key) => { l("user not found"); user = await register({ username: key.substr(0, 8), - password: key, + password: key }); } } else if (exists) ({ user } = exists); diff --git a/routes/payments.js b/routes/payments.js index 6c489372..d1b2ba4c 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -64,7 +64,7 @@ app.post("/lightning/send", auth, lnRoutes.send); if (config.lnurl) { let { channelRequest } = await import("$routes/lightning/channelRequest"); - app.post("/lightning/channelRequest", channelRequest) + app.post("/lightning/channelRequest", channelRequest); } app.post("/bitcoin/broadcast", optionalAuth, btcRoutes.broadcast); diff --git a/routes/users.js b/routes/users.js index 3063758d..c270902c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -29,20 +29,20 @@ app.get("/me", auth, async (req, res) => { let user = req.user.get({ plain: true }); let payments = await req.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" }, { model: db.Payment, - as: "fee_payment", - }, - ], + as: "fee_payment" + } + ] }); user.accounts = await req.user.getAccounts(); user.payments = payments; @@ -56,11 +56,11 @@ app.get("/users/:username", async (req, res) => { const { username } = req.params; const user = await db.User.findOne({ - attributes: ["username", "banner", "profile"], + attributes: ["username", "banner", "profile", "address"], where: Sequelize.where( Sequelize.fn("lower", Sequelize.col("username")), username.toLowerCase() - ), + ) }); if (user) res.send(user); @@ -80,7 +80,7 @@ app.post("/register", async (req, res) => { app.post("/disable2fa", auth, async (req, res) => { let { user, - body: { token }, + body: { token } } = req; if ( @@ -120,7 +120,7 @@ app.post("/2fa", auth, async (req, res) => { app.get("/exists", async (req, res) => { let exists = await db.User.findOne({ - where: { username: req.query.username }, + where: { username: req.query.username } }); res.send(!!exists); @@ -142,11 +142,11 @@ app.post("/user", auth, async (req, res) => { tokens, twofa, unit, - username, + username } = req.body; let exists = await db.User.findOne({ - where: { username }, + where: { username } }); let token; @@ -161,7 +161,7 @@ app.post("/user", auth, async (req, res) => { token = jwt.sign({ username }, config.jwt); res.cookie("token", token, { - expires: new Date(Date.now() + 432000000), + expires: new Date(Date.now() + 432000000) }); } @@ -193,7 +193,7 @@ app.post("/keys", auth, async (req, res) => { const { key: hex } = req.body; const key = await db.Key.create({ user_id: req.user.id, - hex, + hex }); emit(req.user.username, "key", key); res.send(key); @@ -205,8 +205,8 @@ app.post("/keys/delete", auth, async (req, res) => { await db.Key.findOne({ where: { user_id: req.user.id, - hex, - }, + hex + } }) ).destroy(); }); @@ -222,12 +222,12 @@ app.post("/updateSeeds", auth, async (req, res) => { await db.Account.update( { seed }, { - where: { id, user_id: user.id }, + where: { id, user_id: user.id } } ); const account = await db.Account.findOne({ - where: { id }, + where: { id } }); emit(user.username, "account", account); @@ -244,8 +244,16 @@ app.post("/accounts/delete", auth, async (req, res) => { }); app.post("/accounts", auth, async (req, res) => { - const { name, seed, pubkey, ticker, precision, path, privkey, network } = - req.body; + const { + name, + seed, + pubkey, + ticker, + precision, + path, + privkey, + network + } = req.body; const { user } = req; let account = await db.Account.create({ @@ -260,7 +268,7 @@ app.post("/accounts", auth, async (req, res) => { privkey, seed, path, - network, + network }); emit(user.username, "account", account); @@ -346,14 +354,14 @@ app.post("/logout", optionalAuth, async (req, res) => { if (username) { l("logging out", username); let i = req.user.subscriptions.findIndex( - (s) => JSON.stringify(s) === subscription + s => JSON.stringify(s) === subscription ); if (i > -1) { req.user.subscriptions.splice(i, 1); } await req.user.save(); Object.keys(logins).map( - (k) => logins[k]["username"] === username && delete logins[k] + k => logins[k]["username"] === username && delete logins[k] ); } @@ -373,15 +381,15 @@ app.post("/account", auth, async (req, res) => { privkey, path, hide, - index, + index } = req.body; try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { let account = await db.Account.findOne({ where: { id, user_id: user.id }, lock: transaction.LOCK.UPDATE, - transaction, + transaction }); let params = { @@ -394,14 +402,14 @@ app.post("/account", auth, async (req, res) => { path, hide, index, - privkey, + privkey }; if (!account || (account.pubkey && !pubkey)) delete params.pubkey; await db.Account.update(params, { where: { id, user_id: user.id }, - transaction, + transaction }); await account.reload({ transaction }); @@ -420,7 +428,7 @@ app.post("/shiftAccount", auth, async (req, res) => { try { const account = await db.Account.findOne({ - where: { id }, + where: { id } }); if (account.user_id !== user.id) @@ -430,14 +438,14 @@ app.post("/shiftAccount", auth, async (req, res) => { await user.save(); let payments = await db.Payment.findAll({ where: { - account_id: id, + account_id: id }, order: [["id", "DESC"]], limit: 12, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); user = user.get({ plain: true }); @@ -454,18 +462,16 @@ app.post("/shiftAccount", auth, async (req, res) => { } }); -app.get("/vapidPublicKey", function (req, res) { +app.get("/vapidPublicKey", function(req, res) { res.send(config.vapid.publicKey); }); -app.post("/subscribe", auth, async function (req, res) { +app.post("/subscribe", auth, async function(req, res) { let { subscriptions } = req.user; let { subscription } = req.body; if (!subscriptions) subscriptions = []; if ( - !subscriptions.find( - (s) => JSON.stringify(s) === JSON.stringify(subscription) - ) + !subscriptions.find(s => JSON.stringify(s) === JSON.stringify(subscription)) ) subscriptions.push(subscription); req.user.subscriptions = subscriptions; @@ -475,10 +481,10 @@ app.post("/subscribe", auth, async function (req, res) { }); let redeeming = {}; -app.post("/redeem", optionalAuth, async function (req, res) { +app.post("/redeem", optionalAuth, async function(req, res) { const { redeemcode } = req.body; try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { if (redeeming[redeemcode]) fail("redemption in progress"); redeeming[redeemcode] = true; if (!redeemcode) fail("no code provided"); @@ -487,14 +493,14 @@ app.post("/redeem", optionalAuth, async function (req, res) { const source = await db.Payment.findOne({ where: { - redeemcode: req.body.redeemcode, + redeemcode: req.body.redeemcode }, include: { model: db.Account, - as: "account", + as: "account" }, lock: transaction.LOCK.UPDATE, - transaction, + transaction }); l("redeeming", redeemcode); @@ -511,7 +517,7 @@ app.post("/redeem", optionalAuth, async function (req, res) { user = await register( { username: redeemcode.substr(0, 8), - password: "", + password: "" }, ip, false @@ -520,7 +526,7 @@ app.post("/redeem", optionalAuth, async function (req, res) { let payload = { username: user.username }; let token = jwt.sign(payload, config.jwt); res.cookie("token", token, { - expires: new Date(Date.now() + 432000000), + expires: new Date(Date.now() + 432000000) }); delete redeeming[redeemcode]; @@ -545,7 +551,7 @@ app.post("/redeem", optionalAuth, async function (req, res) { confirmed, network, received: true, - fee, + fee }, { transaction } ); @@ -568,14 +574,14 @@ app.post("/redeem", optionalAuth, async function (req, res) { } }); -app.post("/checkRedeemCode", auth, async function (req, res) { +app.post("/checkRedeemCode", auth, async function(req, res) { const { redeemcode } = req.body; const payment = await db.Payment.findOne({ where: { redeemcode } }); res.send(payment); }); -app.post("/password", auth, async function (req, res) { +app.post("/password", auth, async function(req, res) { const { user } = req; const { password } = req.body; @@ -583,7 +589,7 @@ app.post("/password", auth, async function (req, res) { res.send(await bcrypt.compare(password, user.password)); }); -app.get("/isInternal", auth, async function (req, res) { +app.get("/isInternal", auth, async function(req, res) { let { user } = req; let { address } = req.query; if (!address) throw new Error("Address not provided"); @@ -593,8 +599,8 @@ app.get("/isInternal", auth, async function (req, res) { include: { attributes: ["username"], model: db.User, - as: "user", - }, + as: "user" + } }); if (invoice) { @@ -613,21 +619,21 @@ app.get("/isInternal", auth, async function (req, res) { res.send(false); }); -app.get("/invoices", auth, async function (req, res) { +app.get("/invoices", auth, async function(req, res) { let invoices = await db.Invoice.findAll({ - where: { user_id: req.user.id }, + where: { user_id: req.user.id } }); res.send(invoices); }); -app.post("/signMessage", auth, async function (req, res) { +app.post("/signMessage", auth, async function(req, res) { let { address, message } = req.body; let invoices = await db.Invoice.findAll({ - where: { user_id: req.user.id }, + where: { user_id: req.user.id } }); - if (invoices.find((i) => i.address === address)) { + if (invoices.find(i => i.address === address)) { if (config.bitcoin.walletpass) await bc.walletPassphrase(config.bitcoin.walletpass, 300); return res.send(await bc.signMessage(address, message)); |
