diff options
| -rw-r--r-- | db/models/accounts.js | 27 | ||||
| -rw-r--r-- | lib/passport.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/fee.js | 32 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 5 | ||||
| -rw-r--r-- | routes/payments.js | 61 | ||||
| -rw-r--r-- | routes/users.js | 47 |
6 files changed, 121 insertions, 53 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js index 5a6f0bd3..e139956d 100644 --- a/db/models/accounts.js +++ b/db/models/accounts.js @@ -106,6 +106,33 @@ const attributes = { comment: null, field: "pending" }, + index: { + type: DataTypes.INTEGER(11), + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "index" + }, + address: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "address" + }, + pubkey: { + type: DataTypes.STRING, + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "pubkey" + }, precision: { type: DataTypes.INTEGER(11), allowNull: true, diff --git a/lib/passport.js b/lib/passport.js index 660233ca..b26b445e 100644 --- a/lib/passport.js +++ b/lib/passport.js @@ -20,7 +20,7 @@ passport = Passport.use( async (payload, next) => { const { username } = payload; try { - next(null, await db.User.findOne({ where: { username } })); + next(null, await db.User.findOne({ where: { username }, include: { model: db.Account, as: 'account' } })); } catch (e) { l.error("error finding user for session", e); next(null, { username: "guest" }); diff --git a/routes/bitcoin/fee.js b/routes/bitcoin/fee.js index da0fb9e9..cd99d363 100644 --- a/routes/bitcoin/fee.js +++ b/routes/bitcoin/fee.js @@ -1,32 +1,42 @@ +const buildTx = ({ address, amount, feeRate }) => {}; + module.exports = ah(async (req, res) => { let { user } = req; let { address, amount, feeRate } = req.body; let tx, fee; + if (user.account.pubkey) { + tx = buildTx(req.body); + return res.end(); + } + let invoice = await db.Invoice.findOne({ where: { address }, include: { - attributes: ['username'], + attributes: ["username"], model: db.User, - as: 'user', - } + as: "user", + }, }); if (invoice) { - emit(user.username, "to", invoice.user); - return res.end(); + let { ismine } = await bc.getAddressInfo(address); + if (ismine) { + emit(user.username, "to", invoice.user); + return res.end(); + } } try { amount = parseInt(amount); let partial = await bc.createRawTransaction([], { - [address]: (amount / SATS).toFixed(8) + [address]: (amount / SATS).toFixed(8), }); let params = { - subtractFeeFromOutputs: amount === user.balance ? [0] : [] - } + subtractFeeFromOutputs: amount === user.balance ? [0] : [], + }; if (feeRate) params.feeRate = (feeRate / SATS).toFixed(8); @@ -35,10 +45,10 @@ module.exports = ah(async (req, res) => { if (config.bitcoin.walletpass) await bc.walletPassphrase(config.bitcoin.walletpass, 300); - let signed = (await bc.signRawTransactionWithWallet(tx.hex)); + let signed = await bc.signRawTransactionWithWallet(tx.hex); decoded = await bc.decodeRawTransaction(signed.hex); - feeRate = Math.round(tx.fee * SATS * 1000 / decoded.vsize); - + feeRate = Math.round((tx.fee * SATS * 1000) / decoded.vsize); + res.send({ feeRate, tx }); } catch (e) { l.error("bitcoin fee estimation error", e); diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 1ee9f323..1c731118 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -59,13 +59,16 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let confirmed = false; - const account = await db.Account.findOne({ + const accounts = await db.Account.findAll({ where: { user_id: user.id, asset: config.liquid.btcasset, }, }); + let account = accounts.find(a => a.address === address); + if (!account) account = accounts[0]; + account.pending += value; await account.save(); diff --git a/routes/payments.js b/routes/payments.js index d88c95a7..c46af90e 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -5,7 +5,7 @@ const { join } = require("path"); const fs = require("fs"); const read = require("../lib/read"); -(ah(async () => { +ah(async () => { seen = []; addresses = {}; issuances = {}; @@ -25,16 +25,25 @@ const read = require("../lib/read"); }); await db.Invoice.findAll({ - include: [ - { - model: db.User, - as: "user", - }, - ], - }).map((i) => { - if (i.address && i.user) addresses[i.address] = i.user.username; - if (i.unconfidential && i.user) - addresses[i.unconfidential] = i.user.username; + include: { + model: db.User, + as: "user", + }, + }).map(({ address, user, unconfidential }) => { + if (address && user) addresses[address] = user.username; + if (unconfidential && user) addresses[unconfidential] = user.username; + }); + + const accounts = await db.Account.findAll({ + where: { pubkey: { [Op.ne]: null } }, + include: { + model: db.User, + as: "user", + }, + }); + + accounts.map(({ address, user: { username } }) => { + addresses[address] = username; }); payments = ( @@ -141,23 +150,23 @@ const read = require("../lib/read"); "/payment/:redeemcode", ah(async (req, res) => { try { - const { redeemcode } = req.params; - let payment = await db.Payment.findOne({ - where: { - redeemcode, - }, - include: { - model: db.Account, - as: "account", - }, - }); + const { redeemcode } = req.params; + let payment = await db.Payment.findOne({ + where: { + redeemcode, + }, + include: { + model: db.Account, + as: "account", + }, + }); - if (!payment) fail("invalid code"); + if (!payment) fail("invalid code"); - res.send(payment); - } catch(e) { + res.send(payment); + } catch (e) { res.status(500).send(e.message); - } + } }) ); -}))(); +})(); diff --git a/routes/users.js b/routes/users.js index 86af6fa3..36184ab4 100644 --- a/routes/users.js +++ b/routes/users.js @@ -220,6 +220,29 @@ app.post( ); app.post( + "/accounts", + auth, + ah(async (req, res) => { + const { address, pubkey } = req.body; + const { user } = req; + + let account = await db.Account.create({ + address, + user_id: user.id, + asset: config.liquid.btcasset, + balance: 0, + pending: 0, + name: "Bitcoin", + ticker: "BTC", + precision: 8, + pubkey, + }); + + res.send(account); + }) +); + +app.post( "/login", ah(async (req, res) => { const { params, sig, key } = req.body; @@ -273,6 +296,7 @@ app.post( 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 }); @@ -341,21 +365,14 @@ app.post( "/account", auth, ah(async (req, res) => { - let { user } = req; - let { asset, domain, precision, name, ticker, user_id } = req.body; + const { user } = req; + const { id } = req.body; try { - const account = await db.Account.findOne({ - where: { user_id, asset }, + const account = await db.Account.update(req.body, { + where: { id, user_id: user.id }, }); - account.domain = domain; - account.name = name; - account.ticker = ticker; - account.precision = precision; - - await account.save(); - emit(user.username, "account", account); res.end(); } catch (e) { @@ -370,18 +387,18 @@ app.post( auth, ah(async (req, res) => { let { user } = req; - let { asset } = req.body; + let { id } = req.body; try { const account = await db.Account.findOne({ - where: { user_id: user.id, asset }, + where: { id }, }); user.account_id = account.id; await user.save(); let payments = await user.getPayments({ where: { - account_id: user.account_id, + account_id: id, }, order: [["id", "DESC"]], limit: 12, @@ -393,6 +410,8 @@ app.post( user.payments = payments; user.account = account; + console.log(user.account.id); + emit(user.username, "user", user); res.end(); } catch (e) { |
