diff --git a/Address.js b/Address.js index f274333..07333da 100644 --- a/Address.js +++ b/Address.js @@ -9,6 +9,7 @@ function Address() { Address.parent = parent; parent.applyEncodingsTo(Address); + Address.prototype.validate = function() { this.doAsBinary(function() { Address.super(this, 'validate', arguments); @@ -16,4 +17,9 @@ Address.prototype.validate = function() { }); }; +Address.prototype.isValid = function() { + var answer = Address.super(this, 'isValid', arguments); + return answer; +}; + module.exports = require('soop')(Address); diff --git a/README.md b/README.md index 2d0dee3..b65dc77 100644 --- a/README.md +++ b/README.md @@ -31,26 +31,19 @@ Validating a Bitcoin address: ```js var Address = require('bitcore/Address'); -var addrStrings = [ - "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", - "1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", - "A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", - "1600 Pennsylvania Ave NW", +var addrs = [ + '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', + 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1600 Pennsylvania Ave NW', ].map(function(addr) { return new Address(addr); }); -addrStrings.forEach(function(addr) { - - try { - addr.validate(); - console.log(addr.data + ": is valid"); - } catch(e) { - console.log(addr.data + ": is not a valid address. " + e); - } - +addrs.forEach(function(addr) { + var valid = addr.isValid(); + console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); }); - ``` ## Monitoring Blocks and Transactions For this example you need a running bitcoind instance with RPC enabled. diff --git a/examples/Address.js b/examples/Address.js index 1cc0573..af41311 100644 --- a/examples/Address.js +++ b/examples/Address.js @@ -4,22 +4,16 @@ var Address = require('../Address'); -var addrStrings = [ - "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", - "1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", - "A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", - "1600 Pennsylvania Ave NW", +var addrs = [ + '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', + 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1600 Pennsylvania Ave NW', ].map(function(addr) { return new Address(addr); }); -addrStrings.forEach(function(addr) { - - try { - addr.validate(); - console.log(addr.data + ": is valid"); - } catch(e) { - console.log(addr.data + ": is not a valid address. " + e); - } - +addrs.forEach(function(addr) { + var valid = addr.isValid(); + console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); }); diff --git a/test/test.Address.js b/test/test.Address.js index 758440e..21d1534 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -20,17 +20,31 @@ describe('Address', function() { var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T'); should.exist(a); }); - it('should validate correctly', function() { - var a = new Address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); - var m = new Address('32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6'); - var b = new Address('11111111111111111111111111122222234'); - a.validate.bind(a).should.not.throw(Error); - m.validate.bind(m).should.not.throw(Error); - b.validate.bind(b).should.throw(Error); + var data = [ + ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true], + ['11111111111111111111111111122222234', false], // totally invalid + ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true], + ['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true], + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true], + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum + ['1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // data changed, original checksum. + ['1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // invalid chars + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j', false], // checksums don't match. + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!', false], // bad char (!) + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz', false], // too long Bitcoin address + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false],// too long Bitcoin address + ['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data + ]; + data.forEach(function(datum) { + var address = datum[0]; + var result = datum[1]; + it('should validate correctly ' + address, function() { + var a = new Address(address); + var s = a.toString(); + + a.isValid().should.equal(result); + s.should.equal(a.toString()); // check that validation doesn't change data + }); }); }); - - - - -