diff --git a/bench/index.js b/bench/index.js index 541126a..b041cd5 100644 --- a/bench/index.js +++ b/bench/index.js @@ -75,5 +75,5 @@ options.forEach(options => { console.log(`Speed: ${data.addressesPerSecond.toLocaleString()} addr/s`); }); - vain.start(); + vain.generate(); }); diff --git a/src/index.js b/src/index.js index 36741d6..e00ea9e 100644 --- a/src/index.js +++ b/src/index.js @@ -32,55 +32,53 @@ class Vain extends Emitter { this.prefix = `${this.addressFormat.prefix}${prefix}`; } - start() { - return new Promise(resolve => { - const startTime = Date.now(); - - const {generateKey, addressFormat} = this; - - let found; - let attempts = 0; - let keyData; - let address; - let lastUpdate = Date.now(); - - while (!found) { - attempts++; - - keyData = generateKey({addressFormat}); - address = addressFormat.derive(keyData.publicKey); - - 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; - } + generate() { + const startTime = Date.now(); + + const {generateKey, addressFormat} = this; + + let found; + let attempts = 0; + let keyData; + let address; + let lastUpdate = Date.now(); + + while (!found) { + attempts++; + + keyData = generateKey({addressFormat}); + address = addressFormat.derive(keyData.publicKey); + + if (address.startsWith(this.prefix)) { + found = true; } - const endTime = Date.now(); - const duration = endTime - startTime; - const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND)); - - const result = { - duration, - attempts, - addressesPerSecond, - address, - ...keyData.format() - }; - this.emit('found', result); - resolve(result); - }); + 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 endTime = Date.now(); + const duration = endTime - startTime; + const addressesPerSecond = Math.floor(attempts / (duration / ONE_SECOND)); + + const result = { + duration, + attempts, + addressesPerSecond, + address, + ...keyData.format() + }; + this.emit('found', result); + return result; } } diff --git a/test/unit.js b/test/unit.js index b434b41..54bf729 100644 --- a/test/unit.js +++ b/test/unit.js @@ -7,13 +7,13 @@ test('Vain is exported', t => { t.not(Vain, undefined); }); -test('Vain derives a p2pkh vanity address', async t => { +test('Vain derives a p2pkh vanity address', t => { const options = { addressFormat: 'p2pkh', prefix: 'A' }; const vain = new Vain(options); - const {address, wif} = await vain.start(); + const {address, wif} = vain.generate(); const keyPair = bitcoin.ECPair.fromWIF(wif); const {address: wifAddress} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey}); @@ -22,13 +22,13 @@ test('Vain derives a p2pkh vanity address', async t => { t.is(address, wifAddress); }); -test('Vain derives a p2wpkh-p2sh vanity address', async t => { +test('Vain derives a p2wpkh-p2sh vanity address', t => { const options = { addressFormat: 'p2wpkh-p2sh', prefix: 'A' }; const vain = new Vain(options); - const {address, wif} = await vain.start(); + const {address, wif} = vain.generate(); const keyPair = bitcoin.ECPair.fromWIF(wif); const {address: wifAddress} = bitcoin.payments.p2sh({ @@ -39,13 +39,13 @@ test('Vain derives a p2wpkh-p2sh vanity address', async t => { t.is(address, wifAddress); }); -test('Vain derives a p2wpkh vanity address', async t => { +test('Vain derives a p2wpkh vanity address', t => { const options = { addressFormat: 'p2wpkh', prefix: 'a' }; const vain = new Vain(options); - const {address, wif} = await vain.start(); + const {address, wif} = vain.generate(); const keyPair = bitcoin.ECPair.fromWIF(wif); const {address: wifAddress} = bitcoin.payments.p2wpkh({pubkey: keyPair.publicKey}); @@ -61,7 +61,7 @@ test('Vain derives a p2pkh vanity seed', async t => { prefix: 'A' }; const vain = new Vain(options); - const {address, derivationPath, mnemonic} = await vain.start(); + const {address, derivationPath, mnemonic} = vain.generate(); const seed = await bip39.mnemonicToSeed(mnemonic); const node = bitcoin.bip32.fromSeed(seed); @@ -79,7 +79,7 @@ test('Vain derives a p2wpkh-p2sh vanity seed', async t => { prefix: 'A' }; const vain = new Vain(options); - const {address, derivationPath, mnemonic} = await vain.start(); + const {address, derivationPath, mnemonic} = vain.generate(); const seed = await bip39.mnemonicToSeed(mnemonic); const node = bitcoin.bip32.fromSeed(seed); @@ -99,7 +99,7 @@ test('Vain derives a p2wpkh vanity seed', async t => { prefix: 'a' }; const vain = new Vain(options); - const {address, derivationPath, mnemonic} = await vain.start(); + const {address, derivationPath, mnemonic} = vain.generate(); const seed = await bip39.mnemonicToSeed(mnemonic); const node = bitcoin.bip32.fromSeed(seed); @@ -110,12 +110,12 @@ test('Vain derives a p2wpkh vanity seed', async t => { t.is(address, seedAddress); }); -test('Vain defaults to p2pkh if no address format is set', async t => { +test('Vain defaults to p2pkh if no address format is set', t => { const options = { prefix: 'A' }; const vain = new Vain(options); - const {address, wif} = await vain.start(); + const {address, wif} = vain.generate(); const keyPair = bitcoin.ECPair.fromWIF(wif); const {address: wifAddress} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey});