Extend SignerPayload with extra tx/extensions (#2267)

* Extend SignerPayload with extra tx/extensions

* Update registry.ts

* Update extrinsic.ts

* Linting options
This commit is contained in:
Jaco Greeff
2020-05-12 18:32:18 +02:00
committed by GitHub
parent 3f060b9e0d
commit d1f8f8b5e8
7 changed files with 50 additions and 10 deletions
+17 -4
View File
@@ -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);
@@ -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);
});
@@ -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<Index>;
runtimeVersion: RuntimeVersion;
signedExtensions: Vec<Text>;
tip: Compact<Balance>;
version: u8;
}
@@ -35,6 +38,7 @@ const _Payload: Constructor<SignerPayloadType> = Struct.with({
method: 'Call',
nonce: 'Compact<Index>',
runtimeVersion: 'RuntimeVersion',
signedExtensions: 'Vec<Text>',
tip: 'Compact<Balance>',
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()
};
}
+12 -1
View File
@@ -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
*/
+3 -1
View File
@@ -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;
}