aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-06-24 02:52:33 -0400
committerAdam Soltys <[email protected]>2021-06-24 02:52:33 -0400
commitde06cbf6141679b2e10ca01bee1e372efc356c91 (patch)
tree5363e879eecbb3425af8ff621f0c0dd03cc9694f
parente343c8b07794a86156601efebe58b443b15b10da (diff)
downloadcoinos-server-de06cbf6141679b2e10ca01bee1e372efc356c91.tar.gz
coinos-server-de06cbf6141679b2e10ca01bee1e372efc356c91.zip
only send on-chain after db transaction
-rw-r--r--routes/bitcoin/send.js55
-rw-r--r--routes/liquid/fee.js7
-rw-r--r--routes/liquid/send.js28
-rw-r--r--routes/payments.js12
4 files changed, 52 insertions, 50 deletions
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index cab79e06..470889b3 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -1,4 +1,3 @@
-const bitcoin = require("bitcoinjs-lib");
const btc = config.liquid.btcasset;
module.exports = ah(async (req, res) => {
@@ -32,10 +31,10 @@ module.exports = ah(async (req, res) => {
total = total - change + fee;
let amount = total - fee;
- let params;
+ let account, params;
try {
await db.transaction(async transaction => {
- let account = await db.Account.findOne({
+ account = await db.Account.findOne({
where: {
id: user.account_id
},
@@ -62,38 +61,38 @@ module.exports = ah(async (req, res) => {
await account.decrement({ balance: total }, { transaction });
await account.reload({ transaction });
+ });
- params = {
- amount: -amount,
- fee,
- memo,
- account_id: account.id,
- user_id: user.id,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- address,
- confirmed: true,
- received: false,
- network: "bitcoin"
- };
+ params = {
+ amount: -amount,
+ fee,
+ memo,
+ account_id: account.id,
+ user_id: user.id,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ address,
+ confirmed: true,
+ received: false,
+ network: "bitcoin"
+ };
- if (config.bitcoin.walletpass)
- await bc.walletPassphrase(config.bitcoin.walletpass, 300);
+ if (config.bitcoin.walletpass)
+ await bc.walletPassphrase(config.bitcoin.walletpass, 300);
- hex = (await bc.signRawTransactionWithWallet(hex)).hex;
- params.hash = await bc.sendRawTransaction(hex);
+ hex = (await bc.signRawTransactionWithWallet(hex)).hex;
+ params.hash = await bc.sendRawTransaction(hex);
- let payment = await db.Payment.create(params, { transaction });
+ let payment = await db.Payment.create(params);
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- res.send(payment);
+ emit(user.username, "payment", payment);
+ res.send(payment);
- payments.push(params.hash);
- l.info("sent bitcoin", user.username, total);
- });
+ payments.push(params.hash);
+ l.info("sent bitcoin", user.username, total);
} catch (e) {
l.error("error sending bitcoin", e.message);
return res.status(500).send(e.message);
diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js
index 2070873a..a6c29f41 100644
--- a/routes/liquid/fee.js
+++ b/routes/liquid/fee.js
@@ -3,11 +3,6 @@ const buildTx = require("../../lib/buildliquidtx");
liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => {
let tx, fee;
let node = lq;
- /*
- if (asset === config.liquid.btcasset) {
- node = rare;
- }
- */
if (user.account.pubkey) {
let psbt = await buildTx({
@@ -81,7 +76,7 @@ module.exports = ah(async (req, res) => {
let tx = await liquidTx({ ...req.body, user: req.user })
res.send(tx);
} catch (e) {
- l.error("error estimating liquid fee", e.message, e.stack);
+ l.error("error estimating liquid fee", e.message);
return res.status(500).send(e.message);
}
});
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index 6455736c..13f77d26 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -1,5 +1,6 @@
const btc = config.liquid.btcasset;
const lcad = config.liquid.cadasset;
+const { Transaction } = require("liquidjs-lib");
sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
if (amount > 10000000) throw new Error("Amount too large");
@@ -48,7 +49,8 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
const assets = Object.keys(totals);
const payments = [];
- return db.transaction(async transaction => {
+ let main, signed;
+ await db.transaction(async transaction => {
for (let i = 0; i < assets.length; i++) {
let asset = assets[i];
let amount = totals[asset];
@@ -132,16 +134,11 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
}
}
- if (config.liquid.walletpass)
- await lq.walletPassphrase(config.liquid.walletpass, 300);
+ signed = await lq.signRawTransactionWithWallet(
+ await lq.blindRawTransaction(tx.hex)
+ );
+ let txid = Transaction.fromHex(signed.hex).getId();
- let blinded = await lq.blindRawTransaction(tx.hex);
- let signed = await lq.signRawTransactionWithWallet(blinded);
- let txid = await lq.sendRawTransaction(signed.hex);
-
- l.info("sent liquid tx", txid, address);
-
- let main;
for (let i = 0; i < assets.length; i++) {
p = payments[i];
if (p) {
@@ -157,15 +154,22 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
emit(user.username, "account", main.account);
emit(user.username, "payment", main);
- return main;
});
+
+ if (config.liquid.walletpass)
+ await lq.walletPassphrase(config.liquid.walletpass, 300);
+
+ let txid = await lq.sendRawTransaction(signed.hex);
+ l.info("sent liquid tx", txid, address);
+
+ return main;
};
module.exports = ah(async (req, res) => {
let { user } = req;
try {
- res.send(await sendLiquid({...req.body, user }));
+ res.send(await sendLiquid({ ...req.body, user }));
} catch (e) {
l.error("problem sending liquid", user.username, e.message);
return res.status(500).send(e);
diff --git a/routes/payments.js b/routes/payments.js
index e8e94d8a..86c0dd15 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -138,10 +138,14 @@ ah(async () => {
require("./liquid/receive");
setTimeout(async () => {
- const address = await lq.getNewAddress();
- const { hdkeypath } = await lq.getAddressInfo(address);
- const parts = hdkeypath.split("/");
- app.set("lqAddressIndex", parts[parts.length - 1].slice(0, -1));
+ try {
+ const address = await lq.getNewAddress();
+ const { hdkeypath } = await lq.getAddressInfo(address);
+ const parts = hdkeypath.split("/");
+ app.set("lqAddressIndex", parts[parts.length - 1].slice(0, -1));
+ } catch(e) {
+ console.log("Problem getting liquid address", e.message);
+ }
}, 50);
}