From 543762bc671c397b92aa609929ad9a71131ac0e9 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 15 Sep 2014 11:32:39 -0700 Subject: [PATCH] fromBuffer convenience function for pubkey --- lib/pubkey.js | 4 ++++ test/pubkey.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/pubkey.js b/lib/pubkey.js index a0e0c99..4996247 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -29,6 +29,10 @@ Pubkey.prototype.fromPrivkey = function(privkey) { return this; }; +Pubkey.prototype.fromBuffer = function(buf) { + return this.fromDER(buf); +}; + Pubkey.prototype.fromDER = function(buf) { if (buf[0] == 0x04) { var xbuf = buf.slice(1, 33); diff --git a/test/pubkey.js b/test/pubkey.js index 03e7534..f3e3c1f 100644 --- a/test/pubkey.js +++ b/test/pubkey.js @@ -40,6 +40,31 @@ describe('Pubkey', function() { }); + describe('#fromBuffer', function() { + + it('should parse this uncompressed public key', function() { + var pk = new Pubkey(); + pk.fromBuffer(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.fromBuffer(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.fromBuffer(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); + }).should.throw(); + }); + + }); + describe('#fromDER', function() { it('should parse this uncompressed public key', function() {