From 0df97a42fe91e20a90dd14fe13b07e31aef68687 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 21 Nov 2014 19:02:58 -0500 Subject: [PATCH] Address: Fix indentation, typos and simplify isValid --- lib/address.js | 46 +++++++++++++++++++++------------------------- test/address.js | 10 ++++++++++ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/address.js b/lib/address.js index 15c74e4..5304a7f 100644 --- a/lib/address.js +++ b/lib/address.js @@ -4,6 +4,7 @@ var base58check = require('./encoding/base58check'); var networks = require('./networks'); var Hash = require('./crypto/hash'); + /** * * Bitcore Address @@ -51,7 +52,7 @@ function Address(data, network, type) { throw new Error('Unrecognized data format'); } - // set defaults is not set + // set defaults if not set info.network = info.network || network || 'mainnet'; info.type = info.type || type || 'pubkeyhash'; @@ -105,25 +106,25 @@ Address._transformBuffer = function(buffer, network, type){ var bufType = false; switch(buffer[0]){ // the version byte - case networks.mainnet.pubkeyhash: - bufNetwork = 'mainnet'; - bufType = 'pubkeyhash'; - break; - - case networks.mainnet.scripthash: - bufNetwork = 'mainnet'; - bufType = 'scripthash'; - break; + case networks.mainnet.pubkeyhash: + bufNetwork = 'mainnet'; + bufType = 'pubkeyhash'; + break; + + case networks.mainnet.scripthash: + bufNetwork = 'mainnet'; + bufType = 'scripthash'; + break; - case networks.testnet.pubkeyhash: - bufNetwork = 'testnet'; - bufType = 'pubkeyhash'; - break; - - case networks.testnet.scripthash: - bufNetwork = 'testnet'; - bufType = 'scripthash'; - break; + case networks.testnet.pubkeyhash: + bufNetwork = 'testnet'; + bufType = 'pubkeyhash'; + break; + + case networks.testnet.scripthash: + bufNetwork = 'testnet'; + bufType = 'scripthash'; + break; } if (!bufNetwork || (network && network !== bufNetwork)) { @@ -311,12 +312,7 @@ Address.getValidationError = function(data, network, type) { * @returns {null|Error} The corresponding error message */ Address.isValid = function(data, network, type) { - var error = Address.getValidationError(data, network, type); - if (error) { - return false; - } else { - return true; - } + return !Address.getValidationError(data, network, type); }; /** diff --git a/test/address.js b/test/address.js index 66b3324..62ceec5 100644 --- a/test/address.js +++ b/test/address.js @@ -88,6 +88,16 @@ describe('Address', function() { should.exist(error); }); + it('should should return a true boolean', function(){ + var valid = Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'mainnet'); + valid.should.equal(true); + }); + + it('should should return a false boolean', function(){ + var valid = Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'testnet'); + valid.should.equal(false); + }); + it('should validate addresses', function() { for(var i=0;i