From e82ffa50282755ce99e272cc19f831d548c8f6f6 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 29 Mar 2014 04:24:23 +1100 Subject: [PATCH 1/4] Adds recoverPubKey simple test --- test/ecdsa.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/ecdsa.js diff --git a/test/ecdsa.js b/test/ecdsa.js new file mode 100644 index 0000000..f315cee --- /dev/null +++ b/test/ecdsa.js @@ -0,0 +1,20 @@ +var assert = require('assert') +var convert = require('../').convert +var ecdsa = require('../').ecdsa +var Message = require('../').Message + +describe('ecdsa', function() { + describe('recoverPubKey', function() { + it('succesfully recovers a public key', function() { + var addr = 'mgQK8S6CfSXKjPmnujArSmVxafeJfrZsa3' + var signature = convert.base64ToBytes('H0PG6+PUo96UPTJ/DVj8aBU5it+Nuli4YdsLuTMvfJxoHH9Jb7jYTQXCCOX2jrTChD5S1ic3vCrUQHdmB5/sEQY=') + var sighex = convert.bytesToHex(signature) + + var hash = Message.getHash('1111') + var obj = ecdsa.parseSigCompact(signature) + var pubKey = ecdsa.recoverPubKey(obj.r, obj.s, hash, obj.i) + + assert.equal(pubKey.toHex(true), '02e8fcf4d749b35879bc1f3b14b49e67ab7301da3558c5a9b74a54f1e6339c334c') + }) + }) +}) From 669a58bbf88bfdb0c56f86db182ece0b18fbf7e0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 29 Mar 2014 04:26:19 +1100 Subject: [PATCH 2/4] Removes unnecessary SHA256 calculations --- src/ecdsa.js | 16 +++++++--------- src/message.js | 3 ++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index 668555a..f721185 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -289,20 +289,18 @@ var ECDSA = { * This function simply tries all four cases and returns the value * that resulted in a successful pubkey recovery. */ - calcPubkeyRecoveryParam: function (origPubkey, r, s, hash) - { - var address = origPubkey.getAddress().toString(); + calcPubKeyRecoveryParam: function (origPubKey, r, s, hash) { for (var i = 0; i < 4; i++) { - var pubkey = ECDSA.recoverPubKey(r, s, hash, i); - pubkey.compressed = origPubkey.compressed; - if (pubkey.getAddress().toString() == address) { - return i; + var pubKey = ECDSA.recoverPubKey(r, s, hash, i) + pubKey.compressed = origPubKey.compressed + + if (pubKey.pub.equals(origPubKey.pub)) { + return i } } - throw new Error("Unable to find valid recovery factor"); + throw new Error("Unable to find valid recovery factor") } }; module.exports = ECDSA; - diff --git a/src/message.js b/src/message.js index 998c751..7e52b95 100644 --- a/src/message.js +++ b/src/message.js @@ -31,7 +31,8 @@ Message.signMessage = function (key, message) { var hash = Message.getHash(message) var sig = key.sign(hash) var obj = ecdsa.parseSig(sig) - var i = ecdsa.calcPubkeyRecoveryParam(key, obj.r, obj.s, hash) + + var i = ecdsa.calcPubKeyRecoveryParam(key.getPub(key.compressed), obj.r, obj.s, hash) i += 27 if (key.compressed) { From bfc5924522653e0820c16a0c76cd52daffe4aa42 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 29 Mar 2014 17:30:04 +1100 Subject: [PATCH 3/4] Compression is irrelevant in comparison --- src/ecdsa.js | 1 - src/message.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index f721185..cf9c1ad 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -292,7 +292,6 @@ var ECDSA = { calcPubKeyRecoveryParam: function (origPubKey, r, s, hash) { for (var i = 0; i < 4; i++) { var pubKey = ECDSA.recoverPubKey(r, s, hash, i) - pubKey.compressed = origPubKey.compressed if (pubKey.pub.equals(origPubKey.pub)) { return i diff --git a/src/message.js b/src/message.js index 7e52b95..c541495 100644 --- a/src/message.js +++ b/src/message.js @@ -32,7 +32,7 @@ Message.signMessage = function (key, message) { var sig = key.sign(hash) var obj = ecdsa.parseSig(sig) - var i = ecdsa.calcPubKeyRecoveryParam(key.getPub(key.compressed), obj.r, obj.s, hash) + var i = ecdsa.calcPubKeyRecoveryParam(key.getPub(), obj.r, obj.s, hash) i += 27 if (key.compressed) { From 10dea421d50481d6f81846f1a4462adbb3da6b23 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 29 Mar 2014 17:44:03 +1100 Subject: [PATCH 4/4] Removes circular dependancy of ECKey --- src/ecdsa.js | 7 ++----- src/message.js | 5 +++-- test/ecdsa.js | 3 ++- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index cf9c1ad..f55dc7c 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -272,10 +272,7 @@ var ECDSA = { throw new Error("Pubkey recovery unsuccessful"); } - // TODO (shtylman) this is stupid because this file and eckey - // have circular dependencies - var ECPubKey = require('./eckey').ECPubKey; - return ECPubKey(Q); + return Q }, /** @@ -293,7 +290,7 @@ var ECDSA = { for (var i = 0; i < 4; i++) { var pubKey = ECDSA.recoverPubKey(r, s, hash, i) - if (pubKey.pub.equals(origPubKey.pub)) { + if (pubKey.equals(origPubKey)) { return i } } diff --git a/src/message.js b/src/message.js index c541495..1be0463 100644 --- a/src/message.js +++ b/src/message.js @@ -3,6 +3,7 @@ var Address = require('./address') var convert = require('./convert') var ecdsa = require('./ecdsa') +var ECPubKey = require('./eckey').ECPubKey var SHA256 = require('crypto-js/sha256') var Message = {} @@ -32,7 +33,7 @@ Message.signMessage = function (key, message) { var sig = key.sign(hash) var obj = ecdsa.parseSig(sig) - var i = ecdsa.calcPubKeyRecoveryParam(key.getPub(), obj.r, obj.s, hash) + var i = ecdsa.calcPubKeyRecoveryParam(key.getPub().pub, obj.r, obj.s, hash) i += 27 if (key.compressed) { @@ -57,7 +58,7 @@ Message.verifyMessage = function (address, sig, message) { var hash = Message.getHash(message) var isCompressed = !!(sig.i & 4) - var pubKey = ecdsa.recoverPubKey(sig.r, sig.s, hash, sig.i) + var pubKey = new ECPubKey(ecdsa.recoverPubKey(sig.r, sig.s, hash, sig.i)) pubKey.compressed = isCompressed // Compare address to expected address diff --git a/test/ecdsa.js b/test/ecdsa.js index f315cee..c96aaaa 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -1,6 +1,7 @@ var assert = require('assert') var convert = require('../').convert var ecdsa = require('../').ecdsa +var ECPubKey = require('../').ECPubKey var Message = require('../').Message describe('ecdsa', function() { @@ -12,7 +13,7 @@ describe('ecdsa', function() { var hash = Message.getHash('1111') var obj = ecdsa.parseSigCompact(signature) - var pubKey = ecdsa.recoverPubKey(obj.r, obj.s, hash, obj.i) + var pubKey = new ECPubKey(ecdsa.recoverPubKey(obj.r, obj.s, hash, obj.i)) assert.equal(pubKey.toHex(true), '02e8fcf4d749b35879bc1f3b14b49e67ab7301da3558c5a9b74a54f1e6339c334c') })