diff options
| -rw-r--r-- | db/models/accounts.js | 4 | ||||
| -rw-r--r-- | db/schema.sql | 25 | ||||
| -rw-r--r-- | lib/buildliquidtx.js | 3 | ||||
| -rw-r--r-- | lib/buildtx.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 54 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 5 | ||||
| -rw-r--r-- | routes/liquid/fee.js | 1 | ||||
| -rw-r--r-- | routes/send.js | 18 | ||||
| -rw-r--r-- | routes/users.js | 12 |
9 files changed, 67 insertions, 57 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js index acd321b2..fc386816 100644 --- a/db/models/accounts.js +++ b/db/models/accounts.js @@ -135,8 +135,8 @@ const attributes = { }, index: { type: DataTypes.INTEGER(11), - allowNull: true, - defaultValue: null, + allowNull: false, + defaultValue: 0, primaryKey: false, autoIncrement: false, comment: null, diff --git a/db/schema.sql b/db/schema.sql index b423620e..5bfc5c85 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,8 +1,8 @@ --- MySQL dump 10.17 Distrib 10.3.23-MariaDB, for debian-linux-gnu (x86_64) +-- MySQL dump 10.17 Distrib 10.3.24-MariaDB, for debian-linux-gnu (x86_64) -- -- Host: localhost Database: coinos -- ------------------------------------------------------ --- Server version 10.3.23-MariaDB-1:10.3.23+maria~bionic +-- Server version 10.3.24-MariaDB-1:10.3.24+maria~bionic /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -59,10 +59,12 @@ CREATE TABLE `accounts` ( `domain` varchar(255) DEFAULT NULL, `contract` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `pubkey` varchar(255) DEFAULT NULL, - `index` int(11) DEFAULT NULL, + `index` int(11) NOT NULL DEFAULT 0, `hide` tinyint(1) DEFAULT NULL, + `seed` varchar(255) DEFAULT NULL, + `path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3809 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=3943 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -103,8 +105,9 @@ CREATE TABLE `invoices` ( `uuid` varchar(255) DEFAULT NULL, `memo` text DEFAULT NULL, `account_id` int(11) DEFAULT NULL, + `path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=5116 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=5161 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -121,7 +124,7 @@ CREATE TABLE `linkingkeys` ( `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4518 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4554 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -151,8 +154,9 @@ CREATE TABLE `payments` ( `memo` text DEFAULT NULL, `redeemcode` varchar(255) DEFAULT NULL, `redeemed` tinyint(1) NOT NULL DEFAULT 0, + `path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=5169 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=5300 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -175,7 +179,7 @@ CREATE TABLE `proposals` ( `text` text DEFAULT NULL, `public` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8mb4; +) ENGINE=InnoDB AUTO_INCREMENT=173 DEFAULT CHARSET=utf8mb4; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -218,9 +222,10 @@ CREATE TABLE `users` ( `seed` varchar(255) DEFAULT NULL, `linkingKeys` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `fiat` tinyint(1) NOT NULL DEFAULT 0, + `index` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), KEY `ip` (`ip`) -) ENGINE=InnoDB AUTO_INCREMENT=653 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=690 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -232,4 +237,4 @@ CREATE TABLE `users` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2020-08-16 15:42:08 +-- Dump completed on 2020-09-08 7:24:41 diff --git a/lib/buildliquidtx.js b/lib/buildliquidtx.js index aafaf049..cc1c3f10 100644 --- a/lib/buildliquidtx.js +++ b/lib/buildliquidtx.js @@ -22,6 +22,7 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { where: { received: true, account_id: user.account_id, + network: 'LBTC', }, }); @@ -36,7 +37,7 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { } let feeInput = utxos.find((utxo) => utxo.asset === btc && utxo.value > 1000); - if (!feeInput) throw new Error("Couldn't find fee input"); + if (!feeInput) throw new Error("Insufficient balance, couldn't find an input to cover the fee"); utxos = utxos.filter((utxo) => utxo.asset === asset); diff --git a/lib/buildtx.js b/lib/buildtx.js index 56b4bbff..8c8be959 100644 --- a/lib/buildtx.js +++ b/lib/buildtx.js @@ -16,6 +16,7 @@ module.exports = async ({ address, amount, feeRate, user }) => { where: { received: true, account_id: user.account_id, + network: 'BTC', } }); @@ -35,7 +36,6 @@ module.exports = async ({ address, amount, feeRate, user }) => { } ]; - debugger let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate); if (!inputs || !outputs) { diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 987f5079..63a34760 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -26,7 +26,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { if (payments.includes(hash)) return; Promise.all( - tx.outs.map(async (o) => { + tx.outs.map(async o => { const { value } = o; let address; @@ -44,18 +44,24 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let user = await db.User.findOne({ where: { - username: addresses[address], - }, + username: addresses[address] + } }); const invoice = await db.Invoice.findOne({ where: { user_id: user.id, - network: "BTC", + network: "BTC" }, order: [["id", "DESC"]], + include: { + model: db.Account, + as: "account", + }, }); + if (!invoice) return; + const currency = invoice ? invoice.currency : user.currency; const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; const tip = invoice ? invoice.tip : 0; @@ -63,27 +69,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let confirmed = false; - const accounts = await db.Account.findAll({ - where: { - user_id: user.id, - asset: config.liquid.btcasset, - }, - }); - - let account = accounts.find(({ index, pubkey }) => { - const { payments } = bitcoin; - index && - pubkey && - address === - payments.p2wpkh({ - pubkey: fromBase58(pubkey, network).derivePath( - `${index}` - ).publicKey, - network, - }).address; - }); - - if (!account) account = accounts[0]; + let { account } = invoice; account.pending += value; await account.save(); @@ -120,7 +106,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { tip, confirmed, address, - network: "BTC", + network: "BTC" }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); @@ -138,13 +124,13 @@ let queue = {}; zmqRawBlock.on("message", async (topic, message, sequence) => { const payments = await db.Payment.findAll({ - where: { confirmed: false }, + where: { confirmed: false } }); let block = bitcoin.Block.fromHex(message.toString("hex")); - block.transactions.map((tx) => { + block.transactions.map(tx => { let hash = reverse(tx.getHash()).toString("hex"); - if (payments.find((p) => p.hash === hash)) queue[hash] = 1; + if (payments.find(p => p.hash === hash)) queue[hash] = 1; }); }); @@ -154,21 +140,21 @@ setInterval(async () => { for (let i = 0; i < arr.length; i++) { const hash = arr[i]; - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { let p = await db.Payment.findOne({ where: { hash, confirmed: 0, received: 1 }, include: [ { model: db.Account, - as: "account", + as: "account" }, { model: db.User, - as: "user", - }, + as: "user" + } ], lock: transaction.LOCK.UPDATE, - transaction, + transaction }); const { user } = p; diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 8745c3dd..5d22152e 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,4 +1,5 @@ const bitcoin = require("bitcoinjs-lib"); +const btc = config.liquid.btcasset; module.exports = ah(async (req, res) => { let { user } = req; @@ -34,11 +35,11 @@ module.exports = ah(async (req, res) => { let params; try { await db.transaction(async transaction => { - if (account.asset !== btcasset) { + if (account.asset !== btc) { account = await db.Account.findOne({ where: { user_id: user.id, - asset: config.liquid.btcasset + asset: btc, }, lock: transaction.LOCK.UPDATE, transaction diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index acf28160..6bf52afc 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -10,7 +10,6 @@ module.exports = ah(async (req, res) => { let psbt = await buildTx({ address, asset, amount, feeRate, user }); return res.send(psbt); } catch(e) { - throw e; return res.status(500).send(e.message); } } diff --git a/routes/send.js b/routes/send.js index 616ac137..cafbaed8 100644 --- a/routes/send.js +++ b/routes/send.js @@ -63,6 +63,23 @@ module.exports = ah(async (req, res, next) => { where: { username }, }); + + const invoice = await db.Invoice.findOne({ + where: { + user_id: user.id, + network: "BTC" + }, + order: [["id", "DESC"]], + include: { + model: db.Account, + as: "account", + }, + }); + + if (invoice) ({ account } = invoice); + else if (user.account.asset === asset) ({ account } = user); + else { + let params = { user_id: user.id, asset, @@ -73,6 +90,7 @@ module.exports = ah(async (req, res, next) => { lock: transaction.LOCK.UPDATE, transaction, }); + } if (account) { account.balance += amount; diff --git a/routes/users.js b/routes/users.js index 6ffed55b..c7b6bfa4 100644 --- a/routes/users.js +++ b/routes/users.js @@ -251,7 +251,7 @@ app.post( auth, ah(async (req, res) => { const { id } = req.body; - const account = await db.Account.findOne({ where: { id }}); + const account = await db.Account.findOne({ where: { id } }); if (account) await account.destroy(); res.end(); }) @@ -279,11 +279,11 @@ app.post( emit(user.username, "account", account); - if (pubkey) { - user.index++; - await user.save(); - emit(user.username, "user", user); - } + if (pubkey) user.index++; + user.account_id = account.id; + await user.save(); + user.account = account; + emit(user.username, "user", user); res.send(account); }) |
