From 89afb8c5e7b67a46d76fc09159e062a01cde48ea Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Fri, 10 May 2019 16:42:30 +0700 Subject: [PATCH] Return formatting function as part of key generation --- src/index.js | 6 +++--- src/wif.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 7a7997b..ed02b7b 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,7 @@ const addressFormats = new Map(Object.entries({ class Vain extends Emitter { constructor({keyFormat = 'wif', addressFormat = 'p2pkh', prefix}) { super(); - this.keyFormat = keyFormats.get(keyFormat); + this.generateKey = keyFormats.get(keyFormat); this.addressFormat = addressFormats.get(addressFormat); if (typeof prefix !== 'string' || prefix.length === 0) { @@ -44,7 +44,7 @@ class Vain extends Emitter { while (!found) { attempts++; - keyData = this.keyFormat.generate(); + keyData = this.generateKey(); address = this.addressFormat.derive(keyData.publicKey); if (address.startsWith(this.prefix)) { @@ -72,7 +72,7 @@ class Vain extends Emitter { duration, addressesPerSecond, address, - ...this.keyFormat.format(keyData) + ...keyData.format() }; this.emit('found', result); resolve(result); diff --git a/src/wif.js b/src/wif.js index eb97387..e19d596 100644 --- a/src/wif.js +++ b/src/wif.js @@ -1,9 +1,14 @@ const bitcoin = require('bitcoinjs-lib'); -const wif = {}; +const wif = () => { + const keyPair = bitcoin.ECPair.makeRandom(); + const {publicKey} = keyPair; -wif.generate = bitcoin.ECPair.makeRandom; + const format = () => ({ + wif: keyPair.toWIF() + }); -wif.format = keyPair => ({wif: keyPair.toWIF()}); + return {publicKey, format}; +}; module.exports = wif;