address: better network validation.

This commit is contained in:
Christopher Jeffrey 2016-12-06 14:24:00 -08:00
parent 2365355d79
commit 9215e4fe48
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 26 additions and 4 deletions

View File

@ -378,10 +378,8 @@ HTTPServer.prototype._init = function _init() {
enforce(typeof output.address === 'string',
'Address must be a string.');
output.address = Address.fromBase58(output.address);
if (this.network.type === 'main') {
enforce(output.address.network === this.network,
'Wrong network for address.');
}
enforce(output.address.verifyNetwork(this.network),
'Wrong network for address.');
} else if (output.script) {
enforce(typeof output.script === 'string',
'Script must be a string.');

View File

@ -90,6 +90,30 @@ Address.prototype.getHash = function getHash(enc) {
return this.hash;
};
/**
* Get a network address prefix for the address.
* @param {Network?} network
* @returns {Number}
*/
Address.prototype.getPrefix = function getPrefix(network) {
if (!network)
network = this.network;
network = Network.get(network);
return Address.getPrefix(this.type, network);
};
/**
* Verify an address network (compares prefixes).
* @param {Network} network
* @returns {Boolean}
*/
Address.prototype.verifyNetwork = function verifyNetwork(network) {
assert(network);
return this.getPrefix() === this.getPrefix(network);
};
/**
* Get the address type as a string.
* @returns {AddressType}