Allow extensions on typesBundle (#3142)

* Allow extensions on typesBundle

* User extension take precedence
This commit is contained in:
Jaco Greeff
2021-02-06 08:49:11 +01:00
committed by GitHub
parent f643426067
commit 1e87f58f36
7 changed files with 33 additions and 15 deletions
+5 -2
View File
@@ -13,7 +13,7 @@ import BN from 'bn.js';
import { Metadata } from '@polkadot/metadata';
import { TypeRegistry } from '@polkadot/types/create';
import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/Extrinsic';
import { getSpecAlias, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
import { getSpecAlias, getSpecExtensions, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
import { assert, BN_ZERO, logger, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { of } from '@polkadot/x-rxjs';
@@ -91,7 +91,10 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
registry.knownTypes.typesAlias = getSpecAlias(registry, chain, version.specName);
}
registry.setMetadata(metadata, undefined, this._options.signedExtensions);
registry.setMetadata(metadata, undefined, {
...getSpecExtensions(registry, chain, version.specName),
...(this._options.signedExtensions || {})
});
return registry;
}
+14
View File
@@ -3,6 +3,7 @@
import type BN from 'bn.js';
import type { Text } from '@polkadot/types';
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { ChainUpgradeVersion, DefinitionRpc, DefinitionRpcSub, OverrideModuleType, OverrideVersionedType, Registry, RegistryTypes } from '@polkadot/types/types';
@@ -37,6 +38,19 @@ export function getModuleTypes ({ knownTypes }: Registry, section: string): Over
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable signed extensions (ready for registration)
*/
export function getSpecExtensions ({ knownTypes }: Registry, chainName: Text | string, specName: Text | string): ExtDef {
const _chainName = chainName.toString();
const _specName = specName.toString();
return {
...(knownTypes.typesBundle?.spec?.[_specName]?.signedExtensions || {}),
...(knownTypes.typesBundle?.chain?.[_chainName]?.signedExtensions || {})
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable types (ready for registration)
*/
@@ -1,7 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const emptyCheck = {
import type { ExtInfo } from './types';
export const emptyCheck: ExtInfo = {
extrinsic: {},
payload: {}
};
@@ -3,17 +3,14 @@
import type { ExtDef, ExtInfo, ExtTypes } from './types';
import polkadotExtensions from './polkadot';
import substrateExtensions from './substrate';
import { polkadot } from './polkadot';
import { substrate } 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
// deemed fixed and non-changeable)
const allExtensions: ExtDef = {
...substrateExtensions,
...polkadotExtensions
};
const allExtensions: ExtDef = { ...substrate, ...polkadot };
// the v4 signed extensions (the order is important here, as applied by default)
const defaultExtensions: Array<keyof typeof allExtensions> = [
@@ -34,6 +31,7 @@ function findUnknownExtensions (extensions: string[], userExtensions: ExtDef = {
function expandExtensionTypes (extensions: string[], type: keyof ExtInfo, userExtensions: ExtDef = {}): ExtTypes {
return extensions
// Always allow user extensions first - these should provide overrides
.map((key) => userExtensions[key] || allExtensions[key])
.filter((info): info is ExtInfo => !!info)
.reduce((result, info): ExtTypes => ({ ...result, ...info[type] }), {});
@@ -5,11 +5,11 @@ import type { ExtDef } from './types';
import { emptyCheck } from './emptyCheck';
export default {
export const polkadot: ExtDef = {
LimitParathreadCommits: emptyCheck,
OnlyStakingAndClaims: emptyCheck,
PrevalidateAttests: emptyCheck,
RestrictFunctionality: emptyCheck,
TransactionCallFilter: emptyCheck,
ValidateDoubleVoteReports: emptyCheck
} as ExtDef;
};
@@ -1,11 +1,11 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ExtDef } from './types';
import type { ExtDef, ExtInfo } from './types';
import { emptyCheck } from './emptyCheck';
const CheckMortality = {
const CheckMortality: ExtInfo = {
extrinsic: {
era: 'ExtrinsicEra'
},
@@ -14,7 +14,7 @@ const CheckMortality = {
}
};
export default {
export const substrate: ExtDef = {
ChargeTransactionPayment: {
extrinsic: {
tip: 'Compact<Balance>'
@@ -57,4 +57,4 @@ export default {
CheckWeight: emptyCheck,
LockStakingStatus: emptyCheck,
ValidateEquivocationReport: emptyCheck
} as ExtDef;
};
+1
View File
@@ -49,6 +49,7 @@ export interface OverrideBundleDefinition {
derives?: DeriveCustom;
instances?: Record<string, string[]>;
rpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
signedExtensions?: ExtDef;
types?: OverrideVersionedType[];
}