diff --git a/lib/address.js b/lib/address.js index d3159e2..2699fcf 100644 --- a/lib/address.js +++ b/lib/address.js @@ -44,7 +44,7 @@ function Address(data, network, type) { throw new TypeError('Second argument must be "livenet" or "testnet".'); } - if (type && (type !== 'pubkeyhash' && type !== 'scripthash')) { + if (type && (type !== Address.PayToPublicKeyHash && type !== Address.PayToScriptHash)) { throw new TypeError('Third argument must be "pubkeyhash" or "scripthash".'); } @@ -69,7 +69,7 @@ function Address(data, network, type) { // set defaults if not set info.network = info.network || network || networks.defaultNetwork.name; - info.type = info.type || type || 'pubkeyhash'; + info.type = info.type || type || Address.PayToPublicKeyHash; Object.defineProperty(this, 'hashBuffer', { configurable: false, @@ -87,9 +87,11 @@ function Address(data, network, type) { }); return this; - } +Address.PayToPublicKeyHash = 'pubkeyhash'; +Address.PayToScriptHash = 'scripthash'; + /** * * Internal function to transform a hash buffer @@ -182,7 +184,7 @@ Address._transformPublicKey = function(pubkey){ throw new TypeError('Address must be an instance of PublicKey.'); } info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer()); - info.type = 'pubkeyhash'; + info.type = Address.PayToPublicKeyHash; return info; }; @@ -200,7 +202,7 @@ Address._transformScript = function(script){ throw new TypeError('Address must be an instance of Script.'); } info.hashBuffer = Hash.sha256ripemd160(script.toBuffer()); - info.type = 'scripthash'; + info.type = Address.PayToScriptHash; return info; }; @@ -247,7 +249,7 @@ Address.fromPublicKey = function(data, network){ */ Address.fromPublicKeyHash = function(hash, network) { var info = Address._transformHash(hash); - return new Address(info.hashBuffer, network, 'pubkeyhash'); + return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash); }; /** @@ -260,7 +262,7 @@ Address.fromPublicKeyHash = function(hash, network) { */ Address.fromScriptHash = function(hash, network) { var info = Address._transformHash(hash); - return new Address(info.hashBuffer, network, 'scripthash'); + return new Address(info.hashBuffer, network, Address.PayToScriptHash); }; /** @@ -346,6 +348,22 @@ Address.isValid = function(data, network, type) { return !Address.getValidationError(data, network, type); }; +/** + * Returns true if an address is of pay to public key hash type + * @return boolean + */ +Address.prototype.isPayToPublicKeyHash = function() { + return this.type === Address.PayToPublicKeyHash; +}; + +/** + * Returns true if an address is of pay to script hash type + * @return boolean + */ +Address.prototype.isPayToScriptHash = function() { + return this.type === Address.PayToScriptHash; +}; + /** * * Will return a buffer representation of the address diff --git a/test/address.js b/test/address.js index ce798eb..6f224b2 100644 --- a/test/address.js +++ b/test/address.js @@ -1,5 +1,7 @@ 'use strict'; +/* jshint maxstatements: 30 */ + var should = require('chai').should(); var bitcore = require('..'); @@ -13,29 +15,28 @@ describe('Address', function() { var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); var buf = Buffer.concat([new Buffer([0]), pubkeyhash]); var str = '16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'; - var strTest = 'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X'; it('should throw an error because of missing data', function() { (function() { - var a = new Address(); + return new Address(); }).should.throw('First argument is required, please include address data.'); }); it('should throw an error because of bad network param', function() { (function(){ - var a = new Address(validAddresses[0], 'main', 'pubkeyhash'); + return new Address(PKHLivenet[0], 'main', 'pubkeyhash'); }).should.throw('Second argument must be "livenet" or "testnet".'); }); it('should throw an error because of bad type param', function() { (function() { - var a = new Address(validAddresses[0], 'livenet', 'pubkey'); + return new Address(PKHLivenet[0], 'livenet', 'pubkey'); }).should.throw('Third argument must be "pubkeyhash" or "scripthash"'); }); // livenet valid - var validAddresses = [ + var PKHLivenet = [ '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', '1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT', '1BpbpfLdY7oBS9gK7aDXgvMgr1DPvNhEB2', @@ -43,7 +44,7 @@ describe('Address', function() { ]; // livenet p2sh - var validp2shAddresses = [ + var P2SHLivenet = [ '342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey', '33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk', '37Sp6Rv3y4kVd1nQ1JV5pfqXccHNyZm1x3', @@ -51,7 +52,7 @@ describe('Address', function() { ]; // testnet p2sh - var testValidp2shAddresses = [ + var P2SHTestnet = [ '2N7FuwuUuoTBrDFdrAZ9KxBmtqMLxce9i1C', '2NEWDzHWwY5ZZp8CQWbB7ouNMLqCia6YRda', '2MxgPqX1iThW3oZVk9KoFcE5M4JpiETssVN', @@ -75,7 +76,7 @@ describe('Address', function() { ]; //testnet valid - var testValidAddresses = [ + var PKHTestnet = [ 'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X', 'n45x3R2w2jaSC62BMa9MeJCd3TXxgvDEmm', 'mursDVxqNQmmwWHACpM9VHwVVSfTddGsEM', @@ -100,42 +101,42 @@ describe('Address', function() { }); it('should validate addresses', function() { - for(var i=0;i