bitcoinjs-lib: Trezor generated xPub - 'Invalid network version'
Hi. So I am trying to generate HD addresses from xPub that is generated inside the Trezor.
const network = bitcoin.networks.mainnet;
const xpub = 'ypub6XTWVFLfqkFYarn9NArqtBLziffJttqf1Utaur3sTbTGtgfNaTzkGcRpFgiiieBjQ6rV1rJ7iJ9r9oXGpPXZpkq71yfss2mrKLaauxhjXD4';
const node = bitcoin.HDNode.fromBase58(xpub, network).neutered();
const address = node.derive(0).derive(0).getAddress();
console.log('address', address);
# node index.js
/vagrant/btc-js/node_modules/bitcoinjs-lib/src/hdnode.js:78
version !== network.bip32.public) throw new Error('Invalid network version')
Any clues why? It does work with const xpub = 'xpub661MyMwAqRbcG4PQrRAT3N2uxTkXWeRq5kpjyDvStBQP7eW65Lu5rZ3MLoBZJQuZFS9FC7mZZEcgxFZxccRdnqSxopraUB6wVjTqp8ZsS4H'
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 24 (9 by maintainers)
You can basically do the same thing as above. Slightly modified.
If anyone is still looking for a complete example of how to get addresses from the ypub trezor provides.
Consolidated from off all the answers above (working with version 4.0.2 of bitcoinjs-lib):
ok I was able to do this with
@jackylimel Every derivation scheme has two non-hardened laters after the last hardened layer.
Since xpubs and ypubs can not derive hardened layers, usually apps show the xpub of the last hardened layer.
In Electrum’s case, they use BIP44 which is
H H H S S (hard hard hard soft soft)
The last two softs are 0|1 for is_change and the address index.
So to derive the “first non-change address” is derive(0).derive(0)
first change would be 1 0
Second non-change is 0 1
Tenth non-change is 0 9