i have tried this:
const bitcoin=require("bitcoinjs-lib")
const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin);
const { address } = bitcoin.payments.p2pkh({
pubkey: node.derive(0).derive(0).publicKey,
network: bitcoin.networks.bitcoin
}).address;
but i get an error Invalid index
the error is coming from here
const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin)
const { address } = bitcoin.payments.p2pkh({
pubkey: node.derive(0).derive(0).publicKey,
network: bitcoin.networks.bitcoin
}).address;
This code is incorrect, you are both returning the address property and trying to destructure it. You need to do one or the other:
const { address } = bitcoin.payments.p2pkh({
pubkey: node.derive(0).derive(0).publicKey,
network: bitcoin.networks.bitcoin
});
// or
const address = bitcoin.payments.p2pkh({
pubkey: node.derive(0).derive(0).publicKey,
network: bitcoin.networks.bitcoin
}).address;
The rest of your code looks correct, you haven't provided your xpub but if I generate a new one everything works as expected:
const bitcoin = require('bitcoinjs-lib');
const xpubkey = 'xpub6DSb7kDxDNmXLn4TitYWYoh16NpDqJu2Az6wi426TKC7sshjwMvrCv4yPcDkHzHChtbUviXfcNboyMBVaKp4qSdww6GbJcEi7p1V98ywX6r';
const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin);
const { address } = bitcoin.payments.p2pkh({
pubkey: node.derive(0).derive(0).publicKey,
network: bitcoin.networks.bitcoin
});
console.log(address);
// '1QFzqxuwefkAcNR77EakGe41DVa9aqLVRS'
Are you sure your xpub is in the correct format?
@lukechilds I get Invalid index and it happens to be coming from here
const node = bitcoin.bip32.fromBase58(xpubkey, bitcoin.networks.bitcoin);
something is wrong with your xpub string.
@junderw @lukechilds this is my xpubkey xpub661MyMwKB68aRzhwtsUeEQ6W96rBh21cx2iqWBGfAW58FPBsYh65RDWAwkeiFE3tNDo62wP2nqN9d6Ra4EewQXbqck9MsTofit42G64vrVb
this xpub key was generated by coinomi wallet
invalid.
depth is 0 but index is nonzero.
tell coinomi their wallet is generating broken xpubs.
@junderw Don't close this issue yet. I think the problem might be with this library because it works just fine with https://github.com/bitpay/bitcore/tree/v8.0.0/packages/bitcore-lib
Here is a demo with the same xpub key using bitcore-lib https://runkit.com/johnanisere/5d862243b387ad00138e1a42
bitcore-lib is also broken.
a key with depth 0 has not been derived so should have an index of 0.
it is written in the BIP as so, and this library will not loosen the check to allow invalid xpubs, as this can lose money for people.
Coinomi should be notified immediately.
@lukechilds DMed me privately so we are now aware of the situation, our devs will comment shortly. Thanks, Luke.
The xpubs are not "broken", we just never followed the spec because when we implemented them there was no spec (there was just the BIP32 extended key spec). The extended key was primarily added to be able to debug user issues. Nevertheless, we will change it to follow the spec in one of the upcoming releases.
Most helpful comment
something is wrong with your xpub string.