2014-11-20 13:31:08 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
var should = require('chai').should();
|
2014-12-19 10:32:57 -08:00
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
2014-11-21 05:29:41 -08:00
|
|
|
var bitcore = require('..');
|
2014-11-21 08:26:30 -08:00
|
|
|
var Point = bitcore.crypto.Point;
|
|
|
|
var BN = bitcore.crypto.BN;
|
2014-11-25 10:21:53 -08:00
|
|
|
var PublicKey = bitcore.PublicKey;
|
|
|
|
var PrivateKey = bitcore.PrivateKey;
|
2015-01-08 14:43:59 -08:00
|
|
|
var Address = bitcore.Address;
|
2014-12-30 21:15:59 -08:00
|
|
|
var Networks = bitcore.Networks;
|
2014-08-07 16:18:17 -07:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
/* jshint maxlen: 200 */
|
|
|
|
|
2014-11-25 10:21:53 -08:00
|
|
|
describe('PublicKey', function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
/* jshint maxstatements: 30 */
|
2014-11-24 14:23:09 -08:00
|
|
|
|
2014-11-28 15:51:37 -08:00
|
|
|
var invalidPoint = '0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
describe('validating errors on creation', function() {
|
|
|
|
it('errors if data is missing', function() {
|
|
|
|
(function() {
|
|
|
|
return new PublicKey();
|
|
|
|
}).should.throw('First argument is required, please include public key data.');
|
|
|
|
});
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('errors if an invalid point is provided', function() {
|
|
|
|
(function() {
|
|
|
|
return new PublicKey(invalidPoint);
|
|
|
|
}).should.throw('Invalid x,y value for curve, cannot equal 0.');
|
|
|
|
});
|
2014-08-07 16:18:17 -07:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('errors if a point not on the secp256k1 curve is provided', function() {
|
|
|
|
(function() {
|
|
|
|
return new PublicKey(new Point(1000, 1000));
|
|
|
|
}).should.throw('Invalid y value for curve.');
|
|
|
|
});
|
2014-11-24 14:23:09 -08:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('errors if the argument is of an unrecognized type', function() {
|
|
|
|
(function() {
|
|
|
|
return new PublicKey(new Error());
|
|
|
|
}).should.throw('First argument is an unrecognized data format.');
|
|
|
|
});
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
describe('instantiation', function() {
|
2015-01-03 10:20:51 -08:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from a private key', function() {
|
|
|
|
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
|
|
|
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
|
2015-02-05 12:24:48 -08:00
|
|
|
var privkey = new PrivateKey(new BN(new Buffer(privhex, 'hex')));
|
2014-12-19 10:32:57 -08:00
|
|
|
var pk = new PublicKey(privkey);
|
|
|
|
pk.toString().should.equal(pubhex);
|
2015-01-03 10:20:51 -08:00
|
|
|
});
|
|
|
|
|
2015-01-04 06:19:08 -08:00
|
|
|
it('problematic secp256k1 public keys', function() {
|
|
|
|
|
|
|
|
var knownKeys = [
|
|
|
|
{
|
|
|
|
wif: 'KzsjKq2FVqVuQv2ueHVFuB65A9uEZ6S1L6F8NuokCrE3V3kE3Ack',
|
|
|
|
priv: '6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846',
|
|
|
|
pub: '03d6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
|
|
|
|
pubx: 'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
|
|
|
|
puby: 'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
wif: 'L5MgSwNB2R76xBGorofRSTuQFd1bm3hQMFVf3u2CneFom8u1Yt7G',
|
|
|
|
priv: 'f2cc9d2b008927db94b89e04e2f6e70c180e547b3e5e564b06b8215d1c264b53',
|
|
|
|
pub: '03e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
|
|
|
|
pubx: 'e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
|
|
|
|
puby: '367216cb35b086e6686d69dddd822a8f4d52eb82ac5d9de18fdcd9bf44fa7df7'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
for(var i = 0; i < knownKeys.length; i++) {
|
|
|
|
var privkey = new PrivateKey(knownKeys[i].wif);
|
|
|
|
var pubkey = privkey.toPublicKey();
|
|
|
|
pubkey.toString().should.equal(knownKeys[i].pub);
|
|
|
|
pubkey.point.x.toString('hex').should.equal(knownKeys[i].pubx);
|
|
|
|
pubkey.point.y.toString('hex').should.equal(knownKeys[i].puby);
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
});
|
2014-11-24 14:23:09 -08:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from a compressed public key', function() {
|
|
|
|
var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
|
|
|
var publicKey = new PublicKey(publicKeyHex);
|
|
|
|
publicKey.toString().should.equal(publicKeyHex);
|
|
|
|
});
|
2014-12-10 07:40:05 -08:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from another publicKey', function() {
|
|
|
|
var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
|
|
|
var publicKey = new PublicKey(publicKeyHex);
|
|
|
|
var publicKey2 = new PublicKey(publicKey);
|
|
|
|
publicKey.should.equal(publicKey2);
|
|
|
|
});
|
2014-12-10 07:40:05 -08:00
|
|
|
|
2014-12-30 21:15:59 -08:00
|
|
|
it('sets the network to defaultNetwork if none provided', function() {
|
|
|
|
var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
|
|
|
var publicKey = new PublicKey(publicKeyHex);
|
|
|
|
publicKey.network.should.equal(Networks.defaultNetwork);
|
|
|
|
});
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from a hex encoded DER string', function() {
|
|
|
|
var pk = new PublicKey('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
|
|
should.exist(pk.point);
|
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
});
|
2014-08-07 16:18:17 -07:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from a hex encoded DER buffer', function() {
|
|
|
|
var pk = new PublicKey(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
|
|
|
|
should.exist(pk.point);
|
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
});
|
2014-09-01 21:16:10 -07:00
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('from a point', function() {
|
|
|
|
var p = new Point('86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
|
|
|
|
'3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd');
|
|
|
|
var a = new PublicKey(p);
|
|
|
|
should.exist(a.point);
|
|
|
|
a.point.toString().should.equal(p.toString());
|
|
|
|
var c = new PublicKey(p);
|
|
|
|
should.exist(c.point);
|
|
|
|
c.point.toString().should.equal(p.toString());
|
|
|
|
});
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
describe('#getValidationError', function(){
|
2014-11-28 15:51:37 -08:00
|
|
|
|
|
|
|
it('should recieve an invalid point error', function() {
|
|
|
|
var error = PublicKey.getValidationError(invalidPoint);
|
2014-11-24 14:23:09 -08:00
|
|
|
should.exist(error);
|
2014-11-28 15:51:37 -08:00
|
|
|
error.message.should.equal('Invalid x,y value for curve, cannot equal 0.');
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should recieve a boolean as false', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
var valid = PublicKey.isValid(invalidPoint);
|
2014-11-24 14:23:09 -08:00
|
|
|
valid.should.equal(false);
|
2014-08-28 17:41:38 -07:00
|
|
|
});
|
|
|
|
|
2014-12-04 09:46:59 -08:00
|
|
|
it('should recieve a boolean as true for uncompressed', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var valid = PublicKey.isValid('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-11-24 14:23:09 -08:00
|
|
|
valid.should.equal(true);
|
|
|
|
});
|
|
|
|
|
2014-12-04 09:46:59 -08:00
|
|
|
it('should recieve a boolean as true for compressed', function() {
|
|
|
|
var valid = PublicKey.isValid('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
valid.should.equal(true);
|
|
|
|
});
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fromPoint', function() {
|
|
|
|
|
|
|
|
it('should instantiate from a point', function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
var p = new Point('86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
|
|
|
|
'3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd');
|
2014-11-25 10:21:53 -08:00
|
|
|
var b = PublicKey.fromPoint(p);
|
2014-11-24 14:23:09 -08:00
|
|
|
should.exist(b.point);
|
|
|
|
b.point.toString().should.equal(p.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error because paramater is not a point', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromPoint(new Error());
|
2014-11-24 14:23:09 -08:00
|
|
|
}).should.throw('First argument must be an instance of Point.');
|
|
|
|
});
|
2014-08-28 17:41:38 -07:00
|
|
|
});
|
|
|
|
|
2014-12-12 10:39:49 -08:00
|
|
|
describe('#json', function() {
|
|
|
|
|
|
|
|
it('should input/ouput json', function() {
|
2014-12-12 15:47:15 -08:00
|
|
|
var json = JSON.stringify({
|
2014-12-12 10:39:49 -08:00
|
|
|
x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
|
|
|
|
y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
|
|
|
|
compressed: false
|
2014-12-12 15:47:15 -08:00
|
|
|
});
|
2014-12-12 10:39:49 -08:00
|
|
|
PublicKey.fromJSON(json).toJSON().should.deep.equal(json);
|
2014-09-22 13:55:40 -07:00
|
|
|
});
|
|
|
|
|
2014-12-19 10:32:57 -08:00
|
|
|
it('fails if "y" is not provided', function() {
|
|
|
|
expect(function() {
|
|
|
|
return PublicKey.fromJSON('{"x": "1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a"}');
|
|
|
|
}).to.throw();
|
2014-12-19 11:44:39 -08:00
|
|
|
// coverage
|
|
|
|
PublicKey._isJSON({x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'}).should.equal(false);
|
2014-12-19 10:32:57 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if invalid JSON is provided', function() {
|
|
|
|
expect(function() {
|
|
|
|
return PublicKey._transformJSON('¹');
|
|
|
|
}).to.throw();
|
|
|
|
});
|
|
|
|
|
2015-05-11 08:24:45 -07:00
|
|
|
it('works for X starting with 0x00', function() {
|
|
|
|
var a = new PublicKey('030589ee559348bd6a7325994f9c8eff12bd5d73cc683142bd0dd1a17abc99b0dc');
|
|
|
|
var b = new PublicKey('03'+a.toObject().x);
|
|
|
|
b.toString().should.equal(a.toString());
|
|
|
|
});
|
|
|
|
|
2014-09-22 13:55:40 -07:00
|
|
|
});
|
|
|
|
|
2014-11-25 10:21:53 -08:00
|
|
|
describe('#fromPrivateKey', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-08-28 20:19:30 -07:00
|
|
|
it('should make a public key from a privkey', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
should.exist(PublicKey.fromPrivateKey(PrivateKey.fromRandom()));
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should error because not an instance of privkey', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromPrivateKey(new Error());
|
|
|
|
}).should.throw('Must be an instance of PrivateKey');
|
2014-08-28 20:19:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-15 11:32:39 -07:00
|
|
|
describe('#fromBuffer', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-09-15 11:32:39 -07:00
|
|
|
it('should parse this uncompressed public key', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromBuffer(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
|
2014-09-15 11:32:39 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse this compressed public key', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromBuffer(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-09-15 11:32:39 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error on this invalid public key', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromBuffer(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-09-15 11:32:39 -07:00
|
|
|
}).should.throw();
|
|
|
|
});
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
it('should throw error because not a buffer', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromBuffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
2014-11-24 14:23:09 -08:00
|
|
|
}).should.throw('Must be a hex buffer of DER encoded public key');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error because buffer is the incorrect length', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromBuffer(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a34112', 'hex'));
|
2014-11-24 14:23:09 -08:00
|
|
|
}).should.throw('Length of x and y must be 32 bytes');
|
|
|
|
});
|
|
|
|
|
2014-09-15 11:32:39 -07:00
|
|
|
});
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
describe('#fromDER', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
it('should parse this uncompressed public key', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
|
2014-08-13 12:23:06 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse this compressed public key', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromDER(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-08-13 12:23:06 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error on this invalid public key', function() {
|
|
|
|
(function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
PublicKey.fromDER(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-08-07 16:18:17 -07:00
|
|
|
}).should.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fromString', function() {
|
|
|
|
|
|
|
|
it('should parse this known valid public key', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-08-13 12:23:06 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fromX', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
it('should create this known public key', function() {
|
2014-11-21 08:26:30 -08:00
|
|
|
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromX(true, x);
|
2014-08-13 12:23:06 -07:00
|
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
|
|
|
|
it('should error because odd was not included as a param', function() {
|
|
|
|
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
|
|
(function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
return PublicKey.fromX(null, x);
|
2014-11-24 14:23:09 -08:00
|
|
|
}).should.throw('Must specify whether y is odd or not (true or false)');
|
|
|
|
});
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
2014-08-09 22:19:28 -07:00
|
|
|
describe('#toBuffer', function() {
|
|
|
|
|
|
|
|
it('should return this compressed DER format', function() {
|
2014-11-21 08:26:30 -08:00
|
|
|
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromX(true, x);
|
2014-08-09 22:19:28 -07:00
|
|
|
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
|
|
});
|
|
|
|
|
2014-12-11 13:19:36 -08:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2014-08-09 22:19:28 -07:00
|
|
|
});
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
describe('#toDER', function() {
|
|
|
|
|
|
|
|
it('should return this compressed DER format', function() {
|
2014-11-21 08:26:30 -08:00
|
|
|
var x = BN.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromX(true, x);
|
2014-12-11 13:19:36 -08:00
|
|
|
pk.toDER().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return this uncompressed DER format', function() {
|
2014-12-11 13:19:36 -08:00
|
|
|
var pk = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
|
|
pk.toDER().toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|
|
|
|
|
2014-12-01 11:14:21 -08:00
|
|
|
describe('#toAddress', function() {
|
|
|
|
|
|
|
|
it('should output this known mainnet address correctly', function() {
|
|
|
|
var pk = new PublicKey('03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781');
|
2014-12-02 07:48:21 -08:00
|
|
|
var address = pk.toAddress('livenet');
|
2014-12-01 11:14:21 -08:00
|
|
|
address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should output this known testnet address correctly', function() {
|
|
|
|
var pk = new PublicKey('0293126ccc927c111b88a0fe09baa0eca719e2a3e087e8a5d1059163f5c566feef');
|
|
|
|
var address = pk.toAddress('testnet');
|
|
|
|
address.toString().should.equal('mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-01-08 14:43:59 -08:00
|
|
|
describe('hashes', function() {
|
|
|
|
|
|
|
|
// wif private key, address
|
|
|
|
// see: https://github.com/bitcoin/bitcoin/blob/master/src/test/key_tests.cpp#L20
|
|
|
|
var data = [
|
|
|
|
['5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj', '1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ'],
|
|
|
|
['5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3', '1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ'],
|
|
|
|
['Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw', '1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs'],
|
|
|
|
['L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g', '1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs']
|
|
|
|
];
|
|
|
|
|
|
|
|
data.forEach(function(d){
|
|
|
|
var publicKey = PrivateKey.fromWIF(d[0]).toPublicKey();
|
|
|
|
var address = Address.fromString(d[1]);
|
2015-01-09 07:15:48 -08:00
|
|
|
address.hashBuffer.should.deep.equal(publicKey._getID());
|
2015-01-08 14:43:59 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
describe('#toString', function() {
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
it('should print this known public key', function() {
|
|
|
|
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
2014-11-25 10:21:53 -08:00
|
|
|
var pk = PublicKey.fromString(hex);
|
2014-08-07 16:18:17 -07:00
|
|
|
pk.toString().should.equal(hex);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
describe('#inspect', function() {
|
|
|
|
it('should output known uncompressed pubkey for console', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pubkey = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
2015-01-06 15:46:08 -08:00
|
|
|
pubkey.inspect().should.equal('<PublicKey: 041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341, uncompressed>');
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should output known compressed pubkey for console', function() {
|
2014-11-25 10:21:53 -08:00
|
|
|
var pubkey = PublicKey.fromString('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
2015-01-06 15:46:08 -08:00
|
|
|
pubkey.inspect().should.equal('<PublicKey: 031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a>');
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
2014-12-16 09:25:01 -08:00
|
|
|
it('should output known compressed pubkey with network for console', function() {
|
|
|
|
var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
|
|
|
|
var pubkey = new PublicKey(privkey);
|
2015-01-06 15:46:08 -08:00
|
|
|
pubkey.inspect().should.equal('<PublicKey: 03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781>');
|
2014-12-16 09:25:01 -08:00
|
|
|
});
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
});
|
|
|
|
|
2014-08-09 17:43:24 -07:00
|
|
|
describe('#validate', function() {
|
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
it('should not have an error if pubkey is valid', function() {
|
2014-08-09 17:43:24 -07:00
|
|
|
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
2014-12-19 10:32:57 -08:00
|
|
|
expect(function() {
|
|
|
|
return PublicKey.fromString(hex);
|
|
|
|
}).to.not.throw();
|
2014-08-09 17:43:24 -07:00
|
|
|
});
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
it('should throw an error if pubkey is invalid', function() {
|
2014-08-09 17:43:24 -07:00
|
|
|
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
|
|
|
|
(function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
return PublicKey.fromString(hex);
|
2014-11-28 15:51:37 -08:00
|
|
|
}).should.throw('Invalid x,y value for curve, cannot equal 0.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if pubkey is invalid', function() {
|
|
|
|
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a00000000000000000000000000000000000000000000000000000000000000FF';
|
|
|
|
(function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
return PublicKey.fromString(hex);
|
2014-11-28 15:51:37 -08:00
|
|
|
}).should.throw('Invalid y value for curve.');
|
2014-08-09 17:43:24 -07:00
|
|
|
});
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-11-24 14:23:09 -08:00
|
|
|
it('should throw an error if pubkey is infinity', function() {
|
2014-08-09 17:43:24 -07:00
|
|
|
(function() {
|
2014-12-19 10:32:57 -08:00
|
|
|
return new PublicKey(Point.getG().mul(Point.getN()));
|
2014-08-20 13:03:07 -07:00
|
|
|
}).should.throw('Point cannot be equal to Infinity');
|
2014-08-09 17:43:24 -07:00
|
|
|
});
|
2014-11-28 15:51:37 -08:00
|
|
|
|
2014-08-09 17:43:24 -07:00
|
|
|
});
|
|
|
|
|
2014-08-07 16:18:17 -07:00
|
|
|
});
|