|
|
@ -3,7 +3,7 @@ |
|
|
|
var Address = require('./address'); |
|
|
|
var base58check = require('./encoding/base58check'); |
|
|
|
var BN = require('./crypto/bn'); |
|
|
|
var jsUtil = require('./util/js'); |
|
|
|
var JSUtil = require('./util/js'); |
|
|
|
var Networks = require('./networks'); |
|
|
|
var Point = require('./crypto/point'); |
|
|
|
var PublicKey = require('./publickey'); |
|
|
@ -51,7 +51,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { |
|
|
|
} else if (data instanceof Buffer || data instanceof Uint8Array) { |
|
|
|
info = PrivateKey._transformBuffer(data, network, compressed); |
|
|
|
} else if (typeof(data) === 'string'){ |
|
|
|
if (jsUtil.isHexa(data)) { |
|
|
|
if (JSUtil.isHexa(data)) { |
|
|
|
info.bn = BN(new Buffer(data, 'hex')); |
|
|
|
} else { |
|
|
|
info = PrivateKey._transformWIF(data, network, compressed); |
|
|
@ -183,14 +183,17 @@ PrivateKey.fromWIF = function(str) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a PrivateKey from a WIF JSON string |
|
|
|
* Instantiate a PrivateKey from a JSON string |
|
|
|
* |
|
|
|
* @param {String} str - The WIF encoded private key string |
|
|
|
* @param {String} json - The JSON encoded private key string |
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey |
|
|
|
*/ |
|
|
|
PrivateKey.fromJSON = function(json) { |
|
|
|
var info = PrivateKey._transformWIF(json); |
|
|
|
return new PrivateKey(info.bn, info.network, info.compressed); |
|
|
|
if (JSUtil.isValidJson(json)) { |
|
|
|
json = JSON.parse(json); |
|
|
|
} |
|
|
|
var bn = BN(json.bn, 'hex'); |
|
|
|
return new PrivateKey(bn, json.network, json.compressed); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -312,7 +315,11 @@ PrivateKey.prototype.toAddress = function() { |
|
|
|
* @returns {String} A WIF representation of the private key |
|
|
|
*/ |
|
|
|
PrivateKey.prototype.toJSON = function() { |
|
|
|
return this.toString(); |
|
|
|
return { |
|
|
|
bn: this.bn.toString('hex'), |
|
|
|
compressed: this.compressed, |
|
|
|
network: this.network.toString() |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|