|
|
@ -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; |
|
|
|