Invalide private key for testnet

I have this code (node.js):

bitcore = require(‘bitcore’);
bitcore.Networks.defaultNetwork = ‘testnet’;
var privateKey = new bitcore.PrivateKey();
console.log('privateKey: ’ + privateKey);
var exported = privateKey.toWIF();
console.log('privateKey toWIF: ’ + exported);


privateKey: 13421db757900dae2ecd471b2cca0d950f8ca47fd867e3c44dadd64f57194252
privateKey toWIF: 1eSGCW9EJ8pUZ6QEpa3385at83n3hdXrJ57VpmmFQRSv3JcMVHW

This private WIF key seams wrong, when I try to use it with signrawtransaction
I have “Invalid private key (code -5)” from bitcoind:

The WIF private key should start with a “9” or a “c”

The issue is a result of setting the defaultNetwork property to a string instead of the network object. Try this instead:

bitcore.Networks.defaultNetwork = bitcore.Networks.testnet

Or, these options:

var privateKey = new bitcore.PrivateKey('testnet');
var privateKey = new bitcore.PrivateKey(bitcore.Networks.testnet);

Great, it works!