Adjust assert usage (#4966)
* Adjust assert usage * Adjust assert * Apply suggestions from code review
This commit is contained in:
@@ -11,7 +11,7 @@ import type { GenericExtrinsicEra } from './ExtrinsicEra';
|
||||
import type { ExtrinsicValueV4 } from './v4/Extrinsic';
|
||||
|
||||
import { AbstractBase } from '@polkadot/types-codec';
|
||||
import { assert, compactAddLength, compactFromU8a, compactToU8a, isHex, isU8a, objectProperty, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { compactAddLength, compactFromU8a, compactToU8a, isHex, isU8a, objectProperty, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { BIT_SIGNED, BIT_UNSIGNED, DEFAULT_VERSION, UNMASK_VERSION } from './constants';
|
||||
|
||||
@@ -69,7 +69,9 @@ function decodeU8a (registry: Registry, value: Uint8Array, version: number): Ext
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
const total = offset + length.toNumber();
|
||||
|
||||
assert(total <= value.length, () => `Extrinsic: length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
if (total > value.length) {
|
||||
throw new Error(`Extrinsic: length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
}
|
||||
|
||||
const data = value.subarray(offset, total);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { BN } from '@polkadot/util';
|
||||
import type { IExtrinsicEra, INumber } from '../types';
|
||||
|
||||
import { Enum, Raw, Tuple, U64 } from '@polkadot/types-codec';
|
||||
import { assert, bnToBn, formatNumber, hexToU8a, isHex, isObject, isU8a, u8aToBn, u8aToU8a } from '@polkadot/util';
|
||||
import { bnToBn, formatNumber, hexToU8a, isHex, isObject, isU8a, u8aToBn, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { IMMORTAL_ERA } from './constants';
|
||||
|
||||
@@ -76,7 +76,9 @@ function decodeMortalU8a (registry: Registry, value: Uint8Array): MortalEraValue
|
||||
const quantizeFactor = Math.max(period >> 12, 1);
|
||||
const phase = (encoded >> 4) * quantizeFactor;
|
||||
|
||||
assert(period >= 4 && phase < period, 'Invalid data passed to Mortal era');
|
||||
if (period < 4 || phase >= period) {
|
||||
throw new Error('Invalid data passed to Mortal era');
|
||||
}
|
||||
|
||||
return [new U64(registry, period), new U64(registry, phase)];
|
||||
}
|
||||
@@ -255,7 +257,9 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
* @description Returns the item as a [[ImmortalEra]]
|
||||
*/
|
||||
public get asImmortalEra (): ImmortalEra {
|
||||
assert(this.isImmortalEra, () => `Cannot convert '${this.type}' via asImmortalEra`);
|
||||
if (!this.isImmortalEra) {
|
||||
throw new Error(`Cannot convert '${this.type}' via asImmortalEra`);
|
||||
}
|
||||
|
||||
return this.inner as ImmortalEra;
|
||||
}
|
||||
@@ -264,7 +268,9 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
* @description Returns the item as a [[MortalEra]]
|
||||
*/
|
||||
public get asMortalEra (): MortalEra {
|
||||
assert(this.isMortalEra, () => `Cannot convert '${this.type}' via asMortalEra`);
|
||||
if (!this.isMortalEra) {
|
||||
throw new Error(`Cannot convert '${this.type}' via asMortalEra`);
|
||||
}
|
||||
|
||||
return this.inner as MortalEra;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { ExtrinsicPayloadValue, ICompact, IExtrinsicSignature, IKeyringPair
|
||||
import type { ExtrinsicSignatureOptions } from '../types';
|
||||
|
||||
import { Struct } from '@polkadot/types-codec';
|
||||
import { assert, isU8a, isUndefined, objectProperties, objectSpread, stringify, u8aToHex } from '@polkadot/util';
|
||||
import { isU8a, isUndefined, objectProperties, objectSpread, stringify, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
|
||||
import { GenericExtrinsicPayloadV4 } from './ExtrinsicPayload';
|
||||
@@ -165,7 +165,9 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
|
||||
* @description Generate a payload and applies the signature from a keypair
|
||||
*/
|
||||
public sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature {
|
||||
assert(account && account.addressRaw, () => `Expected a valid keypair for signing, found ${stringify(account)}`);
|
||||
if (!account || !account.addressRaw) {
|
||||
throw new Error(`Expected a valid keypair for signing, found ${stringify(account)}`);
|
||||
}
|
||||
|
||||
const payload = this.createPayload(method, options);
|
||||
|
||||
@@ -180,7 +182,9 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
|
||||
* @description Generate a payload and applies a fake signature
|
||||
*/
|
||||
public signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature {
|
||||
assert(address, () => `Expected a valid address for signing, found ${stringify(address)}`);
|
||||
if (!address) {
|
||||
throw new Error(`Expected a valid address for signing, found ${stringify(address)}`);
|
||||
}
|
||||
|
||||
const payload = this.createPayload(method, options);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { AnyString, AnyU8a, Registry } from '@polkadot/types-codec/types';
|
||||
|
||||
import { U8aFixed } from '@polkadot/types-codec';
|
||||
import { assert, hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
/** @internal */
|
||||
@@ -34,7 +34,9 @@ export class GenericAccountId extends U8aFixed {
|
||||
const decoded = decodeAccountId(value);
|
||||
|
||||
// Part of stream containing >= 32 bytes or a all empty (defaults)
|
||||
assert(decoded.length >= 32 || !decoded.some((b) => b), () => `Invalid AccountId provided, expected 32 bytes, found ${decoded.length}`);
|
||||
if (decoded.length < 32 && decoded.some((b) => b)) {
|
||||
throw new Error(`Invalid AccountId provided, expected 32 bytes, found ${decoded.length}`);
|
||||
}
|
||||
|
||||
super(registry, decoded, 256);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { IU8a, Registry } from '@polkadot/types-codec/types';
|
||||
import type { H256 } from '../interfaces/runtime';
|
||||
|
||||
import { Bytes, Enum } from '@polkadot/types-codec';
|
||||
import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
function decodeDataU8a (registry: Registry, value: Uint8Array): [undefined | Uint8Array, number | undefined] {
|
||||
@@ -56,7 +56,9 @@ export class Data extends Enum {
|
||||
ShaThree256: 'H256' // 5
|
||||
}, ...decodeData(registry, value));
|
||||
|
||||
assert(!this.isRaw || this.asRaw.length <= 32, 'Data.Raw values are limited to a maximum length of 32 bytes');
|
||||
if (this.isRaw && this.asRaw.length > 32) {
|
||||
throw new Error('Data.Raw values are limited to a maximum length of 32 bytes');
|
||||
}
|
||||
}
|
||||
|
||||
public get asBlakeTwo256 (): H256 {
|
||||
|
||||
@@ -9,7 +9,7 @@ import type { InterfaceTypes, IStorageKey, Registry } from '../types';
|
||||
import type { StorageEntry } from './types';
|
||||
|
||||
import { Bytes } from '@polkadot/types-codec';
|
||||
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
|
||||
import { isFunction, isString, isU8a } from '@polkadot/util';
|
||||
|
||||
import { getSiName } from '../metadata/util';
|
||||
|
||||
@@ -71,12 +71,16 @@ function decodeStorageKey (value?: string | Uint8Array | StorageKey | StorageEnt
|
||||
} else if (Array.isArray(value)) {
|
||||
const [fn, args = []] = value;
|
||||
|
||||
assert(isFunction(fn), 'Expected function input for key construction');
|
||||
if (!isFunction(fn)) {
|
||||
throw new Error('Expected function input for key construction');
|
||||
}
|
||||
|
||||
if (fn.meta && fn.meta.type.isMap) {
|
||||
const map = fn.meta.type.asMap;
|
||||
|
||||
assert(Array.isArray(args) && args.length === map.hashers.length, () => `Expected an array of ${map.hashers.length} values as params to a Map query`);
|
||||
if (!Array.isArray(args) || args.length !== map.hashers.length) {
|
||||
throw new Error(`Expected an array of ${map.hashers.length} values as params to a Map query`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user