Skip to content

Commit

Permalink
Test events
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed May 11, 2019
1 parent 4992ee5 commit ee11391
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Expand Up @@ -16,6 +16,7 @@ const addressFormats = {
class Vain extends Emitter {
constructor({keyFormat = 'wif', addressFormat = 'p2pkh', prefix}) {
super();
this.generating = false;
this.generateKey = keyFormats[keyFormat];
this.addressFormat = addressFormats[addressFormat];

Expand All @@ -33,6 +34,7 @@ class Vain extends Emitter {
}

generate() {
this.generating = true;
const startTime = Date.now();

const {generateKey, addressFormat} = this;
Expand All @@ -44,13 +46,18 @@ class Vain extends Emitter {
let lastUpdate = Date.now();

while (!found) {
if (!this.generating) {
return {stopped: true};
}

attempts++;

keyData = generateKey({addressFormat});
address = addressFormat.derive(keyData.publicKey);

if (address.startsWith(this.prefix)) {
found = true;
this.generating = false;
}

const now = Date.now();
Expand Down
38 changes: 38 additions & 0 deletions test/events.js
@@ -0,0 +1,38 @@
import test from 'ava';
import * as bitcoin from 'bitcoinjs-lib';
import Vain from '..';

test('Vain instance emits `found` event when vanity address is found', t => {
t.plan(2);

const options = {
prefix: 'A'
};
const vain = new Vain(options);

vain.on('found', ({address, wif}) => {
const keyPair = bitcoin.ECPair.fromWIF(wif);
const {address: wifAddress} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey});

t.true(address.startsWith(`1${options.prefix}`));
t.is(address, wifAddress);
});

vain.generate();
});

test('Vain instance emits `update` event during address generation', t => {
t.plan(1);

const options = {
prefix: '1BitcoinEaterAddressDontSend'
};
const vain = new Vain(options);

vain.on('update', () => {
vain.generating = false;
t.pass();
});

vain.generate();
});

0 comments on commit ee11391

Please sign in to comment.