diff --git a/lib/browser/Key.js b/lib/browser/Key.js index d008d3c..f824939 100644 --- a/lib/browser/Key.js +++ b/lib/browser/Key.js @@ -2,6 +2,7 @@ var ECKey = require('../../browser/vendor-bundle.js').ECKey; var SecureRandom = require('../SecureRandom'); var Curve = require('../Curve'); var bignum = require('bignum'); +var elliptic = require('elliptic'); var Key = function() { this._pub = null; @@ -85,9 +86,17 @@ Key.prototype.regenerateSync = function() { throw new Error('Key does not have a private key set'); } - var eck = new ECKey(this.private.toString('hex')); - eck.setCompressed(this._compressed); - this._pub = new Buffer(eck.getPub()); + var ec = elliptic.curves.secp256k1; + var g = ec.g; + var ecp = ec.g.mul(this.private); + var x = new bignum(ecp.x.toArray()); + var y = new bignum(ecp.y.toArray()); + var p = new Point(x, y); + if (this.compressed) + this._pub = p.toCompressedPubKey(); + else + this._pub = p.toUncompressedPubKey(); + return this; }; diff --git a/lib/browser/Point.js b/lib/browser/Point.js index b70b532..b1482b9 100644 --- a/lib/browser/Point.js +++ b/lib/browser/Point.js @@ -55,4 +55,16 @@ Point.prototype.toUncompressedPubKey = function() { return pub; }; +Point.prototype.toCompressedPubKey = function() { + var xbuf = this.x.toBuffer({size: 32}); + var ybuf = this.y.toBuffer({size: 32}); + if (ybuf[ybuf.length-1] % 2) { //odd + var pub = Buffer.concat([new Buffer([3]), xbuf]); + } + else { //even + var pub = Buffer.concat([new Buffer([2]), xbuf]); + } + return pub; +}; + module.exports = (Point);