diff --git a/src/index.js b/src/index.js index 0f555f4..8ef9690 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,8 @@ const ONE_SECOND = 1000; const keyFormats = { wif: require('./key-formats/wif'), - bip39: require('./key-formats/bip39') + bip39: require('./key-formats/bip39'), + xpub: require('./key-formats/xpub') }; const addressFormats = { @@ -59,7 +60,7 @@ class Vain extends Emitter { attempts++; - keyData = generateKey({addressFormat, options}); + keyData = generateKey({addressFormat, options, attempts}); address = addressFormat.derive(keyData.publicKey); if (address.startsWith(this.prefix)) { diff --git a/src/key-formats/xpub.js b/src/key-formats/xpub.js new file mode 100644 index 0000000..8175021 --- /dev/null +++ b/src/key-formats/xpub.js @@ -0,0 +1,19 @@ +const bitcoin = require('bitcoinjs-lib'); + +const generateXpubKey = ({options, attempts}) => { + const change = 0; + const index = attempts - 1; + const derivationPath = `${change}/${index}`; + + const node = bitcoin.bip32.fromBase58(options.xpub); + const {publicKey} = node.derivePath(derivationPath); + + const format = () => ({ + xpub: options.xpub, + derivationPath + }); + + return {publicKey, format}; +}; + +module.exports = generateXpubKey;