validation: refactor.
This commit is contained in:
parent
5513563892
commit
1df577cb74
30
bin/cli
30
bin/cli
@ -48,8 +48,8 @@ CLI.prototype.createWallet = async function createWallet() {
|
||||
type: this.config.str('type'),
|
||||
master: this.config.str('master'),
|
||||
mnemonic: this.config.str('mnemonic'),
|
||||
m: this.config.num('m'),
|
||||
n: this.config.num('n'),
|
||||
m: this.config.uint('m'),
|
||||
n: this.config.uint('n'),
|
||||
witness: this.config.bool('witness'),
|
||||
passphrase: this.config.str('passphrase'),
|
||||
watchOnly: false,
|
||||
@ -123,8 +123,8 @@ CLI.prototype.createAccount = async function createAccount() {
|
||||
|
||||
const options = {
|
||||
type: this.config.str('type'),
|
||||
m: this.config.num('m'),
|
||||
n: this.config.num('n'),
|
||||
m: this.config.uint('m'),
|
||||
n: this.config.uint('n'),
|
||||
witness: this.config.bool('witness'),
|
||||
accountKey: this.config.str('watch')
|
||||
};
|
||||
@ -199,7 +199,7 @@ CLI.prototype.getBlock = async function getBlock() {
|
||||
|
||||
CLI.prototype.getCoin = async function getCoin() {
|
||||
const hash = this.config.str(0);
|
||||
const index = this.config.num(1);
|
||||
const index = this.config.uint(1);
|
||||
|
||||
if (util.isBase58(hash)) {
|
||||
const coins = await this.client.getCoinsByAddress(hash);
|
||||
@ -288,12 +288,12 @@ CLI.prototype.sendTX = async function sendTX() {
|
||||
if (this.config.has('script')) {
|
||||
outputs.push({
|
||||
script: this.config.str('script'),
|
||||
value: this.config.amt([0, 'value'])
|
||||
value: this.config.ufixed([0, 'value'], 8)
|
||||
});
|
||||
} else {
|
||||
outputs.push({
|
||||
address: this.config.str([0, 'address']),
|
||||
value: this.config.amt([1, 'value'])
|
||||
value: this.config.ufixed([1, 'value'], 8)
|
||||
});
|
||||
}
|
||||
|
||||
@ -309,7 +309,7 @@ CLI.prototype.sendTX = async function sendTX() {
|
||||
passphrase: this.config.str('passphrase'),
|
||||
outputs: outputs,
|
||||
smart: this.config.bool('smart'),
|
||||
rate: this.config.amt('rate'),
|
||||
rate: this.config.ufixed('rate', 8),
|
||||
subtractFee: this.config.bool('subtract-fee')
|
||||
};
|
||||
|
||||
@ -324,12 +324,12 @@ CLI.prototype.createTX = async function createTX() {
|
||||
if (this.config.has('script')) {
|
||||
output = {
|
||||
script: this.config.str('script'),
|
||||
value: this.config.amt([0, 'value'])
|
||||
value: this.config.ufixed([0, 'value'], 8)
|
||||
};
|
||||
} else {
|
||||
output = {
|
||||
address: this.config.str([0, 'address']),
|
||||
value: this.config.amt([1, 'value'])
|
||||
value: this.config.ufixed([1, 'value'], 8)
|
||||
};
|
||||
}
|
||||
|
||||
@ -338,7 +338,7 @@ CLI.prototype.createTX = async function createTX() {
|
||||
passphrase: this.config.str('passphrase'),
|
||||
outputs: [output],
|
||||
smart: this.config.bool('smart'),
|
||||
rate: this.config.amt('rate'),
|
||||
rate: this.config.ufixed('rate', 8),
|
||||
subtractFee: this.config.bool('subtract-fee')
|
||||
};
|
||||
|
||||
@ -356,7 +356,7 @@ CLI.prototype.signTX = async function signTX() {
|
||||
};
|
||||
|
||||
CLI.prototype.zapWallet = async function zapWallet() {
|
||||
const age = this.config.num([0, 'age'], 72 * 60 * 60);
|
||||
const age = this.config.uint([0, 'age'], 72 * 60 * 60);
|
||||
await this.wallet.zap(this.config.str('account'), age);
|
||||
this.log('Zapped!');
|
||||
};
|
||||
@ -386,7 +386,7 @@ CLI.prototype.getWalletBlocks = async function getWalletBlocks() {
|
||||
};
|
||||
|
||||
CLI.prototype.getWalletBlock = async function getWalletBlock() {
|
||||
const height = this.config.num(0);
|
||||
const height = this.config.uint(0);
|
||||
const block = await this.wallet.getBlock(height);
|
||||
this.log(block);
|
||||
};
|
||||
@ -397,7 +397,7 @@ CLI.prototype.retoken = async function retoken() {
|
||||
};
|
||||
|
||||
CLI.prototype.rescan = async function rescan() {
|
||||
const height = this.config.num(0);
|
||||
const height = this.config.uint(0);
|
||||
await this.client.rescan(height);
|
||||
this.log('Rescanning...');
|
||||
};
|
||||
@ -467,7 +467,7 @@ CLI.prototype.lock = async function lock() {
|
||||
|
||||
CLI.prototype.unlock = async function unlock() {
|
||||
const passphrase = this.config.str(0);
|
||||
const timeout = this.config.num(1);
|
||||
const timeout = this.config.uint(1);
|
||||
await this.wallet.unlock(passphrase, timeout);
|
||||
this.log('Unlocked.');
|
||||
};
|
||||
|
||||
@ -469,12 +469,12 @@ ChainEntry.prototype.toJSON = function toJSON() {
|
||||
ChainEntry.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'Block data is required.');
|
||||
assert(typeof json.hash === 'string');
|
||||
assert(util.isUInt32(json.version));
|
||||
assert(util.isU32(json.version));
|
||||
assert(typeof json.prevBlock === 'string');
|
||||
assert(typeof json.merkleRoot === 'string');
|
||||
assert(util.isUInt32(json.time));
|
||||
assert(util.isUInt32(json.bits));
|
||||
assert(util.isUInt32(json.nonce));
|
||||
assert(util.isU32(json.time));
|
||||
assert(util.isU32(json.bits));
|
||||
assert(util.isU32(json.nonce));
|
||||
assert(typeof json.chainwork === 'string');
|
||||
|
||||
this.hash = util.revHex(json.hash);
|
||||
|
||||
@ -140,7 +140,7 @@ Amount.prototype.toString = function toString() {
|
||||
*/
|
||||
|
||||
Amount.prototype.fromValue = function fromValue(value) {
|
||||
assert(util.isInt53(value), 'Value must be an int64.');
|
||||
assert(util.isI64(value), 'Value must be an int64.');
|
||||
this.value = value;
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -52,7 +52,7 @@ URI.prototype.fromOptions = function fromOptions(options) {
|
||||
this.address.fromOptions(options.address);
|
||||
|
||||
if (options.amount != null) {
|
||||
assert(util.isUInt53(options.amount), 'Amount must be a uint53.');
|
||||
assert(util.isU64(options.amount), 'Amount must be a uint64.');
|
||||
this.amount = options.amount;
|
||||
}
|
||||
|
||||
|
||||
@ -1497,7 +1497,7 @@ RPC.prototype.prioritiseTransaction = async function prioritiseTransaction(args,
|
||||
|
||||
const valid = new Validator([args]);
|
||||
const hash = valid.hash(0);
|
||||
const pri = valid.num(1);
|
||||
const pri = valid.i64(1);
|
||||
const fee = valid.i64(2);
|
||||
|
||||
if (!this.mempool)
|
||||
@ -1687,7 +1687,7 @@ RPC.prototype.createRawTransaction = async function createRawTransaction(args, h
|
||||
|
||||
uniq.add(b58);
|
||||
|
||||
const value = sends.btc(key);
|
||||
const value = sends.ufixed(key, 8);
|
||||
|
||||
if (value == null)
|
||||
throw new RPCError(errs.TYPE_ERROR, 'Invalid output value.');
|
||||
@ -1832,7 +1832,7 @@ RPC.prototype.signRawTransaction = async function signRawTransaction(args, help)
|
||||
const hash = valid.hash('txid');
|
||||
const index = valid.u32('index');
|
||||
const scriptRaw = valid.buf('scriptPubKey');
|
||||
const value = valid.btc('amount');
|
||||
const value = valid.ufixed('amount', 8);
|
||||
const redeemRaw = valid.buf('redeemScript');
|
||||
|
||||
if (!hash || index == null || !scriptRaw || value == null)
|
||||
@ -2186,7 +2186,7 @@ RPC.prototype.handleLongpoll = async function handleLongpoll(lpid) {
|
||||
const watched = lpid.slice(0, 64);
|
||||
const lastTX = parseInt(lpid.slice(64, 74), 10);
|
||||
|
||||
if (!util.isHex(watched) || !util.isUInt32(lastTX))
|
||||
if (!util.isHex(watched) || !util.isU32(lastTX))
|
||||
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid longpoll ID.');
|
||||
|
||||
const hash = util.revHex(watched);
|
||||
|
||||
@ -691,7 +691,7 @@ PolicyEstimator.prototype.estimateFee = function estimateFee(target, smart) {
|
||||
if (smart == null)
|
||||
smart = true;
|
||||
|
||||
assert(util.isUInt32(target), 'Target must be a number.');
|
||||
assert(util.isU32(target), 'Target must be a number.');
|
||||
assert(target <= this.feeStats.maxConfirms,
|
||||
'Too many confirmations for estimate.');
|
||||
|
||||
@ -735,7 +735,7 @@ PolicyEstimator.prototype.estimatePriority = function estimatePriority(target, s
|
||||
if (smart == null)
|
||||
smart = true;
|
||||
|
||||
assert(util.isUInt32(target), 'Target must be a number.');
|
||||
assert(util.isU32(target), 'Target must be a number.');
|
||||
assert(target <= this.priStats.maxConfirms,
|
||||
'Too many confirmations for estimate.');
|
||||
|
||||
|
||||
@ -1980,7 +1980,7 @@ MempoolOptions.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.limitFreeRelay != null) {
|
||||
assert(util.isUInt32(options.limitFreeRelay));
|
||||
assert(util.isU32(options.limitFreeRelay));
|
||||
this.limitFreeRelay = options.limitFreeRelay;
|
||||
}
|
||||
|
||||
@ -2015,27 +2015,27 @@ MempoolOptions.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.maxSize != null) {
|
||||
assert(util.isUInt53(options.maxSize));
|
||||
assert(util.isU64(options.maxSize));
|
||||
this.maxSize = options.maxSize;
|
||||
}
|
||||
|
||||
if (options.maxOrphans != null) {
|
||||
assert(util.isUInt32(options.maxOrphans));
|
||||
assert(util.isU32(options.maxOrphans));
|
||||
this.maxOrphans = options.maxOrphans;
|
||||
}
|
||||
|
||||
if (options.maxAncestors != null) {
|
||||
assert(util.isUInt32(options.maxAncestors));
|
||||
assert(util.isU32(options.maxAncestors));
|
||||
this.maxAncestors = options.maxAncestors;
|
||||
}
|
||||
|
||||
if (options.expiryTime != null) {
|
||||
assert(util.isUInt32(options.expiryTime));
|
||||
assert(util.isU32(options.expiryTime));
|
||||
this.expiryTime = options.expiryTime;
|
||||
}
|
||||
|
||||
if (options.minRelay != null) {
|
||||
assert(util.isUint53(options.minRelay));
|
||||
assert(util.isU64(options.minRelay));
|
||||
this.minRelay = options.minRelay;
|
||||
}
|
||||
|
||||
@ -2056,12 +2056,12 @@ MempoolOptions.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.maxFiles != null) {
|
||||
assert(util.isUInt32(options.maxFiles));
|
||||
assert(util.isU32(options.maxFiles));
|
||||
this.maxFiles = options.maxFiles;
|
||||
}
|
||||
|
||||
if (options.cacheSize != null) {
|
||||
assert(util.isUInt53(options.cacheSize));
|
||||
assert(util.isU64(options.cacheSize));
|
||||
this.cacheSize = options.cacheSize;
|
||||
}
|
||||
|
||||
|
||||
@ -1375,7 +1375,7 @@ HostEntry.prototype.fromJSON = function fromJSON(json, network) {
|
||||
assert(json.services.length > 0);
|
||||
assert(json.services.length <= 32);
|
||||
this.addr.services = parseInt(json.services, 2);
|
||||
assert(util.isUInt32(this.addr.services));
|
||||
assert(util.isU32(this.addr.services));
|
||||
}
|
||||
|
||||
if (json.time != null) {
|
||||
|
||||
@ -3900,12 +3900,12 @@ PoolOptions.prototype.fromOptions = function fromOptions(options) {
|
||||
this.listen = false;
|
||||
|
||||
if (options.services != null) {
|
||||
assert(util.isUInt32(options.services));
|
||||
assert(util.isU32(options.services));
|
||||
this.services = options.services;
|
||||
}
|
||||
|
||||
if (options.requiredServices != null) {
|
||||
assert(util.isUInt32(options.requiredServices));
|
||||
assert(util.isU32(options.requiredServices));
|
||||
this.requiredServices = options.requiredServices;
|
||||
}
|
||||
|
||||
|
||||
@ -255,13 +255,13 @@ Config.prototype.str = function str(key, fallback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a config option (as a number).
|
||||
* Get a config option (as an integer).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.num = function num(key, fallback) {
|
||||
Config.prototype.int = function int(key, fallback) {
|
||||
let value = this.get(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
@ -272,17 +272,46 @@ Config.prototype.num = function num(key, fallback) {
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new Error(`${fmt(key)} must be a positive integer.`);
|
||||
throw new Error(`${fmt(key)} must be an int.`);
|
||||
|
||||
if (!Number.isSafeInteger(value))
|
||||
throw new Error(`${fmt(key)} must be an int (53 bit max).`);
|
||||
|
||||
if (value % 1 !== 0)
|
||||
throw new Error(`${fmt(key)} must be an int (float).`);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!/^\d+$/.test(value))
|
||||
throw new Error(`${fmt(key)} must be a positive integer.`);
|
||||
if (!/^\-?\d+$/.test(value))
|
||||
throw new Error(`${fmt(key)} must be an int.`);
|
||||
|
||||
value = parseInt(value, 10);
|
||||
|
||||
if (!isFinite(value))
|
||||
throw new Error(`${fmt(key)} must be a positive integer.`);
|
||||
if (!Number.isSafeInteger(value))
|
||||
throw new Error(`${fmt(key)} must be an int (53 bit max).`);
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a config option (as a unsigned integer).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.uint = function uint(key, fallback) {
|
||||
const value = this.int(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < 0)
|
||||
throw new Error(`${fmt(key)} must be a uint.`);
|
||||
|
||||
return value;
|
||||
};
|
||||
@ -294,7 +323,7 @@ Config.prototype.num = function num(key, fallback) {
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.flt = function flt(key, fallback) {
|
||||
Config.prototype.float = function float(key, fallback) {
|
||||
let value = this.get(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
@ -306,10 +335,17 @@ Config.prototype.flt = function flt(key, fallback) {
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new Error(`${fmt(key)} must be a float.`);
|
||||
|
||||
if (!isFinite(value))
|
||||
throw new Error(`${fmt(key)} must be a float.`);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!/^\d*(?:\.\d*)?$/.test(value))
|
||||
if (!/^\-?\d*(?:\.\d*)?$/.test(value))
|
||||
throw new Error(`${fmt(key)} must be a float.`);
|
||||
|
||||
if (!/\d/.test(value))
|
||||
throw new Error(`${fmt(key)} must be a float.`);
|
||||
|
||||
value = parseFloat(value);
|
||||
@ -321,14 +357,14 @@ Config.prototype.flt = function flt(key, fallback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a satoshi number or btc string).
|
||||
* Get a config option (as a positive float).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.amt = function amt(key, fallback) {
|
||||
const value = this.get(key);
|
||||
Config.prototype.ufloat = function ufloat(key, fallback) {
|
||||
const value = this.float(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -336,21 +372,59 @@ Config.prototype.amt = function amt(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new Error(`${fmt(key)} must be an amount.`);
|
||||
if (value % 1 !== 0 || value < 0 || value > 0x1fffffffffffff)
|
||||
throw new Error(`${fmt(key)} must be an amount (u64).`);
|
||||
return value;
|
||||
}
|
||||
if (value < 0)
|
||||
throw new Error(`${fmt(key)} must be a positive float.`);
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a fixed number).
|
||||
* @param {String} key
|
||||
* @param {Number?} exp
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.fixed = function fixed(key, exp, fallback) {
|
||||
const value = this.float(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
try {
|
||||
return util.fromFixed(value, 8);
|
||||
return util.fromFixed(value.toString(10), exp || 0);
|
||||
} catch (e) {
|
||||
throw new Error(`${fmt(key)} must be an amount (parse).`);
|
||||
throw new Error(`${fmt(key)} must be a fixed number.`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a positive fixed number).
|
||||
* @param {String} key
|
||||
* @param {Number?} exp
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Config.prototype.ufixed = function ufixed(key, exp, fallback) {
|
||||
const value = this.fixed(key, exp);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < 0)
|
||||
throw new Error(`${fmt(key)} must be a positive fixed number.`);
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a config option (as a boolean).
|
||||
* @param {String} key
|
||||
@ -529,7 +603,7 @@ Config.prototype.path = function _path(key, fallback) {
|
||||
*/
|
||||
|
||||
Config.prototype.mb = function mb(key, fallback) {
|
||||
const value = this.num(key);
|
||||
const value = this.uint(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
@ -54,7 +54,7 @@ function FullNode(options) {
|
||||
workers: this.workers,
|
||||
db: this.config.str('db'),
|
||||
prefix: this.config.prefix,
|
||||
maxFiles: this.config.num('max-files'),
|
||||
maxFiles: this.config.uint('max-files'),
|
||||
cacheSize: this.config.mb('cache-size'),
|
||||
forceFlags: this.config.bool('force-flags'),
|
||||
bip91: this.config.bool('bip91'),
|
||||
@ -62,7 +62,7 @@ function FullNode(options) {
|
||||
prune: this.config.bool('prune'),
|
||||
checkpoints: this.config.bool('checkpoints'),
|
||||
coinCache: this.config.mb('coin-cache'),
|
||||
entryCache: this.config.num('entry-cache'),
|
||||
entryCache: this.config.uint('entry-cache'),
|
||||
indexTX: this.config.bool('index-tx'),
|
||||
indexAddress: this.config.bool('index-address')
|
||||
});
|
||||
@ -83,7 +83,7 @@ function FullNode(options) {
|
||||
persistent: this.config.bool('persistent-mempool'),
|
||||
maxSize: this.config.mb('mempool-size'),
|
||||
limitFree: this.config.bool('limit-free'),
|
||||
limitFreeRelay: this.config.num('limit-free-relay'),
|
||||
limitFreeRelay: this.config.uint('limit-free-relay'),
|
||||
requireStandard: this.config.bool('require-standard'),
|
||||
rejectAbsurdFees: this.config.bool('reject-absurd-fees'),
|
||||
replaceByFee: this.config.bool('replace-by-fee'),
|
||||
@ -103,8 +103,8 @@ function FullNode(options) {
|
||||
bip151: this.config.bool('bip151'),
|
||||
bip150: this.config.bool('bip150'),
|
||||
identityKey: this.config.buf('identity-key'),
|
||||
maxOutbound: this.config.num('max-outbound'),
|
||||
maxInbound: this.config.num('max-inbound'),
|
||||
maxOutbound: this.config.uint('max-outbound'),
|
||||
maxInbound: this.config.uint('max-inbound'),
|
||||
proxy: this.config.str('proxy'),
|
||||
onion: this.config.bool('onion'),
|
||||
upnp: this.config.bool('upnp'),
|
||||
@ -112,9 +112,9 @@ function FullNode(options) {
|
||||
nodes: this.config.array('nodes'),
|
||||
only: this.config.array('only'),
|
||||
publicHost: this.config.str('public-host'),
|
||||
publicPort: this.config.num('public-port'),
|
||||
publicPort: this.config.uint('public-port'),
|
||||
host: this.config.str('host'),
|
||||
port: this.config.num('port'),
|
||||
port: this.config.uint('port'),
|
||||
listen: this.config.bool('listen'),
|
||||
persistent: this.config.bool('persistent')
|
||||
});
|
||||
@ -129,9 +129,9 @@ function FullNode(options) {
|
||||
address: this.config.array('coinbase-address'),
|
||||
coinbaseFlags: this.config.str('coinbase-flags'),
|
||||
preverify: this.config.bool('preverify'),
|
||||
maxWeight: this.config.num('max-weight'),
|
||||
reservedWeight: this.config.num('reserved-weight'),
|
||||
reservedSigops: this.config.num('reserved-sigops')
|
||||
maxWeight: this.config.uint('max-weight'),
|
||||
reservedWeight: this.config.uint('reserved-weight'),
|
||||
reservedSigops: this.config.uint('reserved-sigops')
|
||||
});
|
||||
|
||||
// RPC needs access to the node.
|
||||
@ -148,7 +148,7 @@ function FullNode(options) {
|
||||
keyFile: this.config.path('ssl-key'),
|
||||
certFile: this.config.path('ssl-cert'),
|
||||
host: this.config.str('http-host'),
|
||||
port: this.config.num('http-port'),
|
||||
port: this.config.uint('http-port'),
|
||||
apiKey: this.config.str('api-key'),
|
||||
noAuth: this.config.bool('no-auth')
|
||||
});
|
||||
|
||||
@ -87,8 +87,8 @@ Node.prototype.initOptions = function initOptions() {
|
||||
|
||||
this.workers = new WorkerPool({
|
||||
enabled: config.bool('workers'),
|
||||
size: config.num('workers-size'),
|
||||
timeout: config.num('workers-timeout'),
|
||||
size: config.uint('workers-size'),
|
||||
timeout: config.uint('workers-timeout'),
|
||||
file: config.str('worker-file')
|
||||
});
|
||||
};
|
||||
|
||||
@ -49,9 +49,9 @@ function SPVNode(options) {
|
||||
logger: this.logger,
|
||||
db: this.config.str('db'),
|
||||
prefix: this.config.prefix,
|
||||
maxFiles: this.config.num('max-files'),
|
||||
maxFiles: this.config.uint('max-files'),
|
||||
cacheSize: this.config.mb('cache-size'),
|
||||
entryCache: this.config.num('entry-cache'),
|
||||
entryCache: this.config.uint('entry-cache'),
|
||||
forceFlags: this.config.bool('force-flags'),
|
||||
checkpoints: this.config.bool('checkpoints'),
|
||||
bip91: this.config.bool('bip91'),
|
||||
@ -73,7 +73,7 @@ function SPVNode(options) {
|
||||
bip151: this.config.bool('bip151'),
|
||||
bip150: this.config.bool('bip150'),
|
||||
identityKey: this.config.buf('identity-key'),
|
||||
maxOutbound: this.config.num('max-outbound'),
|
||||
maxOutbound: this.config.uint('max-outbound'),
|
||||
persistent: this.config.bool('persistent'),
|
||||
selfish: true,
|
||||
listen: false
|
||||
@ -91,7 +91,7 @@ function SPVNode(options) {
|
||||
keyFile: this.config.path('ssl-key'),
|
||||
certFile: this.config.path('ssl-cert'),
|
||||
host: this.config.str('http-host'),
|
||||
port: this.config.num('http-port'),
|
||||
port: this.config.uint('http-port'),
|
||||
apiKey: this.config.str('api-key'),
|
||||
noAuth: this.config.bool('no-auth')
|
||||
});
|
||||
|
||||
@ -61,13 +61,13 @@ Coin.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'Coin data is required.');
|
||||
|
||||
if (options.version != null) {
|
||||
assert(util.isUInt32(options.version), 'Version must be a uint32.');
|
||||
assert(util.isU32(options.version), 'Version must be a uint32.');
|
||||
this.version = options.version;
|
||||
}
|
||||
|
||||
if (options.height != null) {
|
||||
if (options.height !== -1) {
|
||||
assert(util.isUInt32(options.height), 'Height must be a uint32.');
|
||||
assert(util.isU32(options.height), 'Height must be a uint32.');
|
||||
this.height = options.height;
|
||||
} else {
|
||||
this.height = -1;
|
||||
@ -75,7 +75,7 @@ Coin.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.value != null) {
|
||||
assert(util.isUInt53(options.value), 'Value must be a uint53.');
|
||||
assert(util.isU64(options.value), 'Value must be a uint64.');
|
||||
this.value = options.value;
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ Coin.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.index != null) {
|
||||
assert(util.isUInt32(options.index), 'Index must be a uint32.');
|
||||
assert(util.isU32(options.index), 'Index must be a uint32.');
|
||||
this.index = options.index;
|
||||
}
|
||||
|
||||
@ -262,10 +262,10 @@ Coin.prototype.getJSON = function getJSON(network, minimal) {
|
||||
|
||||
Coin.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'Coin data required.');
|
||||
assert(util.isUInt32(json.version), 'Version must be a uint32.');
|
||||
assert(json.height === -1 || util.isUInt32(json.height),
|
||||
assert(util.isU32(json.version), 'Version must be a uint32.');
|
||||
assert(json.height === -1 || util.isU32(json.height),
|
||||
'Height must be a uint32.');
|
||||
assert(util.isUInt53(json.value), 'Value must be a uint53.');
|
||||
assert(util.isU64(json.value), 'Value must be a uint64.');
|
||||
assert(typeof json.coinbase === 'boolean', 'Coinbase must be a boolean.');
|
||||
|
||||
this.version = json.version;
|
||||
@ -277,7 +277,7 @@ Coin.prototype.fromJSON = function fromJSON(json) {
|
||||
if (json.hash != null) {
|
||||
assert(typeof json.hash === 'string', 'Hash must be a string.');
|
||||
assert(json.hash.length === 64, 'Hash must be a string.');
|
||||
assert(util.isUInt32(json.index), 'Index must be a uint32.');
|
||||
assert(util.isU32(json.index), 'Index must be a uint32.');
|
||||
this.hash = util.revHex(json.hash);
|
||||
this.index = json.index;
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ Input.prototype.fromOptions = function fromOptions(options) {
|
||||
this.script.fromOptions(options.script);
|
||||
|
||||
if (options.sequence != null) {
|
||||
assert(util.isUInt32(options.sequence), 'Sequence must be a uint32.');
|
||||
assert(util.isU32(options.sequence), 'Sequence must be a uint32.');
|
||||
this.sequence = options.sequence;
|
||||
}
|
||||
|
||||
@ -311,7 +311,7 @@ Input.prototype.getJSON = function getJSON(network, coin) {
|
||||
|
||||
Input.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'Input data is required.');
|
||||
assert(util.isUInt32(json.sequence), 'Sequence must be a uint32.');
|
||||
assert(util.isU32(json.sequence), 'Sequence must be a uint32.');
|
||||
this.prevout.fromJSON(json.prevout);
|
||||
this.script.fromJSON(json.script);
|
||||
this.witness.fromJSON(json.witness);
|
||||
|
||||
@ -57,7 +57,7 @@ MerkleBlock.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'MerkleBlock data is required.');
|
||||
assert(Array.isArray(options.hashes));
|
||||
assert(Buffer.isBuffer(options.flags));
|
||||
assert(util.isUInt32(options.totalTX));
|
||||
assert(util.isU32(options.totalTX));
|
||||
|
||||
if (options.hashes) {
|
||||
for (let hash of options.hashes) {
|
||||
@ -74,7 +74,7 @@ MerkleBlock.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.totalTX != null) {
|
||||
assert(util.isUInt32(options.totalTX));
|
||||
assert(util.isU32(options.totalTX));
|
||||
this.totalTX = options.totalTX;
|
||||
}
|
||||
|
||||
@ -468,7 +468,7 @@ MerkleBlock.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'MerkleBlock data is required.');
|
||||
assert(Array.isArray(json.hashes));
|
||||
assert(typeof json.flags === 'string');
|
||||
assert(util.isUInt32(json.totalTX));
|
||||
assert(util.isU32(json.totalTX));
|
||||
|
||||
this.parseJSON(json);
|
||||
|
||||
|
||||
@ -66,12 +66,12 @@ util.inherits(MTX, TX);
|
||||
|
||||
MTX.prototype.fromOptions = function fromOptions(options) {
|
||||
if (options.version != null) {
|
||||
assert(util.isUInt32(options.version), 'Version must a be uint32.');
|
||||
assert(util.isU32(options.version), 'Version must a be uint32.');
|
||||
this.version = options.version;
|
||||
}
|
||||
|
||||
if (options.flag != null) {
|
||||
assert(util.isUInt8(options.flag), 'Flag must be a uint8.');
|
||||
assert(util.isU8(options.flag), 'Flag must be a uint8.');
|
||||
this.flag = options.flag;
|
||||
}
|
||||
|
||||
@ -88,13 +88,13 @@ MTX.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.locktime != null) {
|
||||
assert(util.isUInt32(options.locktime), 'Locktime must be a uint32.');
|
||||
assert(util.isU32(options.locktime), 'Locktime must be a uint32.');
|
||||
this.locktime = options.locktime;
|
||||
}
|
||||
|
||||
if (options.changeIndex != null) {
|
||||
if (options.changeIndex !== -1) {
|
||||
assert(util.isUInt32(options.changeIndex),
|
||||
assert(util.isU32(options.changeIndex),
|
||||
'Change index must be a uint32.');
|
||||
this.changeIndex = options.changeIndex;
|
||||
} else {
|
||||
@ -227,7 +227,7 @@ MTX.prototype.addOutput = function addOutput(script, value) {
|
||||
let output;
|
||||
|
||||
if (value != null) {
|
||||
assert(util.isUInt53(value), 'Value must be a uint53.');
|
||||
assert(util.isU64(value), 'Value must be a uint64.');
|
||||
output = Output.fromScript(script, value);
|
||||
} else {
|
||||
output = Output.fromOptions(script);
|
||||
@ -1290,7 +1290,7 @@ MTX.prototype.avoidFeeSniping = function avoidFeeSniping(height) {
|
||||
*/
|
||||
|
||||
MTX.prototype.setLocktime = function setLocktime(locktime) {
|
||||
assert(util.isUInt32(locktime), 'Locktime must be a uint32.');
|
||||
assert(util.isU32(locktime), 'Locktime must be a uint32.');
|
||||
assert(this.inputs.length > 0, 'Cannot set sequence with no inputs.');
|
||||
|
||||
for (const input of this.inputs) {
|
||||
@ -1312,7 +1312,7 @@ MTX.prototype.setSequence = function setSequence(index, locktime, seconds) {
|
||||
const input = this.inputs[index];
|
||||
|
||||
assert(input, 'Input does not exist.');
|
||||
assert(util.isUInt32(locktime), 'Locktime must be a uint32.');
|
||||
assert(util.isU32(locktime), 'Locktime must be a uint32.');
|
||||
|
||||
this.version = 2;
|
||||
|
||||
@ -1508,7 +1508,7 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) {
|
||||
|
||||
if (options.subtractFee != null) {
|
||||
if (typeof options.subtractFee === 'number') {
|
||||
assert(util.isUInt32(options.subtractFee));
|
||||
assert(util.isU32(options.subtractFee));
|
||||
this.subtractFee = options.subtractFee;
|
||||
this.shouldSubtract = true;
|
||||
} else {
|
||||
|
||||
@ -31,7 +31,7 @@ function Outpoint(hash, index) {
|
||||
|
||||
if (hash != null) {
|
||||
assert(typeof hash === 'string', 'Hash must be a string.');
|
||||
assert(util.isUInt32(index), 'Index must be a uint32.');
|
||||
assert(util.isU32(index), 'Index must be a uint32.');
|
||||
this.hash = hash;
|
||||
this.index = index;
|
||||
}
|
||||
@ -46,7 +46,7 @@ function Outpoint(hash, index) {
|
||||
Outpoint.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'Outpoint data is required.');
|
||||
assert(typeof options.hash === 'string', 'Hash must be a string.');
|
||||
assert(util.isUInt32(options.index), 'Index must be a uint32.');
|
||||
assert(util.isU32(options.index), 'Index must be a uint32.');
|
||||
this.hash = options.hash;
|
||||
this.index = options.index;
|
||||
return this;
|
||||
@ -204,7 +204,7 @@ Outpoint.fromRaw = function fromRaw(data) {
|
||||
Outpoint.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'Outpoint data is required.');
|
||||
assert(typeof json.hash === 'string', 'Hash must be a string.');
|
||||
assert(util.isUInt32(json.index), 'Index must be a uint32.');
|
||||
assert(util.isU32(json.index), 'Index must be a uint32.');
|
||||
this.hash = util.revHex(json.hash);
|
||||
this.index = json.index;
|
||||
return this;
|
||||
|
||||
@ -48,7 +48,7 @@ Output.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'Output data is required.');
|
||||
|
||||
if (options.value) {
|
||||
assert(util.isUInt53(options.value), 'Value must be a uint53.');
|
||||
assert(util.isU64(options.value), 'Value must be a uint64.');
|
||||
this.value = options.value;
|
||||
}
|
||||
|
||||
@ -87,7 +87,7 @@ Output.prototype.fromScript = function fromScript(script, value) {
|
||||
script = Script.fromAddress(script);
|
||||
|
||||
assert(script instanceof Script, 'Script must be a Script.');
|
||||
assert(util.isUInt53(value), 'Value must be a uint53.');
|
||||
assert(util.isU64(value), 'Value must be a uint64.');
|
||||
|
||||
this.script = script;
|
||||
this.value = value;
|
||||
@ -249,7 +249,7 @@ Output.prototype.isDust = function isDust(rate) {
|
||||
|
||||
Output.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'Output data is required.');
|
||||
assert(util.isUInt53(json.value), 'Value must be a uint53.');
|
||||
assert(util.isU64(json.value), 'Value must be a uint64.');
|
||||
this.value = json.value;
|
||||
this.script.fromJSON(json.script);
|
||||
return this;
|
||||
|
||||
@ -80,12 +80,12 @@ TX.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'TX data is required.');
|
||||
|
||||
if (options.version != null) {
|
||||
assert(util.isUInt32(options.version), 'Version must be a uint32.');
|
||||
assert(util.isU32(options.version), 'Version must be a uint32.');
|
||||
this.version = options.version;
|
||||
}
|
||||
|
||||
if (options.flag != null) {
|
||||
assert(util.isUInt8(options.flag), 'Flag must be a uint8.');
|
||||
assert(util.isU8(options.flag), 'Flag must be a uint8.');
|
||||
this.flag = options.flag;
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ TX.prototype.fromOptions = function fromOptions(options) {
|
||||
}
|
||||
|
||||
if (options.locktime != null) {
|
||||
assert(util.isUInt32(options.locktime), 'Locktime must be a uint32.');
|
||||
assert(util.isU32(options.locktime), 'Locktime must be a uint32.');
|
||||
this.locktime = options.locktime;
|
||||
}
|
||||
|
||||
@ -2181,11 +2181,11 @@ TX.prototype.getJSON = function getJSON(network, view, entry, index) {
|
||||
|
||||
TX.prototype.fromJSON = function fromJSON(json) {
|
||||
assert(json, 'TX data is required.');
|
||||
assert(util.isUInt32(json.version), 'Version must be a uint32.');
|
||||
assert(util.isUInt8(json.flag), 'Flag must be a uint8.');
|
||||
assert(util.isU32(json.version), 'Version must be a uint32.');
|
||||
assert(util.isU8(json.flag), 'Flag must be a uint8.');
|
||||
assert(Array.isArray(json.inputs), 'Inputs must be an array.');
|
||||
assert(Array.isArray(json.outputs), 'Outputs must be an array.');
|
||||
assert(util.isUInt32(json.locktime), 'Locktime must be a uint32.');
|
||||
assert(util.isU32(json.locktime), 'Locktime must be a uint32.');
|
||||
|
||||
this.version = json.version;
|
||||
this.flag = json.flag;
|
||||
|
||||
@ -158,7 +158,7 @@ util.isSafeAddition = function isSafeAddition(a, b) {
|
||||
*/
|
||||
|
||||
util.isNumber = function isNumber(value) {
|
||||
return typeof value === 'number' && Number.isSafeInteger(value);
|
||||
return Number.isSafeInteger(value);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -171,24 +171,34 @@ util.isInt = function isInt(value) {
|
||||
return util.isNumber(value) && value % 1 === 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is an int.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isUint = function isUint(value) {
|
||||
return util.isInt(value) && value >= 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is an int8.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isInt8 = function isInt8(value) {
|
||||
util.isI8 = function isI8(value) {
|
||||
return (value | 0) === value && value >= -0x80 && value <= 0x7f;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint8.
|
||||
* Test whether an object is an int16.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isUInt8 = function isUInt8(value) {
|
||||
return (value >>> 0) === value && value >= 0 && value <= 0xff;
|
||||
util.isI16 = function isI16(value) {
|
||||
return (value | 0) === value && value >= -0x8000 && value <= 0x7fff;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -197,38 +207,58 @@ util.isUInt8 = function isUInt8(value) {
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isInt32 = function isInt32(value) {
|
||||
util.isI32 = function isI32(value) {
|
||||
return (value | 0) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint32.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isUInt32 = function isUInt32(value) {
|
||||
return (value >>> 0) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a int53.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isInt53 = function isInt53(value) {
|
||||
util.isI64 = function isI64(value) {
|
||||
return util.isInt(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint8.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isU8 = function isU8(value) {
|
||||
return (value & 0xff) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint16.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isU16 = function isU16(value) {
|
||||
return (value & 0xffff) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint32.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isU32 = function isU32(value) {
|
||||
return (value >>> 0) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether an object is a uint53.
|
||||
* @param {Number?} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isUInt53 = function isUInt53(value) {
|
||||
return util.isInt(value) && value >= 0;
|
||||
util.isU64 = function isU64(value) {
|
||||
return util.isUint(value);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -120,13 +120,13 @@ Validator.prototype.str = function str(key, fallback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a number).
|
||||
* Get a value (as an integer).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.num = function num(key, fallback) {
|
||||
Validator.prototype.int = function int(key, fallback) {
|
||||
let value = this.get(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
@ -137,29 +137,58 @@ Validator.prototype.num = function num(key, fallback) {
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new ValidationError(key, 'number');
|
||||
throw new ValidationError(key, 'int');
|
||||
|
||||
if (!Number.isSafeInteger(value))
|
||||
throw new ValidationError(key, 'int (53 bit max)');
|
||||
|
||||
if (value % 1 !== 0)
|
||||
throw new ValidationError(key, 'int (float)');
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!/^\d+$/.test(value))
|
||||
throw new ValidationError(key, 'number');
|
||||
if (!/^\-?\d+$/.test(value))
|
||||
throw new ValidationError(key, 'int');
|
||||
|
||||
value = parseInt(value, 10);
|
||||
|
||||
if (!isFinite(value))
|
||||
throw new ValidationError(key, 'number');
|
||||
if (!Number.isSafeInteger(value))
|
||||
throw new ValidationError(key, 'int (53 bit max)');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a number).
|
||||
* Get a value (as a signed integer).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.flt = function flt(key, fallback) {
|
||||
Validator.prototype.uint = function uint(key, fallback) {
|
||||
const value = this.int(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < 0)
|
||||
throw new ValidationError(key, 'uint');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a float).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.float = function float(key, fallback) {
|
||||
let value = this.get(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
@ -171,10 +200,17 @@ Validator.prototype.flt = function flt(key, fallback) {
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new ValidationError(key, 'float');
|
||||
|
||||
if (!isFinite(value))
|
||||
throw new ValidationError(key, 'float');
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!/^\d*(?:\.\d*)?$/.test(value))
|
||||
if (!/^\-?\d*(?:\.\d*)?$/.test(value))
|
||||
throw new ValidationError(key, 'float');
|
||||
|
||||
if (!/\d/.test(value))
|
||||
throw new ValidationError(key, 'float');
|
||||
|
||||
value = parseFloat(value);
|
||||
@ -186,14 +222,14 @@ Validator.prototype.flt = function flt(key, fallback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a uint32).
|
||||
* Get a value (as a positive float).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.u32 = function u32(key, fallback) {
|
||||
const value = this.num(key);
|
||||
Validator.prototype.ufloat = function ufloat(key, fallback) {
|
||||
const value = this.float(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -201,21 +237,22 @@ Validator.prototype.u32 = function u32(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if ((value >>> 0) !== value)
|
||||
throw new ValidationError(key, 'uint32');
|
||||
if (value < 0)
|
||||
throw new ValidationError(key, 'positive float');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a uint64).
|
||||
* Get a value (as a fixed number).
|
||||
* @param {String} key
|
||||
* @param {Number?} exp
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.u64 = function u64(key, fallback) {
|
||||
const value = this.num(key);
|
||||
Validator.prototype.fixed = function fixed(key, exp, fallback) {
|
||||
const value = this.float(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -223,8 +260,76 @@ Validator.prototype.u64 = function u64(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value % 1 !== 0 || value < 0 || value > 0x1fffffffffffff)
|
||||
throw new ValidationError(key, 'uint64');
|
||||
try {
|
||||
return util.fromFixed(value.toString(10), exp || 0);
|
||||
} catch (e) {
|
||||
throw new ValidationError(key, 'fixed number');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a positive fixed number).
|
||||
* @param {String} key
|
||||
* @param {Number?} exp
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.ufixed = function ufixed(key, exp, fallback) {
|
||||
const value = this.fixed(key, exp);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < 0)
|
||||
throw new ValidationError(key, 'positive fixed number');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as an int32).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.i8 = function i8(key, fallback) {
|
||||
const value = this.int(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < -0x80 || value > 0x7f)
|
||||
throw new ValidationError(key, 'i8');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as an int32).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.i16 = function i16(key, fallback) {
|
||||
const value = this.int(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < -0x8000 || value > 0x7fff)
|
||||
throw new ValidationError(key, 'i16');
|
||||
|
||||
return value;
|
||||
};
|
||||
@ -237,7 +342,7 @@ Validator.prototype.u64 = function u64(key, fallback) {
|
||||
*/
|
||||
|
||||
Validator.prototype.i32 = function i32(key, fallback) {
|
||||
const value = this.num(key);
|
||||
const value = this.int(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -259,7 +364,18 @@ Validator.prototype.i32 = function i32(key, fallback) {
|
||||
*/
|
||||
|
||||
Validator.prototype.i64 = function i64(key, fallback) {
|
||||
const value = this.num(key);
|
||||
return this.int(key, fallback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a uint32).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.u8 = function u8(key, fallback) {
|
||||
const value = this.uint(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -267,21 +383,21 @@ Validator.prototype.i64 = function i64(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value % 1 !== 0 || Math.abs(value) > 0x1fffffffffffff)
|
||||
throw new ValidationError(key, 'int64');
|
||||
if ((value & 0xff) !== value)
|
||||
throw new ValidationError(key, 'uint8');
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a satoshi number or btc string).
|
||||
* Get a value (as a uint16).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.amt = function amt(key, fallback) {
|
||||
const value = this.get(key);
|
||||
Validator.prototype.u16 = function u16(key, fallback) {
|
||||
const value = this.uint(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -289,30 +405,21 @@ Validator.prototype.amt = function amt(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new ValidationError(key, 'amount');
|
||||
if (value % 1 !== 0 || value < 0 || value > 0x1fffffffffffff)
|
||||
throw new ValidationError(key, 'amount');
|
||||
return value;
|
||||
}
|
||||
if ((value & 0xffff) !== value)
|
||||
throw new ValidationError(key, 'uint16');
|
||||
|
||||
try {
|
||||
return util.fromFixed(value, 8);
|
||||
} catch (e) {
|
||||
throw new ValidationError(key, 'amount');
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a btc float).
|
||||
* Get a value (as a uint32).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.btc = function btc(key, fallback) {
|
||||
const value = this.flt(key);
|
||||
Validator.prototype.u32 = function u32(key, fallback) {
|
||||
const value = this.uint(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
@ -320,14 +427,21 @@ Validator.prototype.btc = function btc(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value < 0 || value > 0x1fffffffffffff)
|
||||
throw new ValidationError(key, 'btc float (uint64)');
|
||||
if ((value >>> 0) !== value)
|
||||
throw new ValidationError(key, 'uint32');
|
||||
|
||||
try {
|
||||
return util.fromFixed(value.toString(10), 8);
|
||||
} catch (e) {
|
||||
throw new ValidationError(key, 'btc float');
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a uint64).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.u64 = function u64(key, fallback) {
|
||||
return this.uint(key, fallback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -386,37 +500,7 @@ Validator.prototype.numhash = function numhash(key, fallback) {
|
||||
if (typeof value === 'string')
|
||||
return this.hash(key);
|
||||
|
||||
return this.num(key);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a value (as a number or string).
|
||||
* @param {String} key
|
||||
* @param {Object?} fallback
|
||||
* @returns {Number|String|null}
|
||||
*/
|
||||
|
||||
Validator.prototype.numstr = function numstr(key, fallback) {
|
||||
const value = this.get(key);
|
||||
|
||||
if (fallback === undefined)
|
||||
fallback = null;
|
||||
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
if (typeof value !== 'number')
|
||||
throw new ValidationError(key, 'number or string');
|
||||
return value;
|
||||
}
|
||||
|
||||
const num = parseInt(value, 10);
|
||||
|
||||
if (!isFinite(num))
|
||||
return value;
|
||||
|
||||
return num;
|
||||
return this.uint(key);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -39,18 +39,18 @@ plugin.init = function init(node) {
|
||||
client: client,
|
||||
prefix: config.prefix,
|
||||
db: config.str(['wallet-db', 'db']),
|
||||
maxFiles: config.num('wallet-max-files'),
|
||||
maxFiles: config.uint('wallet-max-files'),
|
||||
cacheSize: config.mb('wallet-cache-size'),
|
||||
witness: config.bool('wallet-witness'),
|
||||
checkpoints: config.bool('wallet-checkpoints'),
|
||||
startHeight: config.num('wallet-start-height'),
|
||||
startHeight: config.uint('wallet-start-height'),
|
||||
wipeNoReally: config.bool('wallet-wipe-no-really'),
|
||||
apiKey: config.str(['wallet-api-key', 'api-key']),
|
||||
walletAuth: config.bool('wallet-auth'),
|
||||
noAuth: config.bool(['wallet-no-auth', 'no-auth']),
|
||||
ssl: config.str('wallet-ssl'),
|
||||
host: config.str('wallet-host'),
|
||||
port: config.num('wallet-port'),
|
||||
port: config.uint('wallet-port'),
|
||||
spv: node.spv,
|
||||
verify: node.spv,
|
||||
listen: false
|
||||
|
||||
@ -158,7 +158,7 @@ RPC.prototype.fundRawTransaction = async function fundRawTransaction(args, help)
|
||||
const valid = new Validator([options]);
|
||||
|
||||
if (valid.has('feeRate'))
|
||||
rate = valid.btc('feeRate');
|
||||
rate = valid.ufixed('feeRate', 8);
|
||||
|
||||
if (valid.has('changeAddress'))
|
||||
change = valid.str('changeAddress');
|
||||
@ -1248,7 +1248,7 @@ RPC.prototype.sendFrom = async function sendFrom(args, help) {
|
||||
const valid = new Validator([args]);
|
||||
let name = valid.str(0);
|
||||
const str = valid.str(1);
|
||||
const value = valid.btc(2);
|
||||
const value = valid.ufixed(2, 8);
|
||||
const minconf = valid.u32(3, 0);
|
||||
|
||||
const addr = parseAddress(str, this.network);
|
||||
@ -1300,7 +1300,7 @@ RPC.prototype.sendMany = async function sendMany(args, help) {
|
||||
const outputs = [];
|
||||
|
||||
for (const key of Object.keys(sendTo)) {
|
||||
const value = to.btc(key);
|
||||
const value = to.ufixed(key, 8);
|
||||
const addr = parseAddress(key, this.network);
|
||||
const hash = addr.getHash('hex');
|
||||
|
||||
@ -1340,7 +1340,7 @@ RPC.prototype.sendToAddress = async function sendToAddress(args, help) {
|
||||
const wallet = this.wallet;
|
||||
const valid = new Validator([args]);
|
||||
const str = valid.str(0);
|
||||
const value = valid.btc(1);
|
||||
const value = valid.ufixed(1, 8);
|
||||
const subtractFee = valid.bool(4, false);
|
||||
|
||||
const addr = parseAddress(str, this.network);
|
||||
@ -1374,7 +1374,7 @@ RPC.prototype.setAccount = async function setAccount(args, help) {
|
||||
|
||||
RPC.prototype.setTXFee = async function setTXFee(args, help) {
|
||||
const valid = new Validator([args]);
|
||||
const rate = valid.btc(0);
|
||||
const rate = valid.ufixed(0, 8);
|
||||
|
||||
if (help || args.length < 1 || args.length > 1)
|
||||
throw new RPCError(errs.MISC_ERROR, 'settxfee amount');
|
||||
|
||||
@ -54,8 +54,8 @@ server.create = function create(options) {
|
||||
|
||||
const workers = new WorkerPool({
|
||||
enabled: config.str('workers-enabled'),
|
||||
size: config.num('workers-size'),
|
||||
timeout: config.num('workers-timeout')
|
||||
size: config.uint('workers-size'),
|
||||
timeout: config.uint('workers-timeout')
|
||||
});
|
||||
|
||||
const wdb = new WalletDB({
|
||||
@ -65,18 +65,18 @@ server.create = function create(options) {
|
||||
client: client,
|
||||
prefix: config.prefix,
|
||||
db: config.str('db'),
|
||||
maxFiles: config.num('max-files'),
|
||||
maxFiles: config.uint('max-files'),
|
||||
cacheSize: config.mb('cache-size'),
|
||||
witness: config.bool('witness'),
|
||||
checkpoints: config.bool('checkpoints'),
|
||||
startHeight: config.num('start-height'),
|
||||
startHeight: config.uint('start-height'),
|
||||
wipeNoReally: config.bool('wipe-no-really'),
|
||||
apiKey: config.str('api-key'),
|
||||
walletAuth: config.bool('auth'),
|
||||
noAuth: config.bool('no-auth'),
|
||||
ssl: config.str('ssl'),
|
||||
host: config.str('host'),
|
||||
port: config.num('port'),
|
||||
port: config.uint('port'),
|
||||
spv: config.bool('spv'),
|
||||
verify: config.bool('spv'),
|
||||
listen: true
|
||||
|
||||
@ -2478,7 +2478,7 @@ TXDB.prototype.getAccountBalance = async function getAccountBalance(account) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.zap = async function zap(account, age) {
|
||||
assert(util.isUInt32(age));
|
||||
assert(util.isU32(age));
|
||||
|
||||
const now = util.now();
|
||||
|
||||
|
||||
@ -1620,7 +1620,7 @@ Wallet.prototype._send = async function _send(options, passphrase) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.increaseFee = async function increaseFee(hash, rate, passphrase) {
|
||||
assert(util.isUInt32(rate), 'Rate must be a number.');
|
||||
assert(util.isU32(rate), 'Rate must be a number.');
|
||||
|
||||
const wtx = await this.getTX(hash);
|
||||
|
||||
|
||||
@ -447,7 +447,7 @@ WalletDB.prototype.scan = async function scan(height) {
|
||||
if (height == null)
|
||||
height = this.state.startHeight;
|
||||
|
||||
assert(util.isUInt32(height), 'WDB: Must pass in a height.');
|
||||
assert(util.isU32(height), 'WDB: Must pass in a height.');
|
||||
|
||||
await this.rollback(height);
|
||||
|
||||
|
||||
@ -54,7 +54,7 @@ function Coins(options) {
|
||||
|
||||
Coins.prototype.fromOptions = function fromOptions(options) {
|
||||
if (options.version != null) {
|
||||
assert(util.isUInt32(options.version));
|
||||
assert(util.isU32(options.version));
|
||||
this.version = options.version;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user