diff --git a/lib/key.js b/lib/key.js index a73bae2..d7baf4a 100644 --- a/lib/key.js +++ b/lib/key.js @@ -40,7 +40,7 @@ Key.prototype.getAddress = function(networkstr) { }; Key.prototype.privkey2pubkey = function() { - this.pubkey = new Pubkey({point: point.getG().mul(this.privkey.bn), compressed: this.privkey.compressed}); + this.pubkey = Pubkey().fromPrivkey(this.privkey); }; Key.prototype.toString = function() { diff --git a/lib/privkey.js b/lib/privkey.js index d04972a..b1dd12c 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -1,5 +1,5 @@ var BN = require('./bn'); -var point = require('./point'); +var Point = require('./point'); var constants = require('./constants'); var base58check = require('./base58check'); var Random = require('./random'); @@ -22,14 +22,14 @@ Privkey.prototype.fromRandom = function() { do { var privbuf = Random.getRandomBuffer(32); var bn = BN().fromBuffer(privbuf); - var condition = bn.lt(point.getN()); + var condition = bn.lt(Point.getN()); } while (!condition); this.bn = bn; return this; }; Privkey.prototype.validate = function() { - if (!this.bn.lt(point.getN())) + if (!this.bn.lt(Point.getN())) throw new Error('Number must be less than N'); if (typeof constants[this.networkstr] === undefined) throw new Error('Must specify the networkstr ("mainnet" or "testnet")'); diff --git a/lib/pubkey.js b/lib/pubkey.js index b4ffa5f..4642aa8 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -1,5 +1,6 @@ var Point = require('./point'); var bn = require('./bn'); +var privkey = require('./privkey'); var Pubkey = function Pubkey(obj) { if (!(this instanceof Pubkey)) @@ -16,6 +17,14 @@ Pubkey.prototype.set = function(obj) { return this; }; +Pubkey.prototype.fromPrivkey = function(privkey) { + this.set({ + point: Point.getG().mul(privkey.bn), + compressed: privkey.compressed} + ); + return this; +}; + Pubkey.prototype.fromDER = function(buf) { if (buf[0] == 0x04) { var xbuf = buf.slice(1, 33); diff --git a/test/test.pubkey.js b/test/test.pubkey.js index 3510b6d..efee3b6 100644 --- a/test/test.pubkey.js +++ b/test/test.pubkey.js @@ -2,6 +2,7 @@ var should = require('chai').should(); var Pubkey = require('../lib/pubkey'); var Point = require('../lib/point'); var Bn = require('../lib/bn'); +var Privkey = require('../lib/privkey'); describe('Pubkey', function() { @@ -24,6 +25,14 @@ describe('Pubkey', function() { }); + describe('#fromPrivkey', function() { + + it('should make a public key from a privkey', function() { + should.exist(Pubkey().fromPrivkey(Privkey().fromRandom())); + }); + + }); + describe('#fromDER', function() { it('should parse this uncompressed public key', function() {