remove cryptojs dependency from Key

This commit is contained in:
Ryan X. Charles 2014-07-05 17:11:13 -07:00
parent 9ca869b95b
commit 204d8563c8
1 changed files with 30 additions and 47 deletions

View File

@ -1,6 +1,4 @@
var ECKey = require('../../browser/vendor-bundle.js').ECKey;
var SecureRandom = require('../SecureRandom'); var SecureRandom = require('../SecureRandom');
var Curve = require('../Curve');
var bignum = require('bignum'); var bignum = require('bignum');
var elliptic = require('elliptic'); var elliptic = require('elliptic');
@ -42,10 +40,24 @@ Object.defineProperty(Key.prototype, 'compressed', {
return; return;
var oldp = this._pub; var oldp = this._pub;
if (this._pub) { if (this._pub) {
var eckey = new ECKey(); if (this._compressed) {
eckey.setPub(bufferToArray(this.public)); var xbuf = this._pub.slice(1, 33);
eckey.setCompressed(this._compressed); var ybuf = this._pub.slice(33, 65);
this._pub = new Buffer(eckey.getPub()); 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) { if (!this._compressed) {
//bug in eckey //bug in eckey
@ -60,25 +72,19 @@ Object.defineProperty(Key.prototype, 'compressed', {
Key.generateSync = function() { Key.generateSync = function() {
var privbuf; var privbuf;
var ec = elliptic.curves.secp256k1;
while (true) { while (true) {
privbuf = SecureRandom.getRandomBuffer(32); privbuf = SecureRandom.getRandomBuffer(32);
if ((bignum.fromBuffer(privbuf, { if ((bignum.fromBuffer(privbuf, {
size: 32 size: 32
})).cmp(Curve.getN()) < 0) })).cmp(ec.n) < 0)
break; break;
} }
var privhex = privbuf.toString('hex'); var key = new Key();
var eck = new ECKey(privhex); key.private = privbuf;
eck.setCompressed(true); key.regenerateSync();
var pub = eck.getPub(); return key;
ret = new Key();
ret.private = privbuf;
ret._compressed = true;
ret.public = new Buffer(eck.getPub());
return ret;
}; };
Key.prototype.regenerateSync = function() { Key.prototype.regenerateSync = function() {
@ -101,12 +107,6 @@ Key.prototype.regenerateSync = function() {
}; };
Key.prototype.signSync = function(hash) { 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 ec = elliptic.curves.secp256k1;
var genk = function() { var genk = function() {
@ -116,9 +116,7 @@ Key.prototype.signSync = function(hash) {
var sign = function(hash, priv) { var sign = function(hash, priv) {
var d = priv; var d = priv;
//var n = ecparams.getN();
var n = ec.n; var n = ec.n;
//var e = BigInteger.fromByteArrayUnsigned(hash);
var e = new bignum(hash); var e = new bignum(hash);
do { do {
@ -158,10 +156,7 @@ Key.prototype.signSync = function(hash) {
if (!Buffer.isBuffer(hash) || hash.length !== 32) { if (!Buffer.isBuffer(hash) || hash.length !== 32) {
throw new Error('Arg should be a 32 bytes hash buffer'); 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 privnum = new bignum(this.private);
//var signature = sign(bufferToArray(hash), privnum);
var signature = sign(hash, privnum); var signature = sign(hash, privnum);
return new Buffer(signature); return new Buffer(signature);
@ -177,24 +172,12 @@ Key.prototype.verifySignature = function(hash, sig, callback) {
}; };
Key.prototype.verifySignatureSync = function(hash, sig) { Key.prototype.verifySignatureSync = function(hash, sig) {
var self = this; var ec = new elliptic.ec(elliptic.curves.secp256k1);
var msg = hash.toString('hex');
if (!Buffer.isBuffer(hash) || hash.length !== 32) { var pub = this._pub.toString('hex');
throw new Error('Arg 1 should be a 32 bytes hash buffer'); var sig = sig.toString('hex');
} var v = ec.verify(msg, sig, pub);
if (!Buffer.isBuffer(sig)) { return v;
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;
}; };
module.exports = Key; module.exports = Key;