|
const { Worker, isMainThread, parentPort } = require('worker_threads'); |
|
const { cpus } = require('os'); |
|
|
|
const THREAD_COUNT = cpus().length; |
|
const PREFIX = '0000'; |
|
const SUFFIX = '0000'; |
|
|
|
if (isMainThread) { |
|
console.log(' - threads: %s', THREAD_COUNT); |
|
console.log(' - prefix: ', PREFIX); |
|
console.log(' - suffix: ', SUFFIX); |
|
process.stdout.write(' - generated: 0'); |
|
|
|
let i = 0; |
|
const to = setInterval(() => { |
|
process.stdout.clearLine(); |
|
process.stdout.cursorTo(0); |
|
process.stdout.write(' - generated: ' + Number(i).toLocaleString()); |
|
}, 1000); |
|
|
|
const workers = []; |
|
for (let t = 0; t < THREAD_COUNT; t++) { |
|
const worker = new Worker(__filename); |
|
worker.on('message', (event) => { |
|
if (!event) { |
|
i++; |
|
return; |
|
} |
|
clearInterval(to); |
|
process.stdout.write('\n'); |
|
console.log(' - private: ', event.privateKey); |
|
console.log(' - address: ', event.address); |
|
}); |
|
worker.on('exit', () => { |
|
process.exit(); |
|
}); |
|
workers.push(workers); |
|
} |
|
} else { |
|
const secp256k1 = require('secp256k1'); |
|
const keccak = require('keccak'); |
|
const randomBytes = require('randombytes'); |
|
|
|
do { |
|
const rand = randomBytes(32); |
|
const privateKey = rand.toString('hex'); |
|
const publicKey = secp256k1.publicKeyCreate(rand, false).slice(1); |
|
const address = keccak('keccak256').update(publicKey).digest().slice(-20).toString('hex'); |
|
|
|
const prefix = address.slice(0, PREFIX.length); |
|
const suffix = address.slice(-1 * SUFFIX.length); |
|
|
|
if (prefix == PREFIX && suffix == SUFFIX) { |
|
return parentPort.postMessage({ |
|
privateKey, |
|
address: '0x' + address, |
|
}); |
|
} |
|
|
|
parentPort.postMessage(null); |
|
} while (1); |
|
} |