.toWIF() shuld take a network argument

var k = bitcore.HDPrivateKey();


I can add a network argument to the toAddress method to get the address for another network:


(I have added litecoin as a network via Networks.add)

But the same can’t be done for toWIF…


Thats a bitcoin WIF, a litecoin WIF starts with a “T”


The argument is ignored…

If you specify the network when constructing the private key, I don’t think the argument is necessary later on.

Hi braydon,

In my case, the private key is being created from the derive function of the HDPrivateKey class. How can I specify the network I want the private key to be part of?

You should be able to specify the network with the first instantiation:

var hdkey = bitcore.HDPrivateKey('testnet');
var derivedKey = hdkey.derive("m/44'/0'/0'/0")
derivedKey.toAddress() // testnet address
derivedKey.toWIF() // testnet wif

But I’m using the HDPrivate key to derive different altcoins via BIP44. I don’t know what altcoin I’m going to want until after the HDPrivateKey is already created.

xpriv = "xprv9s21ZrQH143K3aJbLkzezYvgq2SFRagANzXoDTmEejYuWt7QvQAN4qSUAQEyx1ML9WTjXVajF4y224zeCzizx1312ZXcagyfzxC4FqiB88V"; // this has already been generated a long time ago and may be saved to paper
hd = new bitcore.HDPrivateKey(xpriv, 'ltc')

>>> hd.privateKey.toWIF();
L43ihVjtWAwpibmBUTF5tHBZrvq18z1snMwAzNZe51VZ7HzPDbAs // wrong WIF, should start with "T"

By the way, when I try your example, I get this error:

hdkey = bitcore.HDPrivateKey(‘ltc’);
bitcore-lib.min.js:5 Uncaught r {message: “Invalid Argument for integer, expected number but got undefined”, stack: “Invalid Argument for integer, expected number but …tcore-lib.min.js:2:14542)↵ at :1:17”}

I’ve also tried “litecoin” but it doesn’t work either. “testnet” and “livenet” do work.

I figured it out:

xpriv = new bitcore.HDPrivateKey("xprv9s21ZrQH143K3aJbLkzezYvgq2SFRagANzXoDTmEejYuWt7QvQAN4qSUAQEyx1ML9WTjXVajF4y224zeCzizx1312ZXcagyfzxC4FqiB88V")
xpriv = xpriv.derive("m/3/4/2/3")
p = bitcore.PrivateKey(xpriv.privateKey.toString(), 'ltc')

Great, I was thinking something similar, and thought there may be a toPrivateKey() method, that would have a network argument, however there is just .privateKey.

Also, it doesn’t look like there is a second argument for the network option for:

var hd = new bitcore.HDPrivateKey(xpriv, 'networkname');

Also feel free to spawn an issue at https://github.com/bitpay/bitcore-lib for further discussion on adding network arguments.

I think the problem is that a HDPrivateKey should be network agnostic. I don’t think any altcoins define different magic bytes for them anyways. It especially causes problems when you’re deriving multiple coins from the same root, ala BIP44.