-
Notifications
You must be signed in to change notification settings - Fork 16
/
cli.js
executable file
·160 lines (135 loc) · 4.29 KB
/
cli.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
console.error('WARNING: This tool does not currently conform with latest specification.');
var program = require('commander'),
bitcoin = require('bitcoin'),
async = require('async'),
baproof = require('./'),
fs = require('fs');
program
.version(JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version)
.usage('<action>')
.option('-h, --host <host>', 'bitcoind RPC host', 'localhost')
.option('-p, --port <port>', 'bitcoind RPC port', parseInt, 8332)
.option('--user <user>', 'bitcoind RPC user')
.option('--pass <pass>', 'bitcoind RPC pass')
.option('--human', 'Human readable output.');
function parse_list (str) {
var arr = str.split(',');
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].trim();
}
return arr;
}
program
.command('verifysignatures <file>')
.description('Verify that all signatures in <file> are valid.')
.action(function (file) {
var obj = JSON.parse(fs.readFileSync(file));
if (baproof.verifySignatures(obj)) {
console.log(obj.message + ' signatures are valid!');
}
else {
console.error('INVALID signature found!');
process.exit(-1);
}
});
program
.command('balance <file>')
.description('Calculate the total balance of an asset proof.')
.action(function (file) {
var obj = JSON.parse(fs.readFileSync(file));
if (!baproof.verifySignatures(obj)) {
console.error('INVALID signature found!');
process.exit(-1);
}
var client = new bitcoin.Client({
host: program.host,
port: program.port,
user: program.user,
pass: program.pass
});
var total = 0;
var addresses = baproof.getAddresses(obj);
async.each(addresses, function (addr, cb) {
client.cmd('getreceivedbyaddress', addr, function (err, res) {
if (err) return console.error(err);
total += Number(res);
cb();
});
}, function (err) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log(total);
});
});
program
.command('signall <message> <blockhash>')
.description('Generates an asset proof file with all private keys in wallet.')
.option('--keys <keys>', 'Comma separated list of private keys used to sign.', parse_list)
.option('--addresses <addresses>', 'Comma separated list of addresses to sign.', parse_list)
.action(function (message, blockhash, opts) {
// Private keys are passed directly, no need to do RPC calls
if (opts.keys) {
var res = baproof.signAll(opts.keys, message, blockhash);
console.log(JSON.stringify(res));
return;
}
var msg = blockhash + '|' + message;
var client = new bitcoin.Client({
host: program.host,
port: program.port,
user: program.user,
pass: program.pass
});
//client.getBalance('*', 6, function (err, balance) {
//if (err) return console.log(err);
//console.log('Balance:', balance);
//});
var addresses = opts.addresses || [],
output = {
message: message,
blockhash: blockhash,
signatures: []
};
//@TODO: batch requests https://github.com/freewil/node-bitcoin#batch-multiple-rpc-calls-into-single-http-request
// nevermind, there seems to be a bug with batch requests :(
async.series([
function (cb) {
if (addresses.length > 0) return cb();
client.cmd('listreceivedbyaddress', 0, true, function (err, res){
if (err) return cb(err);
if (!res.length) {
return cb('No address found in bitcoin wallet!');
}
res.forEach(function (hash) {
addresses.push(hash.address);
});
cb();
});
},
function (cb) {
async.each(addresses, function (addr, cb) {
client.cmd('signmessage', addr, msg, function (err, res) {
if (err) return cb(err);
output.signatures.push({
address: addr,
signature: res
});
cb();
});
}, cb);
}
], function (err) {
if (err) return console.error(err);
if (program.human) {
console.log(output);
}
else {
console.log(JSON.stringify(output));
}
});
});
program.parse(process.argv);
if (!program.args.length) program.help();