From dd343be1f34e38fcb2778b287ab25ef6de1d7bf1 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 23 May 2024 10:54:53 +0100 Subject: [PATCH] Add a basic version of the CheckMetadataHash signed extension (#1590) * Add a basic version of the CheckMetadataHash signed extension * update example too * encode None to signer payload when no hash provided * Enable decoding txs using CheckMetadataHash, too * Tidy up decoding CheckMetadataHash --- core/src/config/default_extrinsic_params.rs | 2 + core/src/config/signed_extensions.rs | 55 +++++++++++++++++++ .../examples/setup_config_signed_extension.rs | 5 +- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/core/src/config/default_extrinsic_params.rs b/core/src/config/default_extrinsic_params.rs index 344c1a3c4e..628f639843 100644 --- a/core/src/config/default_extrinsic_params.rs +++ b/core/src/config/default_extrinsic_params.rs @@ -18,6 +18,7 @@ pub type DefaultExtrinsicParams = signed_extensions::AnyOf< signed_extensions::CheckMortality, signed_extensions::ChargeAssetTxPayment, signed_extensions::ChargeTransactionPayment, + signed_extensions::CheckMetadataHash, ), >; @@ -151,6 +152,7 @@ impl DefaultExtrinsicParamsBuilder { check_mortality_params, charge_asset_tx_params, charge_transaction_params, + (), ) } } diff --git a/core/src/config/signed_extensions.rs b/core/src/config/signed_extensions.rs index 4ecd6684db..893872b6f2 100644 --- a/core/src/config/signed_extensions.rs +++ b/core/src/config/signed_extensions.rs @@ -40,6 +40,61 @@ pub trait SignedExtension: ExtrinsicParams { fn matches(identifier: &str, _type_id: u32, _types: &PortableRegistry) -> bool; } +/// The [`CheckMetadataHash`] signed extension. +pub struct CheckMetadataHash { + // Eventually we might provide or calculate the metadata hash here, + // but for now we never provide a hash and so this is empty. +} + +impl ExtrinsicParams for CheckMetadataHash { + type Params = (); + + fn new(_client: &ClientState, _params: Self::Params) -> Result { + Ok(CheckMetadataHash {}) + } +} + +impl ExtrinsicParamsEncoder for CheckMetadataHash { + fn encode_extra_to(&self, v: &mut Vec) { + // A single 0 byte in the TX payload indicates that the chain should + // _not_ expect any metadata hash to exist in the signer payload. + 0u8.encode_to(v); + } + fn encode_additional_to(&self, v: &mut Vec) { + // We provide no metadata hash in the signer payload to align with the above. + None::<()>.encode_to(v); + } +} + +impl SignedExtension for CheckMetadataHash { + type Decoded = CheckMetadataHashMode; + fn matches(identifier: &str, _type_id: u32, _types: &PortableRegistry) -> bool { + identifier == "CheckMetadataHash" + } +} + +/// Is metadata checking enabled or disabled? +// Dev note: The "Disabled" and "Enabled" variant names match those that the +// signed extension will be encoded with, in order that DecodeAsType will work +// properly. +#[derive(Copy, Clone, Debug, DecodeAsType)] +pub enum CheckMetadataHashMode { + /// No hash was provided in the signer payload. + Disabled, + /// A hash was provided in the signer payload. + Enabled, +} + +impl CheckMetadataHashMode { + /// Is metadata checking enabled or disabled for this transaction? + pub fn is_enabled(&self) -> bool { + match self { + CheckMetadataHashMode::Disabled => false, + CheckMetadataHashMode::Enabled => true, + } + } +} + /// The [`CheckSpecVersion`] signed extension. pub struct CheckSpecVersion(u32); diff --git a/subxt/examples/setup_config_signed_extension.rs b/subxt/examples/setup_config_signed_extension.rs index 66585e46bb..0349d03380 100644 --- a/subxt/examples/setup_config_signed_extension.rs +++ b/subxt/examples/setup_config_signed_extension.rs @@ -37,6 +37,7 @@ impl Config for CustomConfig { signed_extensions::CheckMortality, signed_extensions::ChargeAssetTxPayment, signed_extensions::ChargeTransactionPayment, + signed_extensions::CheckMetadataHash, // And add a new one of our own: CustomSignedExtension, ), @@ -83,8 +84,8 @@ impl ExtrinsicParamsEncoder for CustomSignedExtension { pub fn custom( params: DefaultExtrinsicParamsBuilder, ) -> <::ExtrinsicParams as ExtrinsicParams>::Params { - let (a, b, c, d, e, f, g) = params.build(); - (a, b, c, d, e, f, g, ()) + let (a, b, c, d, e, f, g, h) = params.build(); + (a, b, c, d, e, f, g, h, ()) } #[tokio::main]