Address: Improved error messages and type, and added inspect prototype for console output.

This commit is contained in:
Braydon Fuller 2014-11-21 22:18:28 -05:00
parent 5ff349758c
commit 3c9cc23501
2 changed files with 31 additions and 20 deletions

View File

@ -23,15 +23,15 @@ var Hash = require('./crypto/hash');
function Address(data, network, type) {
if (!data) {
throw Error('Please include address data');
throw new TypeError('First argument is required, please include address data.');
}
if (network && (network !== 'mainnet' && network !== 'testnet')) {
throw new Error('Network must be "mainnet" or "testnet"');
throw new TypeError('Second argument must be "mainnet" or "testnet".');
}
if (type && (type !== 'pubkeyhash' && type !== 'scripthash')) {
throw new Error('Type must be "pubkeyhash" or "scripthash"');
throw new TypeError('Third argument must be "pubkeyhash" or "scripthash".');
}
var info;
@ -48,7 +48,7 @@ function Address(data, network, type) {
} else if (typeof(data) === 'string') {
info = Address._transformString(data, network, type);
} else {
throw new Error('Unrecognized data format');
throw new TypeError('First argument is an unrecognized data format.');
}
// set defaults if not set
@ -74,10 +74,10 @@ function Address(data, network, type) {
Address._transformHash = function(hash){
var info = {};
if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
throw new Error('Address supplied is not a buffer');
throw new TypeError('Address supplied is not a buffer.');
}
if (hash.length !== 20) {
throw new Error('Address hashbuffers must be exactly a 20 bytes');
throw new TypeError('Address hashbuffers must be exactly a 20 bytes.');
}
info.hashBuffer = hash;
return info;
@ -95,10 +95,10 @@ Address._transformHash = function(hash){
Address._transformBuffer = function(buffer, network, type){
var info = {};
if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
throw new Error('Address supplied is not a buffer');
throw new TypeError('Address supplied is not a buffer.');
}
if (buffer.length !== 1 + 20) {
throw new Error('Address buffers must be exactly 21 bytes');
throw new TypeError('Address buffers must be exactly 21 bytes.');
}
var bufNetwork = false;
@ -127,11 +127,11 @@ Address._transformBuffer = function(buffer, network, type){
}
if (!bufNetwork || (network && network !== bufNetwork)) {
throw new Error('Address has mismatched network type');
throw new TypeError('Address has mismatched network type.');
}
if (!bufType || ( type && type !== bufType )) {
throw new Error('Address has mismatched type');
throw new TypeError('Address has mismatched type.');
}
info.hashBuffer = buffer.slice(1);
@ -150,7 +150,7 @@ Address._transformBuffer = function(buffer, network, type){
Address._transformPubkey = function(pubkey){
var info = {};
if (!pubkey.constructor || (pubkey.constructor.name && pubkey.constructor.name !== 'Pubkey')) {
throw new Error('Address must be an instance of Pubkey');
throw new TypeError('Address must be an instance of Pubkey.');
}
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
info.type = 'pubkeyhash';
@ -167,7 +167,7 @@ Address._transformPubkey = function(pubkey){
Address._transformScript = function(script){
var info = {};
if (!script.constructor || (script.constructor.name && script.constructor.name !== 'Script')) {
throw new Error('Address must be an instance of Script');
throw new TypeError('Address must be an instance of Script.');
}
info.hashBuffer = Hash.sha256ripemd160(script.toBuffer());
info.type = 'scripthash';
@ -185,7 +185,7 @@ Address._transformScript = function(script){
*/
Address._transformString = function(data, network, type){
if( typeof(data) !== 'string' ) {
throw Error('Address supplied is not a string');
throw new TypeError('Address supplied is not a string.');
}
var addressBuffer = base58check.decode(data);
var info = Address._transformBuffer(addressBuffer, network, type);
@ -336,4 +336,15 @@ Address.prototype.toString = function() {
return base58check.encode(this.toBuffer());
};
/**
*
* Will return a string formatted for the console
*
* @returns {String} Bitcoin address
*/
Address.prototype.inspect = function() {
return '<Address: ' + this.toString() + ', type: '+this.type+', network: '+this.network+'>';
}
module.exports = Address;

View File

@ -17,19 +17,19 @@ describe('Address', function() {
it('should throw an error because of missing data', function() {
(function() {
var a = new Address();
}).should.throw('Please include address data');
}).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');
}).should.throw('Network must be "mainnet" or "testnet"');
}).should.throw('Second argument must be "mainnet" or "testnet".');
});
it('should throw an error because of bad type param', function() {
(function() {
var a = new Address(validAddresses[0], 'mainnet', 'pubkey');
}).should.throw('Type must be "pubkeyhash" or "scripthash"');
}).should.throw('Third argument must be "pubkeyhash" or "scripthash"');
});
@ -145,7 +145,7 @@ describe('Address', function() {
for(var i=0;i<validAddresses.length;i++){
var error = Address.getValidationError(validAddresses[i], 'testnet', 'pubkeyhash');
should.exist(error);
error.message.should.equal('Address has mismatched network type');
error.message.should.equal('Address has mismatched network type.');
}
});
@ -154,7 +154,7 @@ describe('Address', function() {
for(var i=0;i<validAddresses.length;i++){
var error = Address.getValidationError(validAddresses[i], 'mainnet', 'scripthash');
should.exist(error);
error.message.should.equal('Address has mismatched type');
error.message.should.equal('Address has mismatched type.');
}
});
@ -184,7 +184,7 @@ describe('Address', function() {
for(var i=0;i<testValidAddresses.length;i++){
var error = Address.getValidationError(testValidAddresses[i], 'mainnet', 'pubkeyhash');
should.exist(error);
error.message.should.equal('Address has mismatched network type');
error.message.should.equal('Address has mismatched network type.');
}
});
@ -214,7 +214,7 @@ describe('Address', function() {
it('should throw an error for invalid length hashBuffer', function() {
(function() {
var a = Address.fromPubkeyHash(buf);
}).should.throw('Address hashbuffers must be exactly a 20 bytes');
}).should.throw('Address hashbuffers must be exactly a 20 bytes.');
});
it('should make this address from a compressed pubkey object', function() {