From 80da2ed2d5e91864e7e9295b9e736a6f1588f761 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Fri, 30 May 2014 19:00:49 +1000
Subject: [PATCH] HDWallet: add fromBase58 exception checks

---
 src/hdwallet.js             |  4 ++--
 test/fixtures/hdwallet.json | 17 ++++++++++++++++-
 test/hdwallet.js            | 10 ++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/hdwallet.js b/src/hdwallet.js
index 7df6207..b0ce031 100644
--- a/src/hdwallet.js
+++ b/src/hdwallet.js
@@ -49,8 +49,8 @@ HDWallet.fromBase58 = function(string) {
   var checksum = buffer.slice(-4)
   var newChecksum = crypto.hash256(payload).slice(0, 4)
 
-  assert.deepEqual(newChecksum, checksum)
-  assert.equal(payload.length, HDWallet.LENGTH)
+  assert.deepEqual(newChecksum, checksum, 'Invalid checksum')
+  assert.equal(payload.length, HDWallet.LENGTH, 'Invalid BIP32 string')
 
   return HDWallet.fromBuffer(payload)
 }
diff --git a/test/fixtures/hdwallet.json b/test/fixtures/hdwallet.json
index d3b2744..bcebf2c 100644
--- a/test/fixtures/hdwallet.json
+++ b/test/fixtures/hdwallet.json
@@ -176,5 +176,20 @@
       ]
     }
   ],
-  "invalid": []
+  "invalid": {
+    "fromBase58": [
+      {
+        "exception": "Invalid checksum",
+        "string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"
+      },
+      {
+        "exception": "Invalid BIP32 string",
+        "string": "SQ8nQ2jWarXqLo9oHGKKP6iQDsQbPRftq7rjtYY3hqJRPQRgrmeunFnDKbH7B15yGPLZBrhhkKXx3pwD6LcBooJRGq6x7matAXpMsgn"
+      },
+      {
+        "exception": "Invalid BIP32 string",
+        "string": "37hdAfw3aMiWcBGPP2ywmY5jizTeSSP5GXayKY3RxkEZ7f3SBnRE1pN6eY3VzGkgx6vbdNtuKfrHgEaYvW9KkFZCycaPvWiA9TtfmeVB592Sf9RfSzQzXo72"
+      }
+    ]
+  }
 }
diff --git a/test/hdwallet.js b/test/hdwallet.js
index 03a7c85..f4a5c99 100644
--- a/test/hdwallet.js
+++ b/test/hdwallet.js
@@ -33,6 +33,16 @@ describe('HDWallet', function() {
     })
   })
 
+  describe('fromBase58', function() {
+    fixtures.invalid.fromBase58.forEach(function(f) {
+      it('throws on ' + f.string, function() {
+        assert.throws(function() {
+          HDWallet.fromBase58(f.string)
+        }, new RegExp(f.exception))
+      })
+    })
+  })
+
   describe('constructor & seed deserialization', function() {
     var expectedPrivateKey = '0fd71c652e847ba7ea7956e3cf3fc0a0985871846b1b2c23b9c6a29a38cee860'
     var seed = new Buffer([