remove constructor params in browser version of KeyModule
This commit is contained in:
parent
948f4bb283
commit
cfc3ca35d4
33
Key.js
33
Key.js
|
@ -19,22 +19,41 @@ if (process.versions) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
var kSpec = function(compressed, public, private) {
|
var kSpec = function() {
|
||||||
this.compressed = compressed;
|
this._pub = null;
|
||||||
this.public = public;
|
|
||||||
this.private = private;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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() {
|
kSpec.generateSync = function() {
|
||||||
var eck = new ECKey();
|
var eck = new ECKey();
|
||||||
eck.setCompressed(true);
|
eck.setCompressed(true);
|
||||||
var pub = eck.getPub();
|
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;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
kSpec.prototype.regenerateSync = function() {
|
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));
|
var eck = new ECKey(buffertools.toHex(this.private));
|
||||||
eck.setCompressed(this.compressed);
|
eck.setCompressed(this.compressed);
|
||||||
this.public = new Buffer(eck.getPub());
|
this.public = new Buffer(eck.getPub());
|
||||||
|
@ -47,7 +66,7 @@ if (process.versions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
|
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));
|
var eck = new ECKey(buffertools.toHex(this.private));
|
||||||
eck.setCompressed(this.compressed);
|
eck.setCompressed(this.compressed);
|
||||||
|
|
|
@ -17,6 +17,7 @@ describe('Key', function() {
|
||||||
Key = KeyModule.Key;
|
Key = KeyModule.Key;
|
||||||
should.exist(Key);
|
should.exist(Key);
|
||||||
});
|
});
|
||||||
|
Key = KeyModule.Key;
|
||||||
it('should be able to create instance', function() {
|
it('should be able to create instance', function() {
|
||||||
var k = new Key();
|
var k = new Key();
|
||||||
should.exist(k);
|
should.exist(k);
|
||||||
|
@ -81,12 +82,13 @@ describe('Key', function() {
|
||||||
it('roundtrip for signature/verify', function() {
|
it('roundtrip for signature/verify', function() {
|
||||||
var k = Key.generateSync();
|
var k = Key.generateSync();
|
||||||
var pub = k.public;
|
var pub = k.public;
|
||||||
|
|
||||||
// sign
|
// sign
|
||||||
var sig = k.signSync(a_hash);
|
var sig = k.signSync(a_hash);
|
||||||
//
|
|
||||||
|
|
||||||
// checks sig. priv unknown.
|
// checks sig. priv unknown.
|
||||||
var k2 = new Key(true, pub);
|
var k2 = new Key();
|
||||||
|
k2.public = pub;
|
||||||
var ret= k2.verifySignatureSync(a_hash, sig);
|
var ret= k2.verifySignatureSync(a_hash, sig);
|
||||||
ret.should.equal(true);
|
ret.should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue