You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
5.1 KiB
135 lines
5.1 KiB
var should = require('chai').should();
|
|
var Pubkey = require('../lib/pubkey');
|
|
var Point = require('../lib/point');
|
|
var Bn = require('../lib/bn');
|
|
|
|
describe('Pubkey', function() {
|
|
|
|
it('should create a blank public key', function() {
|
|
var pk = new Pubkey();
|
|
should.exist(pk);
|
|
});
|
|
|
|
it('should create a public key with a point', function() {
|
|
var p = Point();
|
|
var pk = new Pubkey(p);
|
|
should.exist(pk.point);
|
|
});
|
|
|
|
describe('#fromDER', function() {
|
|
|
|
it('should parse this uncompressed public key', function() {
|
|
var pk = new Pubkey();
|
|
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
});
|
|
|
|
it('should parse this compressed public key', function() {
|
|
var pk = new Pubkey();
|
|
pk.fromDER(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
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() {
|
|
var pk = new Pubkey();
|
|
(function() {
|
|
pk.fromDER(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
}).should.throw();
|
|
});
|
|
|
|
});
|
|
|
|
describe('#fromString', function() {
|
|
|
|
it('should parse this known valid public key', function() {
|
|
pk = new Pubkey();
|
|
pk.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
});
|
|
|
|
});
|
|
|
|
describe('#fromX', function() {
|
|
|
|
it('should create this known public key', function() {
|
|
var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
var pk = new Pubkey();
|
|
pk.fromX(true, x);
|
|
pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
});
|
|
|
|
});
|
|
|
|
describe('#toBuffer', function() {
|
|
|
|
it('should return this compressed DER format', function() {
|
|
var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
var pk = new Pubkey();
|
|
pk.fromX(true, x);
|
|
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
});
|
|
|
|
});
|
|
|
|
describe('#toDER', function() {
|
|
|
|
it('should return this compressed DER format', function() {
|
|
var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
var pk = new Pubkey();
|
|
pk.fromX(true, x);
|
|
pk.toDER(true).toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
|
|
});
|
|
|
|
it('should return this uncompressed DER format', function() {
|
|
var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
|
|
var pk = new Pubkey();
|
|
pk.fromX(true, x);
|
|
pk.toDER(false).toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
|
|
});
|
|
|
|
});
|
|
|
|
describe('#toString', function() {
|
|
|
|
it('should print this known public key', function() {
|
|
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
|
var pk = new Pubkey();
|
|
pk.fromString(hex);
|
|
pk.toString().should.equal(hex);
|
|
});
|
|
|
|
});
|
|
|
|
describe('#validate', function() {
|
|
|
|
it('should not throw an error if pubkey is valid', function() {
|
|
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
|
|
var pk = new Pubkey();
|
|
pk.fromString(hex);
|
|
should.exist(pk.validate());
|
|
});
|
|
|
|
it('should not throw an error if pubkey is invalid', function() {
|
|
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
|
|
var pk = new Pubkey();
|
|
pk.fromString(hex);
|
|
(function() {
|
|
pk.validate();
|
|
}).should.throw('Invalid y value of public key');
|
|
});
|
|
|
|
it('should not throw an error if pubkey is infinity', function() {
|
|
var pk = new Pubkey();
|
|
pk.point = Point.getG().mul(Point.getN());
|
|
(function() {
|
|
pk.validate();
|
|
}).should.throw('Point cannot be equal to Infinity');
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|