Expose registry: Registry on Call/Extrinstic/Metadata (#4339)

* Expose registry: Registry on Call/Extrinstic/Metadata

* Adjust

* fix Call

* Adjust

* Fix it?
This commit is contained in:
Jaco
2021-12-16 15:22:51 +02:00
committed by GitHub
parent 30db589eee
commit 1e472eebc9
13 changed files with 85 additions and 29 deletions
+6 -2
View File
@@ -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<T extends Codec> implements Codec {
public readonly registry: CodecRegistry;
readonly #registry: CodecRegistry;
public createdAtHash?: IU8a;
@@ -18,7 +18,7 @@ export abstract class Base<T extends Codec> 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<T extends Codec> 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
*/
+6 -2
View File
@@ -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<keyof S, Codec> implements IStruct<keyof S> {
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
*/
+2 -1
View File
@@ -77,11 +77,12 @@ export interface IVec<T extends Codec = Codec> extends Array<T>, Codec {
toArray (): T[];
}
export interface IMethod<A extends AnyTuple = AnyTuple> extends Codec {
export interface IMethod<A extends AnyTuple = AnyTuple, M = any> extends Codec {
readonly args: A;
readonly argsDef: ArgsDef;
readonly callIndex: Uint8Array;
readonly data: Uint8Array;
readonly meta: M;
is: (tx: IMethod<AnyTuple>) => tx is IMethod<A>;
}
+24 -2
View File
@@ -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<K, T, Types> =
@@ -14,6 +15,16 @@ export type OnlyCodec<K, T, Types> =
? T
: never;
export interface RegistryError {
args: string[];
docs: string[];
index: number;
// compat
method: string;
name: string;
section: string;
}
export type RegistryTypes =
Record<string, CodecClass | string | Record<string, string> |
{ _enum: string[] | Record<string, number> | Record<string, string | null> } |
@@ -25,6 +36,17 @@ export interface CodecCreateOptions {
isPedantic?: boolean;
}
export interface CallBase<A extends AnyTuple, M = any> extends IMethod<A, M> {
readonly method: string;
readonly section: string;
toJSON: () => any;
}
export interface CallFunction<A extends AnyTuple = AnyTuple, M = any> extends CallBase<A, M> {
(...args: any[]): IMethod<A, M>;
}
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<any>;
isLookupType (value: string): boolean;
+5 -1
View File
@@ -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<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
return this.inner.signature.nonce;
}
public override get registry (): Registry {
return super.registry as Registry;
}
/**
* @description The actual [[EcdsaSignature]], [[Ed25519Signature]] or [[Sr25519Signature]]
*/
+5 -1
View File
@@ -5,7 +5,7 @@ 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';
import type { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions } from '../../types';
import type { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, Registry, SignatureOptions } from '../../types';
import type { ExtrinsicOptions } from '../types';
import { Struct } from '@polkadot/types-codec';
@@ -66,6 +66,10 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
return this.getT('method');
}
public override get registry (): Registry {
return super.registry as Registry;
}
/**
* @description The [[ExtrinsicSignatureV4]]
*/
@@ -5,7 +5,7 @@ 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';
import type { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../types';
import type { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, Registry, SignatureOptions } from '../../types';
import type { ExtrinsicSignatureOptions } from '../types';
import { Compact, Struct } from '@polkadot/types-codec';
@@ -90,6 +90,10 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
return this.getT('nonce');
}
public override get registry (): Registry {
return super.registry as Registry;
}
/**
* @description The actual [[EcdsaSignature]], [[Ed25519Signature]] or [[Sr25519Signature]]
*/
+7 -3
View File
@@ -186,14 +186,18 @@ export class GenericCall<A extends AnyTuple = AnyTuple> 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<A extends AnyTuple = AnyTuple> 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
}
@@ -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
*/
+7 -9
View File
@@ -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<A extends AnyTuple> extends IMethod<A> {
readonly method: string;
readonly section: string;
toJSON: () => any;
export interface CallBase<A extends AnyTuple, M = FunctionMetadataLatest> extends CallBaseBase<A, M> {
registry: Registry;
}
export interface CallFunction<A extends AnyTuple = AnyTuple> extends CallBase<A> {
(...args: any[]): Call & IMethod<A>;
export interface CallFunction<A extends AnyTuple = AnyTuple, M = FunctionMetadataLatest> extends CallFunctionBase<A, M> {
(...args: any[]): Call & IMethod<A, M>;
}
+7 -2
View File
@@ -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<T> {
interface IExtrinsicSignable<T> 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<IExtrinsicImpl>, Codec {
export interface IExtrinsicImpl extends IExtrinsicSignable<IExtrinsicImpl> {
readonly method: Call;
readonly signature: IExtrinsicSignature;
readonly version: number;
+3 -2
View File
@@ -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<A extends AnyTuple = AnyTuple> extends IMethodBase<A> {
readonly meta: FunctionMetadataLatest;
export interface IMethod<A extends AnyTuple = AnyTuple, M = FunctionMetadataLatest> extends IMethodBase<A, M> {
readonly registry: Registry;
}
export interface IKeyringPair {
+3 -3
View File
@@ -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<any>;
// findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
// isLookupType (value: string): boolean;
createLookupType (lookupId: SiLookupTypeId | number): string;