From 1e472eebc952fe7c9c5246a507334be2b042c737 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 16 Dec 2021 14:22:51 +0100 Subject: [PATCH] Expose registry: Registry on Call/Extrinstic/Metadata (#4339) * Expose registry: Registry on Call/Extrinstic/Metadata * Adjust * fix Call * Adjust * Fix it? --- packages/types-codec/src/base/Base.ts | 8 ++++-- packages/types-codec/src/native/Struct.ts | 8 ++++-- packages/types-codec/src/types/interfaces.ts | 3 ++- packages/types-codec/src/types/registry.ts | 26 +++++++++++++++++-- packages/types/src/extrinsic/Extrinsic.ts | 6 ++++- packages/types/src/extrinsic/v4/Extrinsic.ts | 6 ++++- .../src/extrinsic/v4/ExtrinsicSignature.ts | 6 ++++- packages/types/src/generic/Call.ts | 10 ++++--- .../types/src/metadata/MetadataVersioned.ts | 5 ++++ packages/types/src/types/calls.ts | 16 +++++------- packages/types/src/types/extrinsic.ts | 9 +++++-- packages/types/src/types/interfaces.ts | 5 ++-- packages/types/src/types/registry.ts | 6 ++--- 13 files changed, 85 insertions(+), 29 deletions(-) diff --git a/packages/types-codec/src/base/Base.ts b/packages/types-codec/src/base/Base.ts index e5eca14d89..5a3a822a8f 100644 --- a/packages/types-codec/src/base/Base.ts +++ b/packages/types-codec/src/base/Base.ts @@ -9,7 +9,7 @@ import type { AnyJson, BareOpts, Codec, CodecRegistry, IU8a } from '../types'; * @description A type extends the Base class, when it holds a value */ export abstract class Base implements Codec { - public readonly registry: CodecRegistry; + readonly #registry: CodecRegistry; public createdAtHash?: IU8a; @@ -18,7 +18,7 @@ export abstract class Base implements Codec { readonly #raw: T; protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) { - this.registry = registry; + this.#registry = registry; this.#raw = value; this.initialU8aLength = initialU8aLength; } @@ -48,6 +48,10 @@ export abstract class Base implements Codec { return this.#raw.isEmpty; } + public get registry (): CodecRegistry { + return this.#registry; + } + /** * @description Compares the value of the input to see if there is a match */ diff --git a/packages/types-codec/src/native/Struct.ts b/packages/types-codec/src/native/Struct.ts index 5dae933937..778cea1a83 100644 --- a/packages/types-codec/src/native/Struct.ts +++ b/packages/types-codec/src/native/Struct.ts @@ -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 { - readonly registry: CodecRegistry; + #registry: CodecRegistry; public createdAtHash?: IU8a; @@ -112,7 +112,7 @@ export class Struct< super(decoded); - this.registry = registry; + this.#registry = registry; this.initialU8aLength = decodedLength; this.#jsonMap = jsonMap; this.#Types = typeMap; @@ -190,6 +190,10 @@ export class Struct< return this.registry.hash(this.toU8a()); } + public get registry (): CodecRegistry { + return this.#registry; + } + /** * @description Compares the value of the input to see if there is a match */ diff --git a/packages/types-codec/src/types/interfaces.ts b/packages/types-codec/src/types/interfaces.ts index 086a0e72d6..3536d3947f 100644 --- a/packages/types-codec/src/types/interfaces.ts +++ b/packages/types-codec/src/types/interfaces.ts @@ -77,11 +77,12 @@ export interface IVec extends Array, Codec { toArray (): T[]; } -export interface IMethod extends Codec { +export interface IMethod extends Codec { readonly args: A; readonly argsDef: ArgsDef; readonly callIndex: Uint8Array; readonly data: Uint8Array; + readonly meta: M; is: (tx: IMethod) => tx is IMethod; } diff --git a/packages/types-codec/src/types/registry.ts b/packages/types-codec/src/types/registry.ts index bf61e58925..2817f6e6e2 100644 --- a/packages/types-codec/src/types/registry.ts +++ b/packages/types-codec/src/types/registry.ts @@ -3,6 +3,7 @@ import type { BN } from '@polkadot/util'; import type { Codec, CodecClass } from './codec'; +import type { AnyTuple } from './helpers'; import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces'; export type OnlyCodec = @@ -14,6 +15,16 @@ export type OnlyCodec = ? T : never; +export interface RegistryError { + args: string[]; + docs: string[]; + index: number; + // compat + method: string; + name: string; + section: string; +} + export type RegistryTypes = Record | { _enum: string[] | Record | Record } | @@ -25,6 +36,17 @@ export interface CodecCreateOptions { isPedantic?: boolean; } +export interface CallBase extends IMethod { + readonly method: string; + readonly section: string; + + toJSON: () => any; +} + +export interface CallFunction extends CallBase { + (...args: any[]): IMethod; +} + export interface CodecRegistry { readonly chainDecimals: number[]; readonly chainSS58: number | undefined; @@ -37,8 +59,8 @@ export interface CodecRegistry { createdAtHash?: IU8a; - findMetaCall (callIndex: Uint8Array): unknown; - findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): unknown; + findMetaCall (callIndex: Uint8Array): CallFunction; + findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError; findMetaEvent (eventIndex: Uint8Array): CodecClass; isLookupType (value: string): boolean; diff --git a/packages/types/src/extrinsic/Extrinsic.ts b/packages/types/src/extrinsic/Extrinsic.ts index d5999376aa..d10e3ce9c6 100644 --- a/packages/types/src/extrinsic/Extrinsic.ts +++ b/packages/types/src/extrinsic/Extrinsic.ts @@ -7,7 +7,7 @@ import type { HexString } from '@polkadot/util/types'; import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics'; import type { FunctionMetadataLatest } from '../interfaces/metadata'; import type { Address, Balance, Call, CodecHash, Index } from '../interfaces/runtime'; -import type { CallBase, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, SignatureOptions } from '../types'; +import type { CallBase, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, Registry, SignatureOptions } from '../types'; import type { GenericExtrinsicEra } from './ExtrinsicEra'; import type { ExtrinsicValueV4 } from './v4/Extrinsic'; @@ -168,6 +168,10 @@ abstract class ExtrinsicBase extends Base extends Struct implement * @description Returns the name of the method */ public get method (): string { - return (this.registry as Registry).findMetaCall(this.callIndex).method; + return this.registry.findMetaCall(this.callIndex).method; + } + + public override get registry (): Registry { + return super.registry as Registry; } /** * @description Returns the module containing the method */ public get section (): string { - return (this.registry as Registry).findMetaCall(this.callIndex).section; + return this.registry.findMetaCall(this.callIndex).section; } /** @@ -210,7 +214,7 @@ export class GenericCall extends Struct implement let call: CallFunction | undefined; try { - call = (this.registry as Registry).findMetaCall(this.callIndex); + call = this.registry.findMetaCall(this.callIndex); } catch (error) { // swallow } diff --git a/packages/types/src/metadata/MetadataVersioned.ts b/packages/types/src/metadata/MetadataVersioned.ts index 1c97a56af7..66145af63e 100644 --- a/packages/types/src/metadata/MetadataVersioned.ts +++ b/packages/types/src/metadata/MetadataVersioned.ts @@ -3,6 +3,7 @@ import type { AnyJson, CodecRegistry } from '@polkadot/types-codec/types'; import type { MetadataAll, MetadataLatest, MetadataV9, MetadataV10, MetadataV11, MetadataV12, MetadataV13, MetadataV14 } from '../interfaces/metadata'; +import type { Registry } from '../types'; import { Struct } from '@polkadot/types-codec'; import { assert } from '@polkadot/util'; @@ -71,6 +72,10 @@ export class MetadataVersioned extends Struct { return this.getT('metadata'); }; + public override get registry (): Registry { + return super.registry as Registry; + } + /** * @description Returns the wrapped metadata as a limited calls-only (latest) version */ diff --git a/packages/types/src/types/calls.ts b/packages/types/src/types/calls.ts index 3631fd16a9..e98491eb9a 100644 --- a/packages/types/src/types/calls.ts +++ b/packages/types/src/types/calls.ts @@ -1,17 +1,15 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AnyTuple } from '@polkadot/types-codec/types'; +import type { AnyTuple, CallBase as CallBaseBase, CallFunction as CallFunctionBase, IMethod } from '@polkadot/types-codec/types'; +import type { FunctionMetadataLatest } from '../interfaces/metadata'; import type { Call } from '../interfaces/runtime'; -import type { IMethod } from './interfaces'; +import type { Registry } from './registry'; -export interface CallBase extends IMethod { - readonly method: string; - readonly section: string; - - toJSON: () => any; +export interface CallBase extends CallBaseBase { + registry: Registry; } -export interface CallFunction extends CallBase { - (...args: any[]): Call & IMethod; +export interface CallFunction extends CallFunctionBase { + (...args: any[]): Call & IMethod; } diff --git a/packages/types/src/types/extrinsic.ts b/packages/types/src/types/extrinsic.ts index ff2d4cdf5a..8f99e656a1 100644 --- a/packages/types/src/types/extrinsic.ts +++ b/packages/types/src/types/extrinsic.ts @@ -8,6 +8,7 @@ import type { EcdsaSignature, Ed25519Signature, Sr25519Signature } from '../inte import type { Address, Balance, Call, H256, Index } from '../interfaces/runtime'; import type { DispatchError, DispatchInfo, EventRecord } from '../interfaces/system'; import type { ICompact, IKeyringPair, IMethod, IRuntimeVersion } from './interfaces'; +import type { Registry } from './registry'; export interface ISubmittableResult { readonly dispatchError?: DispatchError; @@ -188,15 +189,19 @@ export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec { addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: Uint8Array | HexString): IExtrinsicSignature; sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature; signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature; + + readonly registry: Registry; } -interface IExtrinsicSignable { +interface IExtrinsicSignable extends Codec { addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): T; sign (account: IKeyringPair, options: SignatureOptions): T; signFake (address: Address | Uint8Array | string, options: SignatureOptions): T; + + readonly registry: Registry; } -export interface IExtrinsicImpl extends IExtrinsicSignable, Codec { +export interface IExtrinsicImpl extends IExtrinsicSignable { readonly method: Call; readonly signature: IExtrinsicSignature; readonly version: number; diff --git a/packages/types/src/types/interfaces.ts b/packages/types/src/types/interfaces.ts index 18e2df7449..0d9909276e 100644 --- a/packages/types/src/types/interfaces.ts +++ b/packages/types/src/types/interfaces.ts @@ -5,11 +5,12 @@ import type { SignOptions } from '@polkadot/keyring/types'; import type { AnyTuple, IMethod as IMethodBase } from '@polkadot/types-codec/types'; import type { BN } from '@polkadot/util'; import type { FunctionMetadataLatest, StorageEntryMetadataLatest } from '../interfaces/metadata'; +import type { Registry } from './registry'; export type { ICompact, IEnum, IMap, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from '@polkadot/types-codec/types'; -export interface IMethod extends IMethodBase { - readonly meta: FunctionMetadataLatest; +export interface IMethod extends IMethodBase { + readonly registry: Registry; } export interface IKeyringPair { diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 481171fec9..9b8cd631d7 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { RegistryTypes } from '@polkadot/types-codec/types'; +import type { RegistryError as RegistryErrorBase, RegistryTypes } from '@polkadot/types-codec/types'; import type { CreateRegistry, TypeDef } from '@polkadot/types-create/types'; import type { BN } from '@polkadot/util'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; @@ -30,7 +30,7 @@ export interface ChainUpgrades { versions: ChainUpgradeVersion[]; } -export interface RegistryError { +export interface RegistryError extends RegistryErrorBase { args: string[]; docs: string[]; fields: SiField[]; @@ -113,7 +113,7 @@ export interface Registry extends CreateRegistry { 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; + // findMetaEvent (eventIndex: Uint8Array): CodecClass; // isLookupType (value: string): boolean; createLookupType (lookupId: SiLookupTypeId | number): string;