|
|
@ -29,18 +29,17 @@ var Random = require('./crypto/random'); |
|
|
|
* |
|
|
|
* @param {String} data - The encoded data in various formats |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @param {Boolean} [compressed] - If the key is in compressed format |
|
|
|
* @returns {PrivateKey} A new valid instance of an PrivateKey |
|
|
|
* @constructor |
|
|
|
*/ |
|
|
|
var PrivateKey = function PrivateKey(data, network, compressed) { |
|
|
|
var PrivateKey = function PrivateKey(data, network) { |
|
|
|
|
|
|
|
if (!(this instanceof PrivateKey)) { |
|
|
|
return new PrivateKey(data, network, compressed); |
|
|
|
return new PrivateKey(data, network); |
|
|
|
} |
|
|
|
|
|
|
|
var info = { |
|
|
|
compressed: typeof(compressed) !== 'undefined' ? compressed : true, |
|
|
|
compressed: true, |
|
|
|
network: network ? Networks.get(network) : Networks.defaultNetwork |
|
|
|
}; |
|
|
|
|
|
|
@ -50,12 +49,14 @@ var PrivateKey = function PrivateKey(data, network, compressed) { |
|
|
|
} else if (data instanceof BN) { |
|
|
|
info.bn = data; |
|
|
|
} else if (data instanceof Buffer || data instanceof Uint8Array) { |
|
|
|
info = PrivateKey._transformBuffer(data, network, compressed); |
|
|
|
info = PrivateKey._transformBuffer(data, network); |
|
|
|
} else if (PrivateKey._isJSON(data)){ |
|
|
|
info = PrivateKey._transformJSON(data); |
|
|
|
} else if (typeof(data) === 'string'){ |
|
|
|
if (JSUtil.isHexa(data)) { |
|
|
|
info.bn = BN(new Buffer(data, 'hex')); |
|
|
|
} else { |
|
|
|
info = PrivateKey._transformWIF(data, network, compressed); |
|
|
|
info = PrivateKey._transformWIF(data, network); |
|
|
|
} |
|
|
|
} else { |
|
|
|
throw new TypeError('First argument is an unrecognized data type.'); |
|
|
@ -71,9 +72,6 @@ var PrivateKey = function PrivateKey(data, network, compressed) { |
|
|
|
if (typeof(info.network) === 'undefined') { |
|
|
|
throw new TypeError('Must specify the network ("livenet" or "testnet")'); |
|
|
|
} |
|
|
|
if (typeof(info.compressed) !== 'boolean') { |
|
|
|
throw new TypeError('Must specify whether the corresponding public key is compressed or not (true or false)'); |
|
|
|
} |
|
|
|
|
|
|
|
Object.defineProperty(this, 'bn', { |
|
|
|
configurable: false, |
|
|
@ -92,12 +90,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { |
|
|
|
|
|
|
|
Object.defineProperty(this, 'publicKey', { |
|
|
|
configurable: false, |
|
|
|
get: function() { |
|
|
|
if (!info.publicKey) { |
|
|
|
info.publicKey = this.toPublicKey(); |
|
|
|
} |
|
|
|
return info.publicKey; |
|
|
|
} |
|
|
|
get: this.toPublicKey.bind(this) |
|
|
|
}); |
|
|
|
|
|
|
|
return this; |
|
|
@ -121,16 +114,26 @@ PrivateKey._getRandomBN = function(){ |
|
|
|
return bn; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Internal function to detect if a param is a JSON string or plain object |
|
|
|
* |
|
|
|
* @param {*} param - value to test |
|
|
|
* @returns {boolean} |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
PrivateKey._isJSON = function(json) { |
|
|
|
return JSUtil.isValidJSON(json) || (json.bn && json.network && json.compressed); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Internal function to transform a WIF Buffer into a private key |
|
|
|
* |
|
|
|
* @param {Buffer} buf - An WIF string |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @param {String} [compressed] - If the private key is compressed |
|
|
|
* @returns {Object} An object with keys: bn, network and compressed |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
PrivateKey._transformBuffer = function(buf, network, compressed) { |
|
|
|
PrivateKey._transformBuffer = function(buf, network) { |
|
|
|
|
|
|
|
var info = {}; |
|
|
|
|
|
|
@ -154,10 +157,6 @@ PrivateKey._transformBuffer = function(buf, network, compressed) { |
|
|
|
throw TypeError('Private key network mismatch'); |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof(compressed) !== 'undefined' && info.compressed !== compressed){ |
|
|
|
throw TypeError('Private key compression mismatch'); |
|
|
|
} |
|
|
|
|
|
|
|
info.bn = BN.fromBuffer(buf.slice(1, 32 + 1)); |
|
|
|
|
|
|
|
return info; |
|
|
@ -171,56 +170,63 @@ PrivateKey._transformBuffer = function(buf, network, compressed) { |
|
|
|
* @returns {Object} An object with keys: bn, network and compressed |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
PrivateKey._transformWIF = function(str, network, compressed) { |
|
|
|
return PrivateKey._transformBuffer(base58check.decode(str), network, compressed); |
|
|
|
PrivateKey._transformWIF = function(str, network) { |
|
|
|
return PrivateKey._transformBuffer(base58check.decode(str), network); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a PrivateKey from a WIF 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.fromWIF = function(str) { |
|
|
|
var info = PrivateKey._transformWIF(str); |
|
|
|
return new PrivateKey(info.bn, info.network, info.compressed); |
|
|
|
PrivateKey.fromJSON = function(json) { |
|
|
|
if (!PrivateKey._isJSON(json)) { |
|
|
|
throw new TypeError('Must be a valid JSON string or plain object'); |
|
|
|
} |
|
|
|
|
|
|
|
return new PrivateKey(json); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a PrivateKey from a JSON string |
|
|
|
* Internal function to transform a JSON string on plain object into a private key |
|
|
|
* |
|
|
|
* @param {String} json - The JSON encoded private key string |
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey |
|
|
|
* @param {String} json - A JSON string or plain object |
|
|
|
* @returns {Object} An object with keys: bn, network and compressed |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
PrivateKey.fromJSON = function(json) { |
|
|
|
PrivateKey._transformJSON = function(json) { |
|
|
|
if (JSUtil.isValidJSON(json)) { |
|
|
|
json = JSON.parse(json); |
|
|
|
} |
|
|
|
var bn = BN(json.bn, 'hex'); |
|
|
|
return new PrivateKey(bn, json.network, json.compressed); |
|
|
|
return { |
|
|
|
bn: bn, |
|
|
|
network: json.network, |
|
|
|
compressed: json.compressed |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a PrivateKey from random bytes |
|
|
|
* Instantiate a PrivateKey from a WIF string |
|
|
|
* |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @param {String} [compressed] - If the private key is compressed |
|
|
|
* @param {String} str - The WIF encoded private key string |
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey |
|
|
|
*/ |
|
|
|
PrivateKey.fromRandom = function(network, compressed) { |
|
|
|
var bn = PrivateKey._getRandomBN(); |
|
|
|
return new PrivateKey(bn, network, compressed); |
|
|
|
PrivateKey.fromString = PrivateKey.fromWIF = function(str) { |
|
|
|
return new PrivateKey(str); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a PrivateKey from a WIF string |
|
|
|
* Instantiate a PrivateKey from random bytes |
|
|
|
* |
|
|
|
* @param {String} str - The WIF encoded private key string |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey |
|
|
|
*/ |
|
|
|
PrivateKey.fromString = function(str) { |
|
|
|
var info = PrivateKey._transformWIF(str); |
|
|
|
return new PrivateKey(info.bn, info.network, info.compressed); |
|
|
|
PrivateKey.fromRandom = function(network) { |
|
|
|
var bn = PrivateKey._getRandomBN(); |
|
|
|
return new PrivateKey(bn, network); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -228,14 +234,13 @@ PrivateKey.fromString = function(str) { |
|
|
|
* |
|
|
|
* @param {String} data - The encoded data in various formats |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @param {String} [compressed] - If the private key is compressed |
|
|
|
* @returns {null|Error} An error if exists |
|
|
|
*/ |
|
|
|
|
|
|
|
PrivateKey.getValidationError = function(data, network, compressed) { |
|
|
|
PrivateKey.getValidationError = function(data, network) { |
|
|
|
var error; |
|
|
|
try { |
|
|
|
new PrivateKey(data, network, compressed); |
|
|
|
new PrivateKey(data, network); |
|
|
|
} catch (e) { |
|
|
|
error = e; |
|
|
|
} |
|
|
@ -247,11 +252,10 @@ PrivateKey.getValidationError = function(data, network, compressed) { |
|
|
|
* |
|
|
|
* @param {String} data - The encoded data in various formats |
|
|
|
* @param {String} [network] - Either "livenet" or "testnet" |
|
|
|
* @param {String} [compressed] - If the private key is compressed |
|
|
|
* @returns {Boolean} If the private key is would be valid |
|
|
|
*/ |
|
|
|
PrivateKey.isValid = function(data, network, compressed){ |
|
|
|
return !PrivateKey.getValidationError(data, network, compressed); |
|
|
|
PrivateKey.isValid = function(data, network){ |
|
|
|
return !PrivateKey.getValidationError(data, network); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -259,7 +263,7 @@ PrivateKey.isValid = function(data, network, compressed){ |
|
|
|
* |
|
|
|
* @returns {String} A WIP representation of the private key |
|
|
|
*/ |
|
|
|
PrivateKey.prototype.toWIF = function() { |
|
|
|
PrivateKey.prototype.toString = PrivateKey.prototype.toWIF = function() { |
|
|
|
var network = this.network; |
|
|
|
var compressed = this.compressed; |
|
|
|
|
|
|
@ -300,7 +304,10 @@ PrivateKey.prototype.toBuffer = function(){ |
|
|
|
* @returns {PublicKey} A public key generated from the private key |
|
|
|
*/ |
|
|
|
PrivateKey.prototype.toPublicKey = function(){ |
|
|
|
return PublicKey.fromPrivateKey(this); |
|
|
|
if (!this._pubkey) { |
|
|
|
this._pubkey = PublicKey.fromPrivateKey(this); |
|
|
|
} |
|
|
|
return this._pubkey; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -328,22 +335,14 @@ PrivateKey.prototype.toJSON = function toJSON() { |
|
|
|
return JSON.stringify(this.toObject()); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Will output the PrivateKey to a WIF string |
|
|
|
* |
|
|
|
* @returns {String} A WIF representation of the private key |
|
|
|
*/ |
|
|
|
PrivateKey.prototype.toString = function() { |
|
|
|
return this.toWIF(); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Will return a string formatted for the console |
|
|
|
* |
|
|
|
* @returns {String} Private key |
|
|
|
*/ |
|
|
|
PrivateKey.prototype.inspect = function() { |
|
|
|
return '<PrivateKey: ' + this.toString() + ', compressed: '+this.compressed+', network: '+this.network+'>'; |
|
|
|
var uncompressed = !this.compressed ? ', uncompressed' : ''; |
|
|
|
return '<PrivateKey: ' + this.toString() + ', network: ' + this.network + uncompressed + '>'; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = PrivateKey; |
|
|
|