From 5acb847e53ef38b64ca5ac5db74818b797355aa6 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 4 Mar 2014 02:52:17 -0300 Subject: [PATCH 1/5] support for compressed pub key import --- browser/bitcoinjs-lib.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/browser/bitcoinjs-lib.js b/browser/bitcoinjs-lib.js index e91216b..2da8ab0 100644 --- a/browser/bitcoinjs-lib.js +++ b/browser/bitcoinjs-lib.js @@ -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; From e1e30c5f7c067ba671766356f54bd5356936f882 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 4 Mar 2014 03:22:18 -0300 Subject: [PATCH 2/5] change name to print function --- examples/example.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/example.html b/examples/example.html index c971798..41d819c 100644 --- a/examples/example.html +++ b/examples/example.html @@ -11,7 +11,7 @@ From cfc3ca35d47a9ab1c651f363332ed7a828085315 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 4 Mar 2014 17:45:27 -0300 Subject: [PATCH 5/5] remove constructor params in browser version of KeyModule --- Key.js | 33 ++++++++++++++++++++++++++------- test/test.Key.js | 6 ++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Key.js b/Key.js index 21f5086..550c40f 100644 --- a/Key.js +++ b/Key.js @@ -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); diff --git a/test/test.Key.js b/test/test.Key.js index f2cd03d..975964d 100644 --- a/test/test.Key.js +++ b/test/test.Key.js @@ -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); });