From 669a58bbf88bfdb0c56f86db182ece0b18fbf7e0 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <daniel210x@gmail.com>
Date: Sat, 29 Mar 2014 04:26:19 +1100
Subject: [PATCH] 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) {