From f84fc598929025003076ff27d24874f3b5ac487a Mon Sep 17 00:00:00 2001 From: Xiliang Chen <xlchen1291@gmail.com> Date: Tue, 12 Apr 2022 17:26:34 +1200 Subject: [PATCH] add has_identity (#11197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add has_identity * Update frame/identity/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * update * update Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> --- substrate/Cargo.lock | 2 +- substrate/frame/identity/src/lib.rs | 6 +++++ substrate/frame/identity/src/tests.rs | 17 ++++++++++++ substrate/frame/identity/src/types.rs | 37 +++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 609ccb0baee..29e4c2c1ad3 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 51e63541a89..904b71654b4 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 bf41b451cba..8cb0563ebea 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 18fc54c941c..cb8091fe187 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 -- GitLab