You can add and create custom origins in pallets.
In your pallet, you define the origin type.
use frame_support::pallet_prelude::*;
use pallet::*;
use sp_runtime::traits::BadOrigin;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {}
/// My custom origin!
#[pallet::origin]
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum Origin {
/// Custom Origin type.
CustomOrigin(InternalStruct),
}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct InternalStruct {
item: u64,
}
/// This is an analogue of `ensure_signed` for the custom origin type.
pub fn ensure_custom_origin<OuterOrigin>(o: OuterOrigin) -> Result<InternalStruct, BadOrigin>
where
OuterOrigin: Into<Result<Origin, OuterOrigin>>,
{
match o.into() {
Ok(Origin::CustomOrigin(internal)) => Ok(internal),
_ => Err(BadOrigin),
}
}
And then you can integrate this in construct_runtime!
to add your custom origin type to the overarching runtime origin.
construct_runtime! {
pub enum Runtime where
Block = Block,
NodeBlock = OpaqueBlock,
UncheckedExtrinsic = UncheckedExtrinsic
{
// .. other pallets
CustomOriginPallet: custom_origin_pallet::{Pallet, Origin},
}
}
Per your question about whether you can create a custom origin which modifies the behavior of ensure_signed
, the answer is 'no'. ensure_signed
only accepts origins of the form frame_system::RawOrigin::Signed(AccountId)
.