Bitcoinjs-lib: Unable to generate Litecoin address via xpub

Created on 26 Nov 2017  Â·  14Comments  Â·  Source: bitcoinjs/bitcoinjs-lib

I'm attempting to generate/derive an address via a Litecoin xpub key using the following:

var bitcoin = require('bitcoinjs-lib');
import HDNode from "../../bitcoinjs-lib/src/hdnode";

const litecoin = bitcoin.networks.litecoin
const xpub = "Litecoin xpub";
const node = HDNode.fromBase58(xpub);
const address = node.derive(0).getAddress(litecoin);

However, it continues to generate a Bitcoin address. Is there anything I'm doing wrong?

how to / question / docs

Most helpful comment

@tonychew1986 you are trying to use the litecoin network object, which has different BIP32 constants, if you want to support "both", I recommend you do the following:

let litecoin = bitcoin.networks.litecoin
let litecoinX = Object.assign({}, bitcoin.networks.litecoin, {
  bip32: bitcoin.networks.bitcoin.bip32
})

let a = bitcoin.HDNode.fromBase58(xpub, [litecoin, litecoinX])

All 14 comments

Litecoin doesn’t use “xpub”

I think Litecoin is “lpub” or something. The Litecoin network parameter can tell you what you need.

From https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/hdnode.js#L55

const node = HDNode.fromBase58(xpub);

Should be

const node = HDNode.fromBase58(xpub, litecoin);

getAddress doesn't have a network parameter.

@Mimble-Wimble re-open if that doesn't solve your issue :+1:

Thanks @dcousens!

I've tried a number of options:

  • Bitcoin xpub (works)
  • Litecoin Ltub (works)
  • Litecoin xpub (not working)

Not really sure what I'm doing wrong. Perhaps can point me in the right direction?

var xpubBTC = "xpub...";
var xpubLTC = "xpub...";
// var xpubLTC = "Ltub...";

var litecoin = bitcoin.networks.litecoin;

let btc = bitcoin.HDNode.fromBase58(xpubBTC);
let ltc = bitcoin.HDNode.fromBase58(xpubLTC, litecoin);

console.log(btc.derive(0).derive(0).getAddress());
console.log(ltc.derive(0).derive(0).getAddress());

I'm getting this error:

version !== network.bip32.public) throw new Error('Invalid network version')

I've bypassed this issue by using different approach:

  let seed = bip39.mnemonicToSeed(MNEMONIC_SEED);
  let key = bitcoin.HDNode.fromSeedBuffer(seed, bitcoin.networks.litecoin);

@rokanost unfortunately this doesn't quite work for me since I would preferably not expose the mnemonic seed in the code.

@tonychew1986 you are trying to use the litecoin network object, which has different BIP32 constants, if you want to support "both", I recommend you do the following:

let litecoin = bitcoin.networks.litecoin
let litecoinX = Object.assign({}, bitcoin.networks.litecoin, {
  bip32: bitcoin.networks.bitcoin.bip32
})

let a = bitcoin.HDNode.fromBase58(xpub, [litecoin, litecoinX])

Thanks @dcousens. It works perfectly. :)

@junderw maybe the above should be our official recommendation? (short of adding litecoinX)

Thank you @dcousens

Wondering if its possible to generate bip32 litecoin testnet address as well?
Do i need to check the address version field or something?

Litecoin main-net: 0x30
Litecoin test-net: 0x6F

Just not too sure how should I edit them to make it work.

@tonychew1986

  let litecoin ={
    messagePrefix: '\x19Litecoin Signed Message:\n',
    bip32: {
      public: 0x019da462,
      private: 0x019d9cfe
    },
    pubKeyHash: 0x30,    // change to 0x6f
    scriptHash: 0x32,
    wif: 0xb0
  }

Thanks again @dcousens. Really appreciate it. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbndg picture rbndg  Â·  3Comments

hoshsadiq picture hoshsadiq  Â·  3Comments

ishwarchandratiwari picture ishwarchandratiwari  Â·  3Comments

dakk picture dakk  Â·  3Comments

zhaozhiming picture zhaozhiming  Â·  3Comments