From 790d789dcba742a263a72e4e33f8201365a2866e Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sat, 2 Jul 2022 17:52:34 +0200
Subject: [PATCH] add custom IdtyData

---
 node/src/chain_spec/gdev.rs          |  2 +
 pallets/duniter-wot/src/mock.rs      |  2 +
 pallets/duniter-wot/src/tests.rs     |  1 +
 pallets/identity/src/lib.rs          | 64 +++++++++++++++++++++++++++-
 pallets/identity/src/mock.rs         |  1 +
 pallets/identity/src/tests.rs        |  3 +-
 pallets/identity/src/types.rs        |  3 +-
 runtime/common/src/pallets_config.rs |  1 +
 runtime/gdev/tests/common/mod.rs     |  1 +
 9 files changed, 74 insertions(+), 4 deletions(-)

diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs
index 18695102a..a846a23ad 100644
--- a/node/src/chain_spec/gdev.rs
+++ b/node/src/chain_spec/gdev.rs
@@ -377,6 +377,7 @@ fn gen_genesis_for_local_chain(
                         owner_key: owner_key.clone(),
                         removable_on: 0,
                         status: IdtyStatus::Validated,
+                        data: (),
                     },
                 })
                 .collect(),
@@ -506,6 +507,7 @@ fn genesis_data_to_gdev_genesis_conf(
                         owner_key: pubkey,
                         removable_on: 0,
                         status: IdtyStatus::Validated,
+                        data: (),
                     },
                 })
                 .collect(),
diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs
index ae08de65f..5646650d9 100644
--- a/pallets/duniter-wot/src/mock.rs
+++ b/pallets/duniter-wot/src/mock.rs
@@ -113,6 +113,7 @@ impl pallet_identity::Config for Test {
     type Event = Event;
     type EnsureIdtyCallAllowed = DuniterWot;
     type IdtyCreationPeriod = IdtyCreationPeriod;
+    type IdtyData = ();
     type IdtyNameValidator = IdtyNameValidatorTestImpl;
     type IdtyIndex = IdtyIndex;
     type IdtyValidationOrigin = system::EnsureRoot<AccountId>;
@@ -248,6 +249,7 @@ pub fn new_test_ext(
                     owner_key: i as u64,
                     removable_on: 0,
                     status: pallet_identity::IdtyStatus::Validated,
+                    data: (),
                 },
             })
             .collect(),
diff --git a/pallets/duniter-wot/src/tests.rs b/pallets/duniter-wot/src/tests.rs
index 3e237c609..870f29ccd 100644
--- a/pallets/duniter-wot/src/tests.rs
+++ b/pallets/duniter-wot/src/tests.rs
@@ -217,6 +217,7 @@ fn test_new_idty_validation() {
                 owner_key: 6,
                 removable_on: 0,
                 status: IdtyStatus::Validated,
+                data: ()
             })
         );
     });
diff --git a/pallets/identity/src/lib.rs b/pallets/identity/src/lib.rs
index 79c16b06e..0cc9fc089 100644
--- a/pallets/identity/src/lib.rs
+++ b/pallets/identity/src/lib.rs
@@ -68,6 +68,14 @@ pub mod pallet {
         type EnsureIdtyCallAllowed: EnsureIdtyCallAllowed<Self>;
         /// Minimum duration between the creation of 2 identities by the same creator
         type IdtyCreationPeriod: Get<Self::BlockNumber>;
+        /// Custom data to store in each identity
+        type IdtyData: Clone
+            + Codec
+            + Default
+            + Eq
+            + TypeInfo
+            + MaybeSerializeDeserialize
+            + MaxEncodedLen;
         /// A short identity index.
         type IdtyIndex: Parameter
             + Member
@@ -102,7 +110,7 @@ pub mod pallet {
     pub struct GenesisIdty<T: Config> {
         pub index: T::IdtyIndex,
         pub name: IdtyName,
-        pub value: IdtyValue<T::BlockNumber, T::AccountId>,
+        pub value: IdtyValue<T::BlockNumber, T::AccountId, T::IdtyData>,
     }
 
     #[pallet::genesis_config]
@@ -159,7 +167,7 @@ pub mod pallet {
         _,
         Twox64Concat,
         T::IdtyIndex,
-        IdtyValue<T::BlockNumber, T::AccountId>,
+        IdtyValue<T::BlockNumber, T::AccountId, T::IdtyData>,
         OptionQuery,
     >;
 
@@ -280,6 +288,7 @@ pub mod pallet {
                     owner_key: owner_key.clone(),
                     removable_on,
                     status: IdtyStatus::Created,
+                    data: Default::default(),
                 },
             );
             IdentitiesRemovableOn::<T>::append(removable_on, (idty_index, IdtyStatus::Created));
@@ -560,3 +569,54 @@ impl<T: Config> sp_runtime::traits::Convert<T::AccountId, Option<T::IdtyIndex>>
         Self::identity_index_of(account_id)
     }
 }
+
+impl<T> frame_support::traits::StoredMap<T::AccountId, T::IdtyData> for Pallet<T>
+where
+    T: Config,
+{
+    fn get(key: &T::AccountId) -> T::IdtyData {
+        if let Some(idty_index) = Self::identity_index_of(key) {
+            if let Some(idty_val) = Identities::<T>::get(idty_index) {
+                idty_val.data
+            } else {
+                Default::default()
+            }
+        } else {
+            Default::default()
+        }
+    }
+    fn try_mutate_exists<R, E: From<sp_runtime::DispatchError>>(
+        key: &T::AccountId,
+        f: impl FnOnce(&mut Option<T::IdtyData>) -> Result<R, E>,
+    ) -> Result<R, E> {
+        let maybe_idty_index = Self::identity_index_of(key);
+        let mut maybe_idty_data = if let Some(idty_index) = maybe_idty_index {
+            if let Some(idty_val) = Identities::<T>::get(idty_index) {
+                Some(idty_val.data)
+            } else {
+                None
+            }
+        } else {
+            None
+        };
+        let result = f(&mut maybe_idty_data)?;
+        if let Some(idty_index) = maybe_idty_index {
+            Identities::<T>::mutate_exists(idty_index, |idty_val_opt| {
+                if let Some(ref mut idty_val) = idty_val_opt {
+                    idty_val.data = maybe_idty_data.unwrap_or_default();
+                } else if maybe_idty_data.is_some() {
+                    return Err(sp_runtime::DispatchError::Other(
+                        "Tring to set IdtyData for a non-existing identity!",
+                    ));
+                }
+                Ok(())
+            })?;
+        } else if maybe_idty_data.is_some() {
+            return Err(sp_runtime::DispatchError::Other(
+                "Tring to set IdtyData for a non-existing identity!",
+            )
+            .into());
+        }
+        Ok(result)
+    }
+}
diff --git a/pallets/identity/src/mock.rs b/pallets/identity/src/mock.rs
index 420bfa10b..36e24dca7 100644
--- a/pallets/identity/src/mock.rs
+++ b/pallets/identity/src/mock.rs
@@ -102,6 +102,7 @@ impl pallet_identity::Config for Test {
     type Event = Event;
     type EnsureIdtyCallAllowed = ();
     type IdtyCreationPeriod = IdtyCreationPeriod;
+    type IdtyData = ();
     type IdtyNameValidator = IdtyNameValidatorTestImpl;
     type IdtyIndex = u64;
     type IdtyValidationOrigin = system::EnsureRoot<AccountId>;
diff --git a/pallets/identity/src/tests.rs b/pallets/identity/src/tests.rs
index 3a25afc57..f2f50efb1 100644
--- a/pallets/identity/src/tests.rs
+++ b/pallets/identity/src/tests.rs
@@ -22,7 +22,7 @@ use frame_support::assert_ok;
 use frame_system::{EventRecord, Phase};
 use sp_runtime::testing::TestSignature;
 
-type IdtyVal = IdtyValue<u64, u64>;
+type IdtyVal = IdtyValue<u64, u64, ()>;
 
 fn alice() -> GenesisIdty<Test> {
     GenesisIdty {
@@ -33,6 +33,7 @@ fn alice() -> GenesisIdty<Test> {
             owner_key: 1,
             removable_on: 0,
             status: crate::IdtyStatus::Validated,
+            data: (),
         },
     }
 }
diff --git a/pallets/identity/src/types.rs b/pallets/identity/src/types.rs
index 529fb0bab..708b2ed93 100644
--- a/pallets/identity/src/types.rs
+++ b/pallets/identity/src/types.rs
@@ -79,11 +79,12 @@ impl Default for IdtyStatus {
 
 #[cfg_attr(feature = "std", derive(Debug, Deserialize, Serialize))]
 #[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
-pub struct IdtyValue<BlockNumber, AccountId> {
+pub struct IdtyValue<BlockNumber, AccountId, IdtyData> {
     pub next_creatable_identity_on: BlockNumber,
     pub owner_key: AccountId,
     pub removable_on: BlockNumber,
     pub status: IdtyStatus,
+    pub data: IdtyData,
 }
 
 #[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, RuntimeDebug)]
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index e7b880f1f..620f73597 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -416,6 +416,7 @@ macro_rules! pallets_config {
             type Event = Event;
             type EnsureIdtyCallAllowed = Wot;
             type IdtyCreationPeriod = IdtyCreationPeriod;
+			type IdtyData = ();
             type IdtyIndex = IdtyIndex;
             type IdtyNameValidator = IdtyNameValidatorImpl;
             type IdtyValidationOrigin = EnsureRoot<Self::AccountId>;
diff --git a/runtime/gdev/tests/common/mod.rs b/runtime/gdev/tests/common/mod.rs
index f17c23b4c..26e533c48 100644
--- a/runtime/gdev/tests/common/mod.rs
+++ b/runtime/gdev/tests/common/mod.rs
@@ -210,6 +210,7 @@ impl ExtBuilder {
                         owner_key: owner_key.clone(),
                         removable_on: 0,
                         status: IdtyStatus::Validated,
+                        data: (),
                     },
                 })
                 .collect(),
-- 
GitLab