-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·107 lines (91 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const arg = require("arg");
const yesno = require("yesno");
const Client = require("bitcoin-core");
const { construct, broadcast } = require("./cutxo");
const HELP = `Consolidates UTXO on your bitcoin node.
cutxo --username root --password toor --amount 0.0001 --fee 2
--help Show help.
--host Host to connect to. Default is "localhost".
--port Port where bitcoin json-rpc is listerning. Default is "8332".
--limit Limit number of inputs.
--amount Maximum amount for UTXO to include in transaction. Default is "0.0001".
--fee Fee for new transaction in Satoshi/byte. Default is "1".
Designed for Bitcoin version >= 0.17
`
const parseArgs = () => {
const args = arg(
{
"--help": Boolean,
"--host": String,
"--username": String,
"--password": String,
"--port": Number,
"--limit": Number,
"--amount": Number,
"--fee": Number,
},
{ argv: process.argv.slice(2) }
);
if (args["--help"]) {
console.log(HELP);
process.exit();
}
const options = {
host: args["--host"] || "localhost",
port: args["--port"] || 8332,
username: args["--username"],
password: args["--password"],
maximumAmount: args["--amount"] || 0.0001,
limit: args["--limit"],
feeRate: args["--fee"] || 1,
};
return options;
};
(async () => {
const options = parseArgs();
const client = new Client({
port: options.port,
username: options.username,
password: options.password,
});
try {
await client.ping();
} catch (e) {
console.error("Connection error");
console.error(e.toString());
process.exit(1);
}
let tx;
try {
tx = await construct({
client,
maximumAmount: options.maximumAmount,
limit: options.limit,
feeRate: options.feeRate,
});
} catch (e) {
console.error("Constructing transaction error");
console.error(e.toString());
process.exit(1);
}
console.info();
console.info("Number of inputs:", tx.inputsUsed);
console.info("Inputs total amount:", tx.amountInput);
console.info("Output amount:", tx.amountOutput);
console.info("Fee:", tx.fee);
console.info("Output address:", tx.address);
console.info();
const ok = await yesno({
question: "Are you sure you want to broadcast the transaction?",
});
if (!ok) process.exit(0);
try {
const txid = await broadcast({ client, hex: tx.hex });
console.info(txid);
} catch (e) {
console.error("Broadcasting transaction error");
console.error(e.toString());
process.exit(1);
}
})();