-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (105 loc) · 3.66 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
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
require("dotenv-safe").config();
var keypress = require("keypress");
keypress(process.stdin);
const { Worker } = require("worker_threads");
const os = require("os");
const { Listr } = require("listr2");
const alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
const search = process.env.SEARCH;
const start = Date.now();
console.log("Searching for '" + search + "'");
console.log("Started at " + new Date(start).toString());
console.log("");
let currentSearch = search;
while (currentSearch) {
var alphabetPosition = alphabet.indexOf(currentSearch[0]);
if (alphabetPosition < 0) {
console.error("Can't find \"" + currentSearch[0] + '" in the alphabet: "' + alphabet + '"');
process.exit(1);
}
currentSearch = currentSearch.substring(1);
}
let rate = 0;
console.time("vanity");
const workerData = {};
workerData.search = search;
workerData.data = new Int32Array(
// 0 = attempts
// 1 = paused flag
// 2 = done
new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 3)
);
let result;
const workerTasks = [];
// Time between updates in ms
const updateRate = 250;
os.cpus().forEach((cpu, idx) => {
workerTasks.push({
title: "Starting thread " + idx + "...",
task: async (ctx, task) => {
await new Promise((resolve, reject) => {
let attempts = 0;
const worker = new Worker("./worker.js", { workerData });
const interval = setInterval(() => {
task.title = "#" + idx + ": " + attempts.toLocaleString() + " hashes";
}, updateRate);
worker.on("message", (data) => {
if ("result" in data) {
clearInterval(interval);
result = data.result;
resolve(data.result);
} else if ("done" in data) {
clearInterval(interval);
resolve();
} else {
attempts = data.attempts;
}
});
});
},
});
});
workerTasks.push({
title: "Total stats",
task: async (ctx, task) => {
await new Promise((resolve, reject) => {
const interval = setInterval(() => {
const passed = Date.now() - start;
const total = Atomics.load(workerData.data, 0);
rate = (total / (passed / 1000)).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
task.title = total.toLocaleString() + " hashes, " + rate + " hashes/s - press P to pause";
if (result) {
clearInterval(interval);
setTimeout(() => {
console.log("");
console.timeEnd("vanity");
console.log(result.sk + " " + result.pkh);
}, 100);
resolve(result);
}
}, updateRate);
});
},
});
const tasks = new Listr(workerTasks, { concurrent: true });
process.stdin.on("keypress", function (ch, key) {
if (key && key.ctrl && key.name == "c") {
process.exit();
}
if (key && key.name == "p") {
// toggle pause state
const old = Atomics.xor(workerData.data, 1, 1);
// if old value was "paused",
// then new value is "not paused"
// -> wake up worker threads
if (old == 1) {
Atomics.notify(workerData.data, 1);
}
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
tasks.run();