-
Notifications
You must be signed in to change notification settings - Fork 37
/
cli.js
executable file
·190 lines (156 loc) · 5.45 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
var program = require('commander'),
util = require('util'),
async = require('async'),
path = require('path'),
fs = require('fs-extra'),
Tree = require('./lib/tree'),
blproof = require('./lib/blproof');
function format (node) {
var data = node.data;
if (!data) return '';
// leaf nodes
if (data.nonce !== undefined) {
return data.sum + ', ' + data.user + ', ' + data.nonce;
}
else {
return data.sum + ', ' + data.hash;
}
}
function log (str) {
if (!program.verbose) return;
console.error(str);
}
program
.version(JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version)
.usage('<action>')
.option('-f, --file <file>', 'Which file to use.')
.option('-h, --human', 'Print in human readable format instead of serializing.')
.option('--id <id>', 'Identifier for use in blind solvency proof scheme.')
.option('--currency <currency>', 'Specify a currency code.', 'BTC')
.option('-v, --verbose', 'Output debugging information.');
program
.command('completetree')
.description('Generates complete proof tree. Must specify accounts file. See test/accounts.json for format.')
.action(function () {
if (!program.file) program.help();
var accounts = JSON.parse(fs.readFileSync(program.file));
var complete_tree = blproof.generateCompleteTree(accounts);
if (program.human) {
complete_tree.prettyPrint(format);
}
else {
console.log(complete_tree.serializeToArray());
}
});
program
.command('partialtree <user>')
.description('Extracts the partial proof tree for a given user. Must specify complete proof tree file.')
.action(function (user) {
if (!program.file) program.help();
if (!user) program.help();
var complete_tree = Tree.deserializeFromArray(fs.readFileSync(program.file));
var partial_tree = blproof.extractPartialTree(complete_tree, user);
//console.log(util.inspect(partial_tree, { depth: null }));
if (program.human) {
partial_tree.prettyPrint(format);
}
else {
console.log(blproof.serializePartialTree(partial_tree, program.id));
}
});
program
.command('root')
.description('Extracts root node from tree. Must specify complete proof tree file.')
.action(function () {
if (!program.file) program.help();
var complete_tree = Tree.deserializeFromArray(fs.readFileSync(program.file));
var root = complete_tree.root();
if (program.human) {
console.log('Root hash: ' + root.data.hash);
console.log('Root sum: ' + root.data.sum);
}
else {
console.log(blproof.serializeRoot(complete_tree, program.currency));
}
});
program
.command('verify')
.description('Verify a partial proof tree. Must specify root hash and sum. Must specify the partial proof tree file.')
.option('--root <root_file>', 'File of the root')
.option('--hash <hash>', 'Hash of root node')
.option('--sum <value>', 'Sum of root node', parseFloat)
.action(function (action) {
if (!program.file) program.help();
var serialized = fs.readFileSync(program.file);
var id = JSON.parse(serialized).id;
var tree = blproof.deserializePartialTree(serialized);
var root = tree.root();
var root_data = {};
if (action.root) {
root_data = JSON.parse(fs.readFileSync(action.root)).root;
}
else {
root_data = { sum: action.sum, hash: action.hash };
}
try {
var user_data = blproof.verifyTree(tree, root_data);
console.log('Partial tree verified successfuly!\n');
if (id) {
console.log('Site ID: ' + id);
}
console.log('User: ' + user_data.user);
console.log('Balance: ' + user_data.sum);
}
catch (e) {
console.error('INVALID partial tree!');
console.error(e);
process.exit(-1);
}
});
program
.command('generate')
.description('Generate root and partial trees for all users in accounts file.')
.action(function () {
if (!program.file) program.help();
var limit = 20;
var accounts = JSON.parse(fs.readFileSync(program.file));
var len = accounts.length;
var complete_tree = blproof.generateCompleteTree(accounts);
complete_tree.prettyPrint(format);
var pt_path = 'partial_trees';
// remove partial_trees/ recurisvely if it exists
fs.removeSync(pt_path);
// create an empty partial_trees/ directory
fs.mkdirSync(pt_path);
async.parallel([
function (cb) {
fs.writeFile('complete_tree.json', complete_tree.serializeToArray(), cb);
},
function (cb) {
fs.writeFile('root.json', blproof.serializeRoot(complete_tree, program.currency), cb);
},
function (cb) {
// limit is to limit how many files can be written at the same time
log(accounts);
async.eachLimit(accounts, limit, function (account, cb) {
var partial_tree = blproof.extractPartialTree(complete_tree, account.user);
log('Processing ' + account.user);
if (program.verbose) partial_tree.prettyPrint(format);
fs.writeFile(path.join(pt_path, account.user + '.json'),
blproof.serializePartialTree(partial_tree, program.id),
cb);
}, cb);
}
], function (err) {
if (err) {
console.error(err);
process.exit(-1);
}
else {
console.log('Successfuly generated ' + len + ' partial trees in ' + pt_path + '.');
}
});
});
program.parse(process.argv);
if (!program.args.length) program.help();