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) {
|
||||
if (typeof options === 'string')
|
||||
return this.fromBase58(options);
|
||||
|
||||
if (Buffer.isBuffer(options))
|
||||
return this.fromRaw(options);
|
||||
return this.fromString(options);
|
||||
|
||||
return this.fromHash(
|
||||
options.hash,
|
||||
@ -251,7 +248,6 @@ Address.prototype.fromString = function fromString(addr, network) {
|
||||
var i, type, hrp;
|
||||
|
||||
assert(typeof addr === 'string');
|
||||
assert(addr.length > 2);
|
||||
|
||||
if (network) {
|
||||
network = Network.get(network);
|
||||
@ -317,6 +313,9 @@ Address.prototype.fromRaw = function fromRaw(data) {
|
||||
var br = new BufferReader(data, true);
|
||||
var i, prefix, network, type, version, hash;
|
||||
|
||||
if (data.length > 40)
|
||||
throw new Error('Address is too long.');
|
||||
|
||||
prefix = br.readU8();
|
||||
|
||||
for (i = 0; i < networks.types.length; i++) {
|
||||
@ -356,15 +355,18 @@ Address.fromRaw = function fromRaw(data) {
|
||||
/**
|
||||
* Inject properties from base58 address.
|
||||
* @private
|
||||
* @param {Base58Address} data
|
||||
* @param {Base58Address} str
|
||||
* @param {Network?} network
|
||||
* @throws Parse error
|
||||
*/
|
||||
|
||||
Address.prototype.fromBase58 = function fromBase58(data, network) {
|
||||
assert(typeof data === 'string');
|
||||
Address.prototype.fromBase58 = function fromBase58(str, network) {
|
||||
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))
|
||||
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.
|
||||
* @param {Base58Address} address
|
||||
* @param {Base58Address} str
|
||||
* @param {Network?} network
|
||||
* @returns {Address}
|
||||
* @throws Parse error.
|
||||
*/
|
||||
|
||||
Address.fromBase58 = function fromBase58(address, network) {
|
||||
return new Address().fromBase58(address, network);
|
||||
Address.fromBase58 = function fromBase58(str, 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.
|
||||
* @param {String} address
|
||||
* @param {String} str
|
||||
* @param {Network?} network
|
||||
* @returns {Address}
|
||||
* @throws Parse error.
|
||||
*/
|
||||
|
||||
Address.fromBech32 = function fromBech32(address, network) {
|
||||
return new Address().fromBech32(address, network);
|
||||
Address.fromBech32 = function fromBech32(str, 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) {
|
||||
var type = Address.types.PUBKEYHASH;
|
||||
assert(hash.length === 20, 'P2PKH must be 20 bytes.');
|
||||
return this.fromHash(hash, type, -1, network);
|
||||
};
|
||||
|
||||
@ -673,6 +676,7 @@ Address.fromPubkeyhash = function fromPubkeyhash(hash, network) {
|
||||
|
||||
Address.prototype.fromScripthash = function fromScripthash(hash, network) {
|
||||
var type = Address.types.SCRIPTHASH;
|
||||
assert(hash.length === 20, 'P2SH must be 20 bytes.');
|
||||
return this.fromHash(hash, type, -1, network);
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user