fix: CheckMetadataHash signedExtension support (#5882)

* CheckMetadataHash support

* lint

* updtated CheckGenesis

* linting

* added getters for metadataHash

* fixed metadataHash type

* fixed toHuman override

* updated SignerPayload

* updated mode to take AnyNumber

---------

Co-authored-by: bee344 <alberto.penayo@parity.io>
This commit is contained in:
Tarik Gul
2024-05-20 13:31:23 -03:00
committed by GitHub
co-authored by bee344
parent 38de00d8d8
commit b392c19922
9 changed files with 142 additions and 20 deletions
+9 -1
View File
@@ -5,7 +5,7 @@ import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, IMethod, Inspect, IOption } fr
import type { HexString } from '@polkadot/util/types';
import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics/index.js';
import type { FunctionMetadataLatest } from '../interfaces/metadata/index.js';
import type { Address, Call, CodecHash } from '../interfaces/runtime/index.js';
import type { Address, Call, CodecHash, Hash } from '../interfaces/runtime/index.js';
import type { MultiLocation } from '../interfaces/types.js';
import type { CallBase, ExtrinsicPayloadValue, ICompact, IExtrinsic, IKeyringPair, INumber, Registry, SignatureOptions } from '../types/index.js';
import type { GenericExtrinsicEra } from './ExtrinsicEra.js';
@@ -199,6 +199,13 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends AbstractBase<ExtrinsicV
return this.inner.signature.assetId;
}
/**
* @description Forward compat
*/
public get metadataHash (): IOption<Hash> {
return this.inner.signature.metadataHash;
}
/**
* @description Returns the raw transaction version (not flagged with signing information)
*/
@@ -326,6 +333,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
? {
assetId: this.assetId ? this.assetId.toHuman(isExpanded, disableAscii) : null,
era: this.era.toHuman(isExpanded, disableAscii),
metadataHash: this.metadataHash ? this.metadataHash.toHex() : null,
nonce: this.nonce.toHuman(isExpanded, disableAscii),
signature: this.signature.toHex(),
signer: this.signer.toHuman(isExpanded, disableAscii),
@@ -6,7 +6,7 @@ import type { AnyJson, BareOpts, Registry } from '@polkadot/types-codec/types';
import type { HexString } from '@polkadot/util/types';
import type { BlockHash } from '../interfaces/chain/index.js';
import type { ExtrinsicPayloadV4 } from '../interfaces/extrinsics/index.js';
import type { MultiLocation } from '../interfaces/types.js';
import type { Hash, MultiLocation } from '../interfaces/types.js';
import type { ExtrinsicPayloadValue, ICompact, IKeyringPair, INumber, IOption } from '../types/index.js';
import type { GenericExtrinsicEra } from './ExtrinsicEra.js';
@@ -117,6 +117,13 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
return this.inner.assetId;
}
/**
* @description The (optional) [[Hash]] of the genesis metadata for this payload
*/
public get metadataHash (): IOption<Hash> {
return this.inner.metadataHash;
}
/**
* @description Compares the value of the input to see if there is a match
*/
@@ -17,11 +17,15 @@ registry.setMetadata(metadata);
describe('SignerPayload', (): void => {
const TEST = {
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
// eslint-disable-next-line sort-keys
assetId: { parents: 0, interior: { x2: [{ palletInstance: 50 }, { generalIndex: 123 }] } },
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x00231d30',
era: '0x0703',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
metadataHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
method: '0x060000d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
mode: 1,
nonce: '0x00001234',
signedExtensions: ['CheckNonce', 'CheckWeight'],
specVersion: '0x00000006',
@@ -34,11 +38,15 @@ describe('SignerPayload', (): void => {
expect(
new SignerPayload(registry, {
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
// eslint-disable-next-line sort-keys
assetId: { parents: 0, interior: { x2: [{ palletInstance: 50 }, { generalIndex: 123 }] } },
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x231d30',
era: registry.createType('ExtrinsicEra', { current: 2301232, period: 200 }),
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
metadataHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
method: registry.createType('Call', '0x060000d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c'),
mode: 1,
nonce: 0x1234,
signedExtensions: ['CheckNonce'],
tip: 0x5678,
@@ -46,11 +54,15 @@ describe('SignerPayload', (): void => {
}).toPayload()
).toEqual({
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
// eslint-disable-next-line sort-keys
assetId: { parents: 0, interior: { x2: [{ palletInstance: 50 }, { generalIndex: 123 }] } },
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x00231d30',
era: '0x0703',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
metadataHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
method: '0x060000d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
mode: 1,
nonce: '0x00001234',
signedExtensions: ['CheckNonce'],
specVersion: '0x00000000',
@@ -71,25 +83,14 @@ describe('SignerPayload', (): void => {
).toEqual(true);
expect(
// @ts-expect-error We don't have getters for this field
test.toPayload().assetId
).toEqual({
// eslint-disable-next-line sort-keys
parents: 0, interior: { x2: [{ palletInstance: 50 }, { generalIndex: 123 }] }
});
).toEqual({ parents: 0, interior: { x2: [{ palletInstance: 50 }, { generalIndex: 123 }] } });
expect(
// @ts-expect-error We don't have getters for this field
new SignerPayload(registry, { assetId: 0 }).toPayload().assetId
).toEqual({
// eslint-disable-next-line sort-keys
parents: 0, interior: { here: null }
});
expect(
// @ts-expect-error We don't have getters for this field
new SignerPayload(registry, TEST).toPayload().assetId
).toBeUndefined();
).toEqual({ parents: 0, interior: { here: null } });
});
it('re-constructs from JSON', (): void => {
@@ -126,11 +127,55 @@ describe('SignerPayload', (): void => {
expect(payload.blockHash.toHex()).toEqual(TEST.blockHash);
expect(payload.nonce.eq(TEST.nonce)).toBe(true);
expect(payload.tip.eq(TEST.tip)).toBe(true);
// @ts-expect-error assetId is of unknown type, so we don't know about "isSome"
expect(payload.inner?.get('assetId')?.isSome && payload.inner?.get('assetId')
expect(payload.assetId.isSome && payload.assetId
?.eq(registry.createType('MultiLocation', {
// eslint-disable-next-line sort-keys
parents: 0, interior: { X2: [{ palletInstance: 50 }, { generalIndex: 123 }] }
}))).toBe(true);
});
const TEST_WITHOUT_CHECK = {
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x00231d30',
era: '0x0703',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
method: '0x060000d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
nonce: '0x00001234',
signedExtensions: [],
specVersion: '0x00000006',
tip: '0x00000000000000000000000000005678',
transactionVersion: '0x00000007',
version: 4
};
const PAYLOAD_WITHOUT_CHECK = {
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
assetId: null,
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
blockNumber: '0x00231d30',
era: '0x0703',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
metadataHash: null,
method: '0x060000d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
mode: 0,
nonce: '0x00001234',
signedExtensions: [],
specVersion: '0x00000006',
tip: '0x00000000000000000000000000005678',
transactionVersion: '0x00000007',
version: 4
};
it('can build SignerPayload without additional SignedExtensions', (): void => {
expect(
new SignerPayload(
registry,
{
...TEST_WITHOUT_CHECK,
runtimeVersion: { specVersion: 0x06, transactionVersion: 0x07 }
}
).toPayload()
).toEqual(PAYLOAD_WITHOUT_CHECK);
});
});
@@ -12,11 +12,14 @@ import { objectProperty, objectSpread, u8aToHex } from '@polkadot/util';
export interface SignerPayloadType extends Codec {
address: Address;
assetId: IOption<INumber | MultiLocation>;
blockHash: Hash;
blockNumber: INumber;
era: ExtrinsicEra;
genesisHash: Hash;
metadataHash: IOption<Hash>;
method: Call;
mode: INumber;
nonce: ICompact<INumber>;
runtimeVersion: IRuntimeVersion;
signedExtensions: Vec<Text>;
@@ -26,11 +29,14 @@ export interface SignerPayloadType extends Codec {
const knownTypes: Record<string, string> = {
address: 'Address',
assetId: 'Option<TAssetConversion>',
blockHash: 'Hash',
blockNumber: 'BlockNumber',
era: 'ExtrinsicEra',
genesisHash: 'Hash',
metadataHash: 'Option<[u8;32]>',
method: 'Call',
mode: 'u8',
nonce: 'Compact<Index>',
runtimeVersion: 'RuntimeVersion',
signedExtensions: 'Vec<Text>',
@@ -112,6 +118,14 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
return this.getT('version');
}
get mode (): INumber {
return this.getT('mode');
}
get metadataHash (): IOption<Hash> {
return this.getT('metadataHash');
}
/**
* @description Creates an representation of the structure as an ISignerPayload JSON
*/
@@ -139,11 +153,14 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
// the known defaults as managed explicitly and has different
// formatting in cases, e.g. we mostly expose a hex format here
address: this.address.toString(),
assetId: this.assetId ? this.assetId.toJSON() : null,
blockHash: this.blockHash.toHex(),
blockNumber: this.blockNumber.toHex(),
era: this.era.toHex(),
genesisHash: this.genesisHash.toHex(),
metadataHash: this.metadataHash.isSome ? this.metadataHash.toHex() : null,
method: this.method.toHex(),
mode: this.mode.toNumber(),
nonce: this.nonce.toHex(),
signedExtensions: this.signedExtensions.map((e) => e.toString()),
specVersion: this.runtimeVersion.specVersion.toHex(),
@@ -5,6 +5,15 @@ import type { ExtDef, ExtInfo } from './types.js';
import { emptyCheck } from './emptyCheck.js';
const CheckMetadataHash: ExtInfo = {
extrinsic: {
mode: 'u8'
},
payload: {
metadataHash: 'Option<[u8;32]>'
}
};
const CheckMortality: ExtInfo = {
extrinsic: {
era: 'ExtrinsicEra'
@@ -31,6 +40,7 @@ export const substrate: ExtDef = {
genesisHash: 'Hash'
}
},
CheckMetadataHash,
CheckMortality,
CheckNonZeroSender: emptyCheck,
CheckNonce: {
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { SignOptions } from '@polkadot/keyring/types';
import type { MultiLocation } from '@polkadot/types/interfaces';
import type { Hash, MultiLocation } from '@polkadot/types/interfaces';
import type { Bytes } from '@polkadot/types-codec';
import type { Inspect, Registry } from '@polkadot/types-codec/types';
import type { HexString } from '@polkadot/util/types';
@@ -109,6 +109,13 @@ export class GenericExtrinsicPayloadV4 extends Struct {
return this.getT('assetId');
}
/**
* @description The (optional) asset id for this signature for chains that support transaction fees in assets
*/
public get metadataHash (): IOption<Hash> {
return this.getT('metadataHash');
}
/**
* @description Sign the payload with the keypair
*/
@@ -4,7 +4,7 @@
import type { MultiLocation } from '@polkadot/types/interfaces';
import type { HexString } from '@polkadot/util/types';
import type { EcdsaSignature, Ed25519Signature, ExtrinsicEra, ExtrinsicSignature, Sr25519Signature } from '../../interfaces/extrinsics/index.js';
import type { Address, Call } from '../../interfaces/runtime/index.js';
import type { Address, Call, Hash } from '../../interfaces/runtime/index.js';
import type { ExtrinsicPayloadValue, ICompact, IExtrinsicSignature, IKeyringPair, INumber, IOption, Registry, SignatureOptions } from '../../types/index.js';
import type { ExtrinsicSignatureOptions } from '../types.js';
@@ -126,6 +126,13 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
return this.getT('assetId');
}
/**
* @description The [[Hash]] for the metadata
*/
public get metadataHash (): IOption<Hash> {
return this.getT('metadataHash');
}
protected _injectSignature (signer: Address, signature: ExtrinsicSignature, payload: GenericExtrinsicPayloadV4): IExtrinsicSignature {
// use the fields exposed to guide the getters
for (let i = 0, count = this.#signKeys.length; i < count; i++) {
+19
View File
@@ -35,6 +35,11 @@ export interface SignerPayloadJSON {
*/
address: string;
/**
* @description The id of the asset used to pay fees, in hex
*/
assetId?: number | object;
/**
* @description The checkpoint hash of the block, in hex
*/
@@ -55,11 +60,21 @@ export interface SignerPayloadJSON {
*/
genesisHash: HexString;
/**
* @description The metadataHash for the CheckMetadataHash SignedExtension, as hex
*/
metadataHash?: HexString;
/**
* @description The encoded method (with arguments) in hex
*/
method: string;
/**
* @description The mode for the CheckMetadataHash SignedExtension, in hex
*/
mode?: number;
/**
* @description The nonce for this transaction, in hex
*/
@@ -164,6 +179,8 @@ export interface SignatureOptions {
signer?: Signer;
tip?: AnyNumber;
assetId?: AnyNumber | object;
mode?: AnyNumber;
metadataHash?: Uint8Array | string;
}
interface ExtrinsicSignatureBase {
@@ -185,6 +202,8 @@ export interface ExtrinsicPayloadValue {
tip: AnyNumber;
transactionVersion: AnyNumber;
assetId?: AnyNumber | object;
mode?: AnyNumber;
metadataHash?: AnyU8a;
}
export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec {