From d1f8f8b5e8084033e1a30d407e74edbf976083b3 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 12 May 2020 18:32:18 +0200 Subject: [PATCH] Extend SignerPayload with extra tx/extensions (#2267) * Extend SignerPayload with extra tx/extensions * Update registry.ts * Update extrinsic.ts * Linting options --- CHANGELOG.md | 1 + packages/api/src/submittable/createClass.ts | 1 + packages/types/src/create/registry.ts | 21 +++++++++++++++---- .../types/src/extrinsic/SignerPayload.spec.ts | 12 ++++++++--- packages/types/src/extrinsic/SignerPayload.ts | 8 ++++++- packages/types/src/types/extrinsic.ts | 13 +++++++++++- packages/types/src/types/registry.ts | 4 +++- 7 files changed, 50 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a147050375..a3867555c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add `derive.democracy.locks` to return extended lock info based on votes - Ensure metadata is only requested once upon startup (bypass on upgrade subscriptions) - Expand testing around `ExtrinsicEra` construction +- Expand `SignerPayload` to supply both the `txVersion` and active `signedExtensions` # 1.13.1 May 6, 2020 diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index e7ed949f4c..0505917c8a 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -148,6 +148,7 @@ export default function createClass ({ api, apiType, ...options, ...extras, runtimeVersion: api.runtimeVersion, + signedExtensions: api.registry.signedExtensions, version: api.extrinsicType } as SignatureOptions; } diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 3c693058a6..c5f6b62cd9 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -135,6 +135,10 @@ export class TypeRegistry implements Registry { return this.#knownTypes; } + public get signedExtensions (): string[] { + return this.#metadataExtensions; + } + /** * @describe Creates an instance of the class */ @@ -306,15 +310,24 @@ export class TypeRegistry implements Registry { } // sets the metadata - public setMetadata (metadata: RegistryMetadata): void { + public setMetadata (metadata: RegistryMetadata, signedExtensions?: string[]): void { decorateExtrinsics(this, metadata, this.#metadataCalls); decorateErrors(this, metadata, this.#metadataErrors); decorateEvents(this, metadata, this.#metadataEvents); // setup the available extensions - this.#metadataExtensions = metadata.asLatest.extrinsic.version.gtn(0) - ? metadata.asLatest.extrinsic.signedExtensions.map((key): string => key.toString()) - : defaultExtensions; + this.setSignedExtensions( + signedExtensions || ( + metadata.asLatest.extrinsic.version.gtn(0) + ? metadata.asLatest.extrinsic.signedExtensions.map((key) => key.toString()) + : defaultExtensions + ) + ); + } + + // sets the available signed extensions + setSignedExtensions (signedExtensions: string[]): void { + this.#metadataExtensions = signedExtensions; const unknown = findUnknownExtensions(this.#metadataExtensions); diff --git a/packages/types/src/extrinsic/SignerPayload.spec.ts b/packages/types/src/extrinsic/SignerPayload.spec.ts index 77bf3b1b01..b12c9d3c88 100644 --- a/packages/types/src/extrinsic/SignerPayload.spec.ts +++ b/packages/types/src/extrinsic/SignerPayload.spec.ts @@ -22,8 +22,10 @@ describe('SignerPayload', (): void => { genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b', method: '0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c', nonce: '0x00001234', - specVersion: '0x00000000', + signedExtensions: ['CheckEra', 'CheckNonce'], + specVersion: '0x00000006', tip: '0x00000000000000000000000000005678', + txVersion: '0x00000007', version: 3 }; @@ -37,7 +39,9 @@ describe('SignerPayload', (): void => { genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b', method: registry.createType('Call', '0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c'), nonce: 0x1234, + signedExtensions: ['CheckEra', 'CheckNonce'], tip: 0x5678, + txVersion: 0x07, version: 3 }).toPayload() ).toEqual({ @@ -48,15 +52,17 @@ describe('SignerPayload', (): void => { genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b', method: '0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c', nonce: '0x00001234', + signedExtensions: ['CheckEra', 'CheckNonce'], specVersion: '0x00000000', tip: '0x00000000000000000000000000005678', + txVersion: '0x00000000', version: 3 }); }); it('re-constructs from JSON', (): void => { expect( - new SignerPayload(registry, TEST).toPayload() + new SignerPayload(registry, { ...TEST, runtimeVersion: { specVersion: 0x06, transactionVersion: 0x07 } }).toPayload() ).toEqual(TEST); }); @@ -64,7 +70,7 @@ describe('SignerPayload', (): void => { expect( new SignerPayload( registry, - new SignerPayload(registry, TEST) + new SignerPayload(registry, { ...TEST, runtimeVersion: { specVersion: 0x06, transactionVersion: 0x07 } }) ).toPayload() ).toEqual(TEST); }); diff --git a/packages/types/src/extrinsic/SignerPayload.ts b/packages/types/src/extrinsic/SignerPayload.ts index cd4c06eaac..71bd4b8406 100644 --- a/packages/types/src/extrinsic/SignerPayload.ts +++ b/packages/types/src/extrinsic/SignerPayload.ts @@ -9,6 +9,8 @@ import { u8aToHex } from '@polkadot/util'; import Compact from '../codec/Compact'; import Struct from '../codec/Struct'; +import Vec from '../codec/Vec'; +import Text from '../primitive/Text'; import u8 from '../primitive/U8'; export interface SignerPayloadType extends Codec { @@ -20,6 +22,7 @@ export interface SignerPayloadType extends Codec { method: Call; nonce: Compact; runtimeVersion: RuntimeVersion; + signedExtensions: Vec; tip: Compact; version: u8; } @@ -35,6 +38,7 @@ const _Payload: Constructor = Struct.with({ method: 'Call', nonce: 'Compact', runtimeVersion: 'RuntimeVersion', + signedExtensions: 'Vec', tip: 'Compact', version: 'u8' }) as any; @@ -49,7 +53,7 @@ export default class SignerPayload extends _Payload implements ISignerPayload { * @description Creates an representation of the structure as an ISignerPayload JSON */ public toPayload (): SignerPayloadJSON { - const { address, blockHash, blockNumber, era, genesisHash, method, nonce, runtimeVersion: { specVersion }, tip, version } = this; + const { address, blockHash, blockNumber, era, genesisHash, method, nonce, runtimeVersion: { specVersion, transactionVersion }, signedExtensions, tip, version } = this; return { address: address.toString(), @@ -59,8 +63,10 @@ export default class SignerPayload extends _Payload implements ISignerPayload { genesisHash: genesisHash.toHex(), method: method.toHex(), nonce: nonce.toHex(), + signedExtensions: signedExtensions.map((ext) => ext.toString()), specVersion: specVersion.toHex(), tip: tip.toHex(), + txVersion: transactionVersion.toHex(), version: version.toNumber() }; } diff --git a/packages/types/src/types/extrinsic.ts b/packages/types/src/types/extrinsic.ts index 3139fc0553..d096a6deb5 100644 --- a/packages/types/src/types/extrinsic.ts +++ b/packages/types/src/types/extrinsic.ts @@ -60,6 +60,7 @@ export interface SignatureOptions { genesisHash: AnyU8a; nonce: AnyNumber; runtimeVersion: IRuntimeVersion; + signedExtensions?: string[]; signer?: Signer; tip?: AnyNumber; } @@ -155,7 +156,12 @@ export interface SignerPayloadJSON { nonce: string; /** - * @description The current spec version for the runtime + * @description The current signed extensions for the network + */ + signedExtensions: string[]; + + /** + * @description The current spec version for the runtime */ specVersion: string; @@ -164,6 +170,11 @@ export interface SignerPayloadJSON { */ tip: string; + /** + * @description The current transaction version for the runtime + */ + txVersion: string; + /** * @description The version of the extrinsic we are dealing with */ diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 277d08fda0..06e5e7c94e 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -112,6 +112,7 @@ export interface Registry { readonly chainSS58: number | undefined; readonly chainToken: string; readonly knownTypes: RegisteredTypes; + readonly signedExtensions: string[]; findMetaCall (callIndex: Uint8Array): CallFunction; findMetaError (errorIndex: Uint8Array): any; @@ -136,5 +137,6 @@ export interface Registry { register (name: string, type: Constructor): void; register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void; setChainProperties (properties?: ChainProperties): void; - setMetadata (metadata: RegistryMetadata): void; + setMetadata (metadata: RegistryMetadata, signedExtensions?: string[]): void; + setSignedExtensions (signedExtensions: string[]): void; }