diff --git a/packages/api-derive/src/chain/getHeader.ts b/packages/api-derive/src/chain/getHeader.ts index dc5e211324..80917c68ca 100644 --- a/packages/api-derive/src/chain/getHeader.ts +++ b/packages/api-derive/src/chain/getHeader.ts @@ -3,7 +3,6 @@ import type { Observable } from 'rxjs'; import type { AccountId } from '@polkadot/types/interfaces'; -import type { Registry } from '@polkadot/types/types'; import type { HeaderExtended } from '../type/types'; import type { DeriveApi } from '../types'; @@ -39,7 +38,7 @@ export function getHeader (instanceId: string, api: DeriveApi): (blockHash: Uint ) ]).pipe( map(([header, validators]) => - createHeaderExtended(header.registry as Registry, header, validators) + createHeaderExtended(header.registry, header, validators) ), catchError((): Observable => // where rpc.chain.getHeader throws, we will land here - it can happen that diff --git a/packages/api-derive/src/chain/subscribeNewBlocks.ts b/packages/api-derive/src/chain/subscribeNewBlocks.ts index dfbb7a6b29..19534f0e0f 100644 --- a/packages/api-derive/src/chain/subscribeNewBlocks.ts +++ b/packages/api-derive/src/chain/subscribeNewBlocks.ts @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { Registry } from '@polkadot/types/types'; import type { SignedBlockExtended } from '../type/types'; import type { DeriveApi } from '../types'; @@ -33,7 +32,7 @@ export function subscribeNewBlocks (instanceId: string, api: DeriveApi): () => O ]); }), map(([header, block, events]) => - createSignedBlockExtended(block.registry as Registry, block, events, header.validators) + createSignedBlockExtended(block.registry, block, events, header.validators) ) ) ); diff --git a/packages/api-derive/src/chain/subscribeNewHeads.ts b/packages/api-derive/src/chain/subscribeNewHeads.ts index 716cd9976c..4adca91d95 100644 --- a/packages/api-derive/src/chain/subscribeNewHeads.ts +++ b/packages/api-derive/src/chain/subscribeNewHeads.ts @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { Registry } from '@polkadot/types/types'; import type { HeaderExtended } from '../type/types'; import type { DeriveApi } from '../types'; @@ -35,7 +34,7 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob map(([header, validators]): HeaderExtended => { header.createdAtHash = header.hash; - return createHeaderExtended(header.registry as Registry, header, validators); + return createHeaderExtended(header.registry, header, validators); }) ) ); diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index b3c15f8a00..7350e14ce4 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -472,7 +472,7 @@ export abstract class Decorate extends Events { const lazySection = (section: string) => lazyMethods({}, Object.keys(query[section]), (method: string) => blockHash - ? this._decorateStorageEntryAt(registry as Registry, query[section][method], decorateMethod, blockHash) + ? this._decorateStorageEntryAt(registry, query[section][method], decorateMethod, blockHash) : this._decorateStorageEntry(query[section][method], decorateMethod) ); @@ -526,7 +526,7 @@ export abstract class Decorate extends Events { decorated.sizeAt = decorateMethod((blockHash: Hash | Uint8Array | string, ...args: unknown[]): Observable => getQueryAt(blockHash).pipe( switchMap((q) => - this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry as Registry), blockHash)))); + this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry), blockHash)))); // .keys() & .entries() only available on map types if (creator.iterKey && creator.meta.type.isMap) { diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index 382ecaff48..48f5b2e7fc 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -6,7 +6,7 @@ import type { Observable, OperatorFunction } from 'rxjs'; import type { Address, ApplyExtrinsicResult, Call, Extrinsic, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo, SignerPayload } from '@polkadot/types/interfaces'; import type { Callback, Codec, Constructor, IKeyringPair, ISubmittableResult, SignatureOptions } from '@polkadot/types/types'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { ApiInterfaceRx, ApiTypes, PromiseOrObs, SignerResult } from '../types'; import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription } from './types'; @@ -26,7 +26,7 @@ interface SubmittableOptions { const identity = (input: T): T => input; -function makeEraOptions (api: ApiInterfaceRx, registry: CodecRegistry, partialOptions: Partial, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions { +function makeEraOptions (api: ApiInterfaceRx, registry: Registry, partialOptions: Partial, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions { if (!header) { if (isNumber(partialOptions.era)) { // since we have no header, it is immortal, remove any option overrides @@ -84,7 +84,7 @@ export function createClass ({ api, apiType, decorate #transformResult: (input: ISubmittableResult) => ISubmittableResult = identity; - constructor (registry: CodecRegistry, extrinsic: Call | Extrinsic | Uint8Array | string) { + constructor (registry: Registry, extrinsic: Call | Extrinsic | Uint8Array | string) { super(registry, extrinsic, { version: api.extrinsicType }); this.#ignoreStatusCb = apiType === 'rxjs'; diff --git a/packages/typegen/src/generate/lookup.ts b/packages/typegen/src/generate/lookup.ts index a2a097da2c..f82a8f082c 100644 --- a/packages/typegen/src/generate/lookup.ts +++ b/packages/typegen/src/generate/lookup.ts @@ -219,8 +219,8 @@ function generateLookup (destDir: string, entries: [string | undefined, HexStrin const { lookup, registry } = initMeta(staticMeta).metadata.asLatest; const filtered = getFilteredTypes(lookup, exclude); - generateLookupDefs(registry as Registry, filtered, destDir, subPath); - generateLookupTypes(registry as Registry, filtered, destDir, subPath); + generateLookupDefs(registry, filtered, destDir, subPath); + generateLookupTypes(registry, filtered, destDir, subPath); return exclude.concat( ...filtered diff --git a/packages/typegen/src/util/formatting.ts b/packages/typegen/src/util/formatting.ts index 68dcba1761..dc9652d00f 100644 --- a/packages/typegen/src/util/formatting.ts +++ b/packages/typegen/src/util/formatting.ts @@ -4,7 +4,6 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import type { Registry } from '@polkadot/types/types'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; import type { TypeDef } from '@polkadot/types-create/types'; import Handlebars from 'handlebars'; @@ -109,7 +108,7 @@ export function exportInterface (lookupIndex = -1, name = '', base: string, body return `${doc}export interface ${name} extends ${base} {${body.length ? '\n' : ''}${body}}`; } -function singleParamNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean): string { +function singleParamNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean): string { const sub = (typeDef.sub as TypeDef); setImports(definitions, imports, [wrapper, sub.lookupName]); @@ -117,7 +116,7 @@ function singleParamNotation (registry: CodecRegistry, wrapper: string, typeDef: return paramsNotation(wrapper, sub.lookupName || formatType(registry, definitions, sub.type, imports, withShortcut)); } -function dualParamsNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean): string { +function dualParamsNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean): string { const [a, b] = (typeDef.sub as TypeDef[]); setImports(definitions, imports, [wrapper, a.lookupName, b.lookupName]); @@ -128,18 +127,18 @@ function dualParamsNotation (registry: CodecRegistry, wrapper: string, typeDef: ]); } -const formatters: Record, imports: TypeImports, withShortcut: boolean) => string> = { - [TypeDefInfo.Compact]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { +const formatters: Record, imports: TypeImports, withShortcut: boolean) => string> = { + [TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, { lookupName }: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.DoNotConstruct]: (registry: Registry, { lookupName }: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { setImports(definitions, imports, ['DoNotConstruct']); return 'DoNotConstruct'; }, - [TypeDefInfo.Enum]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Enum]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { if (typeDef.lookupName) { return typeDef.lookupName; } @@ -147,43 +146,43 @@ const formatters: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Int]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { throw new Error(`TypeDefInfo.Int: Not implemented on ${stringify(typeDef)}`); }, - [TypeDefInfo.UInt]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.UInt]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { throw new Error(`TypeDefInfo.UInt: Not implemented on ${stringify(typeDef)}`); }, - [TypeDefInfo.Null]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Null]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { setImports(definitions, imports, ['Null']); return 'Null'; }, - [TypeDefInfo.Option]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.Plain]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Plain]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { setImports(definitions, imports, [typeDef.type]); return typeDef.type; }, - [TypeDefInfo.Range]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { throw new Error(`TypeDefInfo.Range: Not implemented on ${stringify(typeDef)}`); }, - [TypeDefInfo.Set]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Set]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { throw new Error(`TypeDefInfo.Set: Not implemented on ${stringify(typeDef)}`); }, - [TypeDefInfo.Si]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { - return formatType(registry, definitions, (registry as Registry).lookup.getTypeDef(typeDef.type), imports, withShortcut); + [TypeDefInfo.Si]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + return formatType(registry, definitions, (registry).lookup.getTypeDef(typeDef.type), imports, withShortcut); }, - [TypeDefInfo.Struct]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Struct]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { if (typeDef.lookupName) { return typeDef.lookupName; } @@ -200,7 +199,7 @@ const formatters: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Tuple]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { const sub = typeDef.sub as TypeDef[]; setImports(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => lookupName)]); @@ -213,11 +212,11 @@ const formatters: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.VecFixed]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.VecFixed]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { const sub = (typeDef.sub as TypeDef); if (sub.type === 'u8') { @@ -229,31 +228,31 @@ const formatters: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.BTreeSet]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.HashMap]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.Linkage]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.Result]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.WrapperKeepOpaque]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'WrapperKeepOpaque', typeDef, definitions, imports, withShortcut); }, - [TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { + [TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record, imports: TypeImports, withShortcut: boolean) => { return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut); } }; @@ -263,7 +262,7 @@ const formatters: Record, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string { +export function formatType (registry: Registry, definitions: Record, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string { let typeDef: TypeDef; if (isString(type)) { diff --git a/packages/types-codec/src/abstract/AbstractArray.ts b/packages/types-codec/src/abstract/AbstractArray.ts index 1d3b1af6af..cbc88b38ab 100644 --- a/packages/types-codec/src/abstract/AbstractArray.ts +++ b/packages/types-codec/src/abstract/AbstractArray.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecRegistry, IU8a, IVec } from '../types'; +import type { AnyJson, Codec, IU8a, IVec, Registry } from '../types'; import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util'; @@ -16,13 +16,13 @@ import { compareArray } from '../utils/compareArray'; * @noInheritDoc */ export abstract class AbstractArray extends Array implements IVec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; readonly initialU8aLength?: number; - protected constructor (registry: CodecRegistry, values: T[], initialU8aLength?: number) { + protected constructor (registry: Registry, values: T[], initialU8aLength?: number) { super(values.length); // explicitly set the values here - this removes the need for any extra allocations diff --git a/packages/types-codec/src/abstract/AbstractInt.ts b/packages/types-codec/src/abstract/AbstractInt.ts index be178d0da5..25f123ba47 100644 --- a/packages/types-codec/src/abstract/AbstractInt.ts +++ b/packages/types-codec/src/abstract/AbstractInt.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types'; +import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types'; import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn } from '@polkadot/util'; @@ -47,7 +47,7 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati * @noInheritDoc */ export abstract class AbstractInt extends BN implements INumber { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -57,7 +57,7 @@ export abstract class AbstractInt extends BN implements INumber { readonly #isSigned: boolean; - constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) { + constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) { // Construct via a string, which will be passed in the BN constructor. // It would be ideal to actually return a BN, but there is an issue: // https://github.com/indutny/bn.js/issues/206 diff --git a/packages/types-codec/src/base/Base.ts b/packages/types-codec/src/base/Base.ts index d9ffe79674..5973b0a5a1 100644 --- a/packages/types-codec/src/base/Base.ts +++ b/packages/types-codec/src/base/Base.ts @@ -2,14 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, BareOpts, Codec, CodecRegistry, IU8a } from '../types'; +import type { AnyJson, BareOpts, Codec, IU8a, Registry } from '../types'; /** * @name Base * @description A type extends the Base class, when it holds a value */ export abstract class Base implements Codec { - readonly #registry: CodecRegistry; + readonly #registry: Registry; public createdAtHash?: IU8a; @@ -17,7 +17,7 @@ export abstract class Base implements Codec { readonly #raw: T; - protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) { + protected constructor (registry: Registry, value: T, initialU8aLength?: number) { this.#registry = registry; this.#raw = value; this.initialU8aLength = initialU8aLength; @@ -48,7 +48,7 @@ export abstract class Base implements Codec { return this.#raw.isEmpty; } - public get registry (): CodecRegistry { + public get registry (): Registry { return this.#registry; } diff --git a/packages/types-codec/src/base/Compact.ts b/packages/types-codec/src/base/Compact.ts index 053082b0e9..f834707ad7 100644 --- a/packages/types-codec/src/base/Compact.ts +++ b/packages/types-codec/src/base/Compact.ts @@ -3,7 +3,7 @@ import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, AnyNumber, CodecClass, CodecRegistry, ICompact, INumber, IU8a } from '../types'; +import type { AnyJson, AnyNumber, CodecClass, ICompact, INumber, IU8a, Registry } from '../types'; import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util'; @@ -18,7 +18,7 @@ import { typeToConstructor } from '../utils'; * a number and making the compact representation thereof */ export class Compact implements ICompact { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -28,7 +28,7 @@ export class Compact implements ICompact { readonly #raw: T; - constructor (registry: CodecRegistry, Type: CodecClass | string, value: Compact | AnyNumber = 0) { + constructor (registry: Registry, Type: CodecClass | string, value: Compact | AnyNumber = 0) { this.registry = registry; this.#Type = typeToConstructor(registry, Type); @@ -40,14 +40,14 @@ export class Compact implements ICompact { public static with (Type: CodecClass | string): CodecClass> { return class extends Compact { - constructor (registry: CodecRegistry, value?: Compact | AnyNumber) { + constructor (registry: Registry, value?: Compact | AnyNumber) { super(registry, Type, value); } }; } /** @internal */ - public static decodeCompact (registry: CodecRegistry, Type: CodecClass, value: Compact | AnyNumber): [T, number] { + public static decodeCompact (registry: Registry, Type: CodecClass, value: Compact | AnyNumber): [T, number] { if (value instanceof Compact) { return [new Type(registry, value.#raw), 0]; } else if (value instanceof Type) { diff --git a/packages/types-codec/src/base/DoNotConstruct.ts b/packages/types-codec/src/base/DoNotConstruct.ts index 328b0758bc..61e6daa933 100644 --- a/packages/types-codec/src/base/DoNotConstruct.ts +++ b/packages/types-codec/src/base/DoNotConstruct.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types'; +import type { AnyJson, Codec, CodecClass, IU8a, Registry } from '../types'; /** * @name DoNotConstruct @@ -10,13 +10,13 @@ import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types'; * An unknown type that fails on construction with the type info */ export class DoNotConstruct implements Codec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; #neverError: Error; - constructor (registry: CodecRegistry, typeName = 'DoNotConstruct') { + constructor (registry: Registry, typeName = 'DoNotConstruct') { this.registry = registry; this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`); @@ -25,7 +25,7 @@ export class DoNotConstruct implements Codec { public static with (typeName?: string): CodecClass { return class extends DoNotConstruct { - constructor (registry: CodecRegistry) { + constructor (registry: Registry) { super(registry, typeName); } }; diff --git a/packages/types-codec/src/base/Enum.spec.ts b/packages/types-codec/src/base/Enum.spec.ts index 548a0ea50b..c217a4dfd5 100644 --- a/packages/types-codec/src/base/Enum.spec.ts +++ b/packages/types-codec/src/base/Enum.spec.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import { TypeRegistry } from '@polkadot/types'; import { Enum, Null, Text, U32 } from '@polkadot/types-codec'; @@ -129,7 +129,7 @@ describe('Enum', (): void => { class B extends Null { } class C extends Null { } class Test extends Enum { - constructor (registry: CodecRegistry, value?: string, index?: number) { + constructor (registry: Registry, value?: string, index?: number) { super(registry, { a: A, b: B, diff --git a/packages/types-codec/src/base/Enum.ts b/packages/types-codec/src/base/Enum.ts index a2612b4d8c..440ca8e51c 100644 --- a/packages/types-codec/src/base/Enum.ts +++ b/packages/types-codec/src/base/Enum.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecClass, CodecRegistry, IEnum, IU8a } from '../types'; +import type { AnyJson, Codec, CodecClass, IEnum, IU8a, Registry } from '../types'; import { assert, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { Null } from './Null'; // export interface, this is used in Enum.with, so required as public by TS export interface EnumCodecClass { - new(registry: CodecRegistry, value?: any, index?: number): T; + new(registry: Registry, value?: any, index?: number): T; } interface EntryDef { @@ -38,7 +38,7 @@ function isRustEnum (def: Record | Record | Record | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } { +function extractDef (registry: Registry, _def: Record | Record | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } { const def: TypesDef = {}; let isBasic: boolean; let isIndexed: boolean; @@ -81,7 +81,7 @@ function extractDef (registry: CodecRegistry, _def: Record e.index === index); assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`); @@ -94,7 +94,7 @@ function createFromValue (registry: CodecRegistry, def: TypesDef, index = 0, val }; } -function decodeFromJSON (registry: CodecRegistry, def: TypesDef, key: string, value?: unknown): Decoded { +function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?: unknown): Decoded { // JSON comes in the form of { "": "" }, here we // additionally force to lower to ensure forward compat const keys = Object.keys(def).map((k) => k.toLowerCase()); @@ -110,7 +110,7 @@ function decodeFromJSON (registry: CodecRegistry, def: TypesDef, key: string, va } } -function decodeEnum (registry: CodecRegistry, def: TypesDef, value?: unknown, index?: number): Decoded { +function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?: number): Decoded { // NOTE We check the index path first, before looking at values - this allows treating // the optional indexes before anything else, more-specific > less-specific if (isNumber(index)) { @@ -145,7 +145,7 @@ function decodeEnum (registry: CodecRegistry, def: TypesDef, value?: unknown, in * an extension to enum where the value type is determined by the actual index. */ export class Enum implements IEnum { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -163,7 +163,7 @@ export class Enum implements IEnum { readonly #raw: Codec; - constructor (registry: CodecRegistry, Types: Record | Record | string[], value?: unknown, index?: number) { + constructor (registry: Registry, Types: Record | Record | string[], value?: unknown, index?: number) { const { def, isBasic, isIndexed } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding @@ -199,7 +199,7 @@ export class Enum implements IEnum { } return class extends Enum { - constructor (registry: CodecRegistry, value?: unknown, index?: number) { + constructor (registry: Registry, value?: unknown, index?: number) { super(registry, Types, value, index); objectProperties(this, isKeys, (_, i) => this.type === keys[i]); diff --git a/packages/types-codec/src/base/Int.ts b/packages/types-codec/src/base/Int.ts index 5bee8a1a77..0dca52ce50 100644 --- a/packages/types-codec/src/base/Int.ts +++ b/packages/types-codec/src/base/Int.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types'; +import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types'; import { AbstractInt } from '../abstract/AbstractInt'; @@ -16,13 +16,13 @@ import { AbstractInt } from '../abstract/AbstractInt'; * @noInheritDoc */ export class Int extends AbstractInt { - constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength?: UIntBitLength) { + constructor (registry: Registry, value: AnyNumber = 0, bitLength?: UIntBitLength) { super(registry, value, bitLength, true); } public static with (bitLength: UIntBitLength, typeName?: string): CodecClass { return class extends Int { - constructor (registry: CodecRegistry, value?: AnyNumber) { + constructor (registry: Registry, value?: AnyNumber) { super(registry, value, bitLength); } diff --git a/packages/types-codec/src/base/Null.ts b/packages/types-codec/src/base/Null.ts index 079bcb481d..ec8a48dce6 100644 --- a/packages/types-codec/src/base/Null.ts +++ b/packages/types-codec/src/base/Null.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { Codec, CodecRegistry, IU8a } from '../types'; +import type { Codec, IU8a, Registry } from '../types'; import { isNull } from '@polkadot/util'; @@ -12,14 +12,14 @@ import { isNull } from '@polkadot/util'; * Implements a type that does not contain anything (apart from `null`) */ export class Null implements Codec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; // Added for compatibility reasons, e.g. see Option public readonly initialU8aLength = 0; - constructor (registry: CodecRegistry) { + constructor (registry: Registry) { this.registry = registry; } diff --git a/packages/types-codec/src/base/Option.ts b/packages/types-codec/src/base/Option.ts index 2d60e97b19..88910acc92 100644 --- a/packages/types-codec/src/base/Option.ts +++ b/packages/types-codec/src/base/Option.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecClass, CodecRegistry, IOption, IU8a } from '../types'; +import type { AnyJson, Codec, CodecClass, IOption, IU8a, Registry } from '../types'; import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util'; @@ -10,7 +10,7 @@ import { typeToConstructor } from '../utils'; import { Null } from './Null'; /** @internal */ -function decodeOption (registry: CodecRegistry, Type: CodecClass, value?: unknown): Codec { +function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): Codec { // In the case of an option, unwrap the inner if (value instanceof Option) { value = value.value; @@ -41,7 +41,7 @@ function decodeOption (registry: CodecRegistry, Type: CodecClass, value?: unknow * with a value if/as required/found. */ export class Option implements IOption { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -51,7 +51,7 @@ export class Option implements IOption { readonly #raw: T; - constructor (registry: CodecRegistry, typeName: CodecClass | string, value?: unknown) { + constructor (registry: Registry, typeName: CodecClass | string, value?: unknown) { const Type = typeToConstructor(registry, typeName); const decoded = isU8a(value) && value.length && !isCodec(value) ? value[0] === 0 @@ -70,7 +70,7 @@ export class Option implements IOption { public static with (Type: CodecClass | string): CodecClass> { return class extends Option { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, Type, value); } }; diff --git a/packages/types-codec/src/base/Result.ts b/packages/types-codec/src/base/Result.ts index ba4d9da42a..828f898d91 100644 --- a/packages/types-codec/src/base/Result.ts +++ b/packages/types-codec/src/base/Result.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry, IResult } from '../types'; +import type { Codec, CodecClass, IResult, Registry } from '../types'; import { assert } from '@polkadot/util'; @@ -13,7 +13,7 @@ import { Enum } from './Enum'; * A Result maps to the Rust Result type, that can either wrap a success or error value */ export class Result extends Enum implements IResult { - constructor (registry: CodecRegistry, Ok: CodecClass | string, Err: CodecClass | string, value?: unknown) { + constructor (registry: Registry, Ok: CodecClass | string, Err: CodecClass | string, value?: unknown) { // NOTE This is order-dependent, Ok (with index 0) needs to be first // eslint-disable-next-line sort-keys super(registry, { Ok, Err }, value); @@ -21,7 +21,7 @@ export class Result extends Enum implements IR public static override with (Types: { Ok: CodecClass | string; Err: CodecClass | string }): CodecClass> { return class extends Result { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, Types.Ok, Types.Err, value); } }; diff --git a/packages/types-codec/src/base/Tuple.ts b/packages/types-codec/src/base/Tuple.ts index 14c57df713..b95d829ab5 100644 --- a/packages/types-codec/src/base/Tuple.ts +++ b/packages/types-codec/src/base/Tuple.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyTupleValue, Codec, CodecClass, CodecRegistry, ITuple } from '../types'; +import type { AnyTupleValue, Codec, CodecClass, ITuple, Registry } from '../types'; import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util'; @@ -19,7 +19,7 @@ type TupleTypes = TupleType[] | { }; /** @internal */ -function decodeTuple (registry: CodecRegistry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] { +function decodeTuple (registry: Registry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] { if (isU8a(value) || isHex(value)) { return decodeU8a(registry, u8aToU8a(value), Classes); } @@ -55,7 +55,7 @@ function decodeTuple (registry: CodecRegistry, Classes: TupleCodecClasss, value? export class Tuple extends AbstractArray implements ITuple { #Types: TupleCodecClasss; - constructor (registry: CodecRegistry, Types: TupleTypes | TupleType, value?: AnyTupleValue) { + constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue) { const Classes = Array.isArray(Types) ? Types.map((t) => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) @@ -72,7 +72,7 @@ export class Tuple extends AbstractArray implements ITuple { public static with (Types: TupleTypes | TupleType): CodecClass { return class extends Tuple { - constructor (registry: CodecRegistry, value?: AnyTupleValue) { + constructor (registry: Registry, value?: AnyTupleValue) { super(registry, Types, value); } }; diff --git a/packages/types-codec/src/base/UInt.ts b/packages/types-codec/src/base/UInt.ts index 8d54c68b90..fd5d893f91 100644 --- a/packages/types-codec/src/base/UInt.ts +++ b/packages/types-codec/src/base/UInt.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types'; +import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types'; import { AbstractInt } from '../abstract/AbstractInt'; @@ -18,7 +18,7 @@ import { AbstractInt } from '../abstract/AbstractInt'; export class UInt extends AbstractInt { public static with (bitLength: UIntBitLength, typeName?: string): CodecClass { return class extends UInt { - constructor (registry: CodecRegistry, value?: AnyNumber) { + constructor (registry: Registry, value?: AnyNumber) { super(registry, value, bitLength); } diff --git a/packages/types-codec/src/base/Vec.ts b/packages/types-codec/src/base/Vec.ts index 8fbb37067e..2039c0e92f 100644 --- a/packages/types-codec/src/base/Vec.ts +++ b/packages/types-codec/src/base/Vec.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util'; @@ -13,7 +13,7 @@ const MAX_LENGTH = 64 * 1024; const l = logger('Vec'); -export function decodeVec (registry: CodecRegistry, Type: CodecClass, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] { +export function decodeVec (registry: Registry, Type: CodecClass, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] { if (Array.isArray(value)) { const result = new Array(value.length); @@ -59,7 +59,7 @@ export function decodeVec (registry: CodecRegistry, Type: Codec export class Vec extends AbstractArray { #Type: CodecClass; - constructor (registry: CodecRegistry, Type: CodecClass | string, value: Uint8Array | HexString | unknown[] = []) { + constructor (registry: Registry, Type: CodecClass | string, value: Uint8Array | HexString | unknown[] = []) { const Clazz = typeToConstructor(registry, Type); const [values, decodedLength] = decodeVec(registry, Clazz, value); @@ -70,7 +70,7 @@ export class Vec extends AbstractArray { public static with (Type: CodecClass | string): CodecClass> { return class extends Vec { - constructor (registry: CodecRegistry, value?: any[]) { + constructor (registry: Registry, value?: any[]) { super(registry, Type, value); } }; diff --git a/packages/types-codec/src/base/VecFixed.ts b/packages/types-codec/src/base/VecFixed.ts index a2dd966f6a..ab432e0990 100644 --- a/packages/types-codec/src/base/VecFixed.ts +++ b/packages/types-codec/src/base/VecFixed.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { assert, isU8a, u8aConcat } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { decodeU8aVec, typeToConstructor } from '../utils'; import { decodeVec } from './Vec'; /** @internal */ -function decodeVecFixed (registry: CodecRegistry, value: Uint8Array | HexString | unknown[], Type: CodecClass, length: number): [T[], number, number] { +function decodeVecFixed (registry: Registry, value: Uint8Array | HexString | unknown[], Type: CodecClass, length: number): [T[], number, number] { const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length); while (values.length < length) { @@ -31,7 +31,7 @@ function decodeVecFixed (registry: CodecRegistry, value: Uint8A export class VecFixed extends AbstractArray { #Type: CodecClass; - constructor (registry: CodecRegistry, Type: CodecClass | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) { + constructor (registry: Registry, Type: CodecClass | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) { const Clazz = typeToConstructor(registry, Type); const [values,, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, value, 0, Clazz, length) @@ -44,7 +44,7 @@ export class VecFixed extends AbstractArray { public static with (Type: CodecClass | string, length: number): CodecClass> { return class extends VecFixed { - constructor (registry: CodecRegistry, value?: any[]) { + constructor (registry: Registry, value?: any[]) { super(registry, Type, length, value); } }; diff --git a/packages/types-codec/src/extended/BTreeMap.ts b/packages/types-codec/src/extended/BTreeMap.ts index e1bd4d7dbf..c151db8d87 100644 --- a/packages/types-codec/src/extended/BTreeMap.ts +++ b/packages/types-codec/src/extended/BTreeMap.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { CodecMap } from './Map'; export class BTreeMap extends CodecMap { public static with (keyType: CodecClass | string, valType: CodecClass | string): CodecClass> { return class extends BTreeMap { - constructor (registry: CodecRegistry, value?: Uint8Array | string | Map) { + constructor (registry: Registry, value?: Uint8Array | string | Map) { super(registry, keyType, valType, value, 'BTreeMap'); } }; diff --git a/packages/types-codec/src/extended/BTreeSet.ts b/packages/types-codec/src/extended/BTreeSet.ts index f01c9f21dc..277b0f9685 100644 --- a/packages/types-codec/src/extended/BTreeSet.ts +++ b/packages/types-codec/src/extended/BTreeSet.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecClass, CodecRegistry, ISet, IU8a } from '../types'; +import type { AnyJson, Codec, CodecClass, ISet, IU8a, Registry } from '../types'; import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils'; const l = logger('BTreeSet'); /** @internal */ -function decodeSetFromU8a (registry: CodecRegistry, ValClass: CodecClass, u8a: Uint8Array): [Set, number] { +function decodeSetFromU8a (registry: Registry, ValClass: CodecClass, u8a: Uint8Array): [Set, number] { const output = new Set(); const [offset, length] = compactFromU8a(u8a); const [values, decodedLength] = decodeU8aVec(registry, u8a, offset, ValClass, length.toNumber()); @@ -24,7 +24,7 @@ function decodeSetFromU8a (registry: CodecRegistry, ValClass: C } /** @internal */ -function decodeSetFromSet (registry: CodecRegistry, ValClass: CodecClass, value: Set | string[]): [Set, number] { +function decodeSetFromSet (registry: Registry, ValClass: CodecClass, value: Set | string[]): [Set, number] { const output = new Set(); value.forEach((val: any) => { @@ -54,7 +54,7 @@ function decodeSetFromSet (registry: CodecRegistry, ValClass: C * @param jsonSet * @internal */ -function decodeSet (registry: CodecRegistry, valType: CodecClass | string, value?: Uint8Array | string | string[] | Set): [Set, number] { +function decodeSet (registry: Registry, valType: CodecClass | string, value?: Uint8Array | string | string[] | Set): [Set, number] { if (!value) { return [new Set(), 0]; } @@ -71,7 +71,7 @@ function decodeSet (registry: CodecRegistry, valType: CodecClas } export class BTreeSet extends Set implements ISet { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -79,7 +79,7 @@ export class BTreeSet extends Set implements ISet readonly #ValClass: CodecClass; - constructor (registry: CodecRegistry, valType: CodecClass | string, rawValue?: Uint8Array | string | string[] | Set) { + constructor (registry: Registry, valType: CodecClass | string, rawValue?: Uint8Array | string | string[] | Set) { const [values, decodedLength] = decodeSet(registry, valType, rawValue); super(sortSet(values)); @@ -91,7 +91,7 @@ export class BTreeSet extends Set implements ISet public static with (valType: CodecClass | string): CodecClass> { return class extends BTreeSet { - constructor (registry: CodecRegistry, value?: Uint8Array | string | Set) { + constructor (registry: Registry, value?: Uint8Array | string | Set) { super(registry, valType, value); } }; diff --git a/packages/types-codec/src/extended/BitVec.ts b/packages/types-codec/src/extended/BitVec.ts index 2f52092166..c08f38b80a 100644 --- a/packages/types-codec/src/extended/BitVec.ts +++ b/packages/types-codec/src/extended/BitVec.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyU8a, CodecRegistry } from '../types'; +import type { AnyU8a, Registry } from '../types'; import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util'; @@ -42,7 +42,7 @@ function decodeBitVec (value?: AnyU8a): [number, Uint8Array] { export class BitVec extends Raw { readonly #decodedLength: number; - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { const [decodedLength, u8a] = decodeBitVec(value); super(registry, u8a); diff --git a/packages/types-codec/src/extended/Bytes.ts b/packages/types-codec/src/extended/Bytes.ts index fa10741d6f..cf2fea26ac 100644 --- a/packages/types-codec/src/extended/Bytes.ts +++ b/packages/types-codec/src/extended/Bytes.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyU8a, CodecRegistry } from '../types'; +import type { AnyU8a, Registry } from '../types'; import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util'; @@ -47,7 +47,7 @@ function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] { * as what is found in [[Text]] and [[Vec]]) */ export class Bytes extends Raw { - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { const [u8a, decodedLength] = decodeBytes(value); super(registry, u8a, decodedLength); diff --git a/packages/types-codec/src/extended/HashMap.ts b/packages/types-codec/src/extended/HashMap.ts index 02ca667c77..f1da0391e4 100644 --- a/packages/types-codec/src/extended/HashMap.ts +++ b/packages/types-codec/src/extended/HashMap.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { CodecMap } from './Map'; export class HashMap extends CodecMap { public static with (keyType: CodecClass | string, valType: CodecClass | string): CodecClass> { return class extends HashMap { - constructor (registry: CodecRegistry, value?: Uint8Array | string | Map) { + constructor (registry: Registry, value?: Uint8Array | string | Map) { super(registry, keyType, valType, value); } }; diff --git a/packages/types-codec/src/extended/Linkage.ts b/packages/types-codec/src/extended/Linkage.ts index c593b0c9a1..73694d68a9 100644 --- a/packages/types-codec/src/extended/Linkage.ts +++ b/packages/types-codec/src/extended/Linkage.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { Option } from '../base/Option'; import { Tuple } from '../base/Tuple'; @@ -18,7 +18,7 @@ const EMPTY = new Uint8Array(); * @description The wrapper for the result from a LinkedMap */ export class Linkage extends Struct { - constructor (registry: CodecRegistry, Type: CodecClass | string, value?: unknown) { + constructor (registry: Registry, Type: CodecClass | string, value?: unknown) { super(registry, { previous: Option.with(Type), // eslint-disable-next-line sort-keys @@ -28,7 +28,7 @@ export class Linkage extends Struct { public static withKey (Type: CodecClass | string): CodecClass> { return class extends Linkage { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, Type, value); } }; @@ -66,7 +66,7 @@ export class Linkage extends Struct { * @description A Linkage keys/Values tuple */ export class LinkageResult extends Tuple { - constructor (registry: CodecRegistry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) { + constructor (registry: Registry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) { super(registry, { Keys: Vec.with(TypeKey), Values: Vec.with(TypeValue) diff --git a/packages/types-codec/src/extended/Map.ts b/packages/types-codec/src/extended/Map.ts index 8da2b9c88b..27d236138f 100644 --- a/packages/types-codec/src/extended/Map.ts +++ b/packages/types-codec/src/extended/Map.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecClass, CodecRegistry, IMap, IU8a } from '../types'; +import type { AnyJson, Codec, CodecClass, IMap, IU8a, Registry } from '../types'; import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -14,7 +14,7 @@ import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils'; const l = logger('Map'); /** @internal */ -function decodeMapFromU8a (registry: CodecRegistry, KeyClass: CodecClass, ValClass: CodecClass, u8a: Uint8Array): [Map, number] { +function decodeMapFromU8a (registry: Registry, KeyClass: CodecClass, ValClass: CodecClass, u8a: Uint8Array): [Map, number] { const output = new Map(); const [offset, length] = compactFromU8a(u8a); const count = length.toNumber(); @@ -34,7 +34,7 @@ function decodeMapFromU8a (registry: CodecRegi } /** @internal */ -function decodeMapFromMap (registry: CodecRegistry, KeyClass: CodecClass, ValClass: CodecClass, value: Map): [Map, number] { +function decodeMapFromMap (registry: Registry, KeyClass: CodecClass, ValClass: CodecClass, value: Map): [Map, number] { const output = new Map(); for (const [key, val] of value.entries()) { @@ -76,7 +76,7 @@ function decodeMapFromMap (registry: CodecRegi * @param jsonMap * @internal */ -function decodeMap (registry: CodecRegistry, keyType: CodecClass | string, valType: CodecClass | string, value?: Uint8Array | string | Map): [Map, number] { +function decodeMap (registry: Registry, keyType: CodecClass | string, valType: CodecClass | string, value?: Uint8Array | string | Map): [Map, number] { const KeyClass = typeToConstructor(registry, keyType); const ValClass = typeToConstructor(registry, valType); @@ -94,7 +94,7 @@ function decodeMap (registry: CodecRegistry, k } export class CodecMap extends Map implements IMap { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -106,7 +106,7 @@ export class CodecMap extends readonly #type: string; - constructor (registry: CodecRegistry, keyType: CodecClass | string, valType: CodecClass | string, rawValue: Uint8Array | string | Map | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') { + constructor (registry: Registry, keyType: CodecClass | string, valType: CodecClass | string, rawValue: Uint8Array | string | Map | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') { const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue); super(type === 'BTreeMap' ? sortMap(decoded) : decoded); diff --git a/packages/types-codec/src/extended/Range.ts b/packages/types-codec/src/extended/Range.ts index 87c06e35dd..59529b15da 100644 --- a/packages/types-codec/src/extended/Range.ts +++ b/packages/types-codec/src/extended/Range.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyTuple, CodecClass, CodecRegistry, INumber } from '../types'; +import type { AnyTuple, CodecClass, INumber, Registry } from '../types'; import { Tuple } from '../base/Tuple'; @@ -15,7 +15,7 @@ type RangeType = 'Range' | 'RangeInclusive'; export class Range extends Tuple { #rangeName: RangeType; - constructor (registry: CodecRegistry, Type: CodecClass | string, value?: AnyTuple, rangeName: RangeType = 'Range') { + constructor (registry: Registry, Type: CodecClass | string, value?: AnyTuple, rangeName: RangeType = 'Range') { super(registry, { end: Type, start: Type @@ -26,7 +26,7 @@ export class Range extends Tuple { public static override with (Types: CodecClass | string): CodecClass> { return class extends Range { - constructor (registry: CodecRegistry, value?: AnyTuple) { + constructor (registry: Registry, value?: AnyTuple) { super(registry, Types, value); } }; @@ -55,13 +55,13 @@ export class Range extends Tuple { } export class RangeInclusive extends Range { - constructor (registry: CodecRegistry, Type: CodecClass | string, value?: AnyTuple) { + constructor (registry: Registry, Type: CodecClass | string, value?: AnyTuple) { super(registry, Type, value, 'RangeInclusive'); } public static override with (Type: CodecClass | string): CodecClass> { return class extends RangeInclusive { - constructor (registry: CodecRegistry, value?: AnyTuple) { + constructor (registry: Registry, value?: AnyTuple) { super(registry, Type, value); } }; diff --git a/packages/types-codec/src/extended/Type.ts b/packages/types-codec/src/extended/Type.ts index 1322f7b2e6..1d09d7fa04 100644 --- a/packages/types-codec/src/extended/Type.ts +++ b/packages/types-codec/src/extended/Type.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '../types'; +import type { Registry } from '../types'; import { Text } from '../native/Text'; import { sanitize } from '../utils'; @@ -14,7 +14,7 @@ import { sanitize } from '../utils'; * i.e. we remove the `T::` prefixes found in some types for consistency across implementation. */ export class Type extends Text { - constructor (registry: CodecRegistry, value: Text | Uint8Array | string = '') { + constructor (registry: Registry, value: Text | Uint8Array | string = '') { super(registry, value); this.setOverride(sanitize(this.toString())); diff --git a/packages/types-codec/src/extended/U8aFixed.ts b/packages/types-codec/src/extended/U8aFixed.ts index 4a18f349f1..c592169e7c 100644 --- a/packages/types-codec/src/extended/U8aFixed.ts +++ b/packages/types-codec/src/extended/U8aFixed.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyU8a, CodecClass, CodecRegistry, U8aBitLength } from '../types'; +import type { AnyU8a, CodecClass, Registry, U8aBitLength } from '../types'; import { assert, u8aToU8a } from '@polkadot/util'; @@ -28,7 +28,7 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, numbe * to be used directly, rather is should be subclassed with the specific lengths. */ export class U8aFixed extends Raw { - constructor (registry: CodecRegistry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) { + constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) { const [u8a, decodedLength] = decodeU8aFixed(value, bitLength); super(registry, u8a, decodedLength); @@ -36,7 +36,7 @@ export class U8aFixed extends Raw { public static with (bitLength: U8aBitLength, typeName?: string): CodecClass { return class extends U8aFixed { - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { super(registry, value, bitLength); } diff --git a/packages/types-codec/src/extended/WrapperKeepOpaque.ts b/packages/types-codec/src/extended/WrapperKeepOpaque.ts index a14fa69f44..e1ed1cd1d1 100644 --- a/packages/types-codec/src/extended/WrapperKeepOpaque.ts +++ b/packages/types-codec/src/extended/WrapperKeepOpaque.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, AnyU8a, Codec, CodecClass, CodecRegistry } from '../types'; +import type { AnyJson, AnyU8a, Codec, CodecClass, Registry } from '../types'; import { assertReturn, compactStripLength, isHex, isU8a } from '@polkadot/util'; @@ -9,7 +9,7 @@ import { Raw } from '../native/Raw'; import { typeToConstructor } from '../utils'; import { Bytes } from './Bytes'; -function decodeRaw (registry: CodecRegistry, typeName: CodecClass | string, value?: AnyU8a | Codec): [CodecClass, T | null, AnyU8a] { +function decodeRaw (registry: Registry, typeName: CodecClass | string, value?: AnyU8a | Codec): [CodecClass, T | null, AnyU8a] { const Type = typeToConstructor(registry, typeName); if (isU8a(value) || isHex(value)) { @@ -34,7 +34,7 @@ export class WrapperKeepOpaque extends Bytes { readonly #decoded: T | null; - constructor (registry: CodecRegistry, typeName: CodecClass | string, value?: AnyU8a | Codec) { + constructor (registry: Registry, typeName: CodecClass | string, value?: AnyU8a | Codec) { const [Type, decoded, u8a] = decodeRaw(registry, typeName, value); super(registry, u8a); @@ -45,7 +45,7 @@ export class WrapperKeepOpaque extends Bytes { public static with (Type: CodecClass | string): CodecClass> { return class extends WrapperKeepOpaque { - constructor (registry: CodecRegistry, value?: AnyU8a | T) { + constructor (registry: Registry, value?: AnyU8a | T) { super(registry, Type, value); } }; diff --git a/packages/types-codec/src/extended/WrapperOpaque.ts b/packages/types-codec/src/extended/WrapperOpaque.ts index 221700bc3f..406ab70e4d 100644 --- a/packages/types-codec/src/extended/WrapperOpaque.ts +++ b/packages/types-codec/src/extended/WrapperOpaque.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { compactAddLength, compactStripLength, isU8a } from '@polkadot/util'; import { Base } from '../base/Base'; import { typeToConstructor } from '../utils'; -function decodeRaw (registry: CodecRegistry, Type: CodecClass | string, value?: unknown): T { +function decodeRaw (registry: Registry, Type: CodecClass | string, value?: unknown): T { const Clazz = typeToConstructor(registry, Type); if (isU8a(value)) { @@ -21,13 +21,13 @@ function decodeRaw (registry: CodecRegistry, Type: CodecClass extends Base { - constructor (registry: CodecRegistry, Type: CodecClass | string, value?: unknown) { + constructor (registry: Registry, Type: CodecClass | string, value?: unknown) { super(registry, decodeRaw(registry, Type, value)); } public static with (Type: CodecClass | string): CodecClass> { return class extends WrapperOpaque { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, Type, value); } }; diff --git a/packages/types-codec/src/native/Bool.ts b/packages/types-codec/src/native/Bool.ts index da3d7b7f8c..a93609ee52 100644 --- a/packages/types-codec/src/native/Bool.ts +++ b/packages/types-codec/src/native/Bool.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { Codec, CodecRegistry, IU8a } from '../types'; +import type { Codec, IU8a, Registry } from '../types'; import { isU8a, u8aToHex } from '@polkadot/util'; @@ -24,12 +24,12 @@ function decodeBool (value: any): boolean { * @noInheritDoc */ export class bool extends Boolean implements Codec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; // eslint-disable-next-line @typescript-eslint/ban-types - constructor (registry: CodecRegistry, value: bool | Boolean | Uint8Array | boolean | number = false) { + constructor (registry: Registry, value: bool | Boolean | Uint8Array | boolean | number = false) { super(decodeBool(value)); this.registry = registry; diff --git a/packages/types-codec/src/native/Date.ts b/packages/types-codec/src/native/Date.ts index a69de9bc0e..5887353b6f 100644 --- a/packages/types-codec/src/native/Date.ts +++ b/packages/types-codec/src/native/Date.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types'; +import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types'; import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util'; @@ -33,11 +33,11 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date { * @noInheritDoc */ export class CodecDate extends Date implements INumber { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; - constructor (registry: CodecRegistry, value: CodecDate | Date | AnyNumber = 0) { + constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) { super(decodeDate(value)); this.registry = registry; diff --git a/packages/types-codec/src/native/Json.ts b/packages/types-codec/src/native/Json.ts index e44efe69a0..cc9ce9482d 100644 --- a/packages/types-codec/src/native/Json.ts +++ b/packages/types-codec/src/native/Json.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, Codec, CodecRegistry, IU8a } from '../types'; +import type { AnyJson, Codec, IU8a, Registry } from '../types'; import { isFunction, objectProperties, stringify } from '@polkadot/util'; @@ -22,11 +22,11 @@ function decodeJson (value?: Record | null): [string, any][] { * @noInheritDoc */ export class Json extends Map implements Codec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; - constructor (registry: CodecRegistry, value?: Record | null) { + constructor (registry: Registry, value?: Record | null) { const decoded = decodeJson(value); super(decoded); diff --git a/packages/types-codec/src/native/Raw.ts b/packages/types-codec/src/native/Raw.ts index 4fbfcb1d6d..ea275c4176 100644 --- a/packages/types-codec/src/native/Raw.ts +++ b/packages/types-codec/src/native/Raw.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, AnyU8a, CodecRegistry, IU8a } from '../types'; +import type { AnyJson, AnyU8a, IU8a, Registry } from '../types'; import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util'; @@ -16,13 +16,13 @@ import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } * @noInheritDoc */ export class Raw extends Uint8Array implements IU8a { - readonly registry: CodecRegistry; + readonly registry: Registry; public createdAtHash?: IU8a; readonly initialU8aLength?: number; - constructor (registry: CodecRegistry, value?: AnyU8a, initialU8aLength?: number) { + constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) { super(u8aToU8a(value)); this.registry = registry; diff --git a/packages/types-codec/src/native/Set.ts b/packages/types-codec/src/native/Set.ts index 79f5233612..e59126f905 100644 --- a/packages/types-codec/src/native/Set.ts +++ b/packages/types-codec/src/native/Set.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { CodecClass, CodecRegistry, ISet, IU8a } from '../types'; +import type { CodecClass, ISet, IU8a, Registry } from '../types'; import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -86,7 +86,7 @@ function decodeSet (setValues: SetValues, value: string[] | Set | Uint8A * a bitwise representation of the values. */ export class CodecSet extends Set implements ISet { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -94,7 +94,7 @@ export class CodecSet extends Set implements ISet { readonly #byteLength: number; - constructor (registry: CodecRegistry, setValues: SetValues, value?: string[] | Set | Uint8Array | BN | number | string, bitLength = 8) { + constructor (registry: Registry, setValues: SetValues, value?: string[] | Set | Uint8Array | BN | number | string, bitLength = 8) { super(decodeSet(setValues, value, bitLength)); this.registry = registry; @@ -111,7 +111,7 @@ export class CodecSet extends Set implements ISet { } return class extends CodecSet { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, values, value as undefined, bitLength); objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i])); diff --git a/packages/types-codec/src/native/Struct.ts b/packages/types-codec/src/native/Struct.ts index edc18f25c2..99bdc29e7a 100644 --- a/packages/types-codec/src/native/Struct.ts +++ b/packages/types-codec/src/native/Struct.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, CodecRegistry, IStruct, IU8a } from '../types'; +import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, IStruct, IU8a, Registry } from '../types'; import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from '../utils'; type TypesDef = Record>; /** @internal */ -function decodeStructFromObject (registry: CodecRegistry, Types: CodecClassDef, value: any, jsonMap: Map): [Iterable<[string, Codec]>, number] { +function decodeStructFromObject (registry: Registry, Types: CodecClassDef, value: any, jsonMap: Map): [Iterable<[string, Codec]>, number] { let jsonObj: Record | undefined; const inputKeys = Object.keys(Types); const typeofArray = Array.isArray(value); @@ -90,7 +90,7 @@ export class Struct< V extends { [K in keyof S]: any } = { [K in keyof S]: any }, // type names, mapped by key, name of Class in S E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map implements IStruct { - #registry: CodecRegistry; + #registry: Registry; public createdAtHash?: IU8a; @@ -100,7 +100,7 @@ export class Struct< readonly #Types: CodecClassDef; - constructor (registry: CodecRegistry, Types: S, value?: V | Map | unknown[] | HexString | null, jsonMap = new Map()) { + constructor (registry: Registry, Types: S, value?: V | Map | unknown[] | HexString | null, jsonMap = new Map()) { const typeMap = mapToTypeMap(registry, Types); const [decoded, decodedLength] = isU8a(value) ? decodeU8a(registry, value, typeMap, true) @@ -122,7 +122,7 @@ export class Struct< const keys = Object.keys(Types); return class extends Struct { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, Types, value as HexString, jsonMap); objectProperties(this, keys, (k) => this.get(k)); @@ -190,7 +190,7 @@ export class Struct< return this.registry.hash(this.toU8a()); } - public get registry (): CodecRegistry { + public get registry (): Registry { return this.#registry; } diff --git a/packages/types-codec/src/native/Text.ts b/packages/types-codec/src/native/Text.ts index ee941b8ba7..5372dbb526 100644 --- a/packages/types-codec/src/native/Text.ts +++ b/packages/types-codec/src/native/Text.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyU8a, Codec, CodecRegistry, IU8a } from '../types'; +import type { AnyU8a, Codec, IU8a, Registry } from '../types'; import { assert, compactAddLength, compactFromU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util'; @@ -48,7 +48,7 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s // TODO // - Strings should probably be trimmed (docs do come through with extra padding) export class Text extends String implements Codec { - public readonly registry: CodecRegistry; + public readonly registry: Registry; public createdAtHash?: IU8a; @@ -56,7 +56,7 @@ export class Text extends String implements Codec { #override: string | null = null; - constructor (registry: CodecRegistry, value?: null | Text | string | AnyU8a | { toString: () => string }) { + constructor (registry: Registry, value?: null | Text | string | AnyU8a | { toString: () => string }) { const [str, decodedLength] = decodeText(value); super(str); diff --git a/packages/types-codec/src/primitive/USize.ts b/packages/types-codec/src/primitive/USize.ts index 67d00afccb..0803ab20a4 100644 --- a/packages/types-codec/src/primitive/USize.ts +++ b/packages/types-codec/src/primitive/USize.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '../types'; +import type { Registry } from '../types'; import { u32 } from './U32'; @@ -13,7 +13,7 @@ import { u32 } from './U32'; * It is not to be used, since it created consensus mismatches. */ export class usize extends u32 { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, value); throw new Error('The `usize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally u64) and WASM (always u32) code. Use one of the `u32` or `u64` types explicitly.'); diff --git a/packages/types-codec/src/types/codec.ts b/packages/types-codec/src/types/codec.ts index 4fab4c7b21..650013a076 100644 --- a/packages/types-codec/src/types/codec.ts +++ b/packages/types-codec/src/types/codec.ts @@ -4,7 +4,7 @@ import type { HexString } from '@polkadot/util/types'; import type { AnyJson } from './helpers'; import type { IU8a } from './interfaces'; -import type { CodecRegistry } from './registry'; +import type { Registry } from './registry'; export type BareOpts = boolean | Record; @@ -39,7 +39,7 @@ export interface Codec { /** * @description The registry associated with this object */ - readonly registry: CodecRegistry; + readonly registry: Registry; /** * @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage) @@ -91,7 +91,7 @@ export interface CodecClass { // NOTE: We need the any[] here, unknown[] does not work as expected // eslint-disable-next-line @typescript-eslint/no-explicit-any - new(registry: CodecRegistry, ...args: any[]): T; + new(registry: Registry, ...args: any[]): T; } export type CodecClassDef = Record>; diff --git a/packages/types-codec/src/types/registry.ts b/packages/types-codec/src/types/registry.ts index a70e6b6800..f2d67f3b52 100644 --- a/packages/types-codec/src/types/registry.ts +++ b/packages/types-codec/src/types/registry.ts @@ -47,12 +47,12 @@ export interface CallFunction extends Ca (...args: any[]): IMethod; } -export interface CodecRegistry { +export interface Registry { readonly chainDecimals: number[]; readonly chainSS58: number | undefined; readonly chainTokens: string[]; // readonly knownTypes: RegisteredTypes; - // readonly lookup: PortableCodecRegistry; + // readonly lookup: PortableRegistry; // readonly metadata: MetadataLatest; // readonly unknownTypes: string[]; // readonly signedExtensions: string[]; @@ -88,13 +88,13 @@ export interface CodecRegistry { hasDef (name: string): boolean; hasType (name: string): boolean; hash (data: Uint8Array): IU8a; - // init (): CodecRegistry; + // init (): Registry; register (type: CodecClass | RegistryTypes): void; register (name: string, type: CodecClass): void; register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void; // setChainProperties (properties?: ChainProperties): void; // setHasher (hasher?: IU8aer | null): void; - // setLookup (lookup: PortableCodecRegistry): void; + // setLookup (lookup: PortableRegistry): void; setMetadata (metadata: unknown, signedExtensions?: string[], userExtensions?: unknown): void; // setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void; } diff --git a/packages/types-codec/src/utils/decodeU8a.ts b/packages/types-codec/src/utils/decodeU8a.ts index 34db761b50..09c4380a32 100644 --- a/packages/types-codec/src/utils/decodeU8a.ts +++ b/packages/types-codec/src/utils/decodeU8a.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { u8aToHex } from '@polkadot/util'; @@ -9,7 +9,7 @@ function formatFailure (error: Error, type: string | null, u8a: Uint8Array, key? return `decodeU8a: failed at ${u8aToHex(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`; } -function getRawType (registry: CodecRegistry, Type: CodecClass): string | null { +function getRawType (registry: Registry, Type: CodecClass): string | null { try { return new Type(registry).toRawType(); } catch { @@ -25,7 +25,7 @@ function getRawType (registry: CodecRegistry, Type: CodecClass): string | null { * @param result - The result array (will be returned with values pushed) * @param types - The array of CodecClass to decode the U8a against. */ -export function decodeU8a (registry: CodecRegistry, u8a: Uint8Array, types: CodecClass[] | { [index: string]: CodecClass }, withZip?: boolean): [E[], number] { +export function decodeU8a (registry: Registry, u8a: Uint8Array, types: CodecClass[] | { [index: string]: CodecClass }, withZip?: boolean): [E[], number] { const [Types, keys]: [CodecClass[], string[]] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)]; @@ -56,7 +56,7 @@ export function decodeU8a (registry: CodecRegis // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but // since we have to do less checks (and these are intensive anyway), much faster -export function decodeU8aVec (registry: CodecRegistry, u8a: Uint8Array, start: number, Type: CodecClass, count: number): [T[], number, number] { +export function decodeU8aVec (registry: Registry, u8a: Uint8Array, start: number, Type: CodecClass, count: number): [T[], number, number] { const result = new Array(count); let offset = start; diff --git a/packages/types-codec/src/utils/mapToTypeMap.ts b/packages/types-codec/src/utils/mapToTypeMap.ts index 13098519f0..6468ffcc13 100644 --- a/packages/types-codec/src/utils/mapToTypeMap.ts +++ b/packages/types-codec/src/utils/mapToTypeMap.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecClass, CodecRegistry } from '../types'; +import type { CodecClass, Registry } from '../types'; import { typeToConstructor } from './typeToConstructor'; /** * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }` */ -export function mapToTypeMap (registry: CodecRegistry, input: Record): Record { +export function mapToTypeMap (registry: Registry, input: Record): Record { const entries = Object.entries(input); const output: Record = {}; diff --git a/packages/types-codec/src/utils/typeToConstructor.ts b/packages/types-codec/src/utils/typeToConstructor.ts index d0934ca429..108d79bc42 100644 --- a/packages/types-codec/src/utils/typeToConstructor.ts +++ b/packages/types-codec/src/utils/typeToConstructor.ts @@ -1,11 +1,11 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '../types'; +import type { Codec, CodecClass, Registry } from '../types'; import { isString } from '@polkadot/util'; -export function typeToConstructor (registry: CodecRegistry, type: string | CodecClass): CodecClass { +export function typeToConstructor (registry: Registry, type: string | CodecClass): CodecClass { return ( isString(type) ? registry.createClassUnsafe(type) diff --git a/packages/types-codec/src/utils/typesToMap.ts b/packages/types-codec/src/utils/typesToMap.ts index 74e7c2ae31..2e8150f46d 100644 --- a/packages/types-codec/src/utils/typesToMap.ts +++ b/packages/types-codec/src/utils/typesToMap.ts @@ -1,9 +1,9 @@ // Copyright 2017-2022 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecClass, CodecRegistry } from '../types'; +import type { CodecClass, Registry } from '../types'; -export function typesToMap (registry: CodecRegistry, Types: Record): Record { +export function typesToMap (registry: Registry, Types: Record): Record { const entries = Object.entries(Types); const result: Record = {}; diff --git a/packages/types-create/src/create/class.ts b/packages/types-create/src/create/class.ts index ac3df17083..502bb50f43 100644 --- a/packages/types-create/src/create/class.ts +++ b/packages/types-create/src/create/class.ts @@ -1,8 +1,8 @@ // Copyright 2017-2022 @polkadot/types-create authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types'; -import type { CreateRegistry, TypeDef } from '../types'; +import type { Codec, CodecClass, Registry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types'; +import type { TypeDef } from '../types'; import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from '@polkadot/types-codec'; import { assert, isNumber, stringify } from '@polkadot/util'; @@ -63,20 +63,20 @@ function createWithSub (Clazz: { with: (t: string) => CodecClass }, value return Clazz.with(getSubType(value)); } -const infoMapping: Record CodecClass> = { - [TypeDefInfo.BTreeMap]: (registry: CodecRegistry, value: TypeDef): CodecClass => +const infoMapping: Record CodecClass> = { + [TypeDefInfo.BTreeMap]: (registry: Registry, value: TypeDef): CodecClass => createHashMap(BTreeMap, value), - [TypeDefInfo.BTreeSet]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.BTreeSet]: (registry: Registry, value: TypeDef): CodecClass => createWithSub(BTreeSet, value), - [TypeDefInfo.Compact]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Compact]: (registry: Registry, value: TypeDef): CodecClass => createWithSub(Compact, value), - [TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): CodecClass => DoNotConstruct.with(value.displayName || value.type), - [TypeDefInfo.Enum]: (registry: CodecRegistry, value: TypeDef): CodecClass => { + [TypeDefInfo.Enum]: (registry: Registry, value: TypeDef): CodecClass => { const subs = getSubDefArray(value); return Enum.with( @@ -90,14 +90,14 @@ const infoMapping: Record => + [TypeDefInfo.HashMap]: (registry: Registry, value: TypeDef): CodecClass => createHashMap(HashMap, value), - [TypeDefInfo.Int]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Int]: (registry: Registry, value: TypeDef): CodecClass => createInt(Int, value), // We have circular deps between Linkage & Struct - [TypeDefInfo.Linkage]: (registry: CodecRegistry, value: TypeDef): CodecClass => { + [TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): CodecClass => { const type = `Option<${getSubType(value)}>`; // eslint-disable-next-line sort-keys const Clazz = Struct.with({ previous: type, next: type } as any); @@ -112,26 +112,26 @@ const infoMapping: Record => + [TypeDefInfo.Null]: (registry: Registry, _: TypeDef): CodecClass => Null, - [TypeDefInfo.Option]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Option]: (registry: Registry, value: TypeDef): CodecClass => createWithSub(Option, value), - [TypeDefInfo.Plain]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Plain]: (registry: Registry, value: TypeDef): CodecClass => registry.getOrUnknown(value.type), - [TypeDefInfo.Range]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Range]: (registry: Registry, value: TypeDef): CodecClass => (value.type.includes('RangeInclusive') ? RangeInclusive : Range).with(getSubType(value)), - [TypeDefInfo.Result]: (registry: CodecRegistry, value: TypeDef): CodecClass => { + [TypeDefInfo.Result]: (registry: Registry, value: TypeDef): CodecClass => { const [Ok, Err] = getTypeClassArray(value); // eslint-disable-next-line @typescript-eslint/no-use-before-define return Result.with({ Err, Ok }); }, - [TypeDefInfo.Set]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Set]: (registry: Registry, value: TypeDef): CodecClass => CodecSet.with( getSubDefArray(value).reduce>((result, { index, name }) => { result[name as string] = index as number; @@ -141,19 +141,19 @@ const infoMapping: Record => - getTypeClass(registry, (registry as CreateRegistry).lookup.getTypeDef(value.type)), + [TypeDefInfo.Si]: (registry: Registry, value: TypeDef): CodecClass => + getTypeClass(registry, registry.lookup.getTypeDef(value.type)), - [TypeDefInfo.Struct]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Struct]: (registry: Registry, value: TypeDef): CodecClass => Struct.with(getTypeClassMap(value), value.alias), - [TypeDefInfo.Tuple]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.Tuple]: (registry: Registry, value: TypeDef): CodecClass => Tuple.with(getTypeClassArray(value)), - [TypeDefInfo.UInt]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.UInt]: (registry: Registry, value: TypeDef): CodecClass => createInt(UInt, value), - [TypeDefInfo.Vec]: (registry: CodecRegistry, { sub }: TypeDef): CodecClass => { + [TypeDefInfo.Vec]: (registry: Registry, { sub }: TypeDef): CodecClass => { assert(sub && !Array.isArray(sub), 'Expected type information for vector'); return ( @@ -163,7 +163,7 @@ const infoMapping: Record => { + [TypeDefInfo.VecFixed]: (registry: Registry, { displayName, length, sub }: TypeDef): CodecClass => { assert(sub && isNumber(length) && !Array.isArray(sub), 'Expected length & type information for fixed vector'); return ( @@ -173,14 +173,14 @@ const infoMapping: Record => + [TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, value: TypeDef): CodecClass => createWithSub(WrapperKeepOpaque, value), - [TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, value: TypeDef): CodecClass => + [TypeDefInfo.WrapperOpaque]: (registry: Registry, value: TypeDef): CodecClass => createWithSub(WrapperOpaque, value) }; -export function constructTypeClass (registry: CodecRegistry, typeDef: TypeDef): CodecClass { +export function constructTypeClass (registry: Registry, typeDef: TypeDef): CodecClass { try { const Type = infoMapping[typeDef.info](registry, typeDef); @@ -200,15 +200,15 @@ export function constructTypeClass (registry: CodecRegi } // Returns the type Class for construction -export function getTypeClass (registry: CodecRegistry, typeDef: TypeDef): CodecClass { - return (registry as CreateRegistry).getUnsafe(typeDef.type, false, typeDef) as CodecClass; +export function getTypeClass (registry: Registry, typeDef: TypeDef): CodecClass { + return registry.getUnsafe(typeDef.type, false, typeDef) as CodecClass; } -export function createClassUnsafe (registry: CodecRegistry, type: K): CodecClass { +export function createClassUnsafe (registry: Registry, type: K): CodecClass { return getTypeClass( registry, registry.isLookupType(type) - ? (registry as CreateRegistry).lookup.getTypeDef(type) + ? registry.lookup.getTypeDef(type) : getTypeDef(type) ); } diff --git a/packages/types-create/src/create/type.ts b/packages/types-create/src/create/type.ts index 3e761c04c8..f4dee1d84c 100644 --- a/packages/types-create/src/create/type.ts +++ b/packages/types-create/src/create/type.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-create authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry, IU8a } from '@polkadot/types-codec/types'; +import type { Codec, CodecClass, IU8a, Registry } from '@polkadot/types-codec/types'; import type { CreateOptions } from '../types'; import { Bytes, Option } from '@polkadot/types-codec'; @@ -20,7 +20,7 @@ function checkInstance (created: Codec, matcher: Uint8Array): void { ( // on a length-prefixed type, just check the actual length ['Bytes', 'Text', 'Type'].includes(rawType) && - matcher.length === (created as Bytes).length + matcher.length === (created as unknown as Bytes).length ) || ( // when the created is empty and matcher is also empty, let it slide... @@ -44,7 +44,7 @@ function checkPedantic (created: Codec, [value]: unknown[], isPedantic = false): // Initializes a type with a value. This also checks for fallbacks and in the cases // where isPedantic is specified (storage decoding), also check the format/structure -function initType (registry: CodecRegistry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T { +function initType (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T { const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params); checkPedantic(created, params, isPedantic); @@ -59,7 +59,7 @@ function initType (registry: CodecRegistry, Type: CodecClass, p // An unsafe version of the `createType` below. It's unsafe because the `type` // argument here can be any string, which, when it cannot parse, will yield a // runtime error. -export function createTypeUnsafe (registry: CodecRegistry, type: K, params: unknown[] = [], options: CreateOptions = {}): T { +export function createTypeUnsafe (registry: Registry, type: K, params: unknown[] = [], options: CreateOptions = {}): T { let Clazz: CodecClass | null = null; let firstError: Error | null = null; diff --git a/packages/types-create/src/types/augmentRegistry.ts b/packages/types-create/src/types/augmentRegistry.ts new file mode 100644 index 0000000000..0090cfafa3 --- /dev/null +++ b/packages/types-create/src/types/augmentRegistry.ts @@ -0,0 +1,16 @@ +// Copyright 2017-2022 @polkadot/types-create authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Codec, CodecClass, ICompact, INumber } from '@polkadot/types-codec/types'; +import type { ILookup, TypeDef } from '@polkadot/types-create/types'; + +declare module '@polkadot/types-codec/types/registry' { + export interface Registry { + readonly lookup: ILookup; + + createLookupType (lookupId: ICompact | number): string; + setLookup (lookup: ILookup): void; + + getUnsafe (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass | undefined; + } +} diff --git a/packages/types-create/src/types/index.ts b/packages/types-create/src/types/index.ts new file mode 100644 index 0000000000..529df13e22 --- /dev/null +++ b/packages/types-create/src/types/index.ts @@ -0,0 +1,9 @@ +// Copyright 2017-2022 @polkadot/types-create authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import './augmentRegistry'; + +export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types'; + +export * from './lookup'; +export * from './types'; diff --git a/packages/types-create/src/types/lookup.ts b/packages/types-create/src/types/lookup.ts new file mode 100644 index 0000000000..5a32e185a5 --- /dev/null +++ b/packages/types-create/src/types/lookup.ts @@ -0,0 +1,14 @@ +// Copyright 2017-2022 @polkadot/types-create authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { ICompact, INumber } from '@polkadot/types-codec/types'; +import type { TypeDef } from './types'; + +export interface ILookup { + getSiType (lookupId: ICompact | string | number): { + def: { + asTuple: ICompact[] + } + }, + getTypeDef (lookupId: ICompact | string | number): TypeDef; +} diff --git a/packages/types-create/src/types.ts b/packages/types-create/src/types/types.ts similarity index 50% rename from packages/types-create/src/types.ts rename to packages/types-create/src/types/types.ts index ffa69eab58..29e06b9731 100644 --- a/packages/types-create/src/types.ts +++ b/packages/types-create/src/types/types.ts @@ -1,10 +1,6 @@ // Copyright 2017-2022 @polkadot/types-create authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types'; - -export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types'; - export enum TypeDefInfo { BTreeMap, BTreeSet, @@ -48,21 +44,3 @@ export interface TypeDef { type: string; typeName?: string; } - -export interface ILookup { - getSiType (lookupId: ICompact | string | number): { - def: { - asTuple: ICompact[] - } - }, - getTypeDef (lookupId: ICompact | string | number): TypeDef; -} - -export interface CreateRegistry extends CodecRegistry { - lookup: ILookup; - - createLookupType (lookupId: ICompact | number): string; - setLookup (lookup: ILookup): void; - - getUnsafe (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass | undefined; -} diff --git a/packages/types-create/src/util/encodeTypes.ts b/packages/types-create/src/util/encodeTypes.ts index e9cc4b3041..937ff4a1a9 100644 --- a/packages/types-create/src/util/encodeTypes.ts +++ b/packages/types-create/src/util/encodeTypes.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types-create authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { TypeDef } from '@polkadot/types-create/types'; import { assert, isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util'; @@ -20,7 +20,7 @@ export function paramsNotation (outer: string, inner?: T | T[], transform: ( }`; } -function encodeWithParams (registry: CodecRegistry, typeDef: TypeDef, outer: string): string { +function encodeWithParams (registry: Registry, typeDef: TypeDef, outer: string): string { const { info, sub } = typeDef; switch (info) { @@ -41,7 +41,7 @@ function encodeWithParams (registry: CodecRegistry, typeDef: TypeDef, outer: str throw new Error(`Unable to encode ${stringify(typeDef)} with params`); } -function encodeSubTypes (registry: CodecRegistry, sub: TypeDef[], asEnum?: boolean, extra?: Record): string { +function encodeSubTypes (registry: Registry, sub: TypeDef[], asEnum?: boolean, extra?: Record): string { const names = sub.map(({ name }) => name); assert(names.every((n) => !!n), () => `Subtypes does not have consistent names, ${names.join(', ')}`); @@ -63,16 +63,16 @@ function encodeSubTypes (registry: CodecRegistry, sub: TypeDef[], asEnum?: boole // We setup a record here to ensure we have comprehensive coverage (any item not covered will result // in a compile-time error with the missing index) -const encoders: Record string> = { - [TypeDefInfo.BTreeMap]: (registry: CodecRegistry, typeDef: TypeDef) => +const encoders: Record string> = { + [TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'), - [TypeDefInfo.BTreeSet]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'), - [TypeDefInfo.Compact]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'Compact'), - [TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, { displayName, lookupIndex, lookupName }: TypeDef) => + [TypeDefInfo.DoNotConstruct]: (registry: Registry, { displayName, lookupIndex, lookupName }: TypeDef) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`, - [TypeDefInfo.Enum]: (registry: CodecRegistry, { sub }: TypeDef): string => { + [TypeDefInfo.Enum]: (registry: Registry, { sub }: TypeDef): string => { assert(sub && Array.isArray(sub), 'Unable to encode Enum type'); // c-like enums have all Null entries @@ -81,26 +81,26 @@ const encoders: Record `${name || `Empty${index}`}`) }) : encodeSubTypes(registry, sub, true); }, - [TypeDefInfo.HashMap]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'HashMap'), - [TypeDefInfo.Int]: (registry: CodecRegistry, { length = 32 }: TypeDef) => + [TypeDefInfo.Int]: (registry: Registry, { length = 32 }: TypeDef) => `Int<${length}>`, - [TypeDefInfo.Linkage]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'Linkage'), // eslint-disable-next-line @typescript-eslint/no-unused-vars - [TypeDefInfo.Null]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Null]: (registry: Registry, typeDef: TypeDef) => 'Null', - [TypeDefInfo.Option]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'Option'), // eslint-disable-next-line @typescript-eslint/no-unused-vars - [TypeDefInfo.Plain]: (registry: CodecRegistry, { displayName, type }: TypeDef) => + [TypeDefInfo.Plain]: (registry: Registry, { displayName, type }: TypeDef) => displayName || type, - [TypeDefInfo.Range]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, typeDef.type.includes('RangeInclusive') ? 'RangeInclusive' : 'Range'), - [TypeDefInfo.Result]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'Result'), // eslint-disable-next-line @typescript-eslint/no-unused-vars - [TypeDefInfo.Set]: (registry: CodecRegistry, { length = 8, sub }: TypeDef): string => { + [TypeDefInfo.Set]: (registry: Registry, { length = 8, sub }: TypeDef): string => { assert(sub && Array.isArray(sub), 'Unable to encode Set type'); return stringify({ @@ -110,9 +110,9 @@ const encoders: Record + [TypeDefInfo.Si]: (registry: Registry, { lookupName, type }: TypeDef) => lookupName || type, - [TypeDefInfo.Struct]: (registry: CodecRegistry, { alias, sub }: TypeDef): string => { + [TypeDefInfo.Struct]: (registry: Registry, { alias, sub }: TypeDef): string => { assert(sub && Array.isArray(sub), 'Unable to encode Struct type'); return encodeSubTypes(registry, sub, false, @@ -125,33 +125,33 @@ const encoders: Record { + [TypeDefInfo.Tuple]: (registry: Registry, { sub }: TypeDef): string => { assert(sub && Array.isArray(sub), 'Unable to encode Tuple type'); return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`; }, - [TypeDefInfo.UInt]: (registry: CodecRegistry, { length = 32 }: TypeDef) => + [TypeDefInfo.UInt]: (registry: Registry, { length = 32 }: TypeDef) => `UInt<${length}>`, - [TypeDefInfo.Vec]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'Vec'), - [TypeDefInfo.VecFixed]: (registry: CodecRegistry, { length, sub }: TypeDef): string => { + [TypeDefInfo.VecFixed]: (registry: Registry, { length, sub }: TypeDef): string => { assert(isNumber(length) && !isUndefined(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type'); return `[${sub.type};${length}]`; }, - [TypeDefInfo.WrapperKeepOpaque]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'), - [TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, typeDef: TypeDef) => + [TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef) => encodeWithParams(registry, typeDef, 'WrapperOpaque') }; -function encodeType (registry: CodecRegistry, typeDef: TypeDef, withLookup = true): string { +function encodeType (registry: Registry, typeDef: TypeDef, withLookup = true): string { return withLookup && typeDef.lookupName ? typeDef.lookupName : encoders[typeDef.info](registry, typeDef); } -export function encodeTypeDef (registry: CodecRegistry, typeDef: TypeDef): string { +export function encodeTypeDef (registry: Registry, typeDef: TypeDef): string { // In the case of contracts we do have the unfortunate situation where the displayName would // refer to "Option" when it is an option. For these, string it out, only using when actually // not a top-level element to be used @@ -160,7 +160,7 @@ export function encodeTypeDef (registry: CodecRegistry, typeDef: TypeDef): strin : encodeType(registry, typeDef); } -export function withTypeString (registry: CodecRegistry, typeDef: Omit & { type?: string }): TypeDef { +export function withTypeString (registry: Registry, typeDef: Omit & { type?: string }): TypeDef { return objectSpread({}, typeDef, { type: encodeType(registry, typeDef as TypeDef, false) }); diff --git a/packages/types/src/create/createClass.ts b/packages/types/src/create/createClass.ts index bff1d4f962..7660ac5ccd 100644 --- a/packages/types/src/create/createClass.ts +++ b/packages/types/src/create/createClass.ts @@ -1,11 +1,11 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types'; +import type { Codec, CodecClass, Registry } from '@polkadot/types-codec/types'; import type { DetectCodec } from '../types'; import { createClassUnsafe } from '@polkadot/types-create'; -export function createClass (registry: CodecRegistry, type: K): CodecClass> { +export function createClass (registry: Registry, type: K): CodecClass> { return createClassUnsafe(registry, type); } diff --git a/packages/types/src/create/createType.ts b/packages/types/src/create/createType.ts index d2fe3fc5b3..06381cd146 100644 --- a/packages/types/src/create/createType.ts +++ b/packages/types/src/create/createType.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecRegistry } from '@polkadot/types-codec/types'; +import type { Codec, Registry } from '@polkadot/types-codec/types'; import type { DetectCodec } from '../types'; import { createTypeUnsafe } from '@polkadot/types-create'; @@ -12,6 +12,6 @@ import { createTypeUnsafe } from '@polkadot/types-create'; * instance from * @param params - The value to instantiate the type with */ -export function createType (registry: CodecRegistry, type: K, ...params: unknown[]): DetectCodec { +export function createType (registry: Registry, type: K, ...params: unknown[]): DetectCodec { return createTypeUnsafe(registry, type, params); } diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 55267cc7fe..94baeaf9d1 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, CodecRegistry, IU8a } from '@polkadot/types-codec/types'; +import type { Codec, CodecClass, IU8a } from '@polkadot/types-codec/types'; import type { CreateOptions, TypeDef } from '@polkadot/types-create/types'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { ChainProperties, DispatchErrorModule, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types'; @@ -89,7 +89,7 @@ function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLate const meta = (registry as Registry).createType('EventMetadataLatest', objectSpread({}, variant, { args: getFieldArgs(lookup, variant.fields) })); return class extends GenericEventData { - constructor (registry: CodecRegistry, value: Uint8Array) { + constructor (registry: Registry, value: Uint8Array) { super(registry, value, meta, stringCamelCase(name), variant.name.toString()); } }; diff --git a/packages/types/src/ethereum/AccountId.ts b/packages/types/src/ethereum/AccountId.ts index 724f99d3c3..dbfb706147 100644 --- a/packages/types/src/ethereum/AccountId.ts +++ b/packages/types/src/ethereum/AccountId.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyString, AnyU8a, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyString, AnyU8a, Registry } from '@polkadot/types-codec/types'; import { U8aFixed } from '@polkadot/types-codec'; import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util'; @@ -28,7 +28,7 @@ function decodeAccountId (value: AnyU8a | AnyString): AnyU8a { * just a Uint8Array wrapper with a fixed length. */ export class GenericEthereumAccountId extends U8aFixed { - constructor (registry: CodecRegistry, value: AnyU8a = new Uint8Array()) { + constructor (registry: Registry, value: AnyU8a = new Uint8Array()) { super(registry, decodeAccountId(value), 160); } diff --git a/packages/types/src/ethereum/LookupSource.ts b/packages/types/src/ethereum/LookupSource.ts index 70b972206d..508a458ff7 100644 --- a/packages/types/src/ethereum/LookupSource.ts +++ b/packages/types/src/ethereum/LookupSource.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; @@ -18,7 +18,7 @@ type AnyAddress = bigint | BN | GenericEthereumLookupSource | GenericEthereumAcc export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]); /** @internal */ -function decodeString (registry: CodecRegistry, value: string): GenericEthereumAccountId | GenericAccountIndex { +function decodeString (registry: Registry, value: string): GenericEthereumAccountId | GenericAccountIndex { const decoded = decodeAddress(value); return decoded.length === 20 @@ -27,7 +27,7 @@ function decodeString (registry: CodecRegistry, value: string): GenericEthereumA } /** @internal */ -function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericEthereumAccountId | GenericAccountIndex { +function decodeU8a (registry: Registry, value: Uint8Array): GenericEthereumAccountId | GenericAccountIndex { // This allows us to instantiate an address with a raw publicKey. Do this first before // we checking the first byte, otherwise we may split an already-existent valid address if (value.length === 20) { @@ -41,7 +41,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericEthereum return registry.createTypeUnsafe('AccountIndex', [u8aToBn(value.subarray(offset, offset + length), true)]); } -function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): GenericEthereumAccountId | GenericAccountIndex { +function decodeAddressOrIndex (registry: Registry, value: AnyAddress): GenericEthereumAccountId | GenericAccountIndex { return value instanceof GenericEthereumLookupSource ? value.inner : value instanceof GenericEthereumAccountId || value instanceof GenericAccountIndex @@ -62,7 +62,7 @@ function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): Gene * is encoded as `[ , ...publicKey/...bytes ]` as per spec */ export class GenericEthereumLookupSource extends Base { - constructor (registry: CodecRegistry, value: AnyAddress = new Uint8Array()) { + constructor (registry: Registry, value: AnyAddress = new Uint8Array()) { super(registry, decodeAddressOrIndex(registry, value)); } diff --git a/packages/types/src/extrinsic/Extrinsic.ts b/packages/types/src/extrinsic/Extrinsic.ts index 284661a59c..b63ae3594a 100644 --- a/packages/types/src/extrinsic/Extrinsic.ts +++ b/packages/types/src/extrinsic/Extrinsic.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Compact } from '@polkadot/types-codec'; -import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, CodecRegistry, IMethod } from '@polkadot/types-codec/types'; +import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, IMethod } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics'; import type { FunctionMetadataLatest } from '../interfaces/metadata'; @@ -37,7 +37,7 @@ const VERSIONS = [ export { EXTRINSIC_VERSION as LATEST_EXTRINSIC_VERSION } from './v4/Extrinsic'; /** @internal */ -function newFromValue (registry: CodecRegistry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown { +function newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown { if (value instanceof GenericExtrinsic) { return value.unwrap(); } @@ -51,7 +51,7 @@ function newFromValue (registry: CodecRegistry, value: any, version: number): Ex } /** @internal */ -function decodeExtrinsic (registry: CodecRegistry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown { +function decodeExtrinsic (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown { if (isU8a(value) || Array.isArray(value) || isHex(value)) { return decodeU8a(registry, u8aToU8a(value), version); } else if (value instanceof registry.createClassUnsafe('Call')) { @@ -62,7 +62,7 @@ function decodeExtrinsic (registry: CodecRegistry, value?: GenericExtrinsic | Ex } /** @internal */ -function decodeU8a (registry: CodecRegistry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown { +function decodeU8a (registry: Registry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown { if (!value.length) { return newFromValue(registry, new Uint8Array(), version); } @@ -78,7 +78,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array, version: number) } abstract class ExtrinsicBase extends Base { - constructor (registry: CodecRegistry, value: ExtrinsicV4 | ExtrinsicUnknown, initialU8aLength?: number) { + constructor (registry: Registry, value: ExtrinsicV4 | ExtrinsicUnknown, initialU8aLength?: number) { super(registry, value, initialU8aLength); const signKeys = Object.keys(registry.getSignedExtensionTypes()); @@ -169,7 +169,7 @@ abstract class ExtrinsicBase extends Base extends Base extends ExtrinsicBase implements IExtrinsic { #hashCache?: CodecHash; - constructor (registry: CodecRegistry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { version }: CreateOptions = {}) { + constructor (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { version }: CreateOptions = {}) { super(registry, decodeExtrinsic(registry, value, version)); } diff --git a/packages/types/src/extrinsic/ExtrinsicEra.ts b/packages/types/src/extrinsic/ExtrinsicEra.ts index 8ae8a9b65f..24a1ac6165 100644 --- a/packages/types/src/extrinsic/ExtrinsicEra.ts +++ b/packages/types/src/extrinsic/ExtrinsicEra.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyU8a, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyU8a, Registry } from '@polkadot/types-codec/types'; import type { BN } from '@polkadot/util'; import type { IExtrinsicEra } from '../types'; @@ -37,7 +37,7 @@ function getTrailingZeros (period: number): number { } /** @internal */ -function decodeMortalEra (registry: CodecRegistry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue { +function decodeMortalEra (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue { if (isU8a(value) || isHex(value) || Array.isArray(value)) { return decodeMortalU8a(registry, u8aToU8a(value)); } else if (!value) { @@ -50,7 +50,7 @@ function decodeMortalEra (registry: CodecRegistry, value?: MortalMethod | Uint8A } /** @internal */ -function decodeMortalObject (registry: CodecRegistry, value: MortalMethod): MortalEraValue { +function decodeMortalObject (registry: Registry, value: MortalMethod): MortalEraValue { const { current, period } = value; let calPeriod = Math.pow(2, Math.ceil(Math.log2(period))); @@ -64,7 +64,7 @@ function decodeMortalObject (registry: CodecRegistry, value: MortalMethod): Mort } /** @internal */ -function decodeMortalU8a (registry: CodecRegistry, value: Uint8Array): MortalEraValue { +function decodeMortalU8a (registry: Registry, value: Uint8Array): MortalEraValue { if (value.length === 0) { return [new U64(registry), new U64(registry)]; } @@ -117,7 +117,7 @@ function decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef */ export class ImmortalEra extends Raw { // eslint-disable-next-line @typescript-eslint/no-unused-vars - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { // For immortals, we always provide the known value (i.e. treated as a // constant no matter how it is constructed - it is a fixed structure) super(registry, IMMORTAL_ERA); @@ -130,7 +130,7 @@ export class ImmortalEra extends Raw { * The MortalEra for an extrinsic, indicating period and phase */ export class MortalEra extends Tuple { - constructor (registry: CodecRegistry, value?: MortalMethod | Uint8Array | number[] | string) { + constructor (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string) { super(registry, { period: U64, phase: U64 @@ -225,7 +225,7 @@ export class MortalEra extends Tuple { * The era for an extrinsic, indicating either a mortal or immortal extrinsic */ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, { ImmortalEra, MortalEra diff --git a/packages/types/src/extrinsic/ExtrinsicPayload.ts b/packages/types/src/extrinsic/ExtrinsicPayload.ts index fa74e27a0b..8cbe54f967 100644 --- a/packages/types/src/extrinsic/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/ExtrinsicPayload.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, BareOpts, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyJson, BareOpts, Registry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { ExtrinsicPayloadV4 } from '../interfaces/extrinsics'; import type { Balance, Hash, Index } from '../interfaces/runtime'; @@ -29,7 +29,7 @@ const VERSIONS = [ ]; /** @internal */ -function decodeExtrinsicPayload (registry: CodecRegistry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version: number = DEFAULT_VERSION): ExtrinsicPayloadVx { +function decodeExtrinsicPayload (registry: Registry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version: number = DEFAULT_VERSION): ExtrinsicPayloadVx { if (value instanceof GenericExtrinsicPayload) { return value.unwrap(); } @@ -44,7 +44,7 @@ function decodeExtrinsicPayload (registry: CodecRegistry, value?: GenericExtrins * on the contents included */ export class GenericExtrinsicPayload extends Base { - constructor (registry: CodecRegistry, value?: Partial | Uint8Array | string, { version }: ExtrinsicPayloadOptions = {}) { + constructor (registry: Registry, value?: Partial | Uint8Array | string, { version }: ExtrinsicPayloadOptions = {}) { super(registry, decodeExtrinsicPayload(registry, value as ExtrinsicPayloadValue, version)); } diff --git a/packages/types/src/extrinsic/ExtrinsicPayloadUnknown.ts b/packages/types/src/extrinsic/ExtrinsicPayloadUnknown.ts index 339aedccc7..57d1de9dc7 100644 --- a/packages/types/src/extrinsic/ExtrinsicPayloadUnknown.ts +++ b/packages/types/src/extrinsic/ExtrinsicPayloadUnknown.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { ExtrinsicPayloadOptions } from './types'; import { Struct } from '@polkadot/types-codec'; @@ -12,7 +12,7 @@ import { Struct } from '@polkadot/types-codec'; * A default handler for payloads where the version is not known (default throw) */ export class GenericExtrinsicPayloadUnknown extends Struct { - constructor (registry: CodecRegistry, value?: unknown, { version = 0 }: Partial = {}) { + constructor (registry: Registry, value?: unknown, { version = 0 }: Partial = {}) { super(registry, {}); throw new Error(`Unsupported extrinsic payload version ${version}`); diff --git a/packages/types/src/extrinsic/ExtrinsicUnknown.ts b/packages/types/src/extrinsic/ExtrinsicUnknown.ts index 64a6695b70..51cb21ce58 100644 --- a/packages/types/src/extrinsic/ExtrinsicUnknown.ts +++ b/packages/types/src/extrinsic/ExtrinsicUnknown.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { ExtrinsicOptions } from './types'; import { Struct } from '@polkadot/types-codec'; @@ -14,7 +14,7 @@ import { UNMASK_VERSION } from './constants'; * A default handler for extrinsics where the version is not known (default throw) */ export class GenericExtrinsicUnknown extends Struct { - constructor (registry: CodecRegistry, value?: unknown, { isSigned = false, version = 0 }: Partial = {}) { + constructor (registry: Registry, value?: unknown, { isSigned = false, version = 0 }: Partial = {}) { super(registry, {}); throw new Error(`Unsupported ${isSigned ? '' : 'un'}signed extrinsic version ${version & UNMASK_VERSION}`); diff --git a/packages/types/src/extrinsic/SignerPayload.ts b/packages/types/src/extrinsic/SignerPayload.ts index 71ddbaaf89..1c1d8016b3 100644 --- a/packages/types/src/extrinsic/SignerPayload.ts +++ b/packages/types/src/extrinsic/SignerPayload.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../interfaces'; import type { Codec, ISignerPayload, SignerPayloadJSON, SignerPayloadRaw } from '../types'; @@ -45,7 +45,7 @@ const knownTypes: Record = { export class GenericSignerPayload extends Struct implements ISignerPayload, SignerPayloadType { readonly #extraTypes: Record; - constructor (registry: CodecRegistry, value?: HexString | { [x: string]: unknown; } | Map | unknown[]) { + constructor (registry: Registry, value?: HexString | { [x: string]: unknown; } | Map | unknown[]) { const extensionTypes = objectSpread>({}, registry.getSignedExtensionTypes(), registry.getSignedExtensionExtra()); super(registry, objectSpread>({}, extensionTypes, knownTypes), value); diff --git a/packages/types/src/extrinsic/util.ts b/packages/types/src/extrinsic/util.ts index 3985a148b9..6b89db4f32 100644 --- a/packages/types/src/extrinsic/util.ts +++ b/packages/types/src/extrinsic/util.ts @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import type { SignOptions } from '@polkadot/keyring/types'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { IKeyringPair } from '../types'; // a helper function for both types of payloads, Raw and metadata-known -export function sign (registry: CodecRegistry, signerPair: IKeyringPair, u8a: Uint8Array, options?: SignOptions): Uint8Array { +export function sign (registry: Registry, signerPair: IKeyringPair, u8a: Uint8Array, options?: SignOptions): Uint8Array { const encoded = u8a.length > 256 ? registry.hash(u8a) : u8a; diff --git a/packages/types/src/extrinsic/v4/Extrinsic.ts b/packages/types/src/extrinsic/v4/Extrinsic.ts index ab8be99996..7c44139d53 100644 --- a/packages/types/src/extrinsic/v4/Extrinsic.ts +++ b/packages/types/src/extrinsic/v4/Extrinsic.ts @@ -1,7 +1,6 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { ExtrinsicSignatureV4 } from '../../interfaces/extrinsics'; import type { Address, Call } from '../../interfaces/runtime'; @@ -24,7 +23,7 @@ export interface ExtrinsicValueV4 { * The third generation of compact extrinsics */ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl { - constructor (registry: CodecRegistry, value?: Uint8Array | ExtrinsicValueV4 | Call, { isSigned }: Partial = {}) { + constructor (registry: Registry, value?: Uint8Array | ExtrinsicValueV4 | Call, { isSigned }: Partial = {}) { super(registry, { signature: 'ExtrinsicSignatureV4', // eslint-disable-next-line sort-keys @@ -33,7 +32,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl { } /** @internal */ - public static decodeExtrinsic (registry: CodecRegistry, value?: Call | Uint8Array | ExtrinsicValueV4, isSigned = false): ExtrinsicValueV4 { + public static decodeExtrinsic (registry: Registry, value?: Call | Uint8Array | ExtrinsicValueV4, isSigned = false): ExtrinsicValueV4 { if (value instanceof GenericExtrinsicV4) { return value; } else if (value instanceof registry.createClassUnsafe('Call')) { @@ -67,7 +66,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl { } public override get registry (): Registry { - return super.registry as Registry; + return super.registry; } /** diff --git a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts index 8b5f377739..f9e9bd8b5c 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { SignOptions } from '@polkadot/keyring/types'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { ExtrinsicEra } from '../../interfaces/extrinsics'; import type { AssetId, Balance, Hash, Index } from '../../interfaces/runtime'; @@ -22,7 +22,7 @@ import { sign } from '../util'; export class GenericExtrinsicPayloadV4 extends Struct { #signOptions: SignOptions; - constructor (registry: CodecRegistry, value?: ExtrinsicPayloadValue | Uint8Array | HexString) { + constructor (registry: Registry, value?: ExtrinsicPayloadValue | Uint8Array | HexString) { super(registry, objectSpread( { method: 'Bytes' }, registry.getSignedExtensionTypes(), diff --git a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts index 8f361a91e0..1ab20d963d 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts @@ -1,7 +1,6 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { EcdsaSignature, Ed25519Signature, ExtrinsicEra, ExtrinsicSignature, Sr25519Signature } from '../../interfaces/extrinsics'; import type { Address, Balance, Call, Index } from '../../interfaces/runtime'; @@ -17,7 +16,7 @@ import { GenericExtrinsicPayloadV4 } from './ExtrinsicPayload'; // Ensure we have enough data for all types of signatures const FAKE_SIGNATURE = new Uint8Array(256).fill(1); -function toAddress (registry: CodecRegistry, address: Address | Uint8Array | string): Address { +function toAddress (registry: Registry, address: Address | Uint8Array | string): Address { return registry.createTypeUnsafe('Address', [isU8a(address) ? u8aToHex(address) : address]); } @@ -29,7 +28,7 @@ function toAddress (registry: CodecRegistry, address: Address | Uint8Array | str export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSignature { #signKeys: string[]; - constructor (registry: CodecRegistry, value?: GenericExtrinsicSignatureV4 | Uint8Array, { isSigned }: ExtrinsicSignatureOptions = {}) { + constructor (registry: Registry, value?: GenericExtrinsicSignatureV4 | Uint8Array, { isSigned }: ExtrinsicSignatureOptions = {}) { const signTypes = registry.getSignedExtensionTypes(); super( @@ -91,7 +90,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig } public override get registry (): Registry { - return super.registry as Registry; + return super.registry; } /** diff --git a/packages/types/src/generic/AccountId.ts b/packages/types/src/generic/AccountId.ts index 1338cc02a7..d5e3a8cc20 100644 --- a/packages/types/src/generic/AccountId.ts +++ b/packages/types/src/generic/AccountId.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyString, AnyU8a, CodecRegistry } from '@polkadot/types-codec/types'; +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'; @@ -30,7 +30,7 @@ function decodeAccountId (value?: AnyU8a | AnyString): Uint8Array { * just a Uint8Array wrapper with a fixed length. */ export class GenericAccountId extends U8aFixed { - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { const decoded = decodeAccountId(value); // Part of stream containing >= 32 bytes or a all empty (defaults) diff --git a/packages/types/src/generic/AccountIndex.ts b/packages/types/src/generic/AccountIndex.ts index 80fb39ad9b..782d0671dd 100644 --- a/packages/types/src/generic/AccountIndex.ts +++ b/packages/types/src/generic/AccountIndex.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyNumber, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyNumber, Registry } from '@polkadot/types-codec/types'; import { u32 } from '@polkadot/types-codec'; import { BN, bnToBn, isBigInt, isBn, isHex, isNumber, isU8a } from '@polkadot/util'; @@ -36,7 +36,7 @@ function decodeAccountIndex (value: AnyNumber): BN | bigint | Uint8Array | numbe * for an Account. We extends from [[U32]] to provide the number-like properties. */ export class GenericAccountIndex extends u32 { - constructor (registry: CodecRegistry, value: AnyNumber = new BN(0)) { + constructor (registry: Registry, value: AnyNumber = new BN(0)) { super(registry, decodeAccountIndex(value)); } diff --git a/packages/types/src/generic/Block.ts b/packages/types/src/generic/Block.ts index 036329a021..6f03395f34 100644 --- a/packages/types/src/generic/Block.ts +++ b/packages/types/src/generic/Block.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Vec } from '@polkadot/types-codec'; -import type { AnyNumber, AnyU8a, CodecRegistry, IU8a } from '@polkadot/types-codec/types'; +import type { AnyNumber, AnyU8a, IU8a, Registry } from '@polkadot/types-codec/types'; import type { GenericExtrinsic } from '../extrinsic/Extrinsic'; import type { Digest, DigestItem, Header } from '../interfaces/runtime'; @@ -27,7 +27,7 @@ export interface BlockValue { * A block encoded with header and extrinsics */ export class GenericBlock extends Struct { - constructor (registry: CodecRegistry, value?: BlockValue | Uint8Array) { + constructor (registry: Registry, value?: BlockValue | Uint8Array) { super(registry, { header: 'Header', // eslint-disable-next-line sort-keys diff --git a/packages/types/src/generic/Call.ts b/packages/types/src/generic/Call.ts index ee64726022..e58456b21b 100644 --- a/packages/types/src/generic/Call.ts +++ b/packages/types/src/generic/Call.ts @@ -1,9 +1,9 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, Codec, CodecRegistry, IMethod } from '@polkadot/types-codec/types'; +import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, Codec, IMethod, Registry } from '@polkadot/types-codec/types'; import type { FunctionMetadataLatest } from '../interfaces/metadata'; -import type { CallBase, CallFunction, InterfaceTypes, Registry } from '../types'; +import type { CallBase, CallFunction, InterfaceTypes } from '../types'; import { Struct, U8aFixed } from '@polkadot/types-codec'; import { isHex, isObject, isU8a, objectSpread, u8aToU8a } from '@polkadot/util'; @@ -26,7 +26,7 @@ interface DecodedMethod extends DecodeMethodInput { * @param meta - The function metadata used to get the definition. * @internal */ -function getArgsDef (registry: CodecRegistry, meta: FunctionMetadataLatest): ArgsDef { +function getArgsDef (registry: Registry, meta: FunctionMetadataLatest): ArgsDef { return meta.fields.reduce((result, { name, type }, index): ArgsDef => { result[name.unwrapOr(`param${index}`).toString()] = registry.createLookupType(type) as keyof InterfaceTypes; @@ -35,7 +35,7 @@ function getArgsDef (registry: CodecRegistry, meta: FunctionMetadataLatest): Arg } /** @internal */ -function decodeCallViaObject (registry: CodecRegistry, value: DecodedMethod, _meta?: FunctionMetadataLatest): DecodedMethod { +function decodeCallViaObject (registry: Registry, value: DecodedMethod, _meta?: FunctionMetadataLatest): DecodedMethod { // we only pass args/methodsIndex out const { args, callIndex } = value; @@ -46,7 +46,7 @@ function decodeCallViaObject (registry: CodecRegistry, value: DecodedMethod, _me : callIndex; // Find metadata with callIndex - const meta = _meta || (registry as Registry).findMetaCall(lookupIndex).meta; + const meta = _meta || registry.findMetaCall(lookupIndex).meta; return { args, @@ -57,14 +57,14 @@ function decodeCallViaObject (registry: CodecRegistry, value: DecodedMethod, _me } /** @internal */ -function decodeCallViaU8a (registry: CodecRegistry, value: Uint8Array, _meta?: FunctionMetadataLatest): DecodedMethod { +function decodeCallViaU8a (registry: Registry, value: Uint8Array, _meta?: FunctionMetadataLatest): DecodedMethod { // We need 2 bytes for the callIndex const callIndex = new Uint8Array(2); callIndex.set(value.subarray(0, 2), 0); // Find metadata with callIndex - const meta = _meta || (registry as Registry).findMetaCall(callIndex).meta; + const meta = _meta || registry.findMetaCall(callIndex).meta; return { args: value.subarray(2), @@ -85,7 +85,7 @@ function decodeCallViaU8a (registry: CodecRegistry, value: Uint8Array, _meta?: F * necessary. * @internal */ -function decodeCall (registry: CodecRegistry, value: unknown | DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataLatest): DecodedMethod { +function decodeCall (registry: Registry, value: unknown | DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataLatest): DecodedMethod { if (isU8a(value) || isHex(value)) { return decodeCallViaU8a(registry, u8aToU8a(value), _meta); } else if (isObject(value) && value.callIndex && value.args) { @@ -101,7 +101,7 @@ function decodeCall (registry: CodecRegistry, value: unknown | DecodedMethod | U * A wrapper around the `[sectionIndex, methodIndex]` value that uniquely identifies a method */ export class GenericCallIndex extends U8aFixed { - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { super(registry, value, 16); } } @@ -114,7 +114,7 @@ export class GenericCallIndex extends U8aFixed { export class GenericCall extends Struct implements CallBase { protected _meta: FunctionMetadataLatest; - constructor (registry: CodecRegistry, value: unknown, meta?: FunctionMetadataLatest) { + constructor (registry: Registry, value: unknown, meta?: FunctionMetadataLatest) { const decoded = decodeCall(registry, value, meta); try { @@ -127,7 +127,7 @@ export class GenericCall extends Struct implement let method = 'unknown.unknown'; try { - const c = (registry as Registry).findMetaCall(decoded.callIndex); + const c = (registry).findMetaCall(decoded.callIndex); method = `${c.section}.${c.method}`; } catch (error) { @@ -190,7 +190,7 @@ export class GenericCall extends Struct implement } public override get registry (): Registry { - return super.registry as Registry; + return super.registry; } /** diff --git a/packages/types/src/generic/ChainProperties.ts b/packages/types/src/generic/ChainProperties.ts index 60131d2b96..0cbed3c39a 100644 --- a/packages/types/src/generic/ChainProperties.ts +++ b/packages/types/src/generic/ChainProperties.ts @@ -1,13 +1,13 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { Codec } from '../types'; import { Json, Option, Text, u32, Vec } from '@polkadot/types-codec'; import { isFunction, isNull, isUndefined } from '@polkadot/util'; -function createValue (registry: CodecRegistry, type: string, value: unknown, asArray = true): Option { +function createValue (registry: Registry, type: string, value: unknown, asArray = true): Option { // We detect codec here as well - when found, generally this is constructed from itself if (value && isFunction((value as Option).unwrapOrDefault)) { return value as Option; @@ -27,7 +27,7 @@ function createValue (registry: CodecRegistry, type: string, value: unknown, asA ); } -function decodeValue (registry: CodecRegistry, key: string, value: unknown): unknown { +function decodeValue (registry: Registry, key: string, value: unknown): unknown { return key === 'ss58Format' ? createValue(registry, 'Option', value, false) : key === 'tokenDecimals' @@ -37,7 +37,7 @@ function decodeValue (registry: CodecRegistry, key: string, value: unknown): unk : value; } -function decode (registry: CodecRegistry, value?: Map | Record | null): Record { +function decode (registry: Registry, value?: Map | Record | null): Record { return ( // allow decoding from a map as well (ourselves) value && isFunction((value as Map).entries) @@ -55,7 +55,7 @@ function decode (registry: CodecRegistry, value?: Map | Record< } export class GenericChainProperties extends Json { - constructor (registry: CodecRegistry, value?: Map | Record | null) { + constructor (registry: Registry, value?: Map | Record | null) { super(registry, decode(registry, value)); } diff --git a/packages/types/src/generic/ConsensusEngineId.ts b/packages/types/src/generic/ConsensusEngineId.ts index 027fa4039f..45ec90a0c9 100644 --- a/packages/types/src/generic/ConsensusEngineId.ts +++ b/packages/types/src/generic/ConsensusEngineId.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyU8a, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyU8a, Registry } from '@polkadot/types-codec/types'; import type { AccountId, RawAuraPreDigest, RawBabePreDigestCompat } from '../interfaces'; import { Bytes, U8aFixed, u32 } from '@polkadot/types-codec'; @@ -12,7 +12,7 @@ export const CID_BABE = stringToU8a('BABE'); export const CID_GRPA = stringToU8a('FRNK'); export const CID_POW = stringToU8a('pow_'); -function getAuraAuthor (registry: CodecRegistry, bytes: Bytes, sessionValidators: AccountId[]): AccountId { +function getAuraAuthor (registry: Registry, bytes: Bytes, sessionValidators: AccountId[]): AccountId { return sessionValidators[ registry.createTypeUnsafe('RawAuraPreDigest', [bytes.toU8a(true)]) .slotNumber @@ -21,7 +21,7 @@ function getAuraAuthor (registry: CodecRegistry, bytes: Bytes, sessionValidators ]; } -function getBabeAuthor (registry: CodecRegistry, bytes: Bytes, sessionValidators: AccountId[]): AccountId { +function getBabeAuthor (registry: Registry, bytes: Bytes, sessionValidators: AccountId[]): AccountId { const digest = registry.createTypeUnsafe('RawBabePreDigestCompat', [bytes.toU8a(true)]); return sessionValidators[ @@ -29,7 +29,7 @@ function getBabeAuthor (registry: CodecRegistry, bytes: Bytes, sessionValidators ]; } -function getBytesAsAuthor (registry: CodecRegistry, bytes: Bytes): AccountId { +function getBytesAsAuthor (registry: Registry, bytes: Bytes): AccountId { return registry.createTypeUnsafe('AccountId', [bytes]); } @@ -39,7 +39,7 @@ function getBytesAsAuthor (registry: CodecRegistry, bytes: Bytes): AccountId { * A 4-byte identifier identifying the engine */ export class GenericConsensusEngineId extends U8aFixed { - constructor (registry: CodecRegistry, value?: AnyU8a) { + constructor (registry: Registry, value?: AnyU8a) { super( registry, isNumber(value) diff --git a/packages/types/src/generic/Event.ts b/packages/types/src/generic/Event.ts index 42f284569e..7e41c99bc4 100644 --- a/packages/types/src/generic/Event.ts +++ b/packages/types/src/generic/Event.ts @@ -1,11 +1,11 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types'; -import type { CreateRegistry, TypeDef } from '@polkadot/types-create/types'; +import type { AnyJson, Codec, CodecClass } from '@polkadot/types-codec/types'; +import type { TypeDef } from '@polkadot/types-create/types'; import type { EventMetadataLatest } from '../interfaces/metadata'; import type { EventId } from '../interfaces/system'; -import type { IEvent, IEventData, InterfaceTypes } from '../types'; +import type { IEvent, IEventData, InterfaceTypes, Registry } from '../types'; import { Null, Struct, Tuple } from '@polkadot/types-codec'; import { objectSpread } from '@polkadot/util'; @@ -19,7 +19,7 @@ interface Decoded { } /** @internal */ -function decodeEvent (registry: CodecRegistry, value?: Uint8Array): Decoded { +function decodeEvent (registry: Registry, value?: Uint8Array): Decoded { if (!value || !value.length) { return { DataType: Null }; } @@ -49,7 +49,7 @@ export class GenericEventData extends Tuple implements IEventData { readonly #typeDef: TypeDef[]; - constructor (registry: CodecRegistry, value: Uint8Array, meta: EventMetadataLatest, section = '', method = '') { + constructor (registry: Registry, value: Uint8Array, meta: EventMetadataLatest, section = '', method = '') { const fields = meta?.fields || []; super(registry, fields.map(({ type }) => registry.createLookupType(type) as keyof InterfaceTypes), value); @@ -57,7 +57,7 @@ export class GenericEventData extends Tuple implements IEventData { this.#meta = meta; this.#method = method; this.#section = section; - this.#typeDef = fields.map(({ type }) => (registry as CreateRegistry).lookup.getTypeDef(type)); + this.#typeDef = fields.map(({ type }) => (registry).lookup.getTypeDef(type)); } /** @@ -98,7 +98,7 @@ export class GenericEventData extends Tuple implements IEventData { export class GenericEvent extends Struct implements IEvent { // Currently we _only_ decode from Uint8Array, since we expect it to // be used via EventRecord - constructor (registry: CodecRegistry, _value?: Uint8Array) { + constructor (registry: Registry, _value?: Uint8Array) { const { DataType, value } = decodeEvent(registry, _value); super(registry, { diff --git a/packages/types/src/generic/LookupSource.ts b/packages/types/src/generic/LookupSource.ts index 45926f0356..492af1875b 100644 --- a/packages/types/src/generic/LookupSource.ts +++ b/packages/types/src/generic/LookupSource.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; @@ -18,7 +18,7 @@ type AnyAddress = bigint | BN | GenericLookupSource | GenericAccountId | Generic export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]); /** @internal */ -function decodeString (registry: CodecRegistry, value: string): GenericAccountId | GenericAccountIndex { +function decodeString (registry: Registry, value: string): GenericAccountId | GenericAccountIndex { const decoded = decodeAddress(value); return decoded.length === 32 @@ -27,7 +27,7 @@ function decodeString (registry: CodecRegistry, value: string): GenericAccountId } /** @internal */ -function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericAccountId | GenericAccountIndex { +function decodeU8a (registry: Registry, value: Uint8Array): GenericAccountId | GenericAccountIndex { // This allows us to instantiate an address with a raw publicKey. Do this first before // we checking the first byte, otherwise we may split an already-existent valid address if (value.length === 32) { @@ -42,7 +42,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericAccountI } /** @internal */ -function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): GenericAccountId | GenericAccountIndex { +function decodeAddressOrIndex (registry: Registry, value: AnyAddress): GenericAccountId | GenericAccountIndex { return value instanceof GenericLookupSource ? value.inner : value instanceof GenericAccountId || value instanceof GenericAccountIndex @@ -63,7 +63,7 @@ function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): Gene * is encoded as `[ , ...publicKey/...bytes ]` as per spec */ export class GenericLookupSource extends Base { - constructor (registry: CodecRegistry, value: AnyAddress = new Uint8Array()) { + constructor (registry: Registry, value: AnyAddress = new Uint8Array()) { super(registry, decodeAddressOrIndex(registry, value)); } diff --git a/packages/types/src/generic/MultiAddress.ts b/packages/types/src/generic/MultiAddress.ts index 019edf975e..2577591fb3 100644 --- a/packages/types/src/generic/MultiAddress.ts +++ b/packages/types/src/generic/MultiAddress.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import { Enum } from '@polkadot/types-codec'; import { isBn, isNumber, isString, isU8a } from '@polkadot/util'; @@ -10,7 +10,7 @@ import { decodeAddress } from '@polkadot/util-crypto'; import { GenericAccountId } from './AccountId'; import { GenericAccountIndex } from './AccountIndex'; -function decodeU8a (registry: CodecRegistry, u8a: Uint8Array): unknown { +function decodeU8a (registry: Registry, u8a: Uint8Array): unknown { if ([0, 32].includes(u8a.length)) { return { Id: u8a }; } else if (u8a.length === 20) { @@ -22,7 +22,7 @@ function decodeU8a (registry: CodecRegistry, u8a: Uint8Array): unknown { return u8a; } -function decodeMultiAny (registry: CodecRegistry, value?: unknown): unknown { +function decodeMultiAny (registry: Registry, value?: unknown): unknown { if (value instanceof GenericAccountId) { return { Id: value }; } else if (isU8a(value)) { @@ -40,7 +40,7 @@ function decodeMultiAny (registry: CodecRegistry, value?: unknown): unknown { } export class GenericMultiAddress extends Enum { - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { super(registry, { Id: 'AccountId', Index: 'Compact', diff --git a/packages/types/src/generic/Vote.ts b/packages/types/src/generic/Vote.ts index 8506401285..c04cb0bd76 100644 --- a/packages/types/src/generic/Vote.ts +++ b/packages/types/src/generic/Vote.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyJson, Registry } from '@polkadot/types-codec/types'; import type { Conviction } from '../interfaces/democracy'; import type { AllConvictions } from '../interfaces/democracy/definitions'; import type { ArrayElementType } from '../types'; @@ -38,7 +38,7 @@ function decodeVoteU8a (value: Uint8Array): Uint8Array { } /** @internal */ -function decodeVoteType (registry: CodecRegistry, value: VoteType): Uint8Array { +function decodeVoteType (registry: Registry, value: VoteType): Uint8Array { return new Uint8Array([ ( new Bool(registry, value.aye).isTrue @@ -50,7 +50,7 @@ function decodeVoteType (registry: CodecRegistry, value: VoteType): Uint8Array { } /** @internal */ -function decodeVote (registry: CodecRegistry, value?: InputTypes): Uint8Array { +function decodeVote (registry: Registry, value?: InputTypes): Uint8Array { if (isU8a(value)) { return decodeVoteU8a(value); } else if (isUndefined(value) || value instanceof Boolean || isBoolean(value)) { @@ -72,7 +72,7 @@ export class GenericVote extends U8aFixed { #conviction: Conviction; - constructor (registry: CodecRegistry, value?: InputTypes) { + constructor (registry: Registry, value?: InputTypes) { // decoded is just 1 byte // Aye: Most Significant Bit // Conviction: 0000 - 0101 diff --git a/packages/types/src/metadata/MagicNumber.ts b/packages/types/src/metadata/MagicNumber.ts index bbbed4bd02..6b4cd38611 100644 --- a/packages/types/src/metadata/MagicNumber.ts +++ b/packages/types/src/metadata/MagicNumber.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyNumber, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyNumber, Registry } from '@polkadot/types-codec/types'; import { U32 } from '@polkadot/types-codec'; import { assert } from '@polkadot/util'; @@ -9,7 +9,7 @@ import { assert } from '@polkadot/util'; export const MAGIC_NUMBER = 0x6174656d; // `meta`, reversed for Little Endian encoding export class MagicNumber extends U32 { - constructor (registry: CodecRegistry, value?: AnyNumber) { + constructor (registry: Registry, value?: AnyNumber) { super(registry, value); if (!this.isEmpty) { diff --git a/packages/types/src/metadata/Metadata.ts b/packages/types/src/metadata/Metadata.ts index e8f7635947..ab1d59df46 100644 --- a/packages/types/src/metadata/Metadata.ts +++ b/packages/types/src/metadata/Metadata.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import { isHex, isU8a, u8aToU8a } from '@polkadot/util'; @@ -22,7 +22,7 @@ function toU8a (value: Uint8Array | HexString): Uint8Array { : value; } -function decodeU8a (registry: CodecRegistry, value: Uint8Array): MetadataVersioned { +function decodeU8a (registry: Registry, value: Uint8Array): MetadataVersioned { try { return new MetadataVersioned(registry, value); } catch (error) { @@ -45,7 +45,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): MetadataVersion * The versioned runtime metadata as a decoded structure */ export class Metadata extends MetadataVersioned { - constructor (registry: CodecRegistry, value?: Uint8Array | HexString | Map | Record) { + constructor (registry: Registry, value?: Uint8Array | HexString | Map | Record) { super( registry, isU8a(value) || isHex(value) diff --git a/packages/types/src/metadata/MetadataVersioned.ts b/packages/types/src/metadata/MetadataVersioned.ts index 0975f301d5..621f19121f 100644 --- a/packages/types/src/metadata/MetadataVersioned.ts +++ b/packages/types/src/metadata/MetadataVersioned.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyJson } from '@polkadot/types-codec/types'; import type { MetadataAll, MetadataLatest, MetadataV9, MetadataV10, MetadataV11, MetadataV12, MetadataV13, MetadataV14 } from '../interfaces/metadata'; import type { Registry } from '../types'; @@ -31,7 +31,7 @@ const LATEST_VERSION = 14; export class MetadataVersioned extends Struct { readonly #converted = new Map(); - constructor (registry: CodecRegistry, value?: unknown) { + constructor (registry: Registry, value?: unknown) { // console.time('MetadataVersioned') super(registry, { @@ -48,7 +48,7 @@ export class MetadataVersioned extends Struct { return this.version === version; }; - #getVersion = (version: MetaVersions, fromPrev: (registry: CodecRegistry, input: F, metaVersion: number) => T): T => { + #getVersion = (version: MetaVersions, fromPrev: (registry: Registry, input: F, metaVersion: number) => T): T => { const asCurr = `asV${version}` as MetaAsX; const asPrev = version === 'latest' ? `asV${LATEST_VERSION}` as MetaAsX @@ -73,7 +73,7 @@ export class MetadataVersioned extends Struct { }; public override get registry (): Registry { - return super.registry as Registry; + return super.registry; } /** diff --git a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts index 4602332f01..19344a15e9 100644 --- a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts +++ b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Text, Type, Vec } from '@polkadot/types-codec'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { ILookup, TypeDef } from '@polkadot/types-create/types'; import type { PortableType } from '../../interfaces/metadata'; import type { SiField, SiLookupTypeId, SiPath, SiType, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '../../interfaces/scaleInfo'; @@ -326,7 +326,7 @@ export class PortableRegistry extends Struct implements ILookup { #typeDefs: Record = {}; #types: Record; - constructor (registry: CodecRegistry, value?: Uint8Array) { + constructor (registry: Registry, value?: Uint8Array) { // console.time('PortableRegistry') super(registry, { diff --git a/packages/types/src/metadata/decorate/constants/index.ts b/packages/types/src/metadata/decorate/constants/index.ts index 802c2ae231..3bd8cd266c 100644 --- a/packages/types/src/metadata/decorate/constants/index.ts +++ b/packages/types/src/metadata/decorate/constants/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, PalletConstantMetadataLatest } from '../../../interfaces'; import type { ConstantCodec, Constants } from '../types'; @@ -11,7 +11,7 @@ import { objectNameToCamel } from '../util'; /** @internal */ // eslint-disable-next-line @typescript-eslint/no-unused-vars -export function decorateConstants (registry: CodecRegistry, { pallets }: MetadataLatest, _version: number): Constants { +export function decorateConstants (registry: Registry, { pallets }: MetadataLatest, _version: number): Constants { const result: Constants = {}; for (let i = 0; i < pallets.length; i++) { diff --git a/packages/types/src/metadata/decorate/errors/index.ts b/packages/types/src/metadata/decorate/errors/index.ts index 24c7e58489..fb9335035c 100644 --- a/packages/types/src/metadata/decorate/errors/index.ts +++ b/packages/types/src/metadata/decorate/errors/index.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Text, u8 } from '@polkadot/types-codec'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { DispatchErrorModule, MetadataLatest, SiField, SiVariant } from '../../../interfaces'; import type { PortableRegistry } from '../../../metadata'; import type { Errors, IsError } from '../types'; @@ -28,7 +28,7 @@ export function variantToMeta (lookup: PortableRegistry, variant: SiVariant): It } /** @internal */ -export function decorateErrors (registry: CodecRegistry, { lookup, pallets }: MetadataLatest, version: number): Errors { +export function decorateErrors (registry: Registry, { lookup, pallets }: MetadataLatest, version: number): Errors { const result: Errors = {}; for (let i = 0; i < pallets.length; i++) { diff --git a/packages/types/src/metadata/decorate/events/index.ts b/packages/types/src/metadata/decorate/events/index.ts index f53a40b4dc..efc0ac76e2 100644 --- a/packages/types/src/metadata/decorate/events/index.ts +++ b/packages/types/src/metadata/decorate/events/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyTuple, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyTuple, Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, PalletMetadataLatest, SiVariant } from '../../../interfaces'; import type { IEvent } from '../../../types'; import type { Events, IsEvent } from '../types'; @@ -17,7 +17,7 @@ export function filterEventsSome ({ events }: PalletMetadataLatest): boolean { } /** @internal */ -export function decorateEvents (registry: CodecRegistry, { lookup, pallets }: MetadataLatest, version: number): Events { +export function decorateEvents (registry: Registry, { lookup, pallets }: MetadataLatest, version: number): Events { const result: Events = {}; const filtered = pallets.filter(filterEventsSome); diff --git a/packages/types/src/metadata/decorate/extrinsics/createUnchecked.ts b/packages/types/src/metadata/decorate/extrinsics/createUnchecked.ts index 4c4b165616..08f3da175c 100644 --- a/packages/types/src/metadata/decorate/extrinsics/createUnchecked.ts +++ b/packages/types/src/metadata/decorate/extrinsics/createUnchecked.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, AnyTuple, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyJson, AnyTuple, Registry } from '@polkadot/types-codec/types'; import type { Call, FunctionMetadataLatest } from '../../../interfaces'; import type { CallFunction, IMethod } from '../../../types'; @@ -12,7 +12,7 @@ function isTx (tx: IMethod, callIndex: Uint8Array } /** @internal */ -export function createUnchecked (registry: CodecRegistry, section: string, callIndex: Uint8Array, callMetadata: FunctionMetadataLatest): CallFunction { +export function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: FunctionMetadataLatest): CallFunction { const expectedArgs = callMetadata.fields; const funcName = stringCamelCase(callMetadata.name); diff --git a/packages/types/src/metadata/decorate/extrinsics/index.ts b/packages/types/src/metadata/decorate/extrinsics/index.ts index aa605ecefb..a573da5c1d 100644 --- a/packages/types/src/metadata/decorate/extrinsics/index.ts +++ b/packages/types/src/metadata/decorate/extrinsics/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, PalletMetadataLatest, SiVariant } from '../../../interfaces'; import type { PortableRegistry } from '../../../metadata'; import type { CallFunction } from '../../../types'; @@ -18,7 +18,7 @@ export function filterCallsSome ({ calls }: PalletMetadataLatest): boolean { return calls.isSome; } -export function createCallFunction (registry: CodecRegistry, lookup: PortableRegistry, variant: SiVariant, sectionName: string, sectionIndex: number): CallFunction { +export function createCallFunction (registry: Registry, lookup: PortableRegistry, variant: SiVariant, sectionName: string, sectionIndex: number): CallFunction { const { fields, index } = variant; const args = new Array>(fields.length); @@ -45,7 +45,7 @@ export function createCallFunction (registry: CodecRegistry, lookup: PortableReg } /** @internal */ -export function decorateExtrinsics (registry: CodecRegistry, { lookup, pallets }: MetadataLatest, version: number): Extrinsics { +export function decorateExtrinsics (registry: Registry, { lookup, pallets }: MetadataLatest, version: number): Extrinsics { const result: Extrinsics = {}; const filtered = pallets.filter(filterCallsSome); diff --git a/packages/types/src/metadata/decorate/index.ts b/packages/types/src/metadata/decorate/index.ts index 6c274e2426..c80fbc16f3 100644 --- a/packages/types/src/metadata/decorate/index.ts +++ b/packages/types/src/metadata/decorate/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { DecoratedMeta } from './types'; import { assert } from '@polkadot/util'; @@ -16,7 +16,7 @@ import { decorateStorage } from './storage'; /** * Expands the metadata by decoration into consts, query and tx sections */ -export function expandMetadata (registry: CodecRegistry, metadata: Metadata): DecoratedMeta { +export function expandMetadata (registry: Registry, metadata: Metadata): DecoratedMeta { assert(metadata instanceof Metadata, 'You need to pass a valid Metadata instance to Decorated'); const latest = metadata.asLatest; diff --git a/packages/types/src/metadata/decorate/storage/createFunction.ts b/packages/types/src/metadata/decorate/storage/createFunction.ts index 813eae35ce..bb3ef8cd90 100644 --- a/packages/types/src/metadata/decorate/storage/createFunction.ts +++ b/packages/types/src/metadata/decorate/storage/createFunction.ts @@ -1,10 +1,10 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types'; -import type { CreateRegistry } from '@polkadot/types-create/types'; +import type { ICompact, INumber } from '@polkadot/types-codec/types'; import type { StorageEntryMetadataLatest, StorageHasher } from '../../../interfaces/metadata'; import type { StorageEntry } from '../../../primitive/types'; +import type { Registry } from '../../../types'; import { Raw } from '@polkadot/types-codec'; import { assert, compactAddLength, compactStripLength, isUndefined, objectSpread, stringCamelCase, stringLowerFirst, u8aConcat, u8aToU8a } from '@polkadot/util'; @@ -46,7 +46,7 @@ export const NO_RAW_ARGS: RawArgs = { }; /** @internal */ -export function createKeyRaw (registry: CodecRegistry, itemFn: CreateItemBase, { args, hashers, keys }: RawArgs): Uint8Array { +export function createKeyRaw (registry: Registry, itemFn: CreateItemBase, { args, hashers, keys }: RawArgs): Uint8Array { const extra = new Array(keys.length); for (let i = 0; i < keys.length; i++) { @@ -68,7 +68,7 @@ function filterDefined (a: unknown): boolean { } /** @internal */ -function createKey (registry: CodecRegistry, itemFn: CreateItemFn, { args, hashers, keys }: RawArgs): Uint8Array { +function createKey (registry: Registry, itemFn: CreateItemFn, { args, hashers, keys }: RawArgs): Uint8Array { const { method, section } = itemFn; assert(Array.isArray(args), () => `Call to ${stringCamelCase(section || 'unknown')}.${stringCamelCase(method || 'unknown')} needs ${keys.length} arguments`); @@ -81,7 +81,7 @@ function createKey (registry: CodecRegistry, itemFn: CreateItemFn, { args, hashe } /** @internal */ -function createWStorageFn (registry: CodecRegistry, itemFn: CreateItemFn, options: CreateItemOptions): (...args: unknown[]) => Uint8Array { +function createWStorageFn (registry: Registry, itemFn: CreateItemFn, options: CreateItemOptions): (...args: unknown[]) => Uint8Array { const { meta: { type } } = itemFn; let cacheKey: Uint8Array | null = null; @@ -104,12 +104,12 @@ function createWStorageFn (registry: CodecRegistry, itemFn: CreateItemFn, option return hashers.length === 1 ? createKey(registry, itemFn, { args, hashers, keys: [key] }) - : createKey(registry, itemFn, { args, hashers, keys: (registry as CreateRegistry).lookup.getSiType(key).def.asTuple }); + : createKey(registry, itemFn, { args, hashers, keys: (registry).lookup.getSiType(key).def.asTuple }); }; } /** @internal */ -function createWithMeta (registry: CodecRegistry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry { +function createWithMeta (registry: Registry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry { const { meta, method, prefix, section } = itemFn; const storageFn = createWStorageFn(registry, itemFn, options) as StorageEntry; @@ -126,7 +126,7 @@ function createWithMeta (registry: CodecRegistry, itemFn: CreateItemFn, options: } /** @internal */ -function extendHeadMeta (registry: CodecRegistry, { meta: { docs, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: (...args: unknown[]) => Raw): (...args: unknown[]) => StorageKey { +function extendHeadMeta (registry: Registry, { meta: { docs, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: (...args: unknown[]) => Raw): (...args: unknown[]) => StorageKey { const outputType = registry.createLookupType(type.asMap.key); // metadata with a fallback value using the type of the key, the normal @@ -145,7 +145,7 @@ function extendHeadMeta (registry: CodecRegistry, { meta: { docs, name, type }, } /** @internal */ -function extendPrefixedMap (registry: CodecRegistry, itemFn: CreateItemFn, storageFn: StorageEntry): StorageEntry { +function extendPrefixedMap (registry: Registry, itemFn: CreateItemFn, storageFn: StorageEntry): StorageEntry { const { meta: { type }, method, section } = itemFn; storageFn.iterKey = extendHeadMeta(registry, itemFn, storageFn, (...args: unknown[]): Raw => { @@ -162,7 +162,7 @@ function extendPrefixedMap (registry: CodecRegistry, itemFn: CreateItemFn, stora const { hashers, key } = type.asMap; const keysVec = hashers.length === 1 ? [key] - : (registry as CreateRegistry).lookup.getSiType(key).def.asTuple; + : (registry).lookup.getSiType(key).def.asTuple; return new Raw(registry, createKeyRaw(registry, itemFn, { args, hashers: hashers.slice(0, args.length), keys: keysVec.slice(0, args.length) })); } @@ -175,7 +175,7 @@ function extendPrefixedMap (registry: CodecRegistry, itemFn: CreateItemFn, stora } /** @internal */ -export function createFunction (registry: CodecRegistry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry { +export function createFunction (registry: Registry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry { const { meta: { type } } = itemFn; const storageFn = createWithMeta(registry, itemFn, options); diff --git a/packages/types/src/metadata/decorate/storage/getStorage.ts b/packages/types/src/metadata/decorate/storage/getStorage.ts index e1a71d83e5..5270b66101 100644 --- a/packages/types/src/metadata/decorate/storage/getStorage.ts +++ b/packages/types/src/metadata/decorate/storage/getStorage.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { StorageEntry } from '../../../primitive/types'; import type { Storage } from '../types'; import { substrate } from './substrate'; /** @internal */ -export function getStorage (registry: CodecRegistry): Storage { +export function getStorage (registry: Registry): Storage { const storage: Record = {}; const entries = Object.entries(substrate); diff --git a/packages/types/src/metadata/decorate/storage/index.ts b/packages/types/src/metadata/decorate/storage/index.ts index a5b59b1400..0e58008970 100644 --- a/packages/types/src/metadata/decorate/storage/index.ts +++ b/packages/types/src/metadata/decorate/storage/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, StorageEntryMetadataLatest } from '../../../interfaces'; import type { Storage } from '../types'; @@ -18,7 +18,7 @@ const VERSION_DOCS = { docs: 'Returns the current pallet version from storage', /** @internal */ // eslint-disable-next-line @typescript-eslint/no-unused-vars -export function decorateStorage (registry: CodecRegistry, { pallets }: MetadataLatest, _metaVersion: number): Storage { +export function decorateStorage (registry: Registry, { pallets }: MetadataLatest, _metaVersion: number): Storage { const result: Storage = getStorage(registry); for (let i = 0; i < pallets.length; i++) { diff --git a/packages/types/src/metadata/decorate/storage/substrate.ts b/packages/types/src/metadata/decorate/storage/substrate.ts index 22a113517b..2efe198070 100644 --- a/packages/types/src/metadata/decorate/storage/substrate.ts +++ b/packages/types/src/metadata/decorate/storage/substrate.ts @@ -1,13 +1,13 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { StorageEntry } from '../../../primitive/types'; import type { ManualMetadata } from './util'; import { createRuntimeFunction } from './util'; -type Creator = (registry: CodecRegistry) => StorageEntry; +type Creator = (registry: Registry) => StorageEntry; const prefix = 'Substrate'; const section = 'substrate'; diff --git a/packages/types/src/metadata/decorate/storage/util.ts b/packages/types/src/metadata/decorate/storage/util.ts index ce77d5c337..b7b4afc999 100644 --- a/packages/types/src/metadata/decorate/storage/util.ts +++ b/packages/types/src/metadata/decorate/storage/util.ts @@ -1,7 +1,6 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; import type { PortableType } from '../../../interfaces'; import type { StorageEntry } from '../../../primitive/types'; import type { Registry } from '../../../types'; @@ -61,15 +60,15 @@ function findSiType (registry: Registry, orig: string): PortableType | undefined // Small helper function to factorize code on this page. /** @internal */ -export function createRuntimeFunction ({ method, prefix, section }: ManualDefinition, key: Uint8Array | string, { docs, type }: ManualMetadata): (registry: CodecRegistry) => StorageEntry { - return (registry: CodecRegistry): StorageEntry => +export function createRuntimeFunction ({ method, prefix, section }: ManualDefinition, key: Uint8Array | string, { docs, type }: ManualMetadata): (registry: Registry) => StorageEntry { + return (registry: Registry): StorageEntry => createFunction(registry, { meta: registry.createTypeUnsafe('StorageEntryMetadataLatest', [{ docs: registry.createTypeUnsafe('Vec', [[docs]]), modifier: registry.createTypeUnsafe('StorageEntryModifierLatest', ['Required']), name: registry.createTypeUnsafe('Text', [method]), toJSON: (): any => key, - type: registry.createTypeUnsafe('StorageEntryTypeLatest', [{ Plain: findSiType(registry as Registry, type)?.id || 0 }]) + type: registry.createTypeUnsafe('StorageEntryTypeLatest', [{ Plain: findSiType(registry, type)?.id || 0 }]) }]), method, prefix, diff --git a/packages/types/src/metadata/decorate/types.ts b/packages/types/src/metadata/decorate/types.ts index 1dfa78db67..2542de1969 100644 --- a/packages/types/src/metadata/decorate/types.ts +++ b/packages/types/src/metadata/decorate/types.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyTuple, Codec, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyTuple, Codec, Registry } from '@polkadot/types-codec/types'; import type { DispatchErrorModule, ErrorMetadataLatest, EventMetadataLatest, PalletConstantMetadataLatest } from '../../interfaces'; import type { StorageEntry } from '../../primitive/types'; import type { CallFunction, IEvent } from '../../types'; @@ -47,6 +47,6 @@ export interface DecoratedMeta { readonly errors: Errors; readonly events: Events; readonly query: Storage; - readonly registry: CodecRegistry; + readonly registry: Registry; readonly tx: Extrinsics } diff --git a/packages/types/src/metadata/util/getUniqTypes.ts b/packages/types/src/metadata/util/getUniqTypes.ts index 3fcb11ee94..7b0fb1a15b 100644 --- a/packages/types/src/metadata/util/getUniqTypes.ts +++ b/packages/types/src/metadata/util/getUniqTypes.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, SiLookupTypeId } from '../../interfaces'; import type { PortableRegistry } from '../../metadata'; @@ -62,6 +62,6 @@ function getPalletNames ({ lookup, pallets }: MetadataLatest): string[][][] { } /** @internal */ -export function getUniqTypes (registry: CodecRegistry, meta: MetadataLatest, throwError: boolean): string[] { +export function getUniqTypes (registry: Registry, meta: MetadataLatest, throwError: boolean): string[] { return validateTypes(registry, throwError, flattenUniq(getPalletNames(meta))); } diff --git a/packages/types/src/metadata/util/testUtil.ts b/packages/types/src/metadata/util/testUtil.ts index 96bdf676c7..f72a7c8277 100644 --- a/packages/types/src/metadata/util/testUtil.ts +++ b/packages/types/src/metadata/util/testUtil.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { Check } from './types'; import fs from 'fs'; @@ -23,7 +23,7 @@ function writeJson (json: unknown, version: number, type: string, sub: 'json' | } /** @internal */ -export function decodeLatestMeta (registry: CodecRegistry, type: string, version: number, { compare, data, types }: Check): void { +export function decodeLatestMeta (registry: Registry, type: string, version: number, { compare, data, types }: Check): void { const metadata = new Metadata(registry, data); registry.setMetadata(metadata); @@ -68,7 +68,7 @@ export function decodeLatestMeta (registry: CodecRegistry, type: string, version } /** @internal */ -export function toLatest (registry: CodecRegistry, version: number, { data }: Check, withThrow = true): void { +export function toLatest (registry: Registry, version: number, { data }: Check, withThrow = true): void { it(`converts v${version} to latest`, (): void => { const metadata = new Metadata(registry, data); @@ -83,7 +83,7 @@ export function toLatest (registry: CodecRegistry, version: number, { data }: Ch } /** @internal */ -export function defaultValues (registry: CodecRegistry, { data, fails = [] }: Check, withThrow = true, withFallbackCheck = false): void { +export function defaultValues (registry: Registry, { data, fails = [] }: Check, withThrow = true, withFallbackCheck = false): void { describe('storage with default values', (): void => { const metadata = new Metadata(registry, data); const { pallets } = metadata.asLatest; @@ -125,7 +125,7 @@ export function defaultValues (registry: CodecRegistry, { data, fails = [] }: Ch }); } -function serialize (registry: CodecRegistry, { data }: Check): void { +function serialize (registry: Registry, { data }: Check): void { const metadata = new Metadata(registry, data); it('serializes to hex in the same form as retrieved', (): void => { diff --git a/packages/types/src/metadata/util/toCallsOnly.ts b/packages/types/src/metadata/util/toCallsOnly.ts index 8e602f9daf..3aebadaf40 100644 --- a/packages/types/src/metadata/util/toCallsOnly.ts +++ b/packages/types/src/metadata/util/toCallsOnly.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, CodecRegistry } from '@polkadot/types-codec/types'; +import type { AnyJson, Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, PalletCallMetadataLatest } from '../../interfaces/metadata'; import { Option, Text, u8 } from '@polkadot/types-codec'; @@ -22,7 +22,7 @@ function trimDocs (docs: Text[]): string[] { } /** @internal */ -export function toCallsOnly (registry: CodecRegistry, { extrinsic, lookup, pallets }: MetadataLatest): AnyJson { +export function toCallsOnly (registry: Registry, { extrinsic, lookup, pallets }: MetadataLatest): AnyJson { return registry.createTypeUnsafe('MetadataLatest', [{ extrinsic, lookup: { diff --git a/packages/types/src/metadata/util/validateTypes.ts b/packages/types/src/metadata/util/validateTypes.ts index 214aa41d18..4749084451 100644 --- a/packages/types/src/metadata/util/validateTypes.ts +++ b/packages/types/src/metadata/util/validateTypes.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import { logger } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { flattenUniq } from './flattenUniq'; const l = logger('metadata'); /** @internal */ -export function validateTypes (registry: CodecRegistry, throwError: boolean, types: string[]): string[] { +export function validateTypes (registry: Registry, throwError: boolean, types: string[]): string[] { const missing = flattenUniq(extractTypes(types)) .filter((type) => !registry.hasType(type)) .sort(); diff --git a/packages/types/src/metadata/v10/toV11.ts b/packages/types/src/metadata/v10/toV11.ts index 26877d4bb2..a46c11def6 100644 --- a/packages/types/src/metadata/v10/toV11.ts +++ b/packages/types/src/metadata/v10/toV11.ts @@ -1,11 +1,11 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataV10, MetadataV11 } from '../../interfaces/metadata'; /** @internal */ -export function toV11 (registry: CodecRegistry, { modules }: MetadataV10): MetadataV11 { +export function toV11 (registry: Registry, { modules }: MetadataV10): MetadataV11 { return registry.createTypeUnsafe('MetadataV11', [{ // This is new in V11, pass V0 here - something non-existing, telling the API to use // the fallback for this information (on-chain detection) diff --git a/packages/types/src/metadata/v11/toV12.ts b/packages/types/src/metadata/v11/toV12.ts index 517d4254e4..18c85b12f5 100644 --- a/packages/types/src/metadata/v11/toV12.ts +++ b/packages/types/src/metadata/v11/toV12.ts @@ -1,13 +1,13 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataV11, MetadataV12, ModuleMetadataV12 } from '../../interfaces/metadata'; /** * @internal **/ -export function toV12 (registry: CodecRegistry, { extrinsic, modules }: MetadataV11): MetadataV12 { +export function toV12 (registry: Registry, { extrinsic, modules }: MetadataV11): MetadataV12 { return registry.createTypeUnsafe('MetadataV12', [{ extrinsic, modules: modules.map((mod): ModuleMetadataV12 => diff --git a/packages/types/src/metadata/v12/toV13.ts b/packages/types/src/metadata/v12/toV13.ts index 6c6eda234e..fb1241a836 100644 --- a/packages/types/src/metadata/v12/toV13.ts +++ b/packages/types/src/metadata/v12/toV13.ts @@ -1,12 +1,12 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataV12, MetadataV13 } from '../../interfaces/metadata'; /** * @internal **/ -export function toV13 (registry: CodecRegistry, metadata: MetadataV12): MetadataV13 { +export function toV13 (registry: Registry, metadata: MetadataV12): MetadataV13 { return registry.createTypeUnsafe('MetadataV13', [metadata]); } diff --git a/packages/types/src/metadata/v13/toV14.ts b/packages/types/src/metadata/v13/toV14.ts index 3321d9f474..adad5ad5da 100644 --- a/packages/types/src/metadata/v13/toV14.ts +++ b/packages/types/src/metadata/v13/toV14.ts @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import type { Text, Type } from '@polkadot/types-codec'; -import type { CodecRegistry } from '@polkadot/types-codec/types'; import type { ErrorMetadataV13, EventMetadataV13, ExtrinsicMetadataV13, ExtrinsicMetadataV14, FunctionMetadataV13, MetadataV13, MetadataV14, ModuleConstantMetadataV13, ModuleMetadataV13, PalletCallMetadataV14, PalletConstantMetadataV14, PalletErrorMetadataV14, PalletEventMetadataV14, PalletMetadataV14, PalletStorageMetadataV14, StorageEntryMetadataV14, StorageEntryTypeV14, StorageHasherV13, StorageMetadataV13 } from '../../interfaces/metadata'; import type { SiVariant } from '../../interfaces/scaleInfo'; import type { OverrideModuleType, Registry } from '../../types'; @@ -73,7 +72,7 @@ function makeVariantType (modName: Text, variantType: string, compatTypes: TypeS * @internal * generate & register the OriginCaller type **/ -function registerOriginCaller (registry: CodecRegistry, modules: ModuleMetadataV13[], metaVersion: number): void { +function registerOriginCaller (registry: Registry, modules: ModuleMetadataV13[], metaVersion: number): void { registry.register({ OriginCaller: { _enum: modules @@ -127,7 +126,7 @@ function setTypeOverride (sectionTypes: OverrideModuleType, types: Type[]): void * Apply module-specific type overrides (always be done as part of toV14) * @internal **/ -function convertCalls (compatTypes: TypeSpec[], registry: CodecRegistry, modName: Text, calls: FunctionMetadataV13[], sectionTypes: OverrideModuleType): PalletCallMetadataV14 { +function convertCalls (compatTypes: TypeSpec[], registry: Registry, modName: Text, calls: FunctionMetadataV13[], sectionTypes: OverrideModuleType): PalletCallMetadataV14 { const variants = calls.map(({ args, docs, name }, index): SiVariant => { setTypeOverride(sectionTypes, args.map(({ type }) => type)); @@ -150,7 +149,7 @@ function convertCalls (compatTypes: TypeSpec[], registry: CodecRegistry, modName * Apply module-specific type overrides (always be done as part of toV14) * @internal */ -function convertConstants (compatTypes: TypeSpec[], registry: CodecRegistry, constants: ModuleConstantMetadataV13[], sectionTypes: OverrideModuleType): PalletConstantMetadataV14[] { +function convertConstants (compatTypes: TypeSpec[], registry: Registry, constants: ModuleConstantMetadataV13[], sectionTypes: OverrideModuleType): PalletConstantMetadataV14[] { return constants.map(({ docs, name, type, value }): PalletConstantMetadataV14 => { setTypeOverride(sectionTypes, [type]); @@ -168,7 +167,7 @@ function convertConstants (compatTypes: TypeSpec[], registry: CodecRegistry, con * @internal */ // eslint-disable-next-line @typescript-eslint/no-unused-vars -function convertErrors (compatTypes: TypeSpec[], registry: CodecRegistry, modName: Text, errors: ErrorMetadataV13[], _sectionTypes: OverrideModuleType): PalletErrorMetadataV14 { +function convertErrors (compatTypes: TypeSpec[], registry: Registry, modName: Text, errors: ErrorMetadataV13[], _sectionTypes: OverrideModuleType): PalletErrorMetadataV14 { const variants = errors.map(({ docs, name }, index): SiVariant => registry.createTypeUnsafe('SiVariant', [{ docs, @@ -187,7 +186,7 @@ function convertErrors (compatTypes: TypeSpec[], registry: CodecRegistry, modNam * Apply module-specific type overrides (always be done as part of toV14) * @internal **/ -function convertEvents (compatTypes: TypeSpec[], registry: CodecRegistry, modName: Text, events: EventMetadataV13[], sectionTypes: OverrideModuleType): PalletEventMetadataV14 { +function convertEvents (compatTypes: TypeSpec[], registry: Registry, modName: Text, events: EventMetadataV13[], sectionTypes: OverrideModuleType): PalletEventMetadataV14 { const variants = events.map(({ args, docs, name }, index): SiVariant => { setTypeOverride(sectionTypes, args); @@ -206,7 +205,7 @@ function convertEvents (compatTypes: TypeSpec[], registry: CodecRegistry, modNam }]); } -function createMapEntry (compatTypes: TypeSpec[], registry: CodecRegistry, sectionTypes: OverrideModuleType, { hashers, keys, value }: MapDef): StorageEntryTypeV14 { +function createMapEntry (compatTypes: TypeSpec[], registry: Registry, sectionTypes: OverrideModuleType, { hashers, keys, value }: MapDef): StorageEntryTypeV14 { setTypeOverride(sectionTypes, [value, ...(Array.isArray(keys) ? keys : [keys])]); return registry.createTypeUnsafe('StorageEntryTypeV14', [{ @@ -224,7 +223,7 @@ function createMapEntry (compatTypes: TypeSpec[], registry: CodecRegistry, secti * Apply module-specific storage type overrides (always part of toV14) * @internal **/ -function convertStorage (compatTypes: TypeSpec[], registry: CodecRegistry, { items, prefix }: StorageMetadataV13, sectionTypes: OverrideModuleType): PalletStorageMetadataV14 { +function convertStorage (compatTypes: TypeSpec[], registry: Registry, { items, prefix }: StorageMetadataV13, sectionTypes: OverrideModuleType): PalletStorageMetadataV14 { return registry.createTypeUnsafe('PalletStorageMetadataV14', [{ items: items.map(({ docs, fallback, modifier, name, type }): StorageEntryMetadataV14 => { let entryType: StorageEntryTypeV14; @@ -277,7 +276,7 @@ function convertStorage (compatTypes: TypeSpec[], registry: CodecRegistry, { ite /** @internal */ // eslint-disable-next-line @typescript-eslint/no-unused-vars -function convertExtrinsic (registry: CodecRegistry, { signedExtensions, version }: ExtrinsicMetadataV13): ExtrinsicMetadataV14 { +function convertExtrinsic (registry: Registry, { signedExtensions, version }: ExtrinsicMetadataV13): ExtrinsicMetadataV14 { return registry.createTypeUnsafe('ExtrinsicMetadataV14', [{ signedExtensions: signedExtensions.map((identifier) => ({ identifier, @@ -289,8 +288,8 @@ function convertExtrinsic (registry: CodecRegistry, { signedExtensions, version } /** @internal */ -function createPallet (compatTypes: TypeSpec[], registry: CodecRegistry, mod: ModuleMetadataV13, { calls, constants, errors, events, storage }: { calls: FunctionMetadataV13[] | null, constants: ModuleConstantMetadataV13[], errors: ErrorMetadataV13[] | null, events: EventMetadataV13[] | null, storage: StorageMetadataV13 | null }): PalletMetadataV14 { - const sectionTypes = getAliasTypes(registry as Registry, stringCamelCase(mod.name)); +function createPallet (compatTypes: TypeSpec[], registry: Registry, mod: ModuleMetadataV13, { calls, constants, errors, events, storage }: { calls: FunctionMetadataV13[] | null, constants: ModuleConstantMetadataV13[], errors: ErrorMetadataV13[] | null, events: EventMetadataV13[] | null, storage: StorageMetadataV13 | null }): PalletMetadataV14 { + const sectionTypes = getAliasTypes(registry, stringCamelCase(mod.name)); return registry.createTypeUnsafe('PalletMetadataV14', [{ calls: calls && convertCalls(compatTypes, registry, mod.name, calls, sectionTypes), @@ -307,7 +306,7 @@ function createPallet (compatTypes: TypeSpec[], registry: CodecRegistry, mod: Mo * Convert the Metadata to v14 * @internal **/ -export function toV14 (registry: CodecRegistry, v13: MetadataV13, metaVersion: number): MetadataV14 { +export function toV14 (registry: Registry, v13: MetadataV13, metaVersion: number): MetadataV14 { const compatTypes: TypeSpec[] = []; compatType(compatTypes, 'Null'); // position 0 always has Null diff --git a/packages/types/src/metadata/v14/toLatest.ts b/packages/types/src/metadata/v14/toLatest.ts index df95e6587d..814c2231ab 100644 --- a/packages/types/src/metadata/v14/toLatest.ts +++ b/packages/types/src/metadata/v14/toLatest.ts @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataLatest, MetadataV14 } from '../../interfaces/metadata'; /** @@ -9,6 +9,6 @@ import type { MetadataLatest, MetadataV14 } from '../../interfaces/metadata'; * @internal **/ // eslint-disable-next-line @typescript-eslint/no-unused-vars -export function toLatest (registry: CodecRegistry, v14: MetadataV14, _metaVersion: number): MetadataLatest { +export function toLatest (registry: Registry, v14: MetadataV14, _metaVersion: number): MetadataLatest { return v14; } diff --git a/packages/types/src/metadata/v9/toV10.ts b/packages/types/src/metadata/v9/toV10.ts index f35b2b5567..48cf8b7dc7 100644 --- a/packages/types/src/metadata/v9/toV10.ts +++ b/packages/types/src/metadata/v9/toV10.ts @@ -1,13 +1,13 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry } from '@polkadot/types-codec/types'; +import type { Registry } from '@polkadot/types-codec/types'; import type { MetadataV9, MetadataV10, ModuleMetadataV9, ModuleMetadataV10, StorageEntryMetadataV9, StorageEntryTypeV9, StorageHasherV9, StorageHasherV10 } from '../../interfaces/metadata'; // migrate a storage hasher type // see https://github.com/paritytech/substrate/pull/4462 /** @internal */ -function createStorageHasher (registry: CodecRegistry, hasher: StorageHasherV9): StorageHasherV10 { +function createStorageHasher (registry: Registry, hasher: StorageHasherV9): StorageHasherV10 { // Blake2_128_Concat has been added at index 2, so we increment all the // indexes greater than 2 if (hasher.toNumber() >= 2) { @@ -18,7 +18,7 @@ function createStorageHasher (registry: CodecRegistry, hasher: StorageHasherV9): } /** @internal */ -function createStorageType (registry: CodecRegistry, entryType: StorageEntryTypeV9): [any, number] { +function createStorageType (registry: Registry, entryType: StorageEntryTypeV9): [any, number] { if (entryType.isMap) { return [{ ...entryType.asMap, @@ -38,7 +38,7 @@ function createStorageType (registry: CodecRegistry, entryType: StorageEntryType } /** @internal */ -function convertModule (registry: CodecRegistry, mod: ModuleMetadataV9): ModuleMetadataV10 { +function convertModule (registry: Registry, mod: ModuleMetadataV9): ModuleMetadataV10 { const storage = mod.storage.unwrapOr(null); return registry.createTypeUnsafe('ModuleMetadataV10', [{ @@ -56,7 +56,7 @@ function convertModule (registry: CodecRegistry, mod: ModuleMetadataV9): ModuleM } /** @internal */ -export function toV10 (registry: CodecRegistry, { modules }: MetadataV9): MetadataV10 { +export function toV10 (registry: Registry, { modules }: MetadataV9): MetadataV10 { return registry.createTypeUnsafe('MetadataV10', [{ modules: modules.map((mod) => convertModule(registry, mod)) }]); diff --git a/packages/types/src/primitive/Data.ts b/packages/types/src/primitive/Data.ts index 28141329d8..ad159d429c 100644 --- a/packages/types/src/primitive/Data.ts +++ b/packages/types/src/primitive/Data.ts @@ -1,14 +1,14 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CodecRegistry, IU8a } from '@polkadot/types-codec/types'; +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'; /** @internal */ -function decodeDataU8a (registry: CodecRegistry, value: Uint8Array): [undefined | Uint8Array, number | undefined] { +function decodeDataU8a (registry: Registry, value: Uint8Array): [undefined | Uint8Array, number | undefined] { const indicator = value[0]; if (!indicator) { @@ -27,7 +27,7 @@ function decodeDataU8a (registry: CodecRegistry, value: Uint8Array): [undefined } /** @internal */ -function decodeData (registry: CodecRegistry, value?: Record | Uint8Array | Enum | string): [any, number | undefined] { +function decodeData (registry: Registry, value?: Record | Uint8Array | Enum | string): [any, number | undefined] { if (isU8a(value) || isString(value)) { return decodeDataU8a(registry, u8aToU8a(value)); } else if (!value) { @@ -44,7 +44,7 @@ function decodeData (registry: CodecRegistry, value?: Record | Uint * A [[Data]] container with node, raw or hashed data */ export class Data extends Enum { - constructor (registry: CodecRegistry, value?: Record | Uint8Array | Enum | string) { + constructor (registry: Registry, value?: Record | Uint8Array | Enum | string) { super(registry, { None: 'Null', // 0 Raw: 'Bytes', // 1 diff --git a/packages/types/src/primitive/StorageKey.ts b/packages/types/src/primitive/StorageKey.ts index b3e1c91607..2bc80dad14 100644 --- a/packages/types/src/primitive/StorageKey.ts +++ b/packages/types/src/primitive/StorageKey.ts @@ -1,12 +1,11 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyJson, AnyTuple, Codec, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types'; -import type { CreateRegistry } from '@polkadot/types-create/types'; +import type { AnyJson, AnyTuple, Codec, ICompact, INumber } from '@polkadot/types-codec/types'; import type { StorageEntryMetadataLatest, StorageEntryTypeLatest, StorageHasher } from '../interfaces/metadata'; import type { AllHashers } from '../interfaces/metadata/definitions'; import type { SiLookupTypeId } from '../interfaces/scaleInfo'; -import type { InterfaceTypes, IStorageKey } from '../types'; +import type { InterfaceTypes, IStorageKey, Registry } from '../types'; import type { StorageEntry } from './types'; import { Bytes } from '@polkadot/types-codec'; @@ -44,8 +43,8 @@ export function unwrapStorageSi (type: StorageEntryTypeLatest): SiLookupTypeId { } /** @internal */ -export function unwrapStorageType (registry: CodecRegistry, type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes { - const outputType = getSiName((registry as CreateRegistry).lookup, unwrapStorageSi(type)); +export function unwrapStorageType (registry: Registry, type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes { + const outputType = getSiName((registry).lookup, unwrapStorageSi(type)); return isOptional ? `Option<${outputType}>` as unknown as keyof InterfaceTypes @@ -91,7 +90,7 @@ function decodeStorageKey (value?: string | Uint8Array | StorageKey | StorageEnt } /** @internal */ -function decodeHashers (registry: CodecRegistry, value: Uint8Array, hashers: [StorageHasher, ICompact][]): A { +function decodeHashers (registry: Registry, value: Uint8Array, hashers: [StorageHasher, ICompact][]): A { // the storage entry is xxhashAsU8a(prefix, 128) + xxhashAsU8a(method, 128), 256 bits total let offset = 32; const result = new Array(hashers.length); @@ -111,7 +110,7 @@ function decodeHashers (registry: CodecRegistry, value: Uin } /** @internal */ -function decodeArgsFromMeta (registry: CodecRegistry, value: Uint8Array, meta?: StorageEntryMetadataLatest): A { +function decodeArgsFromMeta (registry: Registry, value: Uint8Array, meta?: StorageEntryMetadataLatest): A { if (!meta || !meta.type.isMap) { return [] as unknown as A; } @@ -119,7 +118,7 @@ function decodeArgsFromMeta (registry: CodecRegistry, value const { hashers, key } = meta.type.asMap; const keys = hashers.length === 1 ? [key] - : (registry as CreateRegistry).lookup.getSiType(key).def.asTuple; + : (registry).lookup.getSiType(key).def.asTuple; return decodeHashers(registry, value, hashers.map((h, i) => [h, keys[i]])); } @@ -140,7 +139,7 @@ function getMeta (value: StorageKey | StorageEntry | [StorageEntry, unknown[]?]) } /** @internal */ -function getType (registry: CodecRegistry, value: StorageKey | StorageEntry | [StorageEntry, unknown[]?]): string { +function getType (registry: Registry, value: StorageKey | StorageEntry | [StorageEntry, unknown[]?]): string { if (value instanceof StorageKey) { return value.outputType; } else if (isFunction(value)) { @@ -176,7 +175,7 @@ export class StorageKey extends Bytes implements #section?: string; - constructor (registry: CodecRegistry, value?: string | Uint8Array | StorageKey | StorageEntry | [StorageEntry, unknown[]?], override: Partial = {}) { + constructor (registry: Registry, value?: string | Uint8Array | StorageKey | StorageEntry | [StorageEntry, unknown[]?], override: Partial = {}) { const { key, method, section } = decodeStorageKey(value); super(registry, key); diff --git a/packages/types/src/types/augmentLookup.ts b/packages/types/src/types/augmentLookup.ts new file mode 100644 index 0000000000..e367dc3f3f --- /dev/null +++ b/packages/types/src/types/augmentLookup.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2022 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Vec } from '@polkadot/types-codec'; +import type { TypeDef } from '@polkadot/types-create/types'; +import type { PortableType } from '../interfaces/metadata'; +import type { SiLookupTypeId, SiType } from '../interfaces/scaleInfo'; + +declare module '@polkadot/types-create/types/lookup' { + export interface ILookup { + readonly names: string[]; + readonly types: Vec; + + getName (lookupId: SiLookupTypeId | string | number): string | undefined; + getSiType (lookupId: SiLookupTypeId | string | number): SiType; + getTypeDef (lookupId: SiLookupTypeId | string | number): TypeDef; + } +} diff --git a/packages/types/src/types/augmentRegistry.ts b/packages/types/src/types/augmentRegistry.ts new file mode 100644 index 0000000000..1834a58937 --- /dev/null +++ b/packages/types/src/types/augmentRegistry.ts @@ -0,0 +1,74 @@ +// Copyright 2017-2022 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import '@polkadot/types-create/types/augmentRegistry'; + +import type { Codec, CodecClass } from '@polkadot/types-codec/types'; +import type { TypeDef } from '@polkadot/types-create/types'; +import type { BN } from '@polkadot/util'; +import type { ExtDef } from '../extrinsic/signedExtensions/types'; +import type { MetadataLatest } from '../interfaces/metadata'; +import type { SiField, SiLookupTypeId } from '../interfaces/scaleInfo'; +import type { ChainProperties } from '../interfaces/system'; +import type { Metadata, PortableRegistry } from '../metadata'; +import type { CallFunction as CallFunctionExt } from './calls'; +import type { DetectCodec } from './detect'; +import type { CodecHasher, RegisteredTypes } from './registry'; + +declare module '@polkadot/types-codec/types/registry' { + export interface RegistryError { + fields: SiField[]; + } + + export interface Registry { + // readonly chainDecimals: number[]; + // readonly chainSS58: number | undefined; + // readonly chainTokens: string[]; + readonly knownTypes: RegisteredTypes; + // readonly lookup: PortableRegistry; + readonly metadata: MetadataLatest; + readonly unknownTypes: string[]; + readonly signedExtensions: string[]; + + // createdAtHash?: Hash; + + findMetaCall (callIndex: Uint8Array): CallFunctionExt; + findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError; + // due to same circular imports where types don't really want to import from EventData, + // keep this as a generic Codec, however the actual impl. returns the correct + // findMetaEvent (eventIndex: Uint8Array): CodecClass; + + // isLookupType (value: string): boolean; + createLookupType (lookupId: SiLookupTypeId | number): string; + + createClass (type: K): CodecClass>; + createType (type: K, ...params: unknown[]): DetectCodec; + + get (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass> | undefined; + getChainProperties (): ChainProperties | undefined; + // getClassName (clazz: Constructor): string | undefined; + getDefinition (typeName: string): string | undefined; + getModuleInstances (specName: string, moduleName: string): string[] | undefined; + + // getOrThrow (name: K, msg?: string): CodecClass>; + // getOrUnknown (name: K): CodecClass>; + + setKnownTypes (types: RegisteredTypes): void; + // getSignedExtensionExtra (): Record; + // getSignedExtensionTypes (): Record; + + // hasClass (name: string): boolean; + // hasDef (name: string): boolean; + // hasType (name: string): boolean; + // hash (data: Uint8Array): IU8a; + init (): Registry; + // register (type: CodecClass | RegistryTypes): void; + // register (name: string, type: CodecClass): void; + // register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void; + setChainProperties (properties?: ChainProperties): void; + setHasher (hasher?: CodecHasher | null): void; + setLookup (lookup: PortableRegistry): void; + setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: ExtDef): void; + setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void; + } +} diff --git a/packages/types/src/types/index.ts b/packages/types/src/types/index.ts index 718c40cfb9..b37691ae1e 100644 --- a/packages/types/src/types/index.ts +++ b/packages/types/src/types/index.ts @@ -1,6 +1,9 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 +import './augmentLookup'; +import './augmentRegistry'; + // used inside augmented definitions export type { Observable } from 'rxjs'; export * from '@polkadot/types/types/registry'; diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 05255d8ba9..13244e1cb7 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -3,24 +3,18 @@ import type { Observable } from 'rxjs'; import type { BitVec, Bool, bool, Bytes, I8, i8, I16, i16, I32, i32, I64, i64, I128, i128, I256, i256, Json, Null, Raw, Text, Type, U8, u8, U16, u16, U32, u32, U64, u64, U128, u128, U256, u256, USize, usize } from '@polkadot/types-codec'; -import type { Codec, CodecClass, RegistryError as RegistryErrorBase, RegistryTypes } from '@polkadot/types-codec/types'; -import type { CreateRegistry, TypeDef } from '@polkadot/types-create/types'; +import type { RegistryTypes } from '@polkadot/types-codec/types'; import type { BN } from '@polkadot/util'; import type { GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericSignerPayload } from '../extrinsic'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { GenericCall } from '../generic'; -import type { MetadataLatest } from '../interfaces/metadata'; import type { HeaderPartial } from '../interfaces/runtime'; -import type { SiField, SiLookupTypeId } from '../interfaces/scaleInfo'; import type { RuntimeVersionPartial } from '../interfaces/state'; -import type { ChainProperties } from '../interfaces/system'; import type { Metadata, PortableRegistry } from '../metadata'; import type { Data, StorageKey } from '../primitive'; -import type { CallFunction } from './calls'; import type { DefinitionRpc, DefinitionRpcSub } from './definitions'; -import type { DetectCodec } from './detect'; -export type { RegistryTypes } from '@polkadot/types-codec/types'; +export type { Registry, RegistryError, RegistryTypes } from '@polkadot/types-codec/types'; export interface InterfaceTypes { // base codec @@ -50,17 +44,6 @@ export interface ChainUpgrades { versions: ChainUpgradeVersion[]; } -export interface RegistryError extends RegistryErrorBase { - args: string[]; - docs: string[]; - fields: SiField[]; - index: number; - // compat - method: string; - name: string; - section: string; -} - export interface OverrideVersionedType { // min (v >= min) and max (v <= max) minmax: [number | undefined | null, number | undefined | null] | [number?, number?] | (number | undefined | null)[]; @@ -115,56 +98,3 @@ export interface RegisteredTypes { */ typesSpec?: Record; } - -// Note the commented interfaces here are directly from CodecRegistry -export interface Registry extends CreateRegistry { - // readonly chainDecimals: number[]; - // readonly chainSS58: number | undefined; - // readonly chainTokens: string[]; - readonly knownTypes: RegisteredTypes; - readonly lookup: PortableRegistry; - readonly metadata: MetadataLatest; - readonly unknownTypes: string[]; - readonly signedExtensions: string[]; - - // createdAtHash?: Hash; - - findMetaCall (callIndex: Uint8Array): CallFunction; - findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError; - // due to same circular imports where types don't really want to import from EventData, - // keep this as a generic Codec, however the actual impl. returns the correct - // findMetaEvent (eventIndex: Uint8Array): CodecClass; - - // isLookupType (value: string): boolean; - createLookupType (lookupId: SiLookupTypeId | number): string; - - createClass (type: K): CodecClass>; - createType (type: K, ...params: unknown[]): DetectCodec; - - get (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass> | undefined; - getChainProperties (): ChainProperties | undefined; - // getClassName (clazz: Constructor): string | undefined; - getDefinition (typeName: string): string | undefined; - getModuleInstances (specName: string, moduleName: string): string[] | undefined; - - // getOrThrow (name: K, msg?: string): CodecClass>; - // getOrUnknown (name: K): CodecClass>; - - setKnownTypes (types: RegisteredTypes): void; - // getSignedExtensionExtra (): Record; - // getSignedExtensionTypes (): Record; - - // hasClass (name: string): boolean; - // hasDef (name: string): boolean; - // hasType (name: string): boolean; - // hash (data: Uint8Array): IU8a; - init (): Registry; - // register (type: CodecClass | RegistryTypes): void; - // register (name: string, type: CodecClass): void; - // register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void; - setChainProperties (properties?: ChainProperties): void; - setHasher (hasher?: CodecHasher | null): void; - setLookup (lookup: PortableRegistry): void; - setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: ExtDef): void; - setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void; -}