remove constructor params in browser version of KeyModule

This commit is contained in:
Matias Alejo Garcia 2014-03-04 17:45:27 -03:00
parent 948f4bb283
commit cfc3ca35d4
2 changed files with 30 additions and 9 deletions

33
Key.js
View File

@ -19,22 +19,41 @@ if (process.versions) {
return ret;
}
var kSpec = function(compressed, public, private) {
this.compressed = compressed;
this.public = public;
this.private = private;
var kSpec = function() {
this._pub = null;
};
Object.defineProperty(kSpec.prototype, 'public', {
set: function(p){
if (!Buffer.isBuffer(p) ) {
throw new Error('Arg should be a buffer');
}
var type = p[0];
this.compressed = type!==4;
this._pub = p;
},
get: function(){
return this._pub;
}
});
kSpec.generateSync = function() {
var eck = new ECKey();
eck.setCompressed(true);
var pub = eck.getPub();
var ret = new kSpec(true, new Buffer(pub), new Buffer(eck.priv.toByteArrayUnsigned()));
ret.eck = eck;
var ret = new kSpec();
ret.private = new Buffer(eck.priv.toByteArrayUnsigned());
ret.public = new Buffer(pub);
return ret;
};
kSpec.prototype.regenerateSync = function() {
if (!this.private) {
throw new Error('Key does not have a private key set');
}
var eck = new ECKey(buffertools.toHex(this.private));
eck.setCompressed(this.compressed);
this.public = new Buffer(eck.getPub());
@ -47,7 +66,7 @@ if (process.versions) {
}
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
throw new Error('Arg should be a 32 bytes hash');
throw new Error('Arg should be a 32 bytes hash buffer');
}
var eck = new ECKey(buffertools.toHex(this.private));
eck.setCompressed(this.compressed);

View File

@ -17,6 +17,7 @@ describe('Key', function() {
Key = KeyModule.Key;
should.exist(Key);
});
Key = KeyModule.Key;
it('should be able to create instance', function() {
var k = new Key();
should.exist(k);
@ -81,12 +82,13 @@ describe('Key', function() {
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 k2 = new Key();
k2.public = pub;
var ret= k2.verifySignatureSync(a_hash, sig);
ret.should.equal(true);
});