address: sanity checks.
This commit is contained in:
parent
d64bffdd17
commit
e55ca1eb3d
@ -73,10 +73,7 @@ Address.typesByVal = util.revMap(Address.types);
|
|||||||
|
|
||||||
Address.prototype.fromOptions = function fromOptions(options) {
|
Address.prototype.fromOptions = function fromOptions(options) {
|
||||||
if (typeof options === 'string')
|
if (typeof options === 'string')
|
||||||
return this.fromBase58(options);
|
return this.fromString(options);
|
||||||
|
|
||||||
if (Buffer.isBuffer(options))
|
|
||||||
return this.fromRaw(options);
|
|
||||||
|
|
||||||
return this.fromHash(
|
return this.fromHash(
|
||||||
options.hash,
|
options.hash,
|
||||||
@ -251,7 +248,6 @@ Address.prototype.fromString = function fromString(addr, network) {
|
|||||||
var i, type, hrp;
|
var i, type, hrp;
|
||||||
|
|
||||||
assert(typeof addr === 'string');
|
assert(typeof addr === 'string');
|
||||||
assert(addr.length > 2);
|
|
||||||
|
|
||||||
if (network) {
|
if (network) {
|
||||||
network = Network.get(network);
|
network = Network.get(network);
|
||||||
@ -317,6 +313,9 @@ Address.prototype.fromRaw = function fromRaw(data) {
|
|||||||
var br = new BufferReader(data, true);
|
var br = new BufferReader(data, true);
|
||||||
var i, prefix, network, type, version, hash;
|
var i, prefix, network, type, version, hash;
|
||||||
|
|
||||||
|
if (data.length > 40)
|
||||||
|
throw new Error('Address is too long.');
|
||||||
|
|
||||||
prefix = br.readU8();
|
prefix = br.readU8();
|
||||||
|
|
||||||
for (i = 0; i < networks.types.length; i++) {
|
for (i = 0; i < networks.types.length; i++) {
|
||||||
@ -356,15 +355,18 @@ Address.fromRaw = function fromRaw(data) {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from base58 address.
|
* Inject properties from base58 address.
|
||||||
* @private
|
* @private
|
||||||
* @param {Base58Address} data
|
* @param {Base58Address} str
|
||||||
* @param {Network?} network
|
* @param {Network?} network
|
||||||
* @throws Parse error
|
* @throws Parse error
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Address.prototype.fromBase58 = function fromBase58(data, network) {
|
Address.prototype.fromBase58 = function fromBase58(str, network) {
|
||||||
assert(typeof data === 'string');
|
assert(typeof str === 'string');
|
||||||
|
|
||||||
this.fromRaw(base58.decode(data));
|
if (str.length > 55)
|
||||||
|
throw new Error('Address is too long.');
|
||||||
|
|
||||||
|
this.fromRaw(base58.decode(str));
|
||||||
|
|
||||||
if (network && !this.verifyNetwork(network))
|
if (network && !this.verifyNetwork(network))
|
||||||
throw new Error('Network mismatch for address.');
|
throw new Error('Network mismatch for address.');
|
||||||
@ -374,14 +376,14 @@ Address.prototype.fromBase58 = function fromBase58(data, network) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an address object from a base58 address.
|
* Create an address object from a base58 address.
|
||||||
* @param {Base58Address} address
|
* @param {Base58Address} str
|
||||||
* @param {Network?} network
|
* @param {Network?} network
|
||||||
* @returns {Address}
|
* @returns {Address}
|
||||||
* @throws Parse error.
|
* @throws Parse error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Address.fromBase58 = function fromBase58(address, network) {
|
Address.fromBase58 = function fromBase58(str, network) {
|
||||||
return new Address().fromBase58(address, network);
|
return new Address().fromBase58(str, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -419,14 +421,14 @@ Address.prototype.fromBech32 = function fromBech32(str, network) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an address object from a bech32 address.
|
* Create an address object from a bech32 address.
|
||||||
* @param {String} address
|
* @param {String} str
|
||||||
* @param {Network?} network
|
* @param {Network?} network
|
||||||
* @returns {Address}
|
* @returns {Address}
|
||||||
* @throws Parse error.
|
* @throws Parse error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Address.fromBech32 = function fromBech32(address, network) {
|
Address.fromBech32 = function fromBech32(str, network) {
|
||||||
return new Address().fromBech32(address, network);
|
return new Address().fromBech32(str, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -649,6 +651,7 @@ Address.fromHash = function fromHash(hash, type, version, network) {
|
|||||||
|
|
||||||
Address.prototype.fromPubkeyhash = function fromPubkeyhash(hash, network) {
|
Address.prototype.fromPubkeyhash = function fromPubkeyhash(hash, network) {
|
||||||
var type = Address.types.PUBKEYHASH;
|
var type = Address.types.PUBKEYHASH;
|
||||||
|
assert(hash.length === 20, 'P2PKH must be 20 bytes.');
|
||||||
return this.fromHash(hash, type, -1, network);
|
return this.fromHash(hash, type, -1, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -673,6 +676,7 @@ Address.fromPubkeyhash = function fromPubkeyhash(hash, network) {
|
|||||||
|
|
||||||
Address.prototype.fromScripthash = function fromScripthash(hash, network) {
|
Address.prototype.fromScripthash = function fromScripthash(hash, network) {
|
||||||
var type = Address.types.SCRIPTHASH;
|
var type = Address.types.SCRIPTHASH;
|
||||||
|
assert(hash.length === 20, 'P2SH must be 20 bytes.');
|
||||||
return this.fromHash(hash, type, -1, network);
|
return this.fromHash(hash, type, -1, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user