#![cfg_attr(not(feature = "std"), no_std)]
use super::*;
use parity_scale_codec::{Decode, Encode};
use core::fmt::Debug;
pub use common_primitives::msa::{
Delegation, DelegatorId, KeyInfoResponse, MessageSourceId, ProviderId,
};
use common_primitives::{node::BlockNumber, schema::SchemaId};
use scale_info::TypeInfo;
pub const EMPTY_FUNCTION: fn(MessageSourceId) -> DispatchResult = |_| Ok(());
#[derive(TypeInfo, RuntimeDebugNoBound, Clone, Decode, Encode, PartialEq, Eq)]
#[scale_info(skip_type_params(T))]
pub struct AddKeyData<T: Config> {
pub msa_id: MessageSourceId,
pub expiration: BlockNumberFor<T>,
pub new_public_key: T::AccountId,
}
#[derive(TypeInfo, Clone, Debug, Decode, Encode, PartialEq, Eq)]
pub struct AddProvider {
pub authorized_msa_id: MessageSourceId,
pub schema_ids: Vec<SchemaId>,
pub expiration: BlockNumber,
}
impl AddProvider {
pub fn new(
authorized_msa_id: MessageSourceId,
schema_ids: Option<Vec<SchemaId>>,
expiration: BlockNumber,
) -> Self {
let schema_ids = match schema_ids {
Some(schemas) => schemas,
None => Vec::default(),
};
Self { authorized_msa_id, schema_ids, expiration }
}
}
pub trait PermittedDelegationSchemas<T: Config> {
fn try_insert_schema(&mut self, schema_id: SchemaId) -> Result<(), DispatchError>;
fn try_insert_schemas(&mut self, schema_ids: Vec<SchemaId>) -> Result<(), DispatchError> {
for schema_id in schema_ids.into_iter() {
self.try_insert_schema(schema_id)?;
}
Ok(())
}
fn try_get_mut_schemas(
&mut self,
schema_ids: Vec<SchemaId>,
block_number: BlockNumberFor<T>,
) -> Result<(), DispatchError> {
for schema_id in schema_ids.into_iter() {
self.try_get_mut_schema(schema_id, block_number)?;
}
Ok(())
}
fn try_get_mut_schema(
&mut self,
schema_id: SchemaId,
block_number: BlockNumberFor<T>,
) -> Result<(), DispatchError>;
}
impl<T: Config> PermittedDelegationSchemas<T>
for Delegation<SchemaId, BlockNumberFor<T>, T::MaxSchemaGrantsPerDelegation>
{
fn try_insert_schema(&mut self, schema_id: SchemaId) -> Result<(), DispatchError> {
self.schema_permissions
.try_insert(schema_id, Default::default())
.map_err(|_| Error::<T>::ExceedsMaxSchemaGrantsPerDelegation)?;
Ok(())
}
fn try_get_mut_schema(
&mut self,
schema_id: SchemaId,
block_number: BlockNumberFor<T>,
) -> Result<(), DispatchError> {
let schema = self
.schema_permissions
.get_mut(&schema_id)
.ok_or(Error::<T>::SchemaNotGranted)?;
*schema = block_number;
Ok(())
}
}