bitcore-lib-zcash/lib/privatekey.js

374 lines
9.6 KiB
JavaScript
Raw Normal View History

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