Browse Source

Remove compressed argument from private key and public key constructors

patch-2
Yemel Jardi 10 years ago
parent
commit
428bcaf4c9
  1. 123
      lib/privatekey.js
  2. 144
      lib/publickey.js
  3. 42
      test/privatekey.js
  4. 26
      test/publickey.js

123
lib/privatekey.js

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

144
lib/publickey.js

@ -20,14 +20,13 @@ var JSUtil = require('./util/js');
* var imported = PublicKey.fromString(exported); * var imported = PublicKey.fromString(exported);
* *
* @param {String} data - The encoded data in various formats * @param {String} data - The encoded data in various formats
* @param {String} [compressed] - If the public key is compressed
* @returns {PublicKey} A new valid instance of an PublicKey * @returns {PublicKey} A new valid instance of an PublicKey
* @constructor * @constructor
*/ */
var PublicKey = function PublicKey(data, compressed) { var PublicKey = function PublicKey(data) {
if (!(this instanceof PublicKey)) { if (!(this instanceof PublicKey)) {
return new PublicKey(data, compressed); return new PublicKey(data);
} }
if (!data) { if (!data) {
@ -39,18 +38,19 @@ var PublicKey = function PublicKey(data, compressed) {
} }
var info = { var info = {
compressed: typeof(compressed) !== 'undefined' ? compressed : true compressed: true
}; };
// detect type of data // detect type of data
if (data instanceof Point) { if (data instanceof Point) {
info.point = data; info.point = data;
} else if (typeof(data) === 'string'){ } else if (PublicKey._isJSON(data)) {
info = PublicKey._transformJSON(data);
} else if (typeof(data) === 'string') {
info = PublicKey._transformDER(new Buffer(data, 'hex')); info = PublicKey._transformDER(new Buffer(data, 'hex'));
} else if (data instanceof Buffer || data instanceof Uint8Array){ } else if (PublicKey._isBuffer(data)) {
info = PublicKey._transformDER(data); info = PublicKey._transformDER(data);
} else if (data.constructor && (data.constructor.name && } else if (PublicKey._isPrivateKey(data)) {
data.constructor.name === 'PrivateKey')) {
info = PublicKey._transformPrivateKey(data); info = PublicKey._transformPrivateKey(data);
} else { } else {
throw new TypeError('First argument is an unrecognized data format.'); throw new TypeError('First argument is an unrecognized data format.');
@ -69,10 +69,49 @@ var PublicKey = function PublicKey(data, compressed) {
value: info.compressed value: info.compressed
}); });
Object.defineProperty(this, 'address', {
configurable: false,
get: this.toAddress.bind(this)
});
return this; return this;
}; };
/**
* Internal function to detect if an object is a PrivateKey
*
* @param {*} param - object to test
* @returns {boolean}
* @private
*/
PublicKey._isPrivateKey = function(param) {
return param && param.constructor && param.constructor.name
&& param.constructor.name === 'PrivateKey';
};
/**
* Internal function to detect if an object is a Buffer
*
* @param {*} param - object to test
* @returns {boolean}
* @private
*/
PublicKey._isBuffer = function(param) {
return (param instanceof Buffer) || (param instanceof Uint8Array);
};
/**
* Internal function to detect if a param is a JSON string or plain object
*
* @param {*} param - value to test
* @returns {boolean}
* @private
*/
PublicKey._isJSON = function(json) {
return JSUtil.isValidJSON(json) || (json.x && json.y);
};
/** /**
* Internal function to transform a private key into a public key point * Internal function to transform a private key into a public key point
* *
@ -82,8 +121,7 @@ var PublicKey = function PublicKey(data, compressed) {
*/ */
PublicKey._transformPrivateKey = function(privkey) { PublicKey._transformPrivateKey = function(privkey) {
var info = {}; var info = {};
if (!privkey.constructor || if (!PublicKey._isPrivateKey(privkey)) {
(privkey.constructor.name && privkey.constructor.name !== 'PrivateKey')) {
throw new TypeError('Must be an instance of PrivateKey'); throw new TypeError('Must be an instance of PrivateKey');
} }
info.point = Point.getG().mul(privkey.bn); info.point = Point.getG().mul(privkey.bn);
@ -100,7 +138,7 @@ PublicKey._transformPrivateKey = function(privkey) {
*/ */
PublicKey._transformDER = function(buf){ PublicKey._transformDER = function(buf){
var info = {}; var info = {};
if (!(buf instanceof Buffer) && !(buf instanceof Uint8Array)){ if (!PublicKey._isBuffer(buf)) {
throw new TypeError('Must be a hex buffer of DER encoded public key'); throw new TypeError('Must be a hex buffer of DER encoded public key');
} }
@ -159,13 +197,31 @@ PublicKey._transformX = function(odd, x){
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromJSON = function(json) { PublicKey.fromJSON = function(json) {
if (!PublicKey._isJSON(json)) {
throw new TypeError('Must be a valid JSON string or plain object');
}
return new PublicKey(json);
};
/**
* Internal function to transform a JSON into a public key point
*
* @param {Buffer} buf - a JSON string or plain object
* @returns {Object} An object with keys: point and compressed
* @private
*/
PublicKey._transformJSON = function(json) {
if (JSUtil.isValidJSON(json)) { if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json); json = JSON.parse(json);
} }
var x = BN(json.x, 'hex'); var x = BN(json.x, 'hex');
var y = BN(json.y, 'hex'); var y = BN(json.y, 'hex');
var point = new Point(x, y);
return new PublicKey(point, json.compressed); return {
point: Point(x, y),
compressed: json.compressed
};
}; };
/** /**
@ -175,19 +231,23 @@ PublicKey.fromJSON = function(json) {
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPrivateKey = function(privkey) { PublicKey.fromPrivateKey = function(privkey) {
var info = PublicKey._transformPrivateKey(privkey); if (!PublicKey._isPrivateKey(privkey)) {
return new PublicKey(info.point, info.compressed); throw new TypeError('Must be an instance of PrivateKey');
}
return new PublicKey(privkey);
}; };
/** /**
* Instantiate a PublicKey from a Buffer * Instantiate a PublicKey from a Buffer
* *
* @param {Buffer} buf - A DER hex buffer * @param {Buffer} buf - A DER buffer
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromBuffer = function(buf) { PublicKey.fromDER = PublicKey.fromBuffer = function(buf) {
var info = PublicKey._transformDER(buf); if (!PublicKey._isBuffer(buf)) {
return new PublicKey(info.point, info.compressed); throw new TypeError('Must be a hex buffer of DER encoded public key');
}
return new PublicKey(buf);
}; };
/** /**
@ -196,35 +256,23 @@ PublicKey.fromBuffer = function(buf) {
* @param {Point} point - A Point instance * @param {Point} point - A Point instance
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPoint = function(point, compressed){ PublicKey.fromPoint = function(point){
if (!(point instanceof Point)) { if (!(point instanceof Point)) {
throw new TypeError('First argument must be an instance of Point.'); throw new TypeError('First argument must be an instance of Point.');
} }
return new PublicKey(point, compressed); return new PublicKey(point);
}; };
/** /**
* Instantiate a PublicKey from a DER Buffer * Instantiate a PublicKey from a DER Buffer
* *
* @param {Buffer} buf - A DER Buffer
* @returns {PublicKey} A new valid instance of PublicKey
*/
PublicKey.fromDER = function(buf) {
var info = PublicKey._transformDER(buf);
return new PublicKey(info.point, info.compressed);
};
/**
* Instantiate a PublicKey from a DER hex encoded string
*
* @param {String} str - A DER hex string * @param {String} str - A DER hex string
* @param {String} [encoding] - The type of string encoding * @param {String} [encoding] - The type of string encoding
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromString = function(str, encoding) { PublicKey.fromString = function(str, encoding) {
var buf = new Buffer(str, encoding || 'hex'); var buf = new Buffer(str, encoding || 'hex');
var info = PublicKey._transformDER(buf); return new PublicKey(buf);
return new PublicKey(info.point, info.compressed);
}; };
/** /**
@ -236,10 +284,9 @@ PublicKey.fromString = function(str, encoding) {
*/ */
PublicKey.fromX = function(odd, x) { PublicKey.fromX = function(odd, x) {
var info = PublicKey._transformX(odd, x); var info = PublicKey._transformX(odd, x);
return new PublicKey(info.point, info.compressed); return new PublicKey(info.point);
}; };
/** /**
* Check if there would be any errors when initializing a PublicKey * Check if there would be any errors when initializing a PublicKey
* *
@ -283,27 +330,12 @@ PublicKey.prototype.toJSON = function toJSON(){
return JSON.stringify(this.toObject()); return JSON.stringify(this.toObject());
}; };
/**
* Will output the PublicKey to a Buffer
*
* @returns {Buffer} A DER hex encoded buffer
*/
PublicKey.prototype.toBuffer = function() {
var compressed = typeof this.compressed === 'undefined' ? true : this.compressed;
return this.toDER(compressed);
};
/** /**
* Will output the PublicKey to a DER Buffer * Will output the PublicKey to a DER Buffer
* *
* @returns {Buffer} A DER hex encoded buffer * @returns {Buffer} A DER hex encoded buffer
*/ */
PublicKey.prototype.toDER = function(compressed) { PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() {
compressed = typeof(compressed) !== 'undefined' ? compressed : this.compressed;
if (typeof compressed !== 'boolean') {
throw new TypeError('Must specify whether the public key is compressed or not (true or false)');
}
var x = this.point.getX(); var x = this.point.getX();
var y = this.point.getY(); var y = this.point.getY();
@ -311,7 +343,7 @@ PublicKey.prototype.toDER = function(compressed) {
var ybuf = y.toBuffer({size: 32}); var ybuf = y.toBuffer({size: 32});
var prefix; var prefix;
if (!compressed) { if (!this.compressed) {
prefix = new Buffer([0x04]); prefix = new Buffer([0x04]);
return Buffer.concat([prefix, xbuf, ybuf]); return Buffer.concat([prefix, xbuf, ybuf]);
} else { } else {
@ -340,8 +372,7 @@ PublicKey.prototype.toAddress = function(network) {
* @returns {String} A DER hex encoded string * @returns {String} A DER hex encoded string
*/ */
PublicKey.prototype.toString = function() { PublicKey.prototype.toString = function() {
var compressed = typeof this.compressed === 'undefined' ? true : this.compressed; return this.toDER().toString('hex');
return this.toDER(compressed).toString('hex');
}; };
/** /**
@ -350,7 +381,8 @@ PublicKey.prototype.toString = function() {
* @returns {String} Public key * @returns {String} Public key
*/ */
PublicKey.prototype.inspect = function() { PublicKey.prototype.inspect = function() {
return '<PublicKey: ' + this.toString() + ', compressed: '+this.compressed+'>'; var uncompressed = !this.compressed ? ', uncompressed' : '';
return '<PublicKey: ' + this.toString() + uncompressed + '>';
}; };
module.exports = PublicKey; module.exports = PublicKey;

42
test/privatekey.js

@ -79,12 +79,6 @@ describe('PrivateKey', function() {
}).to.throw('Private key network mismatch'); }).to.throw('Private key network mismatch');
}); });
it('should not be able to instantiate private key because of compression mismatch', function() {
expect(function() {
var a = new PrivateKey('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m', 'livenet', false);
}).to.throw('Private key compression mismatch');
});
it('should not be able to instantiate private key WIF is too long', function() { it('should not be able to instantiate private key WIF is too long', function() {
expect(function() { expect(function() {
var buf = base58check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); var buf = base58check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
@ -108,12 +102,6 @@ describe('PrivateKey', function() {
privkey.publicKey.toString().should.equal(pubhex); privkey.publicKey.toString().should.equal(pubhex);
}); });
it('should not be able to instantiate because compressed is non-boolean', function() {
expect(function() {
var a = new PrivateKey(BN(2), 'testnet', 'compressed');
}).to.throw('Must specify whether the corresponding public key is compressed or not (true or false)');
});
it('should not be able to instantiate because of unrecognized data', function() { it('should not be able to instantiate because of unrecognized data', function() {
expect(function() { expect(function() {
var a = new PrivateKey(new Error()); var a = new PrivateKey(new Error());
@ -134,7 +122,7 @@ describe('PrivateKey', function() {
}); });
it('should create a livenet private key', function() { it('should create a livenet private key', function() {
var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', true); var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet');
privkey.toString().should.equal(enclivenet); privkey.toString().should.equal(enclivenet);
}); });
@ -149,16 +137,6 @@ describe('PrivateKey', function() {
Networks.defaultNetwork = Networks.livenet; Networks.defaultNetwork = Networks.livenet;
}); });
it('should create an uncompressed testnet private key', function() {
var privkey = new PrivateKey(BN.fromBuffer(buf), 'testnet', false);
privkey.toString().should.equal(enctu);
});
it('should create an uncompressed livenet private key', function() {
var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', false);
privkey.toString().should.equal(encmu);
});
describe('#json', function() { describe('#json', function() {
it('should input/output json', function() { it('should input/output json', function() {
@ -199,12 +177,12 @@ describe('PrivateKey', function() {
describe('#inspect', function() { describe('#inspect', function() {
it('should output known livenet address for console', function() { it('should output known livenet address for console', function() {
var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
privkey.inspect().should.equal('<PrivateKey: L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m, compressed: true, network: livenet>'); privkey.inspect().should.equal('<PrivateKey: L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m, network: livenet>');
}); });
it('should output known testnet address for console', function() { it('should output known testnet address for console', function() {
var privkey = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq'); var privkey = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq');
privkey.inspect().should.equal('<PrivateKey: cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq, compressed: true, network: testnet>'); privkey.inspect().should.equal('<PrivateKey: cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq, network: testnet>');
}); });
}); });
@ -231,7 +209,7 @@ describe('PrivateKey', function() {
describe('#toBuffer', function() { describe('#toBuffer', function() {
it('should output known buffer', function() { it('should output known buffer', function() {
var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', true); var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet');
var b = privkey.toBuffer().toString('hex').should.equal(buf.toString('hex')); var b = privkey.toBuffer().toString('hex').should.equal(buf.toString('hex'));
}); });
}); });
@ -239,7 +217,7 @@ describe('PrivateKey', function() {
describe('#toBigNumber', function() { describe('#toBigNumber', function() {
it('should output known BN', function() { it('should output known BN', function() {
var a = BN.fromBuffer(buf); var a = BN.fromBuffer(buf);
var privkey = new PrivateKey(a, 'livenet', true); var privkey = new PrivateKey(a, 'livenet');
var b = privkey.toBigNumber(); var b = privkey.toBigNumber();
b.toString('hex').should.equal(a.toString('hex')); b.toString('hex').should.equal(a.toString('hex'));
}); });
@ -310,15 +288,15 @@ describe('PrivateKey', function() {
}); });
it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() { it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privwif = 'L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')), 'livenet', true); var privkey = new PrivateKey(privwif, 'livenet');
var pubkey = privkey.toPublicKey(); var pubkey = privkey.toPublicKey();
pubkey.compressed.should.equal(true); pubkey.compressed.should.equal(true);
}); });
it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() { it('should convert this known PrivateKey to known PublicKey and preserve compressed=false', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privwif = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')), 'livenet', false); var privkey = new PrivateKey(privwif, 'testnet');
var pubkey = privkey.toPublicKey(); var pubkey = privkey.toPublicKey();
pubkey.compressed.should.equal(false); pubkey.compressed.should.equal(false);
}); });

26
test/publickey.js

@ -241,6 +241,12 @@ describe('PublicKey', function() {
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
}); });
it('should return this uncompressed DER format', function() {
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = PublicKey.fromX(true, x);
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
});
}); });
describe('#toDER', function() { describe('#toDER', function() {
@ -248,23 +254,13 @@ describe('PublicKey', function() {
it('should return this compressed DER format', function() { it('should return this compressed DER format', function() {
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = PublicKey.fromX(true, x); var pk = PublicKey.fromX(true, x);
pk.toDER(true).toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.toDER().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
}); });
it('should return this uncompressed DER format', function() { it('should return this uncompressed DER format', function() {
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var pk = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
var pk = PublicKey.fromX(true, x); pk.toDER().toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
pk.toDER(false).toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
});
it('should error because compressed param is invalid', function() {
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = PublicKey.fromX(true, x);
(function() {
pk.toDER('false'); //string not boolean
}).should.throw('Must specify whether the public key is compressed or not (true or false)');
}); });
}); });
describe('#toAddress', function() { describe('#toAddress', function() {
@ -296,12 +292,12 @@ describe('PublicKey', function() {
describe('#inspect', function() { describe('#inspect', function() {
it('should output known uncompressed pubkey for console', function() { it('should output known uncompressed pubkey for console', function() {
var pubkey = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); var pubkey = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
pubkey.inspect().should.equal('<PublicKey: 041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341, compressed: false>'); pubkey.inspect().should.equal('<PublicKey: 041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341, uncompressed>');
}); });
it('should output known compressed pubkey for console', function() { it('should output known compressed pubkey for console', function() {
var pubkey = PublicKey.fromString('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); var pubkey = PublicKey.fromString('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
pubkey.inspect().should.equal('<PublicKey: 031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a, compressed: true>'); pubkey.inspect().should.equal('<PublicKey: 031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a>');
}); });
}); });

Loading…
Cancel
Save