From 3cd4e31c3176079d13bead2ae0fa962482c4a6e1 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 19 Mar 2014 18:44:24 -0300 Subject: [PATCH] fix Address validation issue --- Address.js | 1 + test/test.Address.js | 3 +++ test/test.misc.js | 10 +++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Address.js b/Address.js index 679dbb3..89b8211 100644 --- a/Address.js +++ b/Address.js @@ -16,6 +16,7 @@ Address.prototype.validate = function() { Address.super(this, 'validate', arguments); if(this.data.length !== 21) throw new Error('invalid data length'); }); + if (typeof this.network() === 'undefined') throw new Error('invalid network'); }; Address.prototype.isValid = function() { diff --git a/test/test.Address.js b/test/test.Address.js index e1d73d8..1982553 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -35,6 +35,9 @@ describe('Address', function() { ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz', false], // too long Bitcoin address ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false],// too long Bitcoin address ['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data + ['dB7cwYdcPSgiyAwKWL3JwCVwSk6epU2txw', false], // valid base58, valid length, invalid network + ['2MnmgiRH4eGLyLc9eAqStzk7dFgBjFtUCtu', false], // valid base58, valid length, invalid network + ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true], // valid base58, valid length, valid network ]; data.forEach(function(datum) { var address = datum[0]; diff --git a/test/test.misc.js b/test/test.misc.js index e58bfcc..0a4b368 100644 --- a/test/test.misc.js +++ b/test/test.misc.js @@ -122,12 +122,20 @@ describe('Miscelaneous stuff', function() { a.network().should.equal(network); }); it('should generate correctly from hex', function() { - var version = shouldBeScript? network.addressScript: network.addressPubkey; + var version = shouldBeScript ? network.addressScript : network.addressPubkey; var b = new Address(version, new Buffer(hexPayload, 'hex')); b.toString().should.equal(b58); }); }); } }); + testdata.dataBase58KeysInvalid.forEach(function(datum) { + var b58 = datum[0]; + it('shouldnt be able to create Address nor WalletKey with ' + b58, function() { + var a = new Address(b58); + var invalidAddress = (!a.isValid()); + invalidAddress.should.equal(true); + }); + }); });