Bitcoinjs-lib: Derive public LTC addresses from an extended public LTC address in xpub format

Created on 23 Jan 2018  路  5Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hello everybody!
I'm very new to JS, nodeJS and bitcoinjs, so please excuse my noobidity.

Im trying to derive public LTC addresses from an extended LTC address in xpub format (created by Jaxx and exported by Exodus). For demonstration purposes, I created (in Jaxx) and exported (in Exodus) a wallet with the following mnemonic:

_quit disagree kiss among move interest orchard trade bullet forward usual neither_

The extended public LTC address is:
_xpub6CAp1aH6mzGrH3hkCuaZPCDrgTMkPm55xhBpNETbPPuhAxxoprvsiHBDipL7ePY6aAiZZThyWCN5qiKcCZxtjCJLtVjAeVryZCtamBKyi3m_

And the first public LTC address is:
_LeUD63jWGkLTohEWq6t4gyqpithtnReXEk_

I ran some tests with https://iancoleman.io/bip39/ and they yielded the info, that the derivation path is: _m/44'/2'/0'/0_

So far, I got the following code:
```var bitcoin = require('bitcoinjs-lib');
var network = bitcoin.networks.litecoinXprv;

var xpub = 'xpub6CAp1aH6mzGrH3hkCuaZPCDrgTMkPm55xhBpNETbPPuhAxxoprvsiHBDipL7ePY6aAiZZThyWCN5qiKcCZxtjCJLtVjAeVryZCtamBKyi3m'

var node = bitcoin.HDNode.fromBase58(xpub,network)

var address = bitcoin.HDNode.fromBase58(xpub,network).derivePath("m/44'/2'/0'/0").derive(0).getAddress();

// Possible alternative?
//var address = node.derive(44).derive(2).derive(0).derive(0).derive(0).getAddress()

console.log("First address: " + address)
```

Thanks for bitcoinjs and any provided support.

EDIT: I added the missing apostrophes.

how to / question / docs

Most helpful comment

var address = bitcoin.HDNode.fromBase58(xpub, network).derive(0).derive(0).getAddress();

Your xpub is @ m/44'/2'/0' exported, so you import and derive the last 0/0 and gets you the m/44'/2'/0'/0/0

Usually when you export xprv, you export m but when you export xpub you export the LAST hardened path: (0' in m/44'/2'/0'/0/0)

All 5 comments

Missing apostrophes.

Thanks @dabura667. Absolutely right, therefore I added the missing apostrophes.

It still doesn't work, but I think I might finally have figured out why:
The apostrophes indicate hardened addresses and hardened addresses cannot be derived just from the extended public key but I need to provide the extended private key. Is that right?

However, I do not understand how homepages like blockchain.info can provide a similar service, i.e. returning the total balance of all child addresses, which are also based on a partially hardened derivation path (m/44'/0'/0'/0), based on an extended public BTC key.

I should perhaps also elaborate a bit more on my final goal. I want to derive all (or at least the most probable) public addresses based on a public extended LTC key, to use those addresses in a query to determine the total balance. Just like blockchain.info can do it for BTC, but for LTC.

Thanks for the help so far.

var address = bitcoin.HDNode.fromBase58(xpub, network).derive(0).derive(0).getAddress();

Your xpub is @ m/44'/2'/0' exported, so you import and derive the last 0/0 and gets you the m/44'/2'/0'/0/0

Usually when you export xprv, you export m but when you export xpub you export the LAST hardened path: (0' in m/44'/2'/0'/0/0)

var address = bitcoin.HDNode.fromBase58(xpub, network).derivePath("0/0").getAddress();

Also works. you can't have "m" at start unless your node is depth = 0

@junderw Thanks! That did the job. I'm glad that it is indeed possible to derive the addresses without providing the extended private key.

Here is the complete code:
```
var bitcoin = require('bitcoinjs-lib')

// Definition for LTC network with xpub.../xprv... prefixes.
// Taken from bip39 (see https://github.com/iancoleman/bip39/blob/master/src/js/bitcoinjs-extensions.js)
var network = {
messagePrefix: 'x19Litecoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4,
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0
};

var xpub = 'xpub6CAp1aH6mzGrH3hkCuaZPCDrgTMkPm55xhBpNETbPPuhAxxoprvsiHBDipL7ePY6aAiZZThyWCN5qiKcCZxtjCJLtVjAeVryZCtamBKyi3m'

var address = bitcoin.HDNode.fromBase58(xpub, network).derivePath("0/0").getAddress();

console.log("First address: " + address)```

Thanks for the support!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

dcousens picture dcousens  路  3Comments

dakk picture dakk  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments