Browse Source

Return Promise from start()

master
Luke Childs 6 years ago
parent
commit
5f38016296
  1. 75
      src/index.js

75
src/index.js

@ -10,45 +10,50 @@ class Vain extends Emitter {
} }
start() { start() {
const startTime = Date.now(); return new Promise(resolve => {
const startTime = Date.now();
let found;
let attempts = 0; let found;
let keyPair; let attempts = 0;
let address; let keyPair;
let lastUpdate = Date.now(); let address;
let lastUpdate = Date.now();
while (!found) {
attempts++; while (!found) {
keyPair = bitcoin.ECPair.makeRandom(); attempts++;
({address} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey})); keyPair = bitcoin.ECPair.makeRandom();
({address} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey}));
if (address.startsWith(this.prefix)) {
found = true; if (address.startsWith(this.prefix)) {
found = true;
}
const now = Date.now();
if ((now - lastUpdate) > ONE_SECOND) {
const duration = now - startTime;
const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND));
this.emit('update', {
duration,
attempts,
addressesPerSecond
});
lastUpdate = now;
}
} }
const now = Date.now(); const endTime = Date.now();
if ((now - lastUpdate) > ONE_SECOND) { const duration = endTime - startTime;
const duration = now - startTime; const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND));
const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND));
this.emit('update', {
duration,
attempts,
addressesPerSecond
});
lastUpdate = now;
}
}
const endTime = Date.now();
const duration = endTime - startTime;
const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND));
this.emit('found', { const result = {
duration, duration,
addressesPerSecond, addressesPerSecond,
address, address,
wif: keyPair.toWIF() wif: keyPair.toWIF()
};
this.emit('found', result);
resolve(result);
}); });
} }
} }

Loading…
Cancel
Save