From 8b876894890c8936e434ecbd041013374082329a Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 16 Jul 2021 17:39:38 +0300 Subject: [PATCH] Add PortableRegistry for v14 lookups (#3778) --- packages/types/src/create/createClass.ts | 2 +- packages/types/src/create/registry.ts | 18 +- .../types/src/generic/PortableRegistry.ts | 312 ++++++++++++++++++ packages/types/src/generic/index.ts | 1 + .../types/src/interfaces/metadata/types.ts | 6 +- packages/types/src/interfaces/metadata/v14.ts | 4 +- packages/types/src/types/registry.ts | 3 + 7 files changed, 337 insertions(+), 9 deletions(-) create mode 100644 packages/types/src/generic/PortableRegistry.ts diff --git a/packages/types/src/create/createClass.ts b/packages/types/src/create/createClass.ts index 2911e64634..e249d93713 100644 --- a/packages/types/src/create/createClass.ts +++ b/packages/types/src/create/createClass.ts @@ -82,7 +82,7 @@ const infoMapping: Record C [TypeDefInfo.Compact]: (registry: Registry, value: TypeDef): Constructor => Compact.with(getSubType(value)), - [TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): Constructor => DoNotConstruct.with(value.displayName), + [TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): Constructor => DoNotConstruct.with(value.displayName || value.type), [TypeDefInfo.Enum]: (registry: Registry, value: TypeDef): Constructor => { const subs = getSubDefArray(value); diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 0b66b6721e..30d6c1db66 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -4,7 +4,7 @@ /* eslint-disable @typescript-eslint/no-var-requires */ import type { ExtDef } from '../extrinsic/signedExtensions/types'; -import type { ChainProperties, CodecHash, DispatchErrorModule, Hash } from '../interfaces/types'; +import type { ChainProperties, CodecHash, DispatchErrorModule, Hash, MetadataLatest, PortableRegistry } from '../interfaces/types'; import type { CallFunction, Codec, CodecHasher, Constructor, InterfaceTypes, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types'; import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, logger, stringCamelCase, stringify, u8aToHex } from '@polkadot/util'; @@ -114,6 +114,8 @@ export class TypeRegistry implements Registry { #definitions = new Map(); + #metadata?: MetadataLatest; + readonly #metadataCalls: Record = {}; readonly #metadataErrors: Record = {}; @@ -199,6 +201,18 @@ export class TypeRegistry implements Registry { return this.#knownTypes; } + public get lookup (): PortableRegistry { + throw new Error('Unimplemented'); + + // return this.metadata.lookup; + } + + public get metadata (): MetadataLatest { + assert(this.#metadata, 'Metadata has not been set on this registry'); + + return this.#metadata; + } + public get unknownTypes (): string[] { return [...this.#unknownTypes.keys()]; } @@ -388,6 +402,8 @@ export class TypeRegistry implements Registry { // sets the metadata public setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: ExtDef): void { + this.#metadata = metadata.asLatest; + injectExtrinsics(this, metadata, this.#metadataCalls); injectErrors(this, metadata, this.#metadataErrors); injectEvents(this, metadata, this.#metadataEvents); diff --git a/packages/types/src/generic/PortableRegistry.ts b/packages/types/src/generic/PortableRegistry.ts new file mode 100644 index 0000000000..6048ae447d --- /dev/null +++ b/packages/types/src/generic/PortableRegistry.ts @@ -0,0 +1,312 @@ +// Copyright 2017-2021 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Vec } from '../codec/Vec'; +import type { PortableType } from '../interfaces/metadata'; +import type { SiField, SiLookupTypeId, SiType, SiTypeDefArray, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiVariant } from '../interfaces/scaleInfo'; +import type { Codec, Constructor, Registry, TypeDef } from '../types'; + +import { assert, isNumber, isString, stringCamelCase } from '@polkadot/util'; + +import { Option } from '../codec/Option'; +import { Struct } from '../codec/Struct'; +import { getTypeClass } from '../create/createClass'; +import { withTypeString } from '../create/encodeTypes'; +import { getTypeDef } from '../create/getTypeDef'; +import { TypeDefInfo } from '../types'; + +interface CreateOptions { + blockHash?: Uint8Array | string | null; + isOptional?: boolean; +} + +const PRIMITIVE_ALIAS: Record = { + Char: 'u32', // Rust char is 4-bytes + Str: 'Text' +}; + +const INK_PRIMITIVE_ALWAYS = ['AccountId', 'AccountIndex', 'Address', 'Balance']; + +const LOOKUP_PREFIX = '__lookup_'; + +export class GenericPortableRegistry extends Struct { + #classes: Record = {}; + #typeDefs: Record = {}; + + constructor (registry: Registry, value?: Uint8Array) { + super(registry, { + types: 'Vec' + }, value); + } + + /** + * @description The types of the registry + */ + public get types (): Vec { + return this.get('types') as Vec; + } + + /** + * @description Returns tru if the type is in a Compat format + */ + public isSiString (value: string): boolean { + return value.startsWith(LOOKUP_PREFIX); + } + + /** + * @description Creates a lookup string from the supplied id + */ + public createSiString (lookupId: SiLookupTypeId | number): string { + return `${LOOKUP_PREFIX}${lookupId.toString()}`; + } + + /** + * @description creates a type from the id + */ + public createType (lookupId: SiLookupTypeId | string | number, params: unknown[], { blockHash, isOptional }: CreateOptions = {}): T { + const Clazz = this.getClass(lookupId); + const instance = new (isOptional ? Option.with(Clazz) : Clazz)(this.registry, ...params); + + if (blockHash) { + instance.createdAtHash = this.registry.createType('Hash', blockHash); + } + + return instance as T; + } + + /** + * @description Returns a class from the specified id + */ + public getClass (lookupId: SiLookupTypeId | string | number): Constructor { + const index = this.#getSiIndex(lookupId); + + if (!this.#classes[index]) { + // since we may have recursive lookups, fill in empty details as a start + this.#classes[index] = getTypeClass(this.registry, this.getTypeDef(lookupId)); + } + + return this.#classes[index] as Constructor; + } + + /** + * @description Finds a specific type in the registry + */ + public getSiType (lookupId: SiLookupTypeId | string | number): SiType { + const found = this.types[this.#getSiIndex(lookupId)]; + + assert(found, () => `PortableRegistry: Unable to find type with lookupId ${lookupId.toString()}`); + + return found.type; + } + + /** + * @description Lookup the type definition for the index + */ + public getTypeDef (lookupId: SiLookupTypeId | string | number): TypeDef { + const index = this.#getSiIndex(lookupId); + + if (!this.#typeDefs[index]) { + // since we may have recursive lookups, fill in empty details as a start + this.#typeDefs[index] = { info: TypeDefInfo.DoNotConstruct, type: this.createSiString(index) }; + + const siType = this.getSiType(lookupId); + + Object.entries(this.#extract(siType, lookupId)).forEach(([k, v]): void => { + /* eslint-disable */ + // @ts-ignore Yes... these are crappy, however we do go through key/values, so all safe + this.#typeDefs[index][k] = v; + /* eslint-enable */ + }); + } + + return this.#typeDefs[index]; + } + + #getSiIndex (lookupId: SiLookupTypeId | string | number): number { + if (isString(lookupId)) { + assert(this.isSiString(lookupId), () => `PortableRegistry: Expected a lookup string type, found ${lookupId}`); + + return parseInt(lookupId.replace(LOOKUP_PREFIX, ''), 10); + } else if (isNumber(lookupId)) { + return lookupId; + } + + return lookupId.toNumber(); + } + + #extract (type: SiType, lookupId: SiLookupTypeId | string | number): TypeDef { + const path = [...type.path]; + + // We handle ink! here as well, although we still have a different registry there + const isPrimitivePath = !!path.length && ( + ( + path.length > 2 && + path[0].eq('ink_env') && + path[1].eq('types') + ) || + INK_PRIMITIVE_ALWAYS.includes(path[path.length - 1].toString()) + ); + let typeDef: Omit; + + if (isPrimitivePath) { + typeDef = this.#extractPrimitivePath(type); + } else if (type.def.isArray) { + typeDef = this.#extractArray(type.def.asArray); + } else if (type.def.isCompact) { + typeDef = this.#extractCompact(type.def.asCompact); + } else if (type.def.isComposite) { + typeDef = this.#extractComposite(type.def.asComposite); + } else if (type.def.isHistoricMetaCompat) { + typeDef = getTypeDef(type.def.asHistoricMetaCompat); + } else if (type.def.isPrimitive) { + typeDef = this.#extractPrimitive(type); + } else if (type.def.isSequence) { + typeDef = this.#extractSequence(type.def.asSequence, lookupId); + } else if (type.def.isTuple) { + typeDef = this.#extractTuple(type.def.asTuple); + } else if (type.def.isVariant) { + typeDef = this.#extractVariant(type.def.asVariant, lookupId); + } else { + throw new Error(`PortableRegistry: Invalid type at index ${lookupId.toString()}: No handler for ${type.def.toString()}`); + } + + const displayName = path.pop()?.toString(); + + return withTypeString({ + ...(displayName + ? { displayName } + : {} + ), + ...(path.length > 1 + ? { namespace: path.map((s) => s.toString()).join('::') } + : {} + ), + ...(type.params.length > 0 + ? { sub: type.params.filter(({ type }) => type.isSome).map(({ type }) => this.getTypeDef(type.unwrap())) } + : {} + ), + ...typeDef + }); + } + + #extractArray ({ len: length, type }: SiTypeDefArray): Omit { + assert(!length || length.toNumber() <= 256, 'PortableRegistry: Only support for [Type; ], where length <= 256'); + + return { + info: TypeDefInfo.VecFixed, + length: length.toNumber(), + sub: this.getTypeDef(type) + }; + } + + #extractCompact ({ type }: SiTypeDefCompact): Omit { + return { + info: TypeDefInfo.Compact, + sub: this.getTypeDef(type) + }; + } + + #extractComposite ({ fields }: SiTypeDefComposite): Omit { + return this.#extractFields(fields); + } + + #extractFields (fields: SiField[]): Omit { + const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([ + isAllNamed && name.isSome, + isAllUnnamed && name.isNone + ]), + [true, true]); + + assert(isTuple || isStruct, 'PortableRegistry: Invalid fields type detected, expected either Tuple or Struct'); + + const sub = fields.map(({ name, type }) => ({ + ...this.getTypeDef(type), + ...(name.isSome + ? { name: stringCamelCase(name.unwrap()) } + : {} + ) + })); + + return isTuple && sub.length === 1 + ? sub[0] + : { + // check for tuple first (no fields may be available) + info: isTuple + ? TypeDefInfo.Tuple + : TypeDefInfo.Struct, + sub + }; + } + + #extractPrimitive (type: SiType): TypeDef { + const typeStr = type.def.asPrimitive.type.toString(); + + return { + info: TypeDefInfo.Plain, + type: PRIMITIVE_ALIAS[typeStr] || typeStr.toLowerCase() + }; + } + + #extractPrimitivePath (type: SiType): TypeDef { + return { + info: TypeDefInfo.Plain, + type: type.path[type.path.length - 1].toString() + }; + } + + #extractSequence ({ type }: SiTypeDefSequence, lookupId: SiLookupTypeId | string | number): Omit { + assert(!!type, () => `ContractRegistry: Invalid sequence type found at id ${lookupId.toString()}`); + + return { + info: TypeDefInfo.Vec, + sub: this.getTypeDef(type) + }; + } + + #extractTuple (ids: SiTypeDefTuple): Omit { + return ids.length === 1 + ? this.getTypeDef(ids[0]) + : { + info: TypeDefInfo.Tuple, + sub: ids.map((type) => this.getTypeDef(type)) + }; + } + + #extractVariant ({ variants }: SiTypeDefVariant, lookupId: SiLookupTypeId | string | number): Omit { + const { params, path } = this.getSiType(lookupId); + const specialVariant = path[0].toString(); + + return specialVariant === 'Option' + ? { + info: TypeDefInfo.Option, + sub: this.getTypeDef(params[0].type.unwrap()) + } + : specialVariant === 'Result' + ? { + info: TypeDefInfo.Result, + sub: params.map((p, index) => ({ + name: ['Ok', 'Error'][index], + ...this.getTypeDef(p.type.unwrap()) + })) + } + : { + info: TypeDefInfo.Enum, + sub: this.#extractVariantSub(variants) + }; + } + + #extractVariantSub (variants: SiVariant[]): TypeDef[] { + return variants.every(({ fields }) => fields.length === 0) + ? variants.map(({ index, name }) => ({ + index: index.toNumber(), + info: TypeDefInfo.Plain, + name: name.toString(), + type: 'Null' + })) + : variants.map(({ fields, index, name }) => withTypeString({ + ...this.#extractFields(fields), + index: index.toNumber(), + name: name.toString() + })); + } +} diff --git a/packages/types/src/generic/index.ts b/packages/types/src/generic/index.ts index fa59063e8c..d2e766d3bc 100644 --- a/packages/types/src/generic/index.ts +++ b/packages/types/src/generic/index.ts @@ -12,4 +12,5 @@ export { GenericConsensusEngineId } from './ConsensusEngineId'; export { GenericEvent, GenericEventData } from './Event'; export { GenericLookupSource } from './LookupSource'; export { GenericMultiAddress, GenericMultiAddress as GenericAddress } from './MultiAddress'; +export { GenericPortableRegistry } from './PortableRegistry'; export { GenericVote } from './Vote'; diff --git a/packages/types/src/interfaces/metadata/types.ts b/packages/types/src/interfaces/metadata/types.ts index 8df94612e6..27271b4221 100644 --- a/packages/types/src/interfaces/metadata/types.ts +++ b/packages/types/src/interfaces/metadata/types.ts @@ -1,7 +1,7 @@ // Auto-generated via `yarn polkadot-types-from-defs`, do not edit /* eslint-disable */ -import type { Bytes, Enum, Option, Struct, Text, Type, Vec, bool, u8 } from '@polkadot/types'; +import type { Bytes, Enum, GenericPortableRegistry, Option, Struct, Text, Type, Vec, bool, u8 } from '@polkadot/types'; import type { SiLookupTypeId, SiType } from '@polkadot/types/interfaces/scaleInfo'; /** @name ErrorMetadataLatest */ @@ -303,9 +303,7 @@ export interface PalletStorageMetadataV14 extends Struct { } /** @name PortableRegistry */ -export interface PortableRegistry extends Struct { - readonly types: Vec; -} +export interface PortableRegistry extends GenericPortableRegistry {} /** @name PortableType */ export interface PortableType extends Struct { diff --git a/packages/types/src/interfaces/metadata/v14.ts b/packages/types/src/interfaces/metadata/v14.ts index b811725541..0ae9be4e73 100644 --- a/packages/types/src/interfaces/metadata/v14.ts +++ b/packages/types/src/interfaces/metadata/v14.ts @@ -8,9 +8,7 @@ import type { DefinitionsTypes } from '../../types'; export const v14: DefinitionsTypes = { // registry - PortableRegistry: { - types: 'Vec' - }, + PortableRegistry: 'GenericPortableRegistry', PortableType: { id: 'SiLookupTypeId', type: 'SiType' diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 2acdd46fd6..42fb83c7c0 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -4,6 +4,7 @@ import type { Observable } from 'rxjs'; import type { BN } from '@polkadot/util'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; +import type { MetadataLatest, PortableRegistry } from '../interfaces/metadata'; import type { CodecHash, Hash } from '../interfaces/runtime'; import type { ChainProperties } from '../interfaces/system'; import type { Metadata } from '../metadata'; @@ -100,6 +101,8 @@ export interface Registry { readonly chainSS58: number | undefined; readonly chainTokens: string[]; readonly knownTypes: RegisteredTypes; + readonly lookup: PortableRegistry; + readonly metadata: MetadataLatest; readonly unknownTypes: string[]; readonly signedExtensions: string[];