-
Notifications
You must be signed in to change notification settings - Fork 2
/
findkey
executable file
·43 lines (32 loc) · 1.32 KB
/
findkey
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const ed = require('@noble/ed25519');
const {
pubKeyIsValid,
keyPairFilename,
keyIsUnderDifficultyThreshold
} = require('./common');
async function findMatchingKey (strict, rounds = 0, matchedResolver) {
const next = findMatchingKey.bind(null, strict, rounds + 1);
if (rounds === 0) {
return new Promise((resolve) => next(resolve));
}
const privateKey = ed.utils.randomPrivateKey();
const publicKey = Buffer.from(await ed.getPublicKey(privateKey));
if (pubKeyIsValid(publicKey, strict) && keyIsUnderDifficultyThreshold(publicKey, {})) {
return matchedResolver({ rounds, matched: { privateKey, publicKey } });
}
setTimeout(next.bind(null, matchedResolver), 0);
}
async function main () {
const start = Date.now();
['SIGINT', 'SIGHUP'].forEach(sig => process.on(sig, () => process.exit()));
const { rounds, matched } = await findMatchingKey(process.argv.length > 2 && process.argv[2] === '--strict');
const outLines = Object.entries(matched).map(([, keyData]) => Buffer.from(keyData).toString('hex'));
const outStr = outLines.join('');
const outFileName = keyPairFilename(matched.publicKey);
await fs.promises.writeFile(outFileName, outStr);
console.log(`${rounds} in ${(Date.now() - start) / 1000}s.\nWrote: ${outFileName}`);
}
main();