aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-01-27 13:58:06 -0800
committerAdam Soltys <[email protected]>2024-01-27 13:58:06 -0800
commit96f865cd9c9e70205d062e0cda86e0114afb5fbc (patch)
tree7e15e5e0a178374a5f03d02008b6d866df6fe1ac /routes
parent1b14654bc2d4766353d3f054a7c7902d560bef59 (diff)
downloadcoinos-server-96f865cd9c9e70205d062e0cda86e0114afb5fbc.tar.gz
coinos-server-96f865cd9c9e70205d062e0cda86e0114afb5fbc.zip
autowithdrawals
Diffstat (limited to 'routes')
-rw-r--r--routes/invoices.js16
-rw-r--r--routes/payments.js103
-rw-r--r--routes/users.js1
3 files changed, 19 insertions, 101 deletions
diff --git a/routes/invoices.js b/routes/invoices.js
index 898938dd..85c9166f 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -22,13 +22,15 @@ export default {
"pubkey",
]);
} else if (config.classic) {
- invoice = await got(`${config.classic}/invoice/${id}`).json();
- if (invoice) {
- invoice.id = invoice.uuid;
- invoice.classic = true;
- invoice.user.id = invoice.user.uuid;
- invoice.user.username += "@classic";
- }
+ try {
+ invoice = await got(`${config.classic}/invoice/${id}`).json();
+ if (invoice) {
+ invoice.id = invoice.uuid;
+ invoice.classic = true;
+ invoice.user.id = invoice.user.uuid;
+ invoice.user.username += "@classic";
+ }
+ } catch (e) {}
}
if (invoice) res.send(invoice);
diff --git a/routes/payments.js b/routes/payments.js
index 3dba0385..13ef20c2 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -12,7 +12,8 @@ import {
types,
getNode,
build,
- payLightning,
+ sendLightning,
+ sendOnchain,
} from "$lib/payments";
import { mqtt1, mqtt2 } from "$lib/mqtt";
import got from "got";
@@ -62,7 +63,7 @@ export default {
})
.json();
} else if (payreq) {
- p = await payLightning(user, payreq, amount, maxfee, memo);
+ p = await sendLightning(user, payreq, amount, maxfee, memo);
}
if (!p) {
@@ -79,7 +80,7 @@ export default {
res.send(p);
} catch (e) {
- warn(user.username, "payment failed", amount, balance, hash, payreq);
+ warn(user.username, "payment failed", amount, balance, hash, payreq);
err(e.message);
bail(res, e.message);
}
@@ -282,8 +283,9 @@ export default {
async fee({ body, user }, res) {
try {
- res.send(await build(body, user));
+ res.send(await build({ ...body, user }));
} catch (e) {
+ console.log(e)
warn(
"problem estimating fee",
e.message,
@@ -300,98 +302,11 @@ export default {
async send({ body, user }, res) {
try {
await requirePin({ body, user });
-
- let { memo, hex, rate } = body;
- if (!hex) ({ hex } = await build(body, user));
-
- let type, tx;
- try {
- tx = await bc.decodeRawTransaction(hex);
- type = types.bitcoin;
- } catch (e) {
- try {
- tx = await lq.decodeRawTransaction(hex);
- type = types.liquid;
- } catch (e) {
- fail("unrecognized tx");
- }
- }
-
- let node = getNode(type);
- let { txid } = tx;
-
- if (config[type].walletpass)
- await node.walletPassphrase(config[type].walletpass, 300);
-
- let { hex: signed } = await node.signRawTransactionWithWallet(
- type === types.liquid ? await node.blindRawTransaction(hex) : hex,
- );
-
- ({ txid } = await node.decodeRawTransaction(signed));
-
- let r = await node.testMempoolAccept([signed]);
- if (!r[0].allowed) fail("transaction rejected");
-
- let total = 0;
- let fee = 0;
- let change = 0;
-
- if (type === types.liquid) {
- for (let {
- asset,
- scriptPubKey: { address, type },
- value,
- } of tx.vout) {
- if (asset !== config.liquid.btc) fail("only L-BTC supported");
- if (type === "fee") fee = sats(value);
- else {
- total += sats(value);
-
- if (address) {
- if ((await node.getAddressInfo(address)).ismine) {
- change += sats(value);
- }
- }
- }
- }
- } else {
- let totalIn = 0;
- for await (let { txid, vout } of tx.vin) {
- let hex = await node.getRawTransaction(txid);
- let tx = await node.decodeRawTransaction(hex);
- totalIn += sats(tx.vout[vout].value);
- }
-
- for (let {
- scriptPubKey: { address },
- value,
- } of tx.vout) {
- total += sats(value);
- if (await g(`invoice:${address}`))
- fail("Cannot send to internal address");
-
- if ((await node.getAddressInfo(address)).ismine)
- change += sats(value);
- }
-
- fee = totalIn - total;
- }
-
- let amount = total - change;
-
- await debit({
- hash: txid,
- amount,
- fee,
- rate,
- user,
- type,
- });
-
- await node.sendRawTransaction(signed);
+ let txid = await sendOnchain({ ...body, user });
res.send({ txid });
} catch (e) {
+ console.log(e)
warn("payment failed", e.message);
res.code(500).send(e.message);
}
@@ -450,7 +365,7 @@ export default {
let r = await got(`${callback}?amount=${amount * 1000}`).json();
if (r.reason) fail(r.rason);
let { pr } = r;
- let p = await payLightning(user, pr, amount, maxfee, memo);
+ let p = await sendLightning(user, pr, amount, maxfee, memo);
if (!p) {
p = await debit({ hash: pr, amount, memo, user });
diff --git a/routes/users.js b/routes/users.js
index 230d46a1..dff3f7bc 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -244,6 +244,7 @@ export default {
"twofa",
"shopifyToken",
"shopifyStore",
+ "withdrawal",
];
for (let a of attributes) {