BitCore Playground does not use Testnet


(bedeho) #1

When I paste in a private key, I get the same address whether I have seleted testnet or livenet?

The “Under the hood” code snippet section also does not seem to take network setting into account.


(esteban) #2

Thanks for the feedback! You should use bitcore.PrivateKey('something', 'testnet') to create a testnet address. As far as I can see the generated address is sensitive to the network, but the code example is not aware of this.

Spawned this issue: https://github.com/bitpay/bitcore-playground/issues/111


(bedeho) #3

Your tip actually resolved the issue I was having, that is I was not
providing the network as an argument to the PrivateKey constructor.

  1. It is not clear to me why private keys need network awareness?
  2. Most people will not anticipate this, at least if they are new to
    bitcoin development - like me,
    so adding this to the doc page for PrivateKey could be a good idea.

Thank again esteban, you rule!


(esteban) #4

It’s one of those pain points in bitcoin that are due to legacy… private keys are not “binded” to a network, but because of the so-called “WIF” format (it contains information about which network you are using and if you used the compressed or uncompressed DER encoding for the public key before hashing it to get an address) we added it as an optional member so this works correctly:

var privateKey = new PrivateKey(myWifKey);
assert(privateKey.toWIF() === myWifKey); 

And also you can do privateKey.toAddress(), and that will fetch the network from either the parameter used in the constructor, or the value from bitcore.Networks.defaultNetwork.

The other best alternative we juggled with was having a “PrivateKey” class, and a “WalletPrivateKey” class with the WIF info. It turned out that bitcore 0.1 used this and it was kind of a pain to use (we would have had to test against both kinds of private keys), and new users were inclined to have two instances of the same thing because you could only sign with the WalletPrivateKey.