Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

publickey: read from buffer with additional data #89

Open
wants to merge 2 commits into
base: 1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,17 @@ PublicKey._transformDER = function(buf, strict) {
if (buf[0] === 0x04 || (!strict && (buf[0] === 0x06 || buf[0] === 0x07))) {
xbuf = buf.slice(1, 33);
ybuf = buf.slice(33, 65);
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
throw new TypeError('Length of x and y must be 32 bytes');
}
x = new BN(xbuf);
y = new BN(ybuf);
info.point = new Point(x, y);
info.compressed = false;
} else if (buf[0] === 0x03) {
xbuf = buf.slice(1);
xbuf = buf.slice(1, 33);
x = new BN(xbuf);
info = PublicKey._transformX(true, x);
info.compressed = true;
} else if (buf[0] === 0x02) {
xbuf = buf.slice(1);
xbuf = buf.slice(1, 33);
x = new BN(xbuf);
info = PublicKey._transformX(false, x);
info.compressed = true;
Expand Down
18 changes: 15 additions & 3 deletions test/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ describe('PublicKey', function() {
pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
});

it('should parse this uncompressed public key buffer with additional data at the end of the buffer', function() {
var buf = new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a34126221e5b4e948b99ff08', 'hex');
var pk = PublicKey.fromDER(buf);
pk.toDER().toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
});

it('should parse this compressed public key buffer with additional data at the end of the buffer', function() {
var buf = new Buffer('0333f66380de6fffb380322d3f687feef412cecc388b98cd34083548f18720a6cfbfbc1999136518c531759db7', 'hex');
var pk = PublicKey.fromDER(buf);
pk.toDER().toString('hex').should.equal('0333f66380de6fffb380322d3f687feef412cecc388b98cd34083548f18720a6cf');
});

it('should throw an error on this invalid public key', function() {
(function() {
PublicKey.fromBuffer(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
Expand All @@ -243,10 +255,10 @@ describe('PublicKey', function() {
}).should.throw('Must be a hex buffer of DER encoded public key');
});

it('should throw error because buffer is the incorrect length', function() {
it('should throw error because point not on curve (and with longer buffer)', function() {
(function() {
PublicKey.fromBuffer(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a34112', 'hex'));
}).should.throw('Length of x and y must be 32 bytes');
PublicKey.fromBuffer(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a342000000101010', 'hex'));
}).should.throw('Invalid y value for curve.');
});

});
Expand Down