2014-11-20 07:16:27 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-15 07:20:38 -08:00
|
|
|
var _ = require('lodash');
|
2014-12-11 08:25:02 -08:00
|
|
|
var Address = require('./address');
|
2015-02-13 19:40:20 -08:00
|
|
|
var Base58Check = require('./encoding/base58check');
|
2014-11-20 07:16:27 -08:00
|
|
|
var BN = require('./crypto/bn');
|
2014-12-12 10:39:49 -08:00
|
|
|
var JSUtil = require('./util/js');
|
2014-12-11 08:25:02 -08:00
|
|
|
var Networks = require('./networks');
|
2014-11-20 07:16:27 -08:00
|
|
|
var Point = require('./crypto/point');
|
2014-11-25 10:21:53 -08:00
|
|
|
var PublicKey = require('./publickey');
|
2014-12-11 08:25:02 -08:00
|
|
|
var Random = require('./crypto/random');
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-11-25 10:21:53 -08:00
|
|
|
* Instantiate a PrivateKey from a BN, Buffer and WIF.
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
|
|
|
* @example
|
2014-12-18 08:41:09 -08:00
|
|
|
* ```javascript
|
2014-11-26 10:00:09 -08:00
|
|
|
* // generate a new random key
|
|
|
|
* var key = PrivateKey();
|
|
|
|
*
|
|
|
|
* // get the associated address
|
|
|
|
* var address = key.toAddress();
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2014-12-11 08:25:02 -08:00
|
|
|
* // encode into wallet export format
|
|
|
|
* var exported = key.toWIF();
|
|
|
|
*
|
2014-11-26 10:00:09 -08:00
|
|
|
* // instantiate from the exported (and saved) private key
|
|
|
|
* var imported = PrivateKey.fromWIF(exported);
|
2014-12-18 08:41:09 -08:00
|
|
|
* ```
|
2014-12-11 08:25:02 -08:00
|
|
|
*
|
2014-12-19 10:32:57 -08:00
|
|
|
* @param {string} data - The encoded data in various formats
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
2014-11-25 10:21:53 -08:00
|
|
|
* @returns {PrivateKey} A new valid instance of an PrivateKey
|
2014-11-26 10:00:09 -08:00
|
|
|
* @constructor
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
var PrivateKey = function PrivateKey(data, network) {
|
2014-12-19 10:32:57 -08:00
|
|
|
/* jshint maxstatements: 20 */
|
|
|
|
/* jshint maxcomplexity: 8 */
|
2014-11-24 14:23:09 -08:00
|
|
|
|
2014-11-25 10:21:53 -08:00
|
|
|
if (!(this instanceof PrivateKey)) {
|
2014-12-11 13:19:36 -08:00
|
|
|
return new PrivateKey(data, network);
|
2014-09-01 21:13:44 -07:00
|
|
|
}
|
2014-12-11 08:58:32 -08:00
|
|
|
if (data instanceof PrivateKey) {
|
|
|
|
return data;
|
|
|
|
}
|
2014-08-28 17:26:56 -07:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
var info = this._classifyArguments(data, network);
|
2014-11-24 14:23:09 -08:00
|
|
|
|
|
|
|
// validation
|
2015-02-05 12:24:48 -08:00
|
|
|
if (!info.bn || info.bn.cmp(new BN(0)) === 0){
|
2014-12-13 13:10:41 -08:00
|
|
|
throw new TypeError('Number can not be equal to zero, undefined, null or false');
|
|
|
|
}
|
2014-11-24 14:23:09 -08:00
|
|
|
if (!info.bn.lt(Point.getN())) {
|
|
|
|
throw new TypeError('Number must be less than N');
|
|
|
|
}
|
2014-12-11 08:25:02 -08:00
|
|
|
if (typeof(info.network) === 'undefined') {
|
2014-12-01 14:31:39 -08:00
|
|
|
throw new TypeError('Must specify the network ("livenet" or "testnet")');
|
2014-11-26 13:38:15 -08:00
|
|
|
}
|
2014-11-24 14:23:09 -08:00
|
|
|
|
2015-01-08 05:57:46 -08:00
|
|
|
JSUtil.defineImmutable(this, {
|
|
|
|
bn: info.bn,
|
|
|
|
compressed: info.compressed,
|
|
|
|
network: info.network
|
2014-12-01 09:44:32 -08:00
|
|
|
});
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2014-12-09 06:04:31 -08:00
|
|
|
Object.defineProperty(this, 'publicKey', {
|
|
|
|
configurable: false,
|
2015-01-08 05:57:46 -08:00
|
|
|
enumerable: true,
|
2014-12-11 13:19:36 -08:00
|
|
|
get: this.toPublicKey.bind(this)
|
2014-12-09 06:04:31 -08:00
|
|
|
});
|
|
|
|
|
2014-09-22 13:55:40 -07:00
|
|
|
return this;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-12-19 10:32:57 -08:00
|
|
|
* Internal helper to instantiate PrivateKey internal `info` object from
|
|
|
|
* different kinds of arguments passed to the constructor.
|
|
|
|
*
|
|
|
|
* @param {*} data
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
2014-12-19 10:32:57 -08:00
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
PrivateKey.prototype._classifyArguments = function(data, network) {
|
|
|
|
/* jshint maxcomplexity: 10 */
|
|
|
|
var info = {
|
|
|
|
compressed: true,
|
|
|
|
network: network ? Networks.get(network) : Networks.defaultNetwork
|
|
|
|
};
|
|
|
|
|
|
|
|
// detect type of data
|
2014-12-30 13:44:27 -08:00
|
|
|
if (_.isUndefined(data) || _.isNull(data)){
|
2014-12-19 10:32:57 -08:00
|
|
|
info.bn = PrivateKey._getRandomBN();
|
|
|
|
} else if (data instanceof BN) {
|
|
|
|
info.bn = data;
|
|
|
|
} else if (data instanceof Buffer || data instanceof Uint8Array) {
|
|
|
|
info = PrivateKey._transformBuffer(data, network);
|
|
|
|
} else if (PrivateKey._isJSON(data)){
|
|
|
|
info = PrivateKey._transformJSON(data);
|
2014-12-30 13:44:27 -08:00
|
|
|
} else if (!network && Networks.get(data)) {
|
|
|
|
info.bn = PrivateKey._getRandomBN();
|
|
|
|
info.network = Networks.get(data);
|
2014-12-19 10:32:57 -08:00
|
|
|
} else if (typeof(data) === 'string'){
|
|
|
|
if (JSUtil.isHexa(data)) {
|
2015-02-05 11:46:18 -08:00
|
|
|
info.bn = new BN(new Buffer(data, 'hex'));
|
2014-12-19 10:32:57 -08:00
|
|
|
} else {
|
|
|
|
info = PrivateKey._transformWIF(data, network);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new TypeError('First argument is an unrecognized data type.');
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function to get a random Big Number (BN)
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2014-12-11 10:29:21 -08:00
|
|
|
* @returns {BN} A new randomly generated BN
|
2014-11-26 10:00:09 -08:00
|
|
|
* @private
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-11-25 10:21:53 -08:00
|
|
|
PrivateKey._getRandomBN = function(){
|
2014-11-24 14:23:09 -08:00
|
|
|
var condition;
|
|
|
|
var bn;
|
2014-08-28 19:27:22 -07:00
|
|
|
do {
|
|
|
|
var privbuf = Random.getRandomBuffer(32);
|
2014-12-19 13:28:52 -08:00
|
|
|
bn = BN.fromBuffer(privbuf);
|
2014-11-24 14:23:09 -08:00
|
|
|
condition = bn.lt(Point.getN());
|
2014-08-28 19:27:22 -07:00
|
|
|
} while (!condition);
|
2014-11-24 14:23:09 -08:00
|
|
|
return bn;
|
|
|
|
};
|
|
|
|
|
2014-12-11 13:19:36 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2014-12-19 10:32:57 -08:00
|
|
|
return JSUtil.isValidJSON(json) || (json.bn && json.network);
|
2014-12-11 13:19:36 -08:00
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-12-11 08:25:02 -08:00
|
|
|
* Internal function to transform a WIF Buffer into a private key
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2014-12-11 08:25:02 -08:00
|
|
|
* @param {Buffer} buf - An WIF string
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
2014-11-24 14:23:09 -08:00
|
|
|
* @returns {Object} An object with keys: bn, network and compressed
|
2014-11-26 10:00:09 -08:00
|
|
|
* @private
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey._transformBuffer = function(buf, network) {
|
2014-11-24 14:23:09 -08:00
|
|
|
|
|
|
|
var info = {};
|
|
|
|
|
2015-01-27 08:35:10 -08:00
|
|
|
if (buf.length === 32) {
|
|
|
|
return PrivateKey._transformBNBuffer(buf, network);
|
2014-11-26 13:38:15 -08:00
|
|
|
}
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
info.network = Networks.get(buf[0], 'privatekey');
|
2014-12-11 08:25:02 -08:00
|
|
|
if (buf[0] === Networks.livenet.privatekey) {
|
|
|
|
info.network = Networks.livenet;
|
|
|
|
} else if (buf[0] === Networks.testnet.privatekey) {
|
|
|
|
info.network = Networks.testnet;
|
2014-11-26 13:38:15 -08:00
|
|
|
} else {
|
|
|
|
throw new Error('Invalid network');
|
|
|
|
}
|
|
|
|
|
2014-12-11 08:25:02 -08:00
|
|
|
if (network && info.network !== Networks.get(network)) {
|
2014-12-17 09:34:21 -08:00
|
|
|
throw new TypeError('Private key network mismatch');
|
2014-11-24 14:23:09 -08:00
|
|
|
}
|
|
|
|
|
2015-01-27 08:35:10 -08:00
|
|
|
if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
|
|
|
|
info.compressed = true;
|
|
|
|
} else if (buf.length === 1 + 32) {
|
|
|
|
info.compressed = false;
|
|
|
|
} else {
|
|
|
|
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:38:15 -08:00
|
|
|
info.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
return info;
|
2015-01-27 08:35:10 -08:00
|
|
|
};
|
2014-11-26 13:38:15 -08:00
|
|
|
|
2015-01-27 08:35:10 -08:00
|
|
|
/**
|
|
|
|
* Internal function to transform a BN buffer into a private key
|
|
|
|
*
|
|
|
|
* @param {Buffer} buf
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
2015-01-27 08:35:10 -08:00
|
|
|
* @returns {object} an Object with keys: bn, network, and compressed
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PrivateKey._transformBNBuffer = function(buf, network) {
|
|
|
|
var info = {};
|
|
|
|
info.network = Networks.get(network) || Networks.defaultNetwork;
|
|
|
|
info.bn = BN.fromBuffer(buf);
|
|
|
|
info.compressed = false;
|
|
|
|
return info;
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-12-11 08:25:02 -08:00
|
|
|
* Internal function to transform a WIF string into a private key
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} buf - An WIF string
|
2014-11-24 14:23:09 -08:00
|
|
|
* @returns {Object} An object with keys: bn, network and compressed
|
2014-11-26 10:00:09 -08:00
|
|
|
* @private
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey._transformWIF = function(str, network) {
|
2015-02-13 19:40:20 -08:00
|
|
|
return PrivateKey._transformBuffer(Base58Check.decode(str), network);
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-12-11 13:19:36 -08:00
|
|
|
* Instantiate a PrivateKey from a JSON string
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} json - The JSON encoded private key string
|
2014-11-25 10:21:53 -08:00
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey.fromJSON = function(json) {
|
|
|
|
if (!PrivateKey._isJSON(json)) {
|
|
|
|
throw new TypeError('Must be a valid JSON string or plain object');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PrivateKey(json);
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
2015-01-27 08:35:10 -08:00
|
|
|
/**
|
|
|
|
* Instantiate a PrivateKey from a Buffer with the DER or WIF representation
|
|
|
|
*
|
|
|
|
* @param {Buffer} arg
|
|
|
|
* @param {Network} network
|
|
|
|
* @return {PrivateKey}
|
|
|
|
*/
|
|
|
|
PrivateKey.fromBuffer = function(arg, network) {
|
|
|
|
return new PrivateKey(arg, network);
|
|
|
|
};
|
2014-12-11 13:19:36 -08:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-12-11 13:19:36 -08:00
|
|
|
* Internal function to transform a JSON string on plain object into a private key
|
2015-01-27 08:35:10 -08:00
|
|
|
* return this.
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} json - A JSON string or plain object
|
2014-12-11 13:19:36 -08:00
|
|
|
* @returns {Object} An object with keys: bn, network and compressed
|
|
|
|
* @private
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey._transformJSON = function(json) {
|
2014-12-12 14:54:48 -08:00
|
|
|
if (JSUtil.isValidJSON(json)) {
|
2014-12-12 10:39:49 -08:00
|
|
|
json = JSON.parse(json);
|
|
|
|
}
|
2015-02-05 11:46:18 -08:00
|
|
|
var bn = new BN(json.bn, 'hex');
|
2014-12-11 13:19:36 -08:00
|
|
|
return {
|
|
|
|
bn: bn,
|
|
|
|
network: json.network,
|
|
|
|
compressed: json.compressed
|
2014-12-19 10:32:57 -08:00
|
|
|
};
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-12-11 13:19:36 -08:00
|
|
|
* Instantiate a PrivateKey from a WIF string
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} str - The WIF encoded private key string
|
2014-11-25 10:21:53 -08:00
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey.fromString = PrivateKey.fromWIF = function(str) {
|
|
|
|
return new PrivateKey(str);
|
2014-08-28 19:27:22 -07:00
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-12-11 13:19:36 -08:00
|
|
|
* Instantiate a PrivateKey from random bytes
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {string=} network - Either "livenet" or "testnet"
|
2014-11-25 10:21:53 -08:00
|
|
|
* @returns {PrivateKey} A new valid instance of PrivateKey
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey.fromRandom = function(network) {
|
|
|
|
var bn = PrivateKey._getRandomBN();
|
|
|
|
return new PrivateKey(bn, network);
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-11-25 10:21:53 -08:00
|
|
|
* Check if there would be any errors when initializing a PrivateKey
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} data - The encoded data in various formats
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {string=} network - Either "livenet" or "testnet"
|
2014-11-24 14:23:09 -08:00
|
|
|
* @returns {null|Error} An error if exists
|
|
|
|
*/
|
|
|
|
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey.getValidationError = function(data, network) {
|
2014-11-24 14:23:09 -08:00
|
|
|
var error;
|
|
|
|
try {
|
2014-12-19 10:32:57 -08:00
|
|
|
/* jshint nonew: false */
|
2014-12-11 13:19:36 -08:00
|
|
|
new PrivateKey(data, network);
|
2014-11-24 14:23:09 -08:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
return error;
|
2014-08-06 22:47:10 -07:00
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
|
|
|
* Check if the parameters are valid
|
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @param {string} data - The encoded data in various formats
|
2015-02-24 10:12:03 -08:00
|
|
|
* @param {string=} network - Either "livenet" or "testnet"
|
2014-11-24 14:23:09 -08:00
|
|
|
* @returns {Boolean} If the private key is would be valid
|
|
|
|
*/
|
2014-12-11 13:19:36 -08:00
|
|
|
PrivateKey.isValid = function(data, network){
|
|
|
|
return !PrivateKey.getValidationError(data, network);
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
2015-01-02 13:15:17 -08:00
|
|
|
/**
|
2015-01-02 13:25:08 -08:00
|
|
|
* Will output the PrivateKey encoded as hex string
|
2015-01-02 13:15:17 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @returns {string}
|
2015-01-02 13:15:17 -08:00
|
|
|
*/
|
|
|
|
PrivateKey.prototype.toString = function() {
|
|
|
|
return this.toBuffer().toString('hex');
|
2015-01-28 08:10:06 -08:00
|
|
|
};
|
2015-01-02 13:15:17 -08:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-11-25 10:21:53 -08:00
|
|
|
* Will output the PrivateKey to a WIF string
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @returns {string} A WIP representation of the private key
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2015-01-02 13:15:17 -08:00
|
|
|
PrivateKey.prototype.toWIF = function() {
|
2014-11-24 14:23:09 -08:00
|
|
|
var network = this.network;
|
2014-08-06 22:47:10 -07:00
|
|
|
var compressed = this.compressed;
|
|
|
|
|
|
|
|
var buf;
|
2014-11-24 14:23:09 -08:00
|
|
|
if (compressed) {
|
2014-12-11 08:25:02 -08:00
|
|
|
buf = Buffer.concat([new Buffer([network.privatekey]),
|
|
|
|
this.bn.toBuffer({size: 32}),
|
2014-11-24 14:23:09 -08:00
|
|
|
new Buffer([0x01])]);
|
|
|
|
} else {
|
2014-12-11 08:25:02 -08:00
|
|
|
buf = Buffer.concat([new Buffer([network.privatekey]),
|
2014-11-24 14:23:09 -08:00
|
|
|
this.bn.toBuffer({size: 32})]);
|
|
|
|
}
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2015-02-13 19:40:20 -08:00
|
|
|
return Base58Check.encode(buf);
|
2014-08-06 22:47:10 -07:00
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
|
|
|
* Will return the private key as a BN instance
|
|
|
|
*
|
|
|
|
* @returns {BN} A BN instance of the private key
|
|
|
|
*/
|
2014-11-25 10:21:53 -08:00
|
|
|
PrivateKey.prototype.toBigNumber = function(){
|
2014-11-24 14:23:09 -08:00
|
|
|
return this.bn;
|
|
|
|
};
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
|
|
|
* Will return the private key as a BN buffer
|
|
|
|
*
|
|
|
|
* @returns {Buffer} A buffer of the private key
|
|
|
|
*/
|
2014-11-25 10:21:53 -08:00
|
|
|
PrivateKey.prototype.toBuffer = function(){
|
2014-11-24 14:23:09 -08:00
|
|
|
return this.bn.toBuffer();
|
|
|
|
};
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
|
|
|
* Will return the corresponding public key
|
|
|
|
*
|
2014-11-25 10:21:53 -08:00
|
|
|
* @returns {PublicKey} A public key generated from the private key
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-11-25 10:21:53 -08:00
|
|
|
PrivateKey.prototype.toPublicKey = function(){
|
2014-12-11 13:19:36 -08:00
|
|
|
if (!this._pubkey) {
|
|
|
|
this._pubkey = PublicKey.fromPrivateKey(this);
|
|
|
|
}
|
|
|
|
return this._pubkey;
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will return an address for the private key
|
2015-02-24 10:26:48 -08:00
|
|
|
* @param {Network=} network - optional parameter specifying
|
|
|
|
* the desired network for the address
|
2014-11-24 14:23:09 -08:00
|
|
|
*
|
|
|
|
* @returns {Address} An address generated from the private key
|
|
|
|
*/
|
2015-02-24 10:26:48 -08:00
|
|
|
PrivateKey.prototype.toAddress = function(network) {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pubkey = this.toPublicKey();
|
2015-02-24 10:26:48 -08:00
|
|
|
return Address.fromPublicKey(pubkey, network || this.network);
|
2014-11-24 14:23:09 -08:00
|
|
|
};
|
2014-08-06 22:47:10 -07:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
2014-12-12 15:47:15 -08:00
|
|
|
* @returns {Object} A plain object representation
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-12-12 15:47:15 -08:00
|
|
|
PrivateKey.prototype.toObject = function toObject() {
|
2014-12-12 10:39:49 -08:00
|
|
|
return {
|
|
|
|
bn: this.bn.toString('hex'),
|
|
|
|
compressed: this.compressed,
|
|
|
|
network: this.network.toString()
|
|
|
|
};
|
2014-08-06 22:47:10 -07:00
|
|
|
};
|
|
|
|
|
2014-12-12 15:47:15 -08:00
|
|
|
PrivateKey.prototype.toJSON = function toJSON() {
|
|
|
|
return JSON.stringify(this.toObject());
|
|
|
|
};
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
/**
|
|
|
|
* Will return a string formatted for the console
|
|
|
|
*
|
2015-02-24 09:51:37 -08:00
|
|
|
* @returns {string} Private key
|
2014-11-24 14:23:09 -08:00
|
|
|
*/
|
2014-11-25 10:21:53 -08:00
|
|
|
PrivateKey.prototype.inspect = function() {
|
2014-12-11 13:19:36 -08:00
|
|
|
var uncompressed = !this.compressed ? ', uncompressed' : '';
|
|
|
|
return '<PrivateKey: ' + this.toString() + ', network: ' + this.network + uncompressed + '>';
|
2014-08-06 22:47:10 -07:00
|
|
|
};
|
|
|
|
|
2014-11-25 10:21:53 -08:00
|
|
|
module.exports = PrivateKey;
|