aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-05-29 17:33:10 -0700
committerAdam Soltys <[email protected]>2020-05-29 17:33:10 -0700
commit2b59255e7350c6e359263808b2af263ef374285a (patch)
tree7191e93df0301cf82356050f7d5c62eb5d2d2726 /routes
parent67aa0ec59889f5b0b2d90a96384cfcb7e58d7a07 (diff)
downloadcoinos-server-2b59255e7350c6e359263808b2af263ef374285a.tar.gz
coinos-server-2b59255e7350c6e359263808b2af263ef374285a.zip
private key sweep
Diffstat (limited to 'routes')
-rw-r--r--routes/bitcoin/sweep.js59
-rw-r--r--routes/payments.js1
-rw-r--r--routes/users.js2
3 files changed, 61 insertions, 1 deletions
diff --git a/routes/bitcoin/sweep.js b/routes/bitcoin/sweep.js
new file mode 100644
index 00000000..37efb73f
--- /dev/null
+++ b/routes/bitcoin/sweep.js
@@ -0,0 +1,59 @@
+const axios = require("axios");
+const bitcoin = require("bitcoinjs-lib");
+const coinselect = require("coinselect");
+const split = require("coinselect/split");
+const api = prod ? "https://blockstream.info/api" : "http://localhost:3002";
+const SATS = 100000000;
+const network = prod
+ ? bitcoin.networks["bitcoin"]
+ : bitcoin.networks["regtest"];
+
+module.exports = async (req, res) => {
+ let ecpair = bitcoin.ECPair.fromWIF('cVosbTBiXp7LYSiGVRZfZnrEjpao4X4CUTG7Ze21XM3zFTLPCMvk', network);
+
+ let { address: from, amount, feeRate, target } = req.body;
+ let utxos;
+ if (!feeRate)
+ feeRate = ((await bc.estimateSmartFee(6)).feerate * SATS);
+ feeRate = Math.round(feeRate / 1000);
+ l.info("sweeping", req.user.username, from, amount, feeRate);
+ try {
+ ({ data: utxos } = await axios.get(`${api}/address/${from}/utxo`));
+ } catch (e) {
+ l.error("problem fetching utxos", e.message);
+ }
+
+ let targets = [
+ {
+ address: target,
+ value: amount
+ }
+ ];
+
+ let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate);
+
+ if (!inputs || !outputs) return res.status(500).send("Unable to construct sweep transaction, try a lower fee rate?");
+
+ let psbt = new bitcoin.Psbt({ network });
+
+ outputs.map(({ address, value }) => {
+ psbt = psbt.addOutput({
+ address: address || from,
+ value
+ })
+ });
+
+ for (let i = 0; i < inputs.length; i++) {
+ let { txid, vout } = inputs[i];
+ let nonWitnessUtxo = Buffer.from(await bc.getRawTransaction(txid), 'hex');
+ psbt = psbt.addInput({
+ hash: txid,
+ index: vout,
+ nonWitnessUtxo,
+ });
+ }
+
+ feeRate = Math.round(feeRate * 1000);
+ psbt = psbt.toBase64();
+ res.send({ feeRate, psbt });
+};
diff --git a/routes/payments.js b/routes/payments.js
index ab35c9a0..9586a9a8 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -40,6 +40,7 @@ const { join } = require("path");
if (config.bitcoin) {
bc = new BitcoinCore(config.bitcoin);
+ app.post("/bitcoin/sweep", auth, require("./bitcoin/sweep"));
app.post("/bitcoin/fee", auth, require("./bitcoin/fee"));
app.post("/bitcoin/send", auth, require("./bitcoin/send"));
require("./bitcoin/receive");
diff --git a/routes/users.js b/routes/users.js
index 61421edf..4c39ca07 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -110,7 +110,7 @@ app.post("/register", async (req, res) => {
delete challenge[ip];
let success, score;
- if (process.env.NODE_ENV === "production") {
+ if (prod) {
if (!token) return res.status(500).send("Missing captcha token");