#[cfg(feature = "runtime-benchmarks")]
use alloc::vec;
use codec::{Decode, Encode, MaxEncodedLen};
#[cfg(feature = "runtime-benchmarks")]
use enumflags2::BitFlag;
use enumflags2::{bitflags, BitFlags};
use frame_support::{traits::Get, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound};
use scale_info::{build::Variants, Path, Type, TypeInfo};
use sp_runtime::{BoundedVec, RuntimeDebug};
use crate::types::{Data, IdentityInformationProvider};
#[bitflags]
#[repr(u64)]
#[derive(Clone, Copy, PartialEq, Eq, RuntimeDebug)]
pub enum IdentityField {
Display,
Legal,
Web,
Riot,
Email,
PgpFingerprint,
Image,
Twitter,
}
impl TypeInfo for IdentityField {
type Identity = Self;
fn type_info() -> scale_info::Type {
Type::builder().path(Path::new("IdentityField", module_path!())).variant(
Variants::new()
.variant("Display", |v| v.index(0))
.variant("Legal", |v| v.index(1))
.variant("Web", |v| v.index(2))
.variant("Riot", |v| v.index(3))
.variant("Email", |v| v.index(4))
.variant("PgpFingerprint", |v| v.index(5))
.variant("Image", |v| v.index(6))
.variant("Twitter", |v| v.index(7)),
)
}
}
#[derive(
CloneNoBound,
Encode,
Decode,
EqNoBound,
MaxEncodedLen,
PartialEqNoBound,
RuntimeDebugNoBound,
TypeInfo,
)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(FieldLimit))]
pub struct IdentityInfo<FieldLimit: Get<u32>> {
pub additional: BoundedVec<(Data, Data), FieldLimit>,
pub display: Data,
pub legal: Data,
pub web: Data,
pub riot: Data,
pub email: Data,
pub pgp_fingerprint: Option<[u8; 20]>,
pub image: Data,
pub twitter: Data,
}
impl<FieldLimit: Get<u32> + 'static> IdentityInformationProvider for IdentityInfo<FieldLimit> {
type FieldsIdentifier = u64;
fn has_identity(&self, fields: Self::FieldsIdentifier) -> bool {
self.fields().bits() & fields == fields
}
#[cfg(feature = "runtime-benchmarks")]
fn create_identity_info() -> Self {
let data = Data::Raw(vec![0; 32].try_into().unwrap());
IdentityInfo {
additional: vec![(data.clone(), data.clone()); FieldLimit::get().try_into().unwrap()]
.try_into()
.unwrap(),
display: data.clone(),
legal: data.clone(),
web: data.clone(),
riot: data.clone(),
email: data.clone(),
pgp_fingerprint: Some([0; 20]),
image: data.clone(),
twitter: data,
}
}
#[cfg(feature = "runtime-benchmarks")]
fn all_fields() -> Self::FieldsIdentifier {
IdentityField::all().bits()
}
}
impl<FieldLimit: Get<u32>> Default for IdentityInfo<FieldLimit> {
fn default() -> Self {
IdentityInfo {
additional: BoundedVec::default(),
display: Data::None,
legal: Data::None,
web: Data::None,
riot: Data::None,
email: Data::None,
pgp_fingerprint: None,
image: Data::None,
twitter: Data::None,
}
}
}
impl<FieldLimit: Get<u32>> IdentityInfo<FieldLimit> {
pub(crate) fn fields(&self) -> BitFlags<IdentityField> {
let mut res = BitFlags::<IdentityField>::empty();
if !self.display.is_none() {
res.insert(IdentityField::Display);
}
if !self.legal.is_none() {
res.insert(IdentityField::Legal);
}
if !self.web.is_none() {
res.insert(IdentityField::Web);
}
if !self.riot.is_none() {
res.insert(IdentityField::Riot);
}
if !self.email.is_none() {
res.insert(IdentityField::Email);
}
if self.pgp_fingerprint.is_some() {
res.insert(IdentityField::PgpFingerprint);
}
if !self.image.is_none() {
res.insert(IdentityField::Image);
}
if !self.twitter.is_none() {
res.insert(IdentityField::Twitter);
}
res
}
}