diff options
| author | Adam Soltys <[email protected]> | 2020-09-07 20:04:33 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-09-07 20:04:33 -0700 |
| commit | e40e68f49b10d6ef70546f7128bad891256525c0 (patch) | |
| tree | f2f0b832eb9f8655c7d4bfca7c2ce12aa9634b5c | |
| parent | f82838d4682de4717f6a66be2a7903fdab83cbce (diff) | |
| download | coinos-server-e40e68f49b10d6ef70546f7128bad891256525c0.tar.gz coinos-server-e40e68f49b10d6ef70546f7128bad891256525c0.zip | |
multiple same-asset accounts
| -rw-r--r-- | db/models/accounts.js | 18 | ||||
| -rw-r--r-- | db/models/invoices.js | 9 | ||||
| -rw-r--r-- | db/models/payments.js | 9 | ||||
| -rw-r--r-- | db/models/users.js | 9 | ||||
| -rw-r--r-- | lib/register.js | 1 | ||||
| -rw-r--r-- | lib/whitelist.js | 1 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 23 | ||||
| -rw-r--r-- | routes/users.js | 138 |
8 files changed, 162 insertions, 46 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js index 53684caf..acd321b2 100644 --- a/db/models/accounts.js +++ b/db/models/accounts.js @@ -37,6 +37,24 @@ const attributes = { comment: null, field: "updatedAt" }, + path: { + type: DataTypes.STRING(255), + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "path" + }, + seed: { + type: DataTypes.STRING(255), + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "seed" + }, name: { type: DataTypes.STRING(255), allowNull: true, diff --git a/db/models/invoices.js b/db/models/invoices.js index d2a73f7f..e2c499f4 100644 --- a/db/models/invoices.js +++ b/db/models/invoices.js @@ -55,6 +55,15 @@ const attributes = { comment: null, field: "updatedAt" }, + path: { + type: DataTypes.STRING(255), + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "path" + }, rate: { type: DataTypes.DOUBLE, allowNull: true, diff --git a/db/models/payments.js b/db/models/payments.js index 388010a0..94050a0b 100644 --- a/db/models/payments.js +++ b/db/models/payments.js @@ -28,6 +28,15 @@ const attributes = { comment: null, field: "user_id" }, + path: { + type: DataTypes.STRING(255), + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "path" + }, memo: { type: DataTypes.TEXT, allowNull: true, diff --git a/db/models/users.js b/db/models/users.js index f6446a38..1bb4caaa 100644 --- a/db/models/users.js +++ b/db/models/users.js @@ -19,6 +19,15 @@ const attributes = { comment: null, field: "fiat" }, + index: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: 0, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "index" + }, ip: { type: DataTypes.INTEGER.UNSIGNED, allowNull: true, diff --git a/lib/register.js b/lib/register.js index c8aa8e5d..e339b7c8 100644 --- a/lib/register.js +++ b/lib/register.js @@ -3,6 +3,7 @@ const axios = require("axios"); const bcrypt = require("bcrypt"); register = async (user, ip, requireChallenge) => { + console.log("registering", user); if (!(user && user.username)) throw new Error("Username required"); let exists = await db.User.count({ where: { username: user.username } }); diff --git a/lib/whitelist.js b/lib/whitelist.js index 78369c0e..a2abeb16 100644 --- a/lib/whitelist.js +++ b/lib/whitelist.js @@ -13,4 +13,5 @@ whitelist = [ "payments", "seed", "keys", + "index", ]; diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 5e52b7ed..8745c3dd 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -9,7 +9,7 @@ module.exports = ah(async (req, res) => { const isChange = async address => (await bc.getAddressInfo(address)).ismine && - (!Object.keys(addresses).includes(address)); + !Object.keys(addresses).includes(address); tx = await bc.decodeRawTransaction(hex); @@ -30,17 +30,20 @@ module.exports = ah(async (req, res) => { total = total - change + fee; let amount = total - fee; - let account, params; + let { account } = user; + let params; try { await db.transaction(async transaction => { - account = await db.Account.findOne({ - where: { - user_id: user.id, - asset: config.liquid.btcasset - }, - lock: transaction.LOCK.UPDATE, - transaction - }); + if (account.asset !== btcasset) { + account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + } if (total > account.balance) { l.error("amount exceeds balance", amount, fee, account.balance); diff --git a/routes/users.js b/routes/users.js index 5b5cf1e0..6ffed55b 100644 --- a/routes/users.js +++ b/routes/users.js @@ -13,7 +13,7 @@ require("../lib/whitelist"); const twofa = ah((req, res, next) => { let { user, - body: { token }, + body: { token } } = req; if ( user.twofa && @@ -34,7 +34,7 @@ app.get( where: Sequelize.where( Sequelize.fn("lower", Sequelize.col("username")), username.toLowerCase() - ), + ) }); if (user) res.send(user); @@ -119,7 +119,7 @@ app.get( "/exists", ah(async (req, res) => { let exists = await db.User.findOne({ - where: { username: req.query.username }, + where: { username: req.query.username } }); res.send(!!exists); @@ -141,13 +141,13 @@ app.post( twofa, pin, password, - passconfirm, + confirm, tokens, - seed, + seed } = req.body; let exists = await db.User.findOne({ - where: { username }, + where: { username } }); let token; @@ -159,7 +159,7 @@ app.post( token = jwt.sign({ username }, config.jwt); res.cookie("token", token, { - expires: new Date(Date.now() + 432000000), + expires: new Date(Date.now() + 432000000) }); } @@ -173,7 +173,7 @@ app.post( user.seed = seed; user.fiat = fiat; - if (password && password === passconfirm) { + if (password && password === confirm) { user.password = await bcrypt.hash(password, 1); } @@ -193,7 +193,7 @@ app.post( const { key: hex } = req.body; const key = await db.Key.create({ user_id: req.user.id, - hex, + hex }); emit(req.user.username, "key", key); }) @@ -208,18 +208,60 @@ app.post( await db.Key.findOne({ where: { user_id: req.user.id, - hex, - }, + hex + } }) ).destroy(); }) ); app.post( + "/updateSeeds", + auth, + ah(async (req, res) => { + let { user } = req; + let { seeds } = req.body; + let keys = Object.keys(seeds); + + for (let i = 0; i < keys.length; i++) { + let id = keys[i]; + let seed = seeds[id]; + await db.Account.update( + { seed }, + { + where: { id, user_id: user.id } + } + ); + + const account = await db.Account.findOne({ + where: { id } + }); + + console.log("updated", account.id, account.asset); + + emit(user.username, "account", account); + } + + res.end(); + }) +); + +app.post( + "/accounts/delete", + auth, + ah(async (req, res) => { + const { id } = req.body; + const account = await db.Account.findOne({ where: { id }}); + if (account) await account.destroy(); + res.end(); + }) +); + +app.post( "/accounts", auth, ah(async (req, res) => { - const { name, pubkey, ticker } = req.body; + const { name, seed, pubkey, ticker, precision, path } = req.body; const { user } = req; let account = await db.Account.create({ @@ -229,10 +271,20 @@ app.post( pending: 0, name, ticker, - precision: 8, + precision, pubkey, + seed, + path }); + emit(user.username, "account", account); + + if (pubkey) { + user.index++; + await user.save(); + emit(user.username, "user", user); + } + res.send(account); }) ); @@ -314,14 +366,14 @@ app.post( if (username) { l.info("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] ); } @@ -361,15 +413,28 @@ app.post( auth, ah(async (req, res) => { const { user } = req; - const { id } = req.body; + const { + id, + name, + ticker, + precision, + domain, + seed, + pubkey, + path, + hide + } = req.body; try { - await db.Account.update(req.body, { - where: { id, user_id: user.id }, - }); + await db.Account.update( + { name, ticker, precision, domain, seed, pubkey, path, hide }, + { + where: { id, user_id: user.id } + } + ); const account = await db.Account.findOne({ - where: { id }, + where: { id } }); emit(user.username, "account", account); @@ -390,21 +455,22 @@ app.post( try { const account = await db.Account.findOne({ - where: { id }, + where: { id } }); user.account_id = account.id; await user.save(); - let payments = await user.getPayments({ + let payments = await db.Payment.findAll({ where: { - account_id: id, + user_id: user.id, + account_id: id }, order: [["id", "DESC"]], limit: 12, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); user.payments = payments; user.account = account; @@ -420,20 +486,20 @@ app.post( }) ); -app.get("/vapidPublicKey", function (req, res) { +app.get("/vapidPublicKey", function(req, res) { res.send(config.vapid.publicKey); }); app.post( "/subscribe", auth, - ah(async function (req, res) { + ah(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) + s => JSON.stringify(s) === JSON.stringify(subscription) ) ) subscriptions.push(subscription); @@ -447,7 +513,7 @@ app.post( app.post( "/redeem", optionalAuth, - ah(async function (req, res) { + ah(async function(req, res) { const { redeemcode } = req.body; if (!redeemcode) fail("no code provided"); @@ -455,12 +521,12 @@ app.post( const source = await db.Payment.findOne({ where: { - redeemcode: req.body.redeemcode, + redeemcode: req.body.redeemcode }, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); l.info("redeeming", redeemcode); @@ -473,7 +539,7 @@ app.post( user = await register( { username: redeemcode.substr(0, 8), - password: "", + password: "" }, ip, false @@ -489,7 +555,7 @@ app.post( let account = await getAccount(source.account.asset, user); - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { let { hash, memo, confirmed, fee, network } = source; source.redeemed = true; @@ -507,7 +573,7 @@ app.post( confirmed, network, received: true, - fee, + fee }, { transaction } ); @@ -528,7 +594,7 @@ app.post( app.post( "/password", auth, - ah(async function (req, res) { + ah(async function(req, res) { const { user } = req; const { password } = req.body; |
