2013-07-10 17:07:14 -07:00
|
|
|
|
2014-02-20 13:59:41 -08:00
|
|
|
|
|
|
|
if (process.versions) {
|
|
|
|
// c++ native version
|
|
|
|
module.exports = require('bindings')('KeyModule');
|
|
|
|
} else {
|
|
|
|
// pure js version
|
2014-03-11 11:29:06 -07:00
|
|
|
var ECKey = require('./browser/vendor-bundle.js').ECKey;
|
2014-02-21 10:19:38 -08:00
|
|
|
var buffertools = require('buffertools');
|
2014-03-03 22:23:42 -08:00
|
|
|
|
|
|
|
var bufferToArray = function(buffer) {
|
|
|
|
var ret = [];
|
|
|
|
|
|
|
|
var l = buffer.length;
|
|
|
|
for(var i =0; i<l; i++) {
|
|
|
|
ret.push(buffer.readUInt8(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-04 12:45:27 -08:00
|
|
|
var kSpec = function() {
|
|
|
|
this._pub = null;
|
2014-03-12 19:20:46 -07:00
|
|
|
this.compressed = true; // default
|
2014-02-20 13:59:41 -08:00
|
|
|
};
|
|
|
|
|
2014-03-04 12:45:27 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-20 13:59:41 -08:00
|
|
|
kSpec.generateSync = function() {
|
|
|
|
var eck = new ECKey();
|
|
|
|
eck.setCompressed(true);
|
|
|
|
var pub = eck.getPub();
|
2014-03-04 12:45:27 -08:00
|
|
|
|
|
|
|
var ret = new kSpec();
|
|
|
|
ret.private = new Buffer(eck.priv.toByteArrayUnsigned());
|
|
|
|
ret.public = new Buffer(pub);
|
2014-02-21 10:19:38 -08:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
kSpec.prototype.regenerateSync = function() {
|
2014-03-04 12:45:27 -08:00
|
|
|
if (!this.private) {
|
|
|
|
throw new Error('Key does not have a private key set');
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:23:42 -08:00
|
|
|
var eck = new ECKey(buffertools.toHex(this.private));
|
|
|
|
eck.setCompressed(this.compressed);
|
|
|
|
this.public = new Buffer(eck.getPub());
|
2014-02-21 10:19:38 -08:00
|
|
|
return this;
|
2014-02-20 13:59:41 -08:00
|
|
|
};
|
2014-02-21 10:19:38 -08:00
|
|
|
|
2014-03-03 22:23:42 -08:00
|
|
|
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) {
|
2014-03-04 12:45:27 -08:00
|
|
|
throw new Error('Arg should be a 32 bytes hash buffer');
|
2014-03-03 22:23:42 -08:00
|
|
|
}
|
|
|
|
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();
|
2014-03-11 11:21:24 -07:00
|
|
|
eck.setPub(bufferToArray(self.public));
|
2014-03-03 22:23:42 -08:00
|
|
|
eck.setCompressed(self.compressed);
|
|
|
|
var sigA = bufferToArray(sig);
|
2014-03-11 11:21:24 -07:00
|
|
|
var ret = eck.verify(hash,sigA);
|
|
|
|
return ret;
|
2014-03-03 22:23:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-20 13:59:41 -08:00
|
|
|
module.exports = {
|
|
|
|
Key: kSpec
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:07:14 -07:00
|
|
|
|