Add API option for chain-specific signedExtensions (#3033)
* Add API option for chain-specific signedExtensions * typo
This commit is contained in:
@@ -89,7 +89,7 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
registry.knownTypes.typesAlias = getSpecAlias(registry, chain, version.specName);
|
||||
}
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
registry.setMetadata(metadata, undefined, this._options.signedExtensions);
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import type { DeriveCustom, ExactDerive } from '@polkadot/api-derive';
|
||||
import type { Metadata } from '@polkadot/metadata';
|
||||
import type { RpcInterface } from '@polkadot/rpc-core/types';
|
||||
import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
|
||||
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
|
||||
import type { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
import type { DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, SignatureOptions, Signer } from '@polkadot/types/types';
|
||||
import type { ApiBase } from '../base';
|
||||
@@ -56,6 +57,10 @@ export interface ApiOptions extends RegisteredTypes {
|
||||
* @description User-defined RPC methods
|
||||
*/
|
||||
rpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
|
||||
/**
|
||||
* @description Any chain-specific signed extensions that are now well-known
|
||||
*/
|
||||
signedExtensions?: Record<string, ExtDef>;
|
||||
/**
|
||||
* @description An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
|
||||
import type { ExtDef } from '../extrinsic/signedExtensions/types';
|
||||
import type { ChainProperties, DispatchErrorModule, H256 } from '../interfaces/types';
|
||||
import type { CallFunction, Codec, Constructor, InterfaceTypes, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
|
||||
|
||||
@@ -134,6 +135,8 @@ export class TypeRegistry implements Registry {
|
||||
|
||||
#signedExtensions: string[] = defaultExtensions;
|
||||
|
||||
#userExtensions?: Record<string, ExtDef>;
|
||||
|
||||
constructor () {
|
||||
this.#knownDefaults = { Json, Metadata, Raw, ...baseTypes };
|
||||
this.#knownDefinitions = definitions as unknown as Record<string, { types: RegistryTypes }>;
|
||||
@@ -278,11 +281,11 @@ export class TypeRegistry implements Registry {
|
||||
}
|
||||
|
||||
public getSignedExtensionExtra (): Record<string, keyof InterfaceTypes> {
|
||||
return expandExtensionTypes(this.#signedExtensions, 'extra');
|
||||
return expandExtensionTypes(this.#signedExtensions, 'payload', this.#userExtensions);
|
||||
}
|
||||
|
||||
public getSignedExtensionTypes (): Record<string, keyof InterfaceTypes> {
|
||||
return expandExtensionTypes(this.#signedExtensions, 'types');
|
||||
return expandExtensionTypes(this.#signedExtensions, 'extrinsic', this.#userExtensions);
|
||||
}
|
||||
|
||||
public hasClass (name: string): boolean {
|
||||
@@ -356,7 +359,7 @@ export class TypeRegistry implements Registry {
|
||||
}
|
||||
|
||||
// sets the metadata
|
||||
public setMetadata (metadata: Metadata, signedExtensions?: string[]): void {
|
||||
public setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: Record<string, ExtDef>): void {
|
||||
injectExtrinsics(this, metadata, this.#metadataCalls);
|
||||
injectErrors(this, metadata, this.#metadataErrors);
|
||||
injectEvents(this, metadata, this.#metadataEvents);
|
||||
@@ -367,7 +370,8 @@ export class TypeRegistry implements Registry {
|
||||
metadata.asLatest.extrinsic.version.gt(BN_ZERO)
|
||||
? metadata.asLatest.extrinsic.signedExtensions.map((key) => key.toString())
|
||||
: defaultExtensions
|
||||
)
|
||||
),
|
||||
userExtensions
|
||||
);
|
||||
|
||||
// setup the chain properties with format overrides
|
||||
@@ -377,10 +381,11 @@ export class TypeRegistry implements Registry {
|
||||
}
|
||||
|
||||
// sets the available signed extensions
|
||||
setSignedExtensions (signedExtensions: string[] = defaultExtensions): void {
|
||||
setSignedExtensions (signedExtensions: string[] = defaultExtensions, userExtensions?: Record<string, ExtDef>): void {
|
||||
this.#signedExtensions = signedExtensions;
|
||||
this.#userExtensions = userExtensions;
|
||||
|
||||
const unknown = findUnknownExtensions(this.#signedExtensions);
|
||||
const unknown = findUnknownExtensions(this.#signedExtensions, this.#userExtensions);
|
||||
|
||||
if (unknown.length) {
|
||||
l.warn(`Unknown signed extensions ${unknown.join(', ')} found, treating them as no-effect`);
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const emptyCheck = {
|
||||
extra: {},
|
||||
types: {}
|
||||
extrinsic: {},
|
||||
payload: {}
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ import substrateExtensions from './substrate';
|
||||
// A mapping of the known signed extensions to the extra fields that they contain. Unlike in the actual extensions,
|
||||
// we define the extra fields not as a Tuple, but rather as a struct so they can be named. These will be expanded
|
||||
// into the various fields when added to the payload (we only support V4 onwards with these, V3 and earlier are
|
||||
// deemded fixed and non-changeable)
|
||||
// deemed fixed and non-changeable)
|
||||
const allExtensions: ExtDef = {
|
||||
...substrateExtensions,
|
||||
...polkadotExtensions
|
||||
@@ -26,16 +26,16 @@ const defaultExtensions: Array<keyof typeof allExtensions> = [
|
||||
'CheckBlockGasLimit'
|
||||
];
|
||||
|
||||
function findUnknownExtensions (extensions: string[]): string[] {
|
||||
const names = Object.keys(allExtensions);
|
||||
function findUnknownExtensions (extensions: string[], userExtensions: Record<string, ExtDef> = {}): string[] {
|
||||
const names = [...Object.keys(allExtensions), ...Object.keys(userExtensions)];
|
||||
|
||||
return extensions.filter((key): boolean => !names.includes(key));
|
||||
return extensions.filter((key) => !names.includes(key));
|
||||
}
|
||||
|
||||
function expandExtensionTypes (extensions: string[], type: keyof ExtInfo): ExtTypes {
|
||||
function expandExtensionTypes (extensions: string[], type: keyof ExtInfo, userExtensions: Record<string, ExtDef> = {}): ExtTypes {
|
||||
return extensions
|
||||
.map((key): ExtInfo => allExtensions[key])
|
||||
.filter((info): boolean => !!info)
|
||||
.map((key) => allExtensions[key] || userExtensions[key])
|
||||
.filter((info): info is ExtInfo => !!info)
|
||||
.reduce((result, info): ExtTypes => ({ ...result, ...info[type] }), {});
|
||||
}
|
||||
|
||||
|
||||
@@ -6,53 +6,53 @@ import type { ExtDef } from './types';
|
||||
import { emptyCheck } from './emptyCheck';
|
||||
|
||||
const CheckMortality = {
|
||||
extra: {
|
||||
blockHash: 'Hash'
|
||||
},
|
||||
types: {
|
||||
extrinsic: {
|
||||
era: 'ExtrinsicEra'
|
||||
},
|
||||
payload: {
|
||||
blockHash: 'Hash'
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
ChargeTransactionPayment: {
|
||||
extra: {},
|
||||
types: {
|
||||
extrinsic: {
|
||||
tip: 'Compact<Balance>'
|
||||
}
|
||||
},
|
||||
payload: {}
|
||||
},
|
||||
CheckBlockGasLimit: emptyCheck,
|
||||
CheckEra: CheckMortality,
|
||||
CheckGenesis: {
|
||||
extra: {
|
||||
extrinsic: {},
|
||||
payload: {
|
||||
genesisHash: 'Hash'
|
||||
},
|
||||
types: {}
|
||||
}
|
||||
},
|
||||
CheckMortality,
|
||||
CheckNonce: {
|
||||
extra: {},
|
||||
types: {
|
||||
extrinsic: {
|
||||
nonce: 'Compact<Index>'
|
||||
}
|
||||
},
|
||||
payload: {}
|
||||
},
|
||||
CheckSpecVersion: {
|
||||
extra: {
|
||||
extrinsic: {},
|
||||
payload: {
|
||||
specVersion: 'u32'
|
||||
},
|
||||
types: {}
|
||||
}
|
||||
},
|
||||
CheckTxVersion: {
|
||||
extra: {
|
||||
extrinsic: {},
|
||||
payload: {
|
||||
transactionVersion: 'u32'
|
||||
},
|
||||
types: {}
|
||||
}
|
||||
},
|
||||
CheckVersion: {
|
||||
extra: {
|
||||
extrinsic: {},
|
||||
payload: {
|
||||
specVersion: 'u32'
|
||||
},
|
||||
types: {}
|
||||
}
|
||||
},
|
||||
CheckWeight: emptyCheck,
|
||||
LockStakingStatus: emptyCheck,
|
||||
|
||||
@@ -6,8 +6,8 @@ import type { InterfaceTypes } from '../../types';
|
||||
export type ExtTypes = Record<string, keyof InterfaceTypes>;
|
||||
|
||||
export type ExtInfo = {
|
||||
extra: ExtTypes;
|
||||
types: ExtTypes;
|
||||
extrinsic: ExtTypes;
|
||||
payload: ExtTypes;
|
||||
}
|
||||
|
||||
export type ExtDef = Record<string, ExtInfo>;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import type BN from 'bn.js';
|
||||
import type { Metadata } from '@polkadot/metadata';
|
||||
import type { Observable } from '@polkadot/x-rxjs';
|
||||
import type { ExtDef } from '../extrinsic/signedExtensions/types';
|
||||
import type { H256 } from '../interfaces/runtime';
|
||||
import type { ChainProperties } from '../interfaces/system';
|
||||
import type { CallFunction } from './calls';
|
||||
@@ -113,6 +114,6 @@ export interface Registry {
|
||||
register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void;
|
||||
setChainProperties (properties?: ChainProperties): void;
|
||||
setHasher (hasher?: (data: Uint8Array) => Uint8Array): void;
|
||||
setMetadata (metadata: Metadata, signedExtensions?: string[]): void;
|
||||
setSignedExtensions (signedExtensions?: string[]): void;
|
||||
setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: Record<string, ExtDef>): void;
|
||||
setSignedExtensions (signedExtensions?: string[], userExtensions?: Record<string, ExtDef>): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user