From 76804bef45955aa300c800e403abc8ae370990bb Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 3 Jul 2014 09:17:54 -0700 Subject: [PATCH] Fix checksum comparison error. Closes #402 --- lib/Base58.js | 2 +- test/test.Address.js | 4 +++- test/test.Base58.js | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Base58.js b/lib/Base58.js index 54cd8d151d1..c9cb821224e 100644 --- a/lib/Base58.js +++ b/lib/Base58.js @@ -95,7 +95,7 @@ var base58Check = { var hash = doubleSHA256(data); var hash4 = hash.slice(0, 4); - if (csum.toString() != hash4.toString()) { + if (csum.toString('hex') !== hash4.toString('hex')) { throw new Error("checksum mismatch"); } diff --git a/test/test.Address.js b/test/test.Address.js index b1f3666bce8..dd78d629e67 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -22,6 +22,8 @@ describe('Address', function() { ['11111111111111111111111111122222234', false], // totally invalid ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true], ['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true], + ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true], + ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb', false], //bad checksum ... thanks @wtogami ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true], ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum @@ -225,5 +227,5 @@ describe('Address', function() { }); } }); - + }); diff --git a/test/test.Base58.js b/test/test.Base58.js index 0d2d6c6d03f..14912959033 100644 --- a/test/test.Base58.js +++ b/test/test.Base58.js @@ -1,4 +1,5 @@ var chai = chai || require('chai'); +var should = chai.should(); var assert = chai.assert; var bitcore = bitcore || require('../bitcore'); var base58 = bitcore.Base58.base58; @@ -50,4 +51,13 @@ describe('Base58', function() { base58Check.decodeTest(raw, b58Check); }); }); + + describe('base58Check', function() { + it('should throw a checksum error on this invalid string', function() { + var str1 = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; + var str2 = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb"; //note last character + base58Check.decode(str1).length.should.be.greaterThan(0); + (function(){base58Check.decode(str2)}).should.throw('checksum mismatch'); + }); + }); });