Merge pull request #112 from matiu/feature/sign-verify-browser
Feature/sign verify browser
This commit is contained in:
commit
5b440901d6
87
Key.js
87
Key.js
@ -1,6 +1,5 @@
|
||||
|
||||
|
||||
|
||||
if (process.versions) {
|
||||
// c++ native version
|
||||
module.exports = require('bindings')('KeyModule');
|
||||
@ -8,29 +7,95 @@ if (process.versions) {
|
||||
// pure js version
|
||||
var ECKey = require('./browser/bitcoinjs-lib.js').ECKey;
|
||||
var buffertools = require('buffertools');
|
||||
var kSpec = function(compressed, public, private) {
|
||||
this.compressed = compressed;
|
||||
this.public = public;
|
||||
this.private = private;
|
||||
|
||||
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() {
|
||||
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 this(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() {
|
||||
this.eck = new ECKey(buffertools.toHex(this.private));
|
||||
this.eck.setCompressed(true);
|
||||
this.public = new Buffer(this.eck.getPub());
|
||||
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());
|
||||
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 buffer');
|
||||
}
|
||||
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
|
||||
};
|
||||
|
||||
@ -2286,26 +2286,37 @@ ECPointFp.prototype.getEncoded = function (compressed) {
|
||||
return enc;
|
||||
};
|
||||
|
||||
ECPointFp.decodeFrom = function (curve, enc) {
|
||||
ECPointFp.decodeFrom = function (ecparams, enc) {
|
||||
var type = enc[0];
|
||||
var dataLen = enc.length-1;
|
||||
|
||||
// Extract x and y as byte arrays
|
||||
var xBa = enc.slice(1, 1 + dataLen/2);
|
||||
var yBa = enc.slice(1 + dataLen/2, 1 + dataLen);
|
||||
|
||||
// Prepend zero byte to prevent interpretation as negative integer
|
||||
xBa.unshift(0);
|
||||
yBa.unshift(0);
|
||||
|
||||
// Convert to BigIntegers
|
||||
var x = new BigInteger(xBa);
|
||||
var y = new BigInteger(yBa);
|
||||
if (type == 4) {
|
||||
var xBa = enc.slice(1, 1 + dataLen/2),
|
||||
yBa = enc.slice(1 + dataLen/2, 1 + dataLen),
|
||||
x = BigInteger.fromByteArrayUnsigned(xBa),
|
||||
y = BigInteger.fromByteArrayUnsigned(yBa);
|
||||
}
|
||||
else {
|
||||
var xBa = enc.slice(1),
|
||||
x = BigInteger.fromByteArrayUnsigned(xBa),
|
||||
p = ecparams.getQ(),
|
||||
xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p),
|
||||
pPlus1Over4 = p.add(new BigInteger('1'))
|
||||
.divide(new BigInteger('4')),
|
||||
y = xCubedPlus7.modPow(pPlus1Over4,p);
|
||||
if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) {
|
||||
y = p.subtract(y)
|
||||
}
|
||||
}
|
||||
|
||||
// Return point
|
||||
return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y));
|
||||
return new ECPointFp(ecparams,
|
||||
ecparams.fromBigInteger(x),
|
||||
ecparams.fromBigInteger(y));
|
||||
};
|
||||
|
||||
|
||||
ECPointFp.prototype.add2D = function (b) {
|
||||
if(this.isInfinity()) return b;
|
||||
if(b.isInfinity()) return this;
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
console.log = function(s){
|
||||
print = function(s){
|
||||
var div = document.getElementById('content');
|
||||
div.innerHTML += s + '<br />';
|
||||
};
|
||||
@ -31,13 +31,22 @@
|
||||
|
||||
try {
|
||||
addr.validate();
|
||||
console.log(addr.data + ": is a valid address");
|
||||
print(addr.data + ": is a valid address");
|
||||
} catch(e) {
|
||||
console.log(addr.data + ": is not a valid address.");
|
||||
print(addr.data + ": is not a valid address.");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
var Key = bitcore.KeyModule.Key;
|
||||
var k = Key.generateSync();
|
||||
|
||||
print ('Generated Key Pair:');
|
||||
print ('Private:' + bitcore.buffertools.toHex(k.private));
|
||||
print ('Public:' + bitcore.buffertools.toHex(k.public));
|
||||
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -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);
|
||||
@ -55,7 +56,40 @@ 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();
|
||||
k2.public = pub;
|
||||
var ret= k2.verifySignatureSync(a_hash, sig);
|
||||
ret.should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user