-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcli.js
179 lines (126 loc) · 4.6 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
'use strict';
var path = require('path'),
childProcess = require('child_process'),
os = require('os'),
fs = require('fs'),
workerPath = path.normalize(path.join(__dirname, 'worker.js')),
invalidSecretsPath = path.join(__dirname, 'invalid_secrets.json'),
validSecretPath = path.join(__dirname, 'valid_secret.json'),
numberOfAvailableCPUs = os.cpus().length,
secrets = require('./secrets'),
config = require('./config.json'),
invalidSecrets = [],
testedSecrets = 0,
index = 0,
invalidSecretsSaveInterval;
process.on('exit', function () {
clearCurrentLine();
console.log('Progress: ' + currentProgressInPercent() + '% (' + testedSecrets + '/' + secrets.length + ')');
console.log('Total processing time: ' + process.uptime().toFixed(2) + ' seconds');
console.log('Processing time per secret: ' + (process.uptime() / testedSecrets).toFixed(2) + ' seconds');
process.stdout.write('\x07');
process.exit(0);
});
process.on('SIGINT', process.exit);
console.log('Testing ' + secrets.length + ' secrets on ' + numberOfAvailableCPUs + ' CPU cores...');
console.log('Public address: ' + config.publicAddress);
console.log('BIP38 encrypted private key: ' + config.encryptedPrivateKey);
console.log('Importing invalid secrets from "' + invalidSecretsPath + '"...');
try {
fs.statSync(invalidSecretsPath);
invalidSecrets = JSON.parse(fs.readFileSync(invalidSecretsPath, 'utf8'));
console.log('Imported ' + invalidSecrets.length + ' invalid secrets that will be skipped.');
}
catch (error) {
console.log('No invalid secrets have been found, moving on...');
}
console.log('Filtering secrets...');
secrets = secrets.filter(function removeInvalidSecret (secret) {
return invalidSecrets.indexOf(secret) === -1
});
console.log('Decrypting with ' + secrets.length + ' secrets...');
if (secrets.length === 0) {
process.exit(0);
};
logProgress();
while (numberOfAvailableCPUs--) {
setTimeout(spawnWorker, numberOfAvailableCPUs * 100);
}
invalidSecretsSaveInterval = setInterval(saveInvalidSecretsToFile, 10000);
function spawnWorker () {
var workerProcess;
if (index + 1 > secrets.length) {
return;
}
workerProcess = childProcess.fork(workerPath);
workerProcess.secret = secrets[index];
workerProcess.send({
publicAddress: config.publicAddress,
encryptedPrivateKey: config.encryptedPrivateKey,
secret: secrets[index]
});
index++;
workerProcess.on('exit', function (exitCode) {
testedSecrets++;
logProgress();
if (exitCode === 0) {
clearCurrentLine();
console.log('Saving valid secret to "' + validSecretPath + '"...');
fs.writeFile(
validSecretPath,
JSON.stringify([workerProcess.secret], null, 4),
'utf8',
function (error) {
if (error) {
console.log(error);
}
console.log('A valid secret has been found, aborting...');
process.exit(0);
}
);
return;
}
invalidSecrets.push(workerProcess.secret);
if (testedSecrets === secrets.length) {
clearInterval(invalidSecretsSaveInterval);
saveInvalidSecretsToFile();
}
spawnWorker();
});
}
function logProgress () {
var secretsToCheck = secrets.length - testedSecrets,
estimatedDuration = testedSecrets === 0
? 'calculating estimated duration...'
: 'finished in ~ ' + formatSeconds((process.uptime() / testedSecrets) * secretsToCheck);
clearCurrentLine();
process.stdout.write(
'Progress: ' + currentProgressInPercent() + '% ' +
'(' + testedSecrets + '/' + secrets.length + ')' +
', ' + estimatedDuration
);
};
function formatSeconds (seconds) {
var days = Math.floor(seconds / 86400),
hhmmss = new Date(seconds * 1000).toISOString().substr(11, 8);
return days + ':' + hhmmss;
};
function currentProgressInPercent () {
var percentComplete = secrets.length === 0 ? 100 : testedSecrets * 100 / secrets.length;
return Math.round(percentComplete * 100) / 100;
};
function clearCurrentLine () {
process.stdout.write('\x1b[0G\x1b[0K');
};
function saveInvalidSecretsToFile () {
fs.writeFile(
invalidSecretsPath,
JSON.stringify(invalidSecrets, null, 4),
'utf8',
function (error) {
if (error) {
console.log(error);
}
}
);
}