Browse Source

Merge pull request #112 from matiu/feature/sign-verify-browser

Feature/sign verify browser
patch-2
Ryan X. Charles 11 years ago
parent
commit
5b440901d6
  1. 87
      Key.js
  2. 35
      browser/bitcoinjs-lib.js
  3. 15
      examples/example.html
  4. 34
      test/test.Key.js

87
Key.js

@ -1,6 +1,5 @@
if (process.versions) { if (process.versions) {
// c++ native version // c++ native version
module.exports = require('bindings')('KeyModule'); module.exports = require('bindings')('KeyModule');
@ -8,29 +7,95 @@ if (process.versions) {
// pure js version // pure js version
var ECKey = require('./browser/bitcoinjs-lib.js').ECKey; var ECKey = require('./browser/bitcoinjs-lib.js').ECKey;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var kSpec = function(compressed, public, private) {
this.compressed = compressed; var bufferToArray = function(buffer) {
this.public = public; var ret = [];
this.private = private;
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() { 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 this(true, new Buffer(pub), new Buffer(eck.priv.toByteArrayUnsigned())); var ret = new kSpec();
ret.eck = eck; ret.private = new Buffer(eck.priv.toByteArrayUnsigned());
ret.public = new Buffer(pub);
return ret; return ret;
}; };
kSpec.prototype.regenerateSync = function() { kSpec.prototype.regenerateSync = function() {
this.eck = new ECKey(buffertools.toHex(this.private)); if (!this.private) {
this.eck.setCompressed(true); throw new Error('Key does not have a private key set');
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; 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 = { module.exports = {
Key: kSpec Key: kSpec
}; };

35
browser/bitcoinjs-lib.js

@ -2286,26 +2286,37 @@ ECPointFp.prototype.getEncoded = function (compressed) {
return enc; return enc;
}; };
ECPointFp.decodeFrom = function (curve, enc) { ECPointFp.decodeFrom = function (ecparams, enc) {
var type = enc[0]; var type = enc[0];
var dataLen = enc.length-1; var dataLen = enc.length-1;
// Extract x and y as byte arrays // Extract x and y as byte arrays
var xBa = enc.slice(1, 1 + dataLen/2); if (type == 4) {
var yBa = enc.slice(1 + dataLen/2, 1 + dataLen); var xBa = enc.slice(1, 1 + dataLen/2),
yBa = enc.slice(1 + dataLen/2, 1 + dataLen),
// Prepend zero byte to prevent interpretation as negative integer x = BigInteger.fromByteArrayUnsigned(xBa),
xBa.unshift(0); y = BigInteger.fromByteArrayUnsigned(yBa);
yBa.unshift(0); }
else {
// Convert to BigIntegers var xBa = enc.slice(1),
var x = new BigInteger(xBa); x = BigInteger.fromByteArrayUnsigned(xBa),
var y = new BigInteger(yBa); 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 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) { ECPointFp.prototype.add2D = function (b) {
if(this.isInfinity()) return b; if(this.isInfinity()) return b;
if(b.isInfinity()) return this; if(b.isInfinity()) return this;

15
examples/example.html

@ -11,7 +11,7 @@
<script type="text/javascript"> <script type="text/javascript">
console.log = function(s){ print = function(s){
var div = document.getElementById('content'); var div = document.getElementById('content');
div.innerHTML += s + '<br />'; div.innerHTML += s + '<br />';
}; };
@ -31,13 +31,22 @@
try { try {
addr.validate(); addr.validate();
console.log(addr.data + ": is a valid address"); print(addr.data + ": is a valid address");
} catch(e) { } 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> </script>
</body> </body>
</html> </html>

34
test/test.Key.js

@ -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);
@ -55,7 +56,40 @@ describe('Key', function() {
buffertools.toHex(k.private).should.equal(pkshex); buffertools.toHex(k.private).should.equal(pkshex);
buffertools.toHex(k.public).should.equal(pubhex); 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…
Cancel
Save