referrerpolicy=no-referrer-when-downgrade

Crate frame_metadata_hash_extension

source ·
Expand description

The CheckMetadataHash transaction extension.

The extension for optionally checking the metadata hash. For information how it works and what it does exactly, see the docs of CheckMetadataHash.

§Integration

As any transaction extension you will need to add it to your runtime transaction extensions:

mod add_metadata_hash_extension {
	frame_support::construct_runtime! {
		pub enum Runtime {
			System: frame_system,
		}
	}

	/// The `TransactionExtension` to the basic transaction logic.
	pub type TxExtension = (
		frame_system::CheckNonZeroSender<Runtime>,
		frame_system::CheckSpecVersion<Runtime>,
		frame_system::CheckTxVersion<Runtime>,
		frame_system::CheckGenesis<Runtime>,
		frame_system::CheckMortality<Runtime>,
		frame_system::CheckNonce<Runtime>,
		frame_system::CheckWeight<Runtime>,
		// Add the `CheckMetadataHash` extension.
		// The position in this list is not important, so we could also add it to beginning.
		frame_metadata_hash_extension::CheckMetadataHash<Runtime>,
	);

	/// In your runtime this will be your real address type.
	type Address = ();
	/// In your runtime this will be your real signature type.
	type Signature = ();

	/// Unchecked extrinsic type as expected by this runtime.
	pub type UncheckedExtrinsic =
		sp_runtime::generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
}

As the extension requires the RUNTIME_METADATA_HASH environment variable to be present at compile time, it requires a little bit more setup. To have this environment variable available at compile time required to tell the substrate-wasm-builder to do so:

fn enable_metadata_hash_in_wasm_builder() {
	substrate_wasm_builder::WasmBuilder::init_with_defaults()
		// Requires the `metadata-hash` feature to be activated.
		// You need to pass the main token symbol and its number of decimals.
		.enable_metadata_hash("TOKEN", 12)
		// The runtime will be build twice and the second time the `RUNTIME_METADATA_HASH`
		// environment variable will be set for the `CheckMetadataHash` extension.
		.build()
}

As generating the metadata hash requires to compile the runtime twice, it is recommended to only enable the metadata hash generation when doing a build for a release or when you want to test this feature.

Structs§