add support for Sign and Verify in Key for Browser. Tests added

This commit is contained in:
Matias Alejo Garcia 2014-03-04 03:23:42 -03:00
parent e1e30c5f7c
commit 8f61401099
2 changed files with 84 additions and 6 deletions

58
Key.js
View File

@ -1,6 +1,5 @@
if (process.versions) {
// c++ native version
module.exports = require('bindings')('KeyModule');
@ -8,6 +7,18 @@ if (process.versions) {
// pure js version
var ECKey = require('./browser/bitcoinjs-lib.js').ECKey;
var buffertools = require('buffertools');
var bufferToArray = function(buffer) {
var ret = [];
var l = buffer.length;
for(var i =0; i<l; i++) {
ret.push(buffer.readUInt8(i));
}
return ret;
}
var kSpec = function(compressed, public, private) {
this.compressed = compressed;
this.public = public;
@ -18,19 +29,54 @@ if (process.versions) {
var eck = new ECKey();
eck.setCompressed(true);
var pub = eck.getPub();
var ret = new this(true, new Buffer(pub), new Buffer(eck.priv.toByteArrayUnsigned()));
var ret = new kSpec(true, new Buffer(pub), new Buffer(eck.priv.toByteArrayUnsigned()));
ret.eck = eck;
return ret;
};
kSpec.prototype.regenerateSync = function() {
this.eck = new ECKey(buffertools.toHex(this.private));
this.eck.setCompressed(true);
this.public = new Buffer(this.eck.getPub());
var eck = new ECKey(buffertools.toHex(this.private));
eck.setCompressed(this.compressed);
this.public = new Buffer(eck.getPub());
return this;
};
kSpec.prototype.signSync = function(hash) {
if (!this.private) {
throw new Error('Key does not have a private key set');
}
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
throw new Error('Arg should be a 32 bytes hash');
}
var eck = new ECKey(buffertools.toHex(this.private));
eck.setCompressed(this.compressed);
var signature = eck.sign(hash);
// return it as a buffer to keep c++ compatibility
return new Buffer(signature);
};
kSpec.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);
return eck.verify(hash,sigA);
};
module.exports = {
Key: kSpec
};

View File

@ -55,7 +55,39 @@ describe('Key', function() {
buffertools.toHex(k.private).should.equal(pkshex);
buffertools.toHex(k.public).should.equal(pubhex);
});
it('should not fail checking good signSync status', function() {
var k = Key.generateSync();
var b = new Buffer(32);
k.signSync.bind(k,b).should.not.Throw(Error);
});
it('should fail checking bad signSync params', function() {
var k = Key.generateSync();
k.signSync.bind(k,'1').should.Throw(Error);
k.signSync.bind(k,new Buffer(10)).should.Throw(Error);
k.signSync.bind(k,new Buffer(32)).should.not.Throw(Error);
});
var a_hash = buffertools.fromHex(new Buffer('1122334455667788990011223344556677889900112233445566778899001122'));
it('should create a signature without failling', function() {
var k = Key.generateSync();
var pkshex = 'b7dafe35d7d1aab78b53982c8ba554584518f86d50af565c98e053613c8f15e0';
k.private = buffertools.fromHex(new Buffer(pkshex));
k.regenerateSync();
k.compressed.should.be.ok;
buffertools.toHex(k.private).should.equal(pkshex);
k.signSync.bind(k,a_hash).should.not.Throw(Error);
});
it('roundtrip for signature/verify', function() {
var k = Key.generateSync();
var pub = k.public;
// sign
var sig = k.signSync(a_hash);
//
// checks sig. priv unknown.
var k2 = new Key(true, pub);
var ret= k2.verifySignatureSync(a_hash, sig);
ret.should.equal(true);
});
});