diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 609ccb0baeef9646d06d5bfecd885e28f15e0aed..29e4c2c1ad32039b1932691cb41defce372a6587 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -9106,7 +9106,7 @@ dependencies = [ name = "sc-sysinfo" version = "6.0.0-dev" dependencies = [ - "futures 0.3.19", + "futures 0.3.21", "libc", "log 0.4.14", "rand 0.7.3", diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 51e63541a89b17985952da8d4bb695aff0c598cb..904b71654b416f52b2f227c5332d6ecb87a8be48 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -978,4 +978,10 @@ impl<T: Config> Pallet<T> { .filter_map(|a| SuperOf::<T>::get(&a).map(|x| (a, x.1))) .collect() } + + /// Check if the account has corresponding identity information by the identity field. + pub fn has_identity(who: &T::AccountId, fields: u64) -> bool { + IdentityOf::<T>::get(who) + .map_or(false, |registration| (registration.info.fields().0.bits() & fields) == fields) + } } diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index bf41b451cbaa318136b175822c14b6fad6700782..8cb0563ebeaa1a059757f3fe1558731b3b6debc6 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -500,3 +500,20 @@ fn setting_account_id_should_work() { assert_ok!(Identity::set_account_id(Origin::signed(4), 0, 3)); }); } + +#[test] +fn test_has_identity() { + new_test_ext().execute_with(|| { + assert_ok!(Identity::set_identity(Origin::signed(10), Box::new(ten()))); + assert!(Identity::has_identity(&10, IdentityField::Display as u64)); + assert!(Identity::has_identity(&10, IdentityField::Legal as u64)); + assert!(Identity::has_identity( + &10, + IdentityField::Display as u64 | IdentityField::Legal as u64 + )); + assert!(!Identity::has_identity( + &10, + IdentityField::Display as u64 | IdentityField::Legal as u64 | IdentityField::Web as u64 + )); + }); +} diff --git a/substrate/frame/identity/src/types.rs b/substrate/frame/identity/src/types.rs index 18fc54c941cbdabfa1297d8cd740011adf6477b6..cb8091fe1874701e7d3df0992dc05a5350b9fc9a 100644 --- a/substrate/frame/identity/src/types.rs +++ b/substrate/frame/identity/src/types.rs @@ -53,6 +53,12 @@ pub enum Data { ShaThree256([u8; 32]), } +impl Data { + pub fn is_none(&self) -> bool { + self == &Data::None + } +} + impl Decode for Data { fn decode<I: codec::Input>(input: &mut I) -> sp_std::result::Result<Self, codec::Error> { let b = input.read_byte()?; @@ -333,6 +339,37 @@ pub struct IdentityInfo<FieldLimit: Get<u32>> { pub twitter: Data, } +impl<FieldLimit: Get<u32>> IdentityInfo<FieldLimit> { + pub(crate) fn fields(&self) -> IdentityFields { + 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); + } + IdentityFields(res) + } +} + /// Information concerning the identity of the controller of an account. /// /// NOTE: This is stored separately primarily to facilitate the addition of extra fields in a