From 7719d1bfd4b04a646b1df68c109a95af2ff33e27 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 25 May 2015 23:31:42 -0400 Subject: [PATCH 1/3] Add test with uncompressed pubkey compact signature. --- test/crypto/signature.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/crypto/signature.js b/test/crypto/signature.js index 809d9b9..aa2080c 100644 --- a/test/crypto/signature.js +++ b/test/crypto/signature.js @@ -51,6 +51,14 @@ describe('Signature', function() { sig.s.cmp(BN.Zero).should.equal(0); }); + it('should create a signature from an uncompressed signature', function() { + var uncompressed = '1c49f92a610fa934f45b3a01036055ebbe65a6b910e8c3cfbbb0b64f575' + + '196f2fe18dc5a236d7883434cf48c11d56c804493dfe31b5a09a1a3cab378b2aac8c934'; + var sig = Signature.fromCompact(uncompressed); + sig.r.toString('hex').should.equal('38a5e6d9a9fda73a2a8275413be68f408a1859173f07ae557be54ce1fdd5f3b1'); + sig.s.toString('hex').should.equal('21135fd5b20a73b09a7389910fe2b20f59b6fd0990c84169367db069cd6e3ea1'); + }); + }); describe('#fromDER', function() { From a5cd5a1407c23d12d425904487a7c67208310ecf Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 25 May 2015 23:57:46 -0400 Subject: [PATCH 2/3] Added support for uncompressed public keys to Signature.fromCompact --- lib/crypto/signature.js | 12 ++++++++++-- test/crypto/signature.js | 12 +++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/crypto/signature.js b/lib/crypto/signature.js index deaa660..5b0c2a0 100644 --- a/lib/crypto/signature.js +++ b/lib/crypto/signature.js @@ -31,10 +31,17 @@ Signature.prototype.set = function(obj) { }; Signature.fromCompact = function(buf) { + $.checkArgument(BufferUtil.isBuffer(buf), 'Argument is expected to be a Buffer'); + var sig = new Signature(); - //TODO: handle uncompressed pubkeys + var compressed = true; var i = buf.slice(0, 1)[0] - 27 - 4; + if (i < 0) { + compressed = false; + i = i + 4; + } + var b2 = buf.slice(1, 33); var b3 = buf.slice(33, 65); @@ -142,8 +149,9 @@ Signature.prototype.toCompact = function(i, compressed) { } var val = i + 27 + 4; - if (compressed === false) + if (compressed === false) { val = val - 4; + } var b1 = new Buffer([val]); var b2 = this.r.toBuffer({ size: 32 diff --git a/test/crypto/signature.js b/test/crypto/signature.js index aa2080c..87b93e4 100644 --- a/test/crypto/signature.js +++ b/test/crypto/signature.js @@ -52,11 +52,13 @@ describe('Signature', function() { }); it('should create a signature from an uncompressed signature', function() { - var uncompressed = '1c49f92a610fa934f45b3a01036055ebbe65a6b910e8c3cfbbb0b64f575' + - '196f2fe18dc5a236d7883434cf48c11d56c804493dfe31b5a09a1a3cab378b2aac8c934'; - var sig = Signature.fromCompact(uncompressed); - sig.r.toString('hex').should.equal('38a5e6d9a9fda73a2a8275413be68f408a1859173f07ae557be54ce1fdd5f3b1'); - sig.s.toString('hex').should.equal('21135fd5b20a73b09a7389910fe2b20f59b6fd0990c84169367db069cd6e3ea1'); + var sigHexaStr = '1cd5e61ab5bfd0d1450997894cb1a53e917f89d82eb43f06fa96f32c96e061aec12fc1188e8b' + + '0dc553a2588be2b5b68dbbd7f092894aa3397786e9c769c5348dc6'; + var sig = Signature.fromCompact(new Buffer(sigHexaStr, 'hex')); + var r = 'd5e61ab5bfd0d1450997894cb1a53e917f89d82eb43f06fa96f32c96e061aec1'; + var s = '2fc1188e8b0dc553a2588be2b5b68dbbd7f092894aa3397786e9c769c5348dc6'; + sig.r.toString('hex').should.equal(r); + sig.s.toString('hex').should.equal(s); }); }); From ffe6b449cbc2d9ba8456cbc2e7cc35b55fa1cea7 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 26 May 2015 01:02:46 -0400 Subject: [PATCH 3/3] Check the compressed property in Signature.fromCompact tests. --- test/crypto/signature.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/crypto/signature.js b/test/crypto/signature.js index 87b93e4..74e3ba7 100644 --- a/test/crypto/signature.js +++ b/test/crypto/signature.js @@ -49,6 +49,7 @@ describe('Signature', function() { var sig = Signature.fromCompact(compressed); sig.r.cmp(BN.Zero).should.equal(0); sig.s.cmp(BN.Zero).should.equal(0); + sig.compressed.should.equal(true); }); it('should create a signature from an uncompressed signature', function() { @@ -59,6 +60,7 @@ describe('Signature', function() { var s = '2fc1188e8b0dc553a2588be2b5b68dbbd7f092894aa3397786e9c769c5348dc6'; sig.r.toString('hex').should.equal(r); sig.s.toString('hex').should.equal(s); + sig.compressed.should.equal(false); }); });