diff --git a/lib/browser/Key.js b/lib/browser/Key.js index 73d8716..08425e1 100644 --- a/lib/browser/Key.js +++ b/lib/browser/Key.js @@ -1,6 +1,4 @@ -var ECKey = require('../../browser/vendor-bundle.js').ECKey; var SecureRandom = require('../SecureRandom'); -var Curve = require('../Curve'); var bignum = require('bignum'); var elliptic = require('elliptic'); @@ -42,10 +40,24 @@ Object.defineProperty(Key.prototype, 'compressed', { return; var oldp = this._pub; if (this._pub) { - var eckey = new ECKey(); - eckey.setPub(bufferToArray(this.public)); - eckey.setCompressed(this._compressed); - this._pub = new Buffer(eckey.getPub()); + if (this._compressed) { + var xbuf = this._pub.slice(1, 33); + var ybuf = this._pub.slice(33, 65); + var x = new bignum(xbuf); + var y = new bignum(ybuf); + var p = new Point(x, y); + this._pub = p.toCompressedPubKey(); + } else { + var ec = elliptic.curves.secp256k1; + var xbuf = this._pub.slice(1, 33); + var odd = this._pub[0] == 3 ? true : false; + var p = ec.curve.pointFromX(odd, xbuf); + var ybuf = new Buffer(p.y.toArray()); + var xb = new bignum(xbuf); + var yb = new bignum(ybuf); + var pb = new Point(xb, yb); + this._pub = pb.toUncompressedPubKey(); + } } if (!this._compressed) { //bug in eckey @@ -60,25 +72,19 @@ Object.defineProperty(Key.prototype, 'compressed', { Key.generateSync = function() { var privbuf; + var ec = elliptic.curves.secp256k1; while (true) { privbuf = SecureRandom.getRandomBuffer(32); if ((bignum.fromBuffer(privbuf, { size: 32 - })).cmp(Curve.getN()) < 0) + })).cmp(ec.n) < 0) break; } - var privhex = privbuf.toString('hex'); - var eck = new ECKey(privhex); - eck.setCompressed(true); - var pub = eck.getPub(); - - ret = new Key(); - ret.private = privbuf; - ret._compressed = true; - ret.public = new Buffer(eck.getPub()); - - return ret; + var key = new Key(); + key.private = privbuf; + key.regenerateSync(); + return key; }; Key.prototype.regenerateSync = function() { @@ -101,12 +107,6 @@ Key.prototype.regenerateSync = function() { }; Key.prototype.signSync = function(hash) { - /* - var getSECCurveByName = require('../../browser/vendor-bundle.js').getSECCurveByName; - var BigInteger = require('../../browser/vendor-bundle.js').BigInteger; - var rng = new SecureRandom(); - var ecparams = getSECCurveByName('secp256k1'); - */ var ec = elliptic.curves.secp256k1; var genk = function() { @@ -116,9 +116,7 @@ Key.prototype.signSync = function(hash) { var sign = function(hash, priv) { var d = priv; - //var n = ecparams.getN(); var n = ec.n; - //var e = BigInteger.fromByteArrayUnsigned(hash); var e = new bignum(hash); do { @@ -158,10 +156,7 @@ Key.prototype.signSync = function(hash) { if (!Buffer.isBuffer(hash) || hash.length !== 32) { throw new Error('Arg should be a 32 bytes hash buffer'); } - //var privhex = this.private.toString('hex'); - //var privnum = new BigInteger(privhex, 16); var privnum = new bignum(this.private); - //var signature = sign(bufferToArray(hash), privnum); var signature = sign(hash, privnum); return new Buffer(signature); @@ -177,24 +172,12 @@ Key.prototype.verifySignature = function(hash, sig, callback) { }; Key.prototype.verifySignatureSync = function(hash, sig) { - var self = this; - - if (!Buffer.isBuffer(hash) || hash.length !== 32) { - throw new Error('Arg 1 should be a 32 bytes hash buffer'); - } - if (!Buffer.isBuffer(sig)) { - throw new Error('Arg 2 should be a buffer'); - } - if (!self.public) { - throw new Error('Key does not have a public key set'); - } - - var eck = new ECKey(); - eck.setPub(bufferToArray(self.public)); - eck.setCompressed(self._compressed); - var sigA = bufferToArray(sig); - var ret = eck.verify(bufferToArray(hash), sigA); - return ret; + var ec = new elliptic.ec(elliptic.curves.secp256k1); + var msg = hash.toString('hex'); + var pub = this._pub.toString('hex'); + var sig = sig.toString('hex'); + var v = ec.verify(msg, sig, pub); + return v; }; module.exports = Key;