bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.7k stars 2.11k forks source link

Is there a way for create a bunch of public address using xpub #1499

Closed soyjavi closed 5 years ago

soyjavi commented 5 years ago

Hi there, I'm developing an frontend invoicer using the xpub of my ledger nano S. My question is simple, is there a way to create a bunch of public address using the ledger's xpub?

I know how get one using

const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin);

const { address } = bitcoin.payments.p2wpkh({
    pubkey: node.derive(0).derive(1).publicKey,
    network: bitcoin.networks.bitcoin,
});

Someone can help me?

junderw commented 5 years ago

Just add a for loop?

const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin);
const receiveBase = node.derive(0);

const addresses = [];
for (let i = 0; i < 100; i++) {
    const { address } = bitcoin.payments.p2wpkh({
        pubkey: receiveBase.derive(i).publicKey,
        network: bitcoin.networks.bitcoin,
    });
    addresses.push(address);
}
console.log(JSON.stringify(addresses, null, 2));