use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::sr25519;
use sp_runtime::AccountId32;
#[frame_support::pallet]
#[allow(dead_code)]
pub mod mock_origin {
use sp_std::marker::PhantomData;
use frame_support::{
traits::{EnsureOrigin, EnsureOriginWithArg},
Parameter,
};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::AccountId32;
use crate::traits::CallSources;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeOrigin: From<Origin<Self>>;
type AccountId: Parameter;
type SubjectId: Parameter;
}
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::origin]
pub type Origin<T> = DoubleOrigin<<T as Config>::AccountId, <T as Config>::SubjectId>;
#[derive(Debug, Clone, PartialEq, Eq, TypeInfo, Encode, Decode, MaxEncodedLen)]
pub struct DoubleOrigin<AccountId, SubjectId>(pub AccountId, pub SubjectId);
impl<AccountId: Clone, SubjectId: Clone> CallSources<AccountId, SubjectId> for DoubleOrigin<AccountId, SubjectId> {
fn sender(&self) -> AccountId {
self.0.clone()
}
fn subject(&self) -> SubjectId {
self.1.clone()
}
}
pub struct EnsureDoubleOrigin<AccountId, SubjectId>(PhantomData<(AccountId, SubjectId)>);
impl<OuterOrigin, AccountId, SubjectId> EnsureOrigin<OuterOrigin> for EnsureDoubleOrigin<AccountId, SubjectId>
where
OuterOrigin:
Into<Result<DoubleOrigin<AccountId, SubjectId>, OuterOrigin>> + From<DoubleOrigin<AccountId, SubjectId>>,
AccountId: From<AccountId32>,
SubjectId: From<AccountId32>,
{
type Success = DoubleOrigin<AccountId, SubjectId>;
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
o.into()
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()> {
const TEST_ACC: AccountId32 = AccountId32::new([0u8; 32]);
Ok(OuterOrigin::from(DoubleOrigin(
TEST_ACC.clone().into(),
TEST_ACC.into(),
)))
}
}
impl<OuterOrigin, AccountId, SubjectId> EnsureOriginWithArg<OuterOrigin, SubjectId>
for EnsureDoubleOrigin<AccountId, SubjectId>
where
OuterOrigin: Into<Result<DoubleOrigin<AccountId, SubjectId>, OuterOrigin>>
+ From<DoubleOrigin<AccountId, SubjectId>>
+ Clone,
SubjectId: PartialEq<SubjectId> + Clone,
AccountId: Clone + Decode,
{
type Success = DoubleOrigin<AccountId, SubjectId>;
fn try_origin(o: OuterOrigin, a: &SubjectId) -> Result<Self::Success, OuterOrigin> {
let did_origin: DoubleOrigin<AccountId, SubjectId> = o.clone().into()?;
if did_origin.1 == *a {
Ok(did_origin)
} else {
Err(o)
}
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &SubjectId) -> Result<OuterOrigin, ()> {
let zero_account_id = AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed");
Ok(OuterOrigin::from(DoubleOrigin(zero_account_id, a.clone())))
}
}
#[cfg(feature = "runtime-benchmarks")]
impl<OuterOrigin, AccountId, SubjectId> crate::traits::GenerateBenchmarkOrigin<OuterOrigin, AccountId, SubjectId>
for EnsureDoubleOrigin<AccountId, SubjectId>
where
OuterOrigin:
Into<Result<DoubleOrigin<AccountId, SubjectId>, OuterOrigin>> + From<DoubleOrigin<AccountId, SubjectId>>,
{
fn generate_origin(sender: AccountId, subject: SubjectId) -> OuterOrigin {
OuterOrigin::from(DoubleOrigin(sender, subject))
}
}
}
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct SubjectId(pub AccountId32);
impl From<AccountId32> for SubjectId {
fn from(acc: AccountId32) -> Self {
SubjectId(acc)
}
}
impl From<sr25519::Public> for SubjectId {
fn from(acc: sr25519::Public) -> Self {
SubjectId(acc.into())
}
}
impl AsRef<[u8]> for SubjectId {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}