use codec::{Decode, DecodeWithMemTracking, Encode}; use frame_support::dispatch::{DispatchInfo, PostDispatchInfo}; use frame_support::traits::IsSubType; use frame_system::Config; use pallet_sudo::Call as SudoCall; use scale_info::TypeInfo; use sp_runtime::impl_tx_ext_default; use sp_runtime::traits::{ AsSystemOriginSigner, DispatchInfoOf, Dispatchable, Implication, TransactionExtension, ValidateResult, }; use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource}; use sp_std::marker::PhantomData; use subtensor_macros::freeze_struct; #[freeze_struct("99dce71278b36b44")] #[derive(Default, Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)] pub struct SudoTransactionExtension(pub PhantomData); impl sp_std::fmt::Debug for SudoTransactionExtension { #[cfg(feature = "std")] fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "SudoTransactionExtension",) } #[cfg(not(feature = "std"))] fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result { Ok(()) } } impl SudoTransactionExtension { pub fn new() -> Self { Self(Default::default()) } } impl TransactionExtension<::RuntimeCall> for SudoTransactionExtension where ::RuntimeCall: Dispatchable, ::RuntimeOrigin: AsSystemOriginSigner + Clone, ::RuntimeCall: IsSubType>, { const IDENTIFIER: &'static str = "SudoTransactionExtension"; type Implicit = (); type Val = (); type Pre = (); impl_tx_ext_default!(::RuntimeCall; weight prepare); fn validate( &self, origin: ::RuntimeOrigin, call: &::RuntimeCall, _info: &DispatchInfoOf<::RuntimeCall>, _len: usize, _self_implicit: Self::Implicit, _inherited_implication: &impl Implication, _source: TransactionSource, ) -> ValidateResult::RuntimeCall> { // Ensure the transaction is signed, else we just skip the extension. let Some(who) = origin.as_system_origin_signer() else { return Ok((Default::default(), (), origin)); }; // Check validity of the signer for sudo call if let Some(_sudo_call) = IsSubType::>::is_sub_type(call) { let sudo_key = pallet_sudo::pallet::Key::::get(); // No sudo key configured → reject let Some(expected_who) = sudo_key else { return Err(InvalidTransaction::BadSigner.into()); }; // Signer does not match the sudo key → reject if *who != expected_who { return Err(InvalidTransaction::BadSigner.into()); } } Ok((Default::default(), (), origin)) } }