Adjust some API assertions (#5056)
This commit is contained in:
+1
-2
@@ -6,8 +6,7 @@ const base = require('@polkadot/dev/config/eslint.cjs');
|
||||
module.exports = {
|
||||
...base,
|
||||
ignorePatterns: [
|
||||
...base.ignorePatterns,
|
||||
'**/deno/*.ts'
|
||||
...base.ignorePatterns
|
||||
],
|
||||
parserOptions: {
|
||||
...base.parserOptions,
|
||||
|
||||
+1
-4
@@ -1,9 +1,6 @@
|
||||
_book/
|
||||
build/
|
||||
build-cjs/
|
||||
build-esm/
|
||||
build-deno/
|
||||
build-docs/
|
||||
build-*/
|
||||
coverage/
|
||||
docs/.vuepress/dist/
|
||||
docs/substrate/*.md
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@
|
||||
"@babel/core": "^7.18.6",
|
||||
"@babel/register": "^7.18.6",
|
||||
"@babel/runtime": "^7.18.6",
|
||||
"@polkadot/dev": "^0.67.69",
|
||||
"@polkadot/dev": "^0.67.73",
|
||||
"@polkadot/typegen": "workspace:packages/typegen",
|
||||
"@types/jest": "^28.1.4",
|
||||
"copyfiles": "^2.4.1"
|
||||
|
||||
@@ -21,7 +21,7 @@ import { memo, RpcCore } from '@polkadot/rpc-core';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { expandMetadata, Metadata, typeDefinitions, TypeRegistry, unwrapStorageType } from '@polkadot/types';
|
||||
import { getSpecRuntime } from '@polkadot/types-known';
|
||||
import { arrayChunk, arrayFlatten, assert, assertReturn, BN, compactStripLength, lazyMethod, lazyMethods, logger, nextTick, objectSpread, stringCamelCase, stringUpperFirst, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
||||
import { arrayChunk, arrayFlatten, assertReturn, BN, compactStripLength, lazyMethod, lazyMethods, logger, nextTick, objectSpread, stringCamelCase, stringUpperFirst, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
||||
import { blake2AsHex } from '@polkadot/util-crypto';
|
||||
|
||||
import { createSubmittable } from '../submittable';
|
||||
@@ -918,7 +918,9 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
}
|
||||
|
||||
private _retrieveMapKeys ({ iterKey, meta, method, section }: StorageEntry, at: Hash | Uint8Array | string | null, args: unknown[]): Observable<StorageKey[]> {
|
||||
assert(iterKey && meta.type.isMap, 'keys can only be retrieved on maps');
|
||||
if (!iterKey || !meta.type.isMap) {
|
||||
throw new Error('keys can only be retrieved on maps');
|
||||
}
|
||||
|
||||
const headKey = iterKey(...args).toHex();
|
||||
const startSubject = new BehaviorSubject<string>(headKey);
|
||||
@@ -945,7 +947,9 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
}
|
||||
|
||||
private _retrieveMapKeysPaged ({ iterKey, meta, method, section }: StorageEntry, at: Hash | Uint8Array | string | undefined, opts: PaginationOptions): Observable<StorageKey[]> {
|
||||
assert(iterKey && meta.type.isMap, 'keys can only be retrieved on maps');
|
||||
if (!iterKey || !meta.type.isMap) {
|
||||
throw new Error('keys can only be retrieved on maps');
|
||||
}
|
||||
|
||||
const setMeta = (key: StorageKey) => key.setMeta(meta, section, method);
|
||||
const query = at
|
||||
|
||||
@@ -15,7 +15,7 @@ import { firstValueFrom, map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { Metadata, TypeRegistry } from '@polkadot/types';
|
||||
import { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
|
||||
import { assert, assertReturn, BN_ZERO, isUndefined, logger, objectSpread, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { assertReturn, BN_ZERO, isUndefined, logger, objectSpread, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { cryptoWaitReady } from '@polkadot/util-crypto';
|
||||
|
||||
import { Decorate } from './Decorate';
|
||||
@@ -180,7 +180,9 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
|
||||
private async _getBlockRegistryViaHash (blockHash: Uint8Array): Promise<VersionedRegistry<ApiType>> {
|
||||
// ensure we have everything required
|
||||
assert(this._genesisHash && this._runtimeVersion, 'Cannot retrieve data on an uninitialized chain');
|
||||
if (!this._genesisHash || !this._runtimeVersion) {
|
||||
throw new Error('Cannot retrieve data on an uninitialized chain');
|
||||
}
|
||||
|
||||
// We have to assume that on the RPC layer the calls used here does not call back into
|
||||
// the registry swap, so getHeader & getRuntimeVersion should not be historic
|
||||
@@ -190,7 +192,9 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
: await firstValueFrom(this._rpcCore.chain.getHeader.raw(blockHash))
|
||||
);
|
||||
|
||||
assert(!header.parentHash.isEmpty, 'Unable to retrieve header and parent from supplied hash');
|
||||
if (header.parentHash.isEmpty) {
|
||||
throw new Error('Unable to retrieve header and parent from supplied hash');
|
||||
}
|
||||
|
||||
// get the runtime version, either on-chain or via an known upgrade history
|
||||
const [firstVersion, lastVersion] = getUpgradeVersion(this._genesisHash, header.number);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { SignerPayloadRawBase } from '@polkadot/types/types';
|
||||
import type { ApiOptions, ApiTypes, DecorateMethod, Signer } from '../types';
|
||||
|
||||
import { assert, isString, objectSpread, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { isString, objectSpread, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { Getters } from './Getters';
|
||||
|
||||
@@ -69,7 +69,9 @@ export abstract class ApiBase<ApiType extends ApiTypes> extends Getters<ApiType>
|
||||
if (isString(address)) {
|
||||
const _signer = signer || this._rx.signer;
|
||||
|
||||
assert(_signer?.signRaw, 'No signer exists with a signRaw interface. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.');
|
||||
if (!_signer || !_signer.signRaw) {
|
||||
throw new Error('No signer exists with a signRaw interface. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.');
|
||||
}
|
||||
|
||||
return (
|
||||
await _signer.signRaw(
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { DecorateFn, DecorateMethodOptions, ObsInnerType, StorageEntryPromi
|
||||
|
||||
import { catchError, EMPTY, Subscription, tap } from 'rxjs';
|
||||
|
||||
import { assert, isFunction, nextTick } from '@polkadot/util';
|
||||
import { isFunction, nextTick } from '@polkadot/util';
|
||||
|
||||
interface Tracker<T> {
|
||||
reject: (value: Error) => Observable<never>;
|
||||
@@ -57,7 +57,9 @@ function extractArgs (args: unknown[], needsCallback: boolean): [unknown[], Call
|
||||
: undefined;
|
||||
|
||||
// When we need a subscription, ensure that a valid callback is actually passed
|
||||
assert(!needsCallback || isFunction(callback), 'Expected a callback to be passed with subscriptions');
|
||||
if (needsCallback && !isFunction(callback)) {
|
||||
throw new Error('Expected a callback to be passed with subscriptions');
|
||||
}
|
||||
|
||||
return [actualArgs, callback];
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, Submittable
|
||||
|
||||
import { catchError, first, map, mapTo, mergeMap, of, switchMap, tap } from 'rxjs';
|
||||
|
||||
import { assert, isBn, isFunction, isNumber, isString, isU8a, isUndefined, objectSpread } from '@polkadot/util';
|
||||
import { isBn, isFunction, isNumber, isString, isU8a, objectSpread } from '@polkadot/util';
|
||||
|
||||
import { ApiBase } from '../base';
|
||||
import { filterEvents, isKeyringPair } from '../util';
|
||||
@@ -34,7 +34,9 @@ const identity = <T> (input: T): T => input;
|
||||
|
||||
function makeEraOptions (api: ApiInterfaceRx, registry: Registry, partialOptions: Partial<SignerOptions>, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions {
|
||||
if (!header) {
|
||||
assert(partialOptions.era === 0 || !isUndefined(partialOptions.blockHash), 'Expected blockHash to be passed alongside non-immortal era options');
|
||||
if (partialOptions.era && !partialOptions.blockHash) {
|
||||
throw new Error('Expected blockHash to be passed alongside non-immortal era options');
|
||||
}
|
||||
|
||||
if (isNumber(partialOptions.era)) {
|
||||
// since we have no header, it is immortal, remove any option overrides
|
||||
@@ -299,7 +301,9 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHas
|
||||
#signViaSigner = async (address: Address | string | Uint8Array, options: SignatureOptions, header: Header | null): Promise<number> => {
|
||||
const signer = options.signer || api.signer;
|
||||
|
||||
assert(signer, 'No signer specified, either via api.setSigner or via sign options. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.');
|
||||
if (!signer) {
|
||||
throw new Error('No signer specified, either via api.setSigner or via sign options. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.');
|
||||
}
|
||||
|
||||
const payload = this.registry.createTypeUnsafe<SignerPayload>('SignerPayload', [objectSpread({}, options, {
|
||||
address,
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { Signer, SignerResult } from '@polkadot/api/types';
|
||||
import type { KeyringPair } from '@polkadot/keyring/types';
|
||||
import type { Registry, SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
|
||||
|
||||
import { assert, hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
import { hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
let id = 0;
|
||||
|
||||
@@ -23,7 +23,9 @@ export class SingleAccountSigner implements Signer {
|
||||
}
|
||||
|
||||
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
|
||||
assert(payload.address === this.#keyringPair.address, 'Signer does not have the keyringPair');
|
||||
if (payload.address !== this.#keyringPair.address) {
|
||||
throw new Error('Signer does not have the keyringPair');
|
||||
}
|
||||
|
||||
return new Promise((resolve): void => {
|
||||
setTimeout((): void => {
|
||||
@@ -38,7 +40,9 @@ export class SingleAccountSigner implements Signer {
|
||||
}
|
||||
|
||||
public async signRaw ({ address, data }: SignerPayloadRaw): Promise<SignerResult> {
|
||||
assert(address === this.#keyringPair.address, 'Signer does not have the keyringPair');
|
||||
if (address !== this.#keyringPair.address) {
|
||||
throw new Error('Signer does not have the keyringPair');
|
||||
}
|
||||
|
||||
return new Promise((resolve): void => {
|
||||
setTimeout((): void => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { SiLookupTypeId } from '@polkadot/types/interfaces';
|
||||
import type { StorageEntry } from '@polkadot/types/primitive/types';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
|
||||
import { assert, isUndefined } from '@polkadot/util';
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
function sig ({ lookup }: Registry, { method, section }: StorageEntry, args: SiLookupTypeId[]): string {
|
||||
return `${section}.${method}(${args.map((a) => lookup.getTypeDef(a).type).join(', ')})`;
|
||||
@@ -17,14 +17,18 @@ export function extractStorageArgs (registry: Registry, creator: StorageEntry, _
|
||||
const args = _args.filter((a) => !isUndefined(a));
|
||||
|
||||
if (creator.meta.type.isPlain) {
|
||||
assert(args.length === 0, () => `${sig(registry, creator, [])} does not take any arguments, ${args.length} found`);
|
||||
if (args.length !== 0) {
|
||||
throw new Error(`${sig(registry, creator, [])} does not take any arguments, ${args.length} found`);
|
||||
}
|
||||
} else {
|
||||
const { hashers, key } = creator.meta.type.asMap;
|
||||
const keys = hashers.length === 1
|
||||
? [key]
|
||||
: registry.lookup.getSiType(key).def.asTuple.map((t) => t);
|
||||
|
||||
assert(args.length === keys.length, () => `${sig(registry, creator, keys)} is a map, requiring ${keys.length} arguments, ${args.length} found`);
|
||||
if (args.length !== keys.length) {
|
||||
throw new Error(`${sig(registry, creator, keys)} is a map, requiring ${keys.length} arguments, ${args.length} found`);
|
||||
}
|
||||
}
|
||||
|
||||
// pass as tuple
|
||||
|
||||
@@ -2044,9 +2044,9 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@polkadot/dev@npm:^0.67.69":
|
||||
version: 0.67.69
|
||||
resolution: "@polkadot/dev@npm:0.67.69"
|
||||
"@polkadot/dev@npm:^0.67.73":
|
||||
version: 0.67.73
|
||||
resolution: "@polkadot/dev@npm:0.67.73"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.18.6
|
||||
"@babel/core": ^7.18.6
|
||||
@@ -2135,7 +2135,7 @@ __metadata:
|
||||
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
|
||||
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
|
||||
checksum: e500a308d445756338ece0c9318cf801b291fa677bb21907337c4e662be7c26821f46aba2a41d1da927777d4745e4b456a82d071f79d47b9b66c57c10ad6a5df
|
||||
checksum: 01afdfab153b4c86832389fb806cc3b0a338e6125ca6280ffbcad39758a6fcad5005161c21f922788dd4ec8a320ca7b7c7f5862c00c141170c0d7a554caf8ed7
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -9242,7 +9242,7 @@ resolve@^2.0.0-next.3:
|
||||
"@babel/core": ^7.18.6
|
||||
"@babel/register": ^7.18.6
|
||||
"@babel/runtime": ^7.18.6
|
||||
"@polkadot/dev": ^0.67.69
|
||||
"@polkadot/dev": ^0.67.73
|
||||
"@polkadot/typegen": "workspace:packages/typegen"
|
||||
"@types/jest": ^28.1.4
|
||||
copyfiles: ^2.4.1
|
||||
|
||||
Reference in New Issue
Block a user