From ad34fd191daa7d29f4992273209d0eddacd5ac02 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 11 Mar 2014 10:21:26 -0300 Subject: [PATCH 1/2] starting Address idiom changes --- Address.js | 4 +++- test/test.Address.js | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Address.js b/Address.js index f274333..ad032be 100644 --- a/Address.js +++ b/Address.js @@ -10,10 +10,12 @@ Address.parent = parent; parent.applyEncodingsTo(Address); Address.prototype.validate = function() { + var answer; this.doAsBinary(function() { Address.super(this, 'validate', arguments); - if(this.data.length !== 21) throw new Error('invalid data length'); + answer = (this.data.length === 21); }); + return answer; }; module.exports = require('soop')(Address); diff --git a/test/test.Address.js b/test/test.Address.js index 758440e..11b939b 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -26,7 +26,11 @@ describe('Address', function() { 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); + b.validate.bind(b).should.not.throw(Error); + + a.validate().should.equal(true); + m.validate().should.equal(true); + b.validate().should.equal(false); }); }); From 13b1431c5615411883244efe271c38d7bbd03ada Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 11 Mar 2014 11:27:29 -0300 Subject: [PATCH 2/2] change default way of validating Addresses --- Address.js | 8 ++++++-- README.md | 23 ++++++++--------------- examples/Address.js | 22 ++++++++-------------- test/test.Address.js | 42 ++++++++++++++++++++++++++---------------- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Address.js b/Address.js index ad032be..07333da 100644 --- a/Address.js +++ b/Address.js @@ -9,12 +9,16 @@ function Address() { Address.parent = parent; parent.applyEncodingsTo(Address); + Address.prototype.validate = function() { - var answer; this.doAsBinary(function() { Address.super(this, 'validate', arguments); - answer = (this.data.length === 21); + if(this.data.length !== 21) throw new Error('invalid data length'); }); +}; + +Address.prototype.isValid = function() { + var answer = Address.super(this, 'isValid', arguments); return answer; }; 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 11b939b..21d1534 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -20,21 +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.not.throw(Error); - - a.validate().should.equal(true); - m.validate().should.equal(true); - b.validate().should.equal(false); + 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 + }); }); }); - - - - -