question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Derive public LTC addresses from an extended public LTC address in xpub format

See original GitHub issue

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 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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
junderwcommented, Jan 24, 2018

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)

1reaction
nnymsgthbcommented, Jan 24, 2018

@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!
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extended public key (xPub)
The extended public key (xpub) is the master public key of an account. All public addresses of a Ledger Live account are generated...
Read more >
ledgerhq/xpub-scan
Given an extended public key (xpub, Ltub, etc.), get the balances of its derived active addresses and the associated list of operations, ...
Read more >
The Bitcoin Extended Public Key: Explaining The Mystery
xpub is the oldest format of extended public key address – that's where the e('x')tended comes from! In a lot of applications, it's...
Read more >
Calculate Segwit address from public address
You have the private key so you can just derive the address from there. Get the corresponding public key, hash it with SHA256...
Read more >
How do I export my 'xpub' or 'zpub' key? - Exodus Support
The extended public key or xpub can be used to generate all of the public addresses controlled by your Exodus wallet for UTXO-based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found