diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index 08228677460bc255eb407e0175a55fb4a13c7f6d..b43a097208f1a23730fd426eb15a271f93f8907e 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -19,10 +19,7 @@ pub mod gdev; pub mod gtest; use common_runtime::IdtyIndex; -use common_runtime::{ - entities::{IdtyDid, Planet}, - AccountId, Signature, -}; +use common_runtime::{entities::IdtyName, AccountId, Signature}; use sp_core::{Pair, Public}; use sp_runtime::traits::{IdentifyAccount, Verify}; use std::collections::{BTreeMap, BTreeSet}; @@ -65,12 +62,7 @@ fn clique_wot(initial_identities_len: usize) -> BTreeMap<IdtyIndex, BTreeSet<Idt certs_by_issuer } -/// Create a fake did (for dev and testnet) -fn did(u8_: u8) -> IdtyDid { - IdtyDid { - hash: sp_core::H256::repeat_byte(u8_), - planet: Planet::Earth, - latitude: 0, - longitude: 0, - } +/// Create a fake IdtyName (for dev and testnet) +fn idty_name(u8_: u8) -> IdtyName { + IdtyName(vec![u8_]) } diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs index 34f65b4b09ce05ead44a1db690822f5ea7d1b61f..c94f15e9b4e895fbac82e7fac9d8d350a8f2e4fb 100644 --- a/node/src/chain_spec/gdev.rs +++ b/node/src/chain_spec/gdev.rs @@ -15,10 +15,11 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use super::*; +use common_runtime::entities::IdtyName; use gdev_runtime::{ - AccountId, BalancesConfig, GenesisConfig, GrandpaConfig, IdentityConfig, IdtyDid, IdtyRight, - IdtyValue, StrongCertConfig, SudoConfig, SystemConfig, UdAccountsStorageConfig, - UniversalDividendConfig, WASM_BINARY, + AccountId, BalancesConfig, GenesisConfig, GrandpaConfig, IdentityConfig, IdtyRight, IdtyValue, + StrongCertConfig, SudoConfig, SystemConfig, UdAccountsStorageConfig, UniversalDividendConfig, + WASM_BINARY, }; use maplit::btreemap; use sc_service::ChainType; @@ -57,9 +58,9 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { vec![authority_keys_from_seed("Alice")], // Inital identities btreemap![ - did(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), - did(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), - did(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), + idty_name(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), + idty_name(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), + idty_name(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), ], // Sudo account get_account_id_from_seed::<sr25519::Public>("Alice"), @@ -90,7 +91,7 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { fn devnet_genesis( wasm_binary: &[u8], initial_authorities: Vec<(sp_consensus_aura::sr25519::AuthorityId, GrandpaId)>, - initial_identities: BTreeMap<IdtyDid, AccountId>, + initial_identities: BTreeMap<IdtyName, AccountId>, root_key: AccountId, _enable_println: bool, ) -> gdev_runtime::GenesisConfig { @@ -119,8 +120,8 @@ fn devnet_genesis( identity: IdentityConfig { identities: initial_identities .iter() - .map(|(did, account)| IdtyValue { - did: *did, + .map(|(name, account)| IdtyValue { + name: name.clone(), expire_on: gdev_runtime::MaxInactivityPeriod::get(), owner_key: account.clone(), removable_on: 0, diff --git a/node/src/chain_spec/gtest.rs b/node/src/chain_spec/gtest.rs index 7326f1c78432cd613147932b81c8ffe12c2aaa15..95c58d6bb22f7066266a786d331365fa6c3c790e 100644 --- a/node/src/chain_spec/gtest.rs +++ b/node/src/chain_spec/gtest.rs @@ -15,9 +15,10 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use super::*; +use common_runtime::entities::IdtyName; use gtest_runtime::{ - AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, IdentityConfig, IdtyDid, - IdtyRight, IdtyValue, StrongCertConfig, SudoConfig, SystemConfig, UdAccountsStorageConfig, + AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, IdentityConfig, IdtyRight, + IdtyValue, StrongCertConfig, SudoConfig, SystemConfig, UdAccountsStorageConfig, UniversalDividendConfig, WASM_BINARY, }; use maplit::btreemap; @@ -55,9 +56,9 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> { vec![authority_keys_from_seed("Alice")], // Inital identities btreemap![ - did(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), - did(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), - did(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), + idty_name(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), + idty_name(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), + idty_name(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), ], // Sudo account get_account_id_from_seed::<sr25519::Public>("Alice"), @@ -104,12 +105,12 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> { ], // Initial identities btreemap![ - did(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), - did(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), - did(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), - did(4) => get_account_id_from_seed::<sr25519::Public>("Dave"), - did(5) => get_account_id_from_seed::<sr25519::Public>("Eve"), - did(6) => get_account_id_from_seed::<sr25519::Public>("Ferdie"), + idty_name(1) => get_account_id_from_seed::<sr25519::Public>("Alice"), + idty_name(2) => get_account_id_from_seed::<sr25519::Public>("Bob"), + idty_name(3) => get_account_id_from_seed::<sr25519::Public>("Charlie"), + idty_name(4) => get_account_id_from_seed::<sr25519::Public>("Dave"), + idty_name(5) => get_account_id_from_seed::<sr25519::Public>("Eve"), + idty_name(6) => get_account_id_from_seed::<sr25519::Public>("Ferdie"), ], // Sudo account get_account_id_from_seed::<sr25519::Public>("Alice"), @@ -140,7 +141,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> { fn devnet_genesis( wasm_binary: &[u8], initial_authorities: Vec<(AuraId, GrandpaId)>, - initial_identities: BTreeMap<IdtyDid, AccountId>, + initial_identities: BTreeMap<IdtyName, AccountId>, root_key: AccountId, _enable_println: bool, ) -> GenesisConfig { @@ -169,8 +170,8 @@ fn devnet_genesis( identity: IdentityConfig { identities: initial_identities .iter() - .map(|(did, account)| IdtyValue { - did: *did, + .map(|(name, account)| IdtyValue { + name: name.clone(), expire_on: gtest_runtime::MaxInactivityPeriod::get(), owner_key: account.clone(), removable_on: 0, @@ -202,7 +203,7 @@ fn devnet_genesis( fn testnet_genesis( wasm_binary: &[u8], initial_authorities: Vec<(AuraId, GrandpaId)>, - initial_identities: BTreeMap<IdtyDid, AccountId>, + initial_identities: BTreeMap<IdtyName, AccountId>, root_key: AccountId, _enable_println: bool, ) -> GenesisConfig { @@ -231,8 +232,8 @@ fn testnet_genesis( identity: IdentityConfig { identities: initial_identities .iter() - .map(|(did, account)| IdtyValue { - did: *did, + .map(|(name, account)| IdtyValue { + name: name.clone(), expire_on: gtest_runtime::MaxInactivityPeriod::get(), owner_key: account.clone(), removable_on: 0, diff --git a/pallets/identity/src/lib.rs b/pallets/identity/src/lib.rs index a11aaba472d28ee8d1269798d4b1d4f9b0de90b0..4ab1afa758bb37e7b6608917168f226f54c33536 100644 --- a/pallets/identity/src/lib.rs +++ b/pallets/identity/src/lib.rs @@ -64,8 +64,6 @@ pub mod pallet { type IdtyData: Parameter + Member + MaybeSerializeDeserialize + Debug + Default; /// Identity custom data provider type IdtyDataProvider: ProvideIdtyData<Self>; - /// Identity decentralized identifier - type IdtyDid: IdtyDid; /// A short identity index. type IdtyIndex: Parameter + Member @@ -76,6 +74,8 @@ pub mod pallet { + MaybeSerializeDeserialize + Debug + MaxEncodedLen; + /// Handle logic to validate an identity name + type IdtyNameValidator: IdtyNameValidator; /// Origin allowed to validate identity type IdtyValidationOrigin: EnsureOrigin<Self::Origin>; /// Rights that an identity can have @@ -127,7 +127,7 @@ pub mod pallet { _, Blake2_128Concat, T::IdtyIndex, - IdtyValue<T::AccountId, T::BlockNumber, T::IdtyData, T::IdtyDid, T::IdtyRight>, + IdtyValue<T::AccountId, T::BlockNumber, T::IdtyData, T::IdtyRight>, OptionQuery, >; @@ -135,7 +135,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn identity_by_did)] pub type IdentitiesByDid<T: Config> = - StorageMap<_, Blake2_128Concat, T::IdtyDid, T::IdtyIndex, ValueQuery>; + StorageMap<_, Blake2_128Concat, IdtyName, T::IdtyIndex, ValueQuery>; #[pallet::storage] pub(super) type NextIdtyIndex<T: Config> = StorageValue<_, T::IdtyIndex, ValueQuery>; @@ -164,8 +164,7 @@ pub mod pallet { // GENESIS // #[pallet::genesis_config] pub struct GenesisConfig<T: Config> { - pub identities: - Vec<IdtyValue<T::AccountId, T::BlockNumber, T::IdtyData, T::IdtyDid, T::IdtyRight>>, + pub identities: Vec<IdtyValue<T::AccountId, T::BlockNumber, T::IdtyData, T::IdtyRight>>, } #[cfg(feature = "std")] @@ -180,12 +179,12 @@ pub mod pallet { #[pallet::genesis_build] impl<T: Config> GenesisBuild<T> for GenesisConfig<T> { fn build(&self) { - let mut dids = sp_std::collections::btree_set::BTreeSet::new(); + let mut names = sp_std::collections::btree_set::BTreeSet::new(); for idty_value in &self.identities { assert!( - !dids.contains(&idty_value.did), - "Did {:?} is present twice", - idty_value.did + !names.contains(&idty_value.name), + "Idty name {:?} is present twice", + &idty_value.name ); if idty_value.status == IdtyStatus::Validated { if idty_value.rights.is_empty() { @@ -197,12 +196,12 @@ pub mod pallet { assert!(idty_value.removable_on > T::BlockNumber::zero()); assert!(idty_value.rights.is_empty()) } - dids.insert(idty_value.did); + names.insert(idty_value.name.clone()); } // We need to sort identities to ensure determinisctic result let mut identities = self.identities.clone(); - identities.sort_by(|idty_val_1, idty_val_2| idty_val_1.did.cmp(&idty_val_2.did)); + identities.sort_by(|idty_val_1, idty_val_2| idty_val_1.name.cmp(&idty_val_2.name)); <StorageVersion<T>>::put(Releases::V1_0_0); <IdentitiesCount<T>>::put(self.identities.len() as u64); @@ -241,26 +240,26 @@ pub mod pallet { pub enum Event<T: Config> { /// A new identity has been created /// [idty, owner_key] - IdtyCreated(T::IdtyDid, T::AccountId), + IdtyCreated(IdtyName, T::AccountId), /// An identity has been confirmed by it's owner /// [idty] - IdtyConfirmed(T::IdtyDid), + IdtyConfirmed(IdtyName), /// An identity has been validated /// [idty] - IdtyValidated(T::IdtyDid), + IdtyValidated(IdtyName), /// An identity was renewed by it's owner /// [idty] - IdtyRenewed(T::IdtyDid), + IdtyRenewed(IdtyName), /// An identity has acquired a new right /// [idty, right] - IdtyAcquireRight(T::IdtyDid, T::IdtyRight), + IdtyAcquireRight(IdtyName, T::IdtyRight), /// An identity lost a right /// [idty, righ] - IdtyLostRight(T::IdtyDid, T::IdtyRight), + IdtyLostRight(IdtyName, T::IdtyRight), /// An identity has modified a subkey associated with a right - /// [idty_did, right, old_subkey_opt, new_subkey_opt] + /// [idty_name, right, old_subkey_opt, new_subkey_opt] IdtySetRightSubKey( - T::IdtyDid, + IdtyName, T::IdtyRight, Option<T::AccountId>, Option<T::AccountId>, @@ -278,14 +277,17 @@ pub mod pallet { pub fn create_identity( origin: OriginFor<T>, creator: T::IdtyIndex, - idty_did: T::IdtyDid, + idty_name: IdtyName, owner_key: T::AccountId, ) -> DispatchResultWithPostInfo { - T::EnsureIdtyCallAllowed::can_create_identity(origin, creator, &idty_did, &owner_key)?; + T::EnsureIdtyCallAllowed::can_create_identity(origin, creator, &idty_name, &owner_key)?; + if !T::IdtyNameValidator::validate(&idty_name) { + return Err(Error::<T>::IdtyNameInvalid.into()); + } let idty_data = - T::IdtyDataProvider::provide_identity_data(creator, &idty_did, &owner_key); - if <IdentitiesByDid<T>>::contains_key(&idty_did) { - return Err(Error::<T>::IdtyAlreadyExist.into()); + T::IdtyDataProvider::provide_identity_data(creator, &idty_name, &owner_key); + if <IdentitiesByDid<T>>::contains_key(&idty_name) { + return Err(Error::<T>::IdtyNameAlreadyExist.into()); } let block_number = frame_system::pallet::Pallet::<T>::block_number(); @@ -295,7 +297,7 @@ pub mod pallet { <Identities<T>>::insert( idty_index, IdtyValue { - did: idty_did, + name: idty_name.clone(), expire_on: T::BlockNumber::zero(), owner_key: owner_key.clone(), removable_on, @@ -305,17 +307,17 @@ pub mod pallet { data: idty_data, }, ); - <IdentitiesByDid<T>>::insert(idty_did, idty_index); + <IdentitiesByDid<T>>::insert(idty_name.clone(), idty_index); IdentitiesRemovableOn::<T>::append(removable_on, (idty_index, IdtyStatus::Created)); Self::inc_identities_counter(); - Self::deposit_event(Event::IdtyCreated(idty_did, owner_key)); + Self::deposit_event(Event::IdtyCreated(idty_name, owner_key)); T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Created { creator }); Ok(().into()) } #[pallet::weight(0)] pub fn confirm_identity( origin: OriginFor<T>, - idty_did: T::IdtyDid, + idty_name: IdtyName, idty_index: T::IdtyIndex, ) -> DispatchResultWithPostInfo { let who = ensure_signed(origin)?; @@ -341,7 +343,7 @@ pub mod pallet { removable_on, (idty_index, IdtyStatus::ConfirmedByOwner), ); - Self::deposit_event(Event::IdtyConfirmed(idty_did)); + Self::deposit_event(Event::IdtyConfirmed(idty_name)); T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Confirmed); Ok(().into()) } else { @@ -354,7 +356,7 @@ pub mod pallet { #[pallet::weight(0)] pub fn renew_identity( origin: OriginFor<T>, - idty_did: T::IdtyDid, + idty_name: IdtyName, idty_index: T::IdtyIndex, ) -> DispatchResultWithPostInfo { let who = ensure_signed(origin)?; @@ -379,7 +381,7 @@ pub mod pallet { <Identities<T>>::insert(idty_index, idty_value); IdentitiesExpireOn::<T>::append(expire_on, idty_index); - Self::deposit_event(Event::IdtyRenewed(idty_did)); + Self::deposit_event(Event::IdtyRenewed(idty_name)); if old_status == IdtyStatus::Expired { T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Validated); } @@ -408,14 +410,14 @@ pub mod pallet { let removable_on = block_number + T::MaxNoRightPeriod::get(); idty_value.removable_on = removable_on; idty_value.status = IdtyStatus::Validated; - let did = idty_value.did; + let name = idty_value.name.clone(); <Identities<T>>::insert(idty_index, idty_value); <IdentitiesRemovableOn<T>>::append( removable_on, (idty_index, IdtyStatus::Validated), ); - Self::deposit_event(Event::IdtyValidated(did)); + Self::deposit_event(Event::IdtyValidated(name)); T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Validated); Ok(().into()) } @@ -442,14 +444,14 @@ pub mod pallet { idty_value.removable_on = T::BlockNumber::zero(); idty_value.rights = rights.iter().map(|right| (*right, None)).collect(); idty_value.status = IdtyStatus::Validated; - let did = idty_value.did; + let name = idty_value.name.clone(); let owner_key = idty_value.owner_key.clone(); <Identities<T>>::insert(idty_index, idty_value); - Self::deposit_event(Event::IdtyValidated(did)); + Self::deposit_event(Event::IdtyValidated(name.clone())); T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Validated); for right in rights { - Self::deposit_event(Event::IdtyAcquireRight(did, right)); + Self::deposit_event(Event::IdtyAcquireRight(name.clone(), right)); if right.allow_owner_key() { T::OnRightKeyChange::on_right_key_change( idty_index, @@ -486,7 +488,7 @@ pub mod pallet { .rights .binary_search_by(|(right_, _)| right_.cmp(&right)) { - let did = idty_value.did; + let name = idty_value.name.clone(); let new_key = if right.allow_owner_key() { Some(idty_value.owner_key.clone()) } else { @@ -496,7 +498,7 @@ pub mod pallet { idty_value.removable_on = T::BlockNumber::zero(); idty_value.rights.insert(index, (right, None)); <Identities<T>>::insert(idty_index, idty_value); - Self::deposit_event(Event::<T>::IdtyAcquireRight(did, right)); + Self::deposit_event(Event::<T>::IdtyAcquireRight(name, right)); if new_key.is_some() { T::OnRightKeyChange::on_right_key_change(idty_index, right, None, new_key); } @@ -525,7 +527,7 @@ pub mod pallet { .rights .binary_search_by(|(right_, _)| right_.cmp(&right)) { - let did = idty_value.did; + let name = idty_value.name.clone(); let old_key_opt = if let Some(ref subkey) = idty_value.rights[index].1 { Some(subkey.clone()) } else if right.allow_owner_key() { @@ -546,7 +548,7 @@ pub mod pallet { } <Identities<T>>::insert(idty_index, idty_value); - Self::deposit_event(Event::<T>::IdtyLostRight(did, right)); + Self::deposit_event(Event::<T>::IdtyLostRight(name, right)); if old_key_opt.is_some() { T::OnRightKeyChange::on_right_key_change( idty_index, @@ -582,7 +584,7 @@ pub mod pallet { .rights .binary_search_by(|(right_, _)| right_.cmp(&right)) { - let did = idty_value.did; + let name = idty_value.name.clone(); let old_subkey_opt = idty_value.rights[index].1.clone(); idty_value.rights[index].1 = subkey_opt.clone(); let new_key = if let Some(ref subkey) = subkey_opt { @@ -595,7 +597,7 @@ pub mod pallet { <Identities<T>>::insert(idty_index, idty_value); Self::deposit_event(Event::<T>::IdtySetRightSubKey( - did, + name, right, old_subkey_opt.clone(), subkey_opt, @@ -625,12 +627,14 @@ pub mod pallet { pub enum Error<T> { /// Identity already confirmed IdtyAlreadyConfirmed, - /// Identity already exist - IdtyAlreadyExist, /// Identity already validated IdtyAlreadyValidated, /// You are not allowed to create a new identity now IdtyCreationNotAllowed, + /// Identity name already exist + IdtyNameAlreadyExist, + /// Idty name invalid + IdtyNameInvalid, /// Identity not confirmed by owner IdtyNotConfirmedByOwner, /// Identity not found @@ -720,9 +724,9 @@ pub mod pallet { for (idty_index, idty_status) in identities { if let Ok(idty_val) = <Identities<T>>::try_get(idty_index) { if idty_val.removable_on == block_number && idty_val.status == idty_status { - let did = idty_val.did; + let name = idty_val.name; <Identities<T>>::remove(idty_index); - <IdentitiesByDid<T>>::remove(did); + <IdentitiesByDid<T>>::remove(name); Self::dec_identities_counter(); total_weight += T::OnIdtyChange::on_idty_change(idty_index, IdtyEvent::Removed); diff --git a/pallets/identity/src/mock.rs b/pallets/identity/src/mock.rs index fb6c72fbb7c0f8abbe08ad247a4645108acab358..0dfaf06f01176921df6f7ba54b7f418b1ca8c25a 100644 --- a/pallets/identity/src/mock.rs +++ b/pallets/identity/src/mock.rs @@ -36,24 +36,6 @@ type AccountId = u64; type Block = frame_system::mocking::MockBlock<Test>; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; -#[derive( - Encode, - Decode, - Default, - Clone, - Copy, - PartialEq, - Eq, - PartialOrd, - Ord, - RuntimeDebug, - Deserialize, - Serialize, - TypeInfo, -)] -pub struct IdtyDid(pub u64); -impl pallet_identity::traits::IdtyDid for IdtyDid {} - #[derive( Encode, Decode, @@ -138,6 +120,13 @@ parameter_types! { pub const ValidationPeriod: u64 = 2; } +pub struct IdtyNameValidatorTestImpl; +impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorTestImpl { + fn validate(idty_name: &pallet_identity::IdtyName) -> bool { + idty_name.0.len() == 1 + } +} + impl pallet_identity::Config for Test { type ConfirmPeriod = ConfirmPeriod; type Event = Event; @@ -146,7 +135,7 @@ impl pallet_identity::Config for Test { type EnsureIdtyCallAllowed = (); type IdtyData = (); type IdtyDataProvider = (); - type IdtyDid = IdtyDid; + type IdtyNameValidator = IdtyNameValidatorTestImpl; type IdtyIndex = u64; type IdtyValidationOrigin = system::EnsureRoot<AccountId>; type IdtyRight = IdtyRight; diff --git a/pallets/identity/src/tests.rs b/pallets/identity/src/tests.rs index 3097644910e843c334fc9bb8a2a9d8e6ab778fc5..f0ff67e69c827e212c9ed41d73e362c8d2daf70b 100644 --- a/pallets/identity/src/tests.rs +++ b/pallets/identity/src/tests.rs @@ -14,10 +14,9 @@ // You should have received a copy of the GNU Affero General Public License // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. -use crate::mock::IdtyDid as Did; use crate::mock::IdtyRight as Right; use crate::mock::*; -use crate::Error; +use crate::{Error, IdtyName}; use frame_support::assert_err; use frame_support::assert_ok; use frame_system::{EventRecord, Phase}; @@ -34,7 +33,7 @@ fn test_no_identity() { fn test_two_identities() { let identities = vec![ crate::IdtyValue { - did: Did(0), + name: IdtyName(vec![0]), expire_on: 5, owner_key: 1, removable_on: 0, @@ -44,7 +43,7 @@ fn test_two_identities() { data: (), }, crate::IdtyValue { - did: Did(1), + name: IdtyName(vec![1]), expire_on: 5, owner_key: 2, removable_on: 0, @@ -62,7 +61,7 @@ fn test_two_identities() { // We need to initialize at least one block before any call run_to_block(1); - // Add right Right1 for Did(0) + // Add right Right1 for IdtyName(vec![0]) // Should succes and trigger the correct event assert_ok!(Identity::add_right(Origin::root(), 1, Right::Right1)); let events = System::events(); @@ -71,12 +70,12 @@ fn test_two_identities() { events[0], EventRecord { phase: Phase::Initialization, - event: Event::Identity(crate::Event::IdtyAcquireRight(Did(0), Right::Right1)), + event: Event::Identity(crate::Event::IdtyAcquireRight(IdtyName(vec![0]), Right::Right1)), topics: vec![], } ); - // Add right Right2 for Did(0) - // Should fail because Did(0) already have this right + // Add right Right2 for IdtyName(vec![0]) + // Should fail because IdtyName(vec![0]) already have this right assert_err!( Identity::add_right(Origin::root(), 1, Right::Right2), Error::<Test>::RightAlreadyAdded @@ -84,7 +83,7 @@ fn test_two_identities() { run_to_block(3); - // Delete right Right1 for Did(1) + // Delete right Right1 for IdtyName(vec![1]) // Should succes and trigger the correct event assert_ok!(Identity::del_right(Origin::root(), 2, Right::Right1)); let events = System::events(); @@ -93,12 +92,12 @@ fn test_two_identities() { events[1], EventRecord { phase: Phase::Initialization, - event: Event::Identity(crate::Event::IdtyLostRight(Did(1), Right::Right1)), + event: Event::Identity(crate::Event::IdtyLostRight(IdtyName(vec![1]), Right::Right1)), topics: vec![], } ); - // The Did(1) identity has no more rights, the inactivity period must start to run + // The IdtyName(vec![1]) identity has no more rights, the inactivity period must start to run let idty2 = Identity::identity(2).expect("idty not found"); assert!(idty2.rights.is_empty()); assert_eq!(idty2.removable_on, 7); diff --git a/pallets/identity/src/traits.rs b/pallets/identity/src/traits.rs index 0550946da779fac236c26f134f2ab437b7132d3e..44862c5e4e27ebac430728cffccdd3aa4cf31922 100644 --- a/pallets/identity/src/traits.rs +++ b/pallets/identity/src/traits.rs @@ -24,7 +24,7 @@ pub trait EnsureIdtyCallAllowed<T: Config> { fn can_create_identity( origin: T::Origin, creator: T::IdtyIndex, - idty_did: &T::IdtyDid, + idty_name: &IdtyName, idty_owner_key: &T::AccountId, ) -> Result<(), DispatchError>; } @@ -33,7 +33,7 @@ impl<T: Config> EnsureIdtyCallAllowed<T> for () { fn can_create_identity( origin: T::Origin, _creator: T::IdtyIndex, - _idty_did: &T::IdtyDid, + _idty_name: &IdtyName, _idty_owner_key: &T::AccountId, ) -> Result<(), DispatchError> { match ensure_root(origin) { @@ -46,7 +46,7 @@ impl<T: Config> EnsureIdtyCallAllowed<T> for () { pub trait ProvideIdtyData<T: Config> { fn provide_identity_data( creator: T::IdtyIndex, - idty_did: &T::IdtyDid, + idty_name: &IdtyName, idty_owner_key: &T::AccountId, ) -> T::IdtyData; } @@ -57,22 +57,15 @@ where { fn provide_identity_data( _creator: T::IdtyIndex, - _idty_did: &T::IdtyDid, + _idty_name: &IdtyName, _idty_owner_key: &T::AccountId, ) -> T::IdtyData { Default::default() } } -pub trait IdtyDid: - frame_support::Parameter - + frame_support::pallet_prelude::Member - + MaybeSerializeDeserialize - + Debug - + Default - + Copy - + Ord -{ +pub trait IdtyNameValidator { + fn validate(idty_name: &IdtyName) -> bool; } pub trait IdtyRight: diff --git a/pallets/identity/src/types.rs b/pallets/identity/src/types.rs index 420c912a080e70f9dc6f377148c674a6d5ee889a..f1b361b36608a380961d6e0093499847996fbe69 100644 --- a/pallets/identity/src/types.rs +++ b/pallets/identity/src/types.rs @@ -23,6 +23,12 @@ use scale_info::TypeInfo; use serde::{Deserialize, Serialize}; use sp_std::vec::Vec; +#[cfg_attr(feature = "std", derive(Deserialize, Serialize))] +#[derive( + Encode, Decode, Default, Clone, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, TypeInfo, +)] +pub struct IdtyName(pub sp_std::vec::Vec<u8>); + #[cfg_attr(feature = "std", derive(Deserialize, Serialize))] #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)] pub enum IdtyStatus { @@ -43,10 +49,9 @@ pub struct IdtyValue< AccountId: Decode + Encode + TypeInfo, BlockNumber: Decode + Encode + TypeInfo, IdtyData: Decode + Encode + TypeInfo, - IdtyDid: Decode + Encode + TypeInfo, IdtyRight: Decode + Encode + TypeInfo, > { - pub did: IdtyDid, + pub name: IdtyName, pub expire_on: BlockNumber, pub owner_key: AccountId, pub removable_on: BlockNumber, @@ -56,13 +61,12 @@ pub struct IdtyValue< pub data: IdtyData, } -impl<AccountId, BlockNumber, IdtyData, IdtyDid, IdtyRight> - IdtyValue<AccountId, BlockNumber, IdtyData, IdtyDid, IdtyRight> +impl<AccountId, BlockNumber, IdtyData, IdtyRight> + IdtyValue<AccountId, BlockNumber, IdtyData, IdtyRight> where AccountId: Clone + Decode + Encode + TypeInfo, BlockNumber: Decode + Encode + TypeInfo, IdtyData: Decode + Encode + TypeInfo, - IdtyDid: Decode + Encode + TypeInfo, IdtyRight: crate::traits::IdtyRight + Decode + Encode + TypeInfo, { pub fn get_right_key(&self, right: IdtyRight) -> Option<AccountId> { diff --git a/runtime/common/src/authorizations.rs b/runtime/common/src/authorizations.rs index f4c28688218cae76c512928f86fef841fff57df9..febd81ecc096add7a392aa38069acc3101e3f2cd 100644 --- a/runtime/common/src/authorizations.rs +++ b/runtime/common/src/authorizations.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. -use crate::entities::{IdtyData, IdtyDid, IdtyRight}; +use crate::entities::{IdtyData, IdtyRight}; use crate::{BlockNumber, IdtyIndex}; use frame_support::pallet_prelude::DispatchError; use frame_support::traits::EnsureOrigin; @@ -28,7 +28,6 @@ impl< Runtime: frame_system::Config<BlockNumber = BlockNumber> + pallet_identity::Config< IdtyData = IdtyData, - IdtyDid = IdtyDid, IdtyIndex = IdtyIndex, IdtyRight = IdtyRight, >, @@ -39,7 +38,7 @@ impl< fn can_create_identity( origin: Runtime::Origin, creator: IdtyIndex, - _idty_did: &IdtyDid, + _idty_name: &pallet_identity::IdtyName, _idty_owner_key: &Runtime::AccountId, ) -> Result<(), DispatchError> { match origin.into() { diff --git a/runtime/common/src/entities.rs b/runtime/common/src/entities.rs index 877ab4dab43e290f9a9c807947b54c563fe0edf6..5cd00143d5171d14cdb50f7ea8227e9343a782c2 100644 --- a/runtime/common/src/entities.rs +++ b/runtime/common/src/entities.rs @@ -14,12 +14,13 @@ // You should have received a copy of the GNU Affero General Public License // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. +pub use pallet_identity::IdtyName; + use crate::BlockNumber; use frame_support::pallet_prelude::*; use scale_info::TypeInfo; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; -use sp_core::H256; #[cfg_attr(feature = "std", derive(Deserialize, Serialize))] #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, TypeInfo)] @@ -47,61 +48,8 @@ impl pallet_identity::traits::IdtyRight for IdtyRight { } } -#[cfg_attr(feature = "std", derive(Deserialize, Serialize))] -#[derive(Encode, Decode, Default, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)] -pub struct IdtyDid { - pub hash: H256, - pub planet: Planet, - pub latitude: u32, - pub longitude: u32, -} -impl PartialOrd for IdtyDid { - fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { - match self.hash.partial_cmp(&other.hash) { - Some(core::cmp::Ordering::Equal) => match self.planet.partial_cmp(&other.planet) { - Some(core::cmp::Ordering::Equal) => { - match self.latitude.partial_cmp(&other.latitude) { - Some(core::cmp::Ordering::Equal) => { - self.longitude.partial_cmp(&other.longitude) - } - o => o, - } - } - o => o, - }, - o => o, - } - } -} -impl Ord for IdtyDid { - fn cmp(&self, other: &Self) -> core::cmp::Ordering { - match self.hash.cmp(&other.hash) { - core::cmp::Ordering::Equal => match self.planet.cmp(&other.planet) { - core::cmp::Ordering::Equal => match self.latitude.cmp(&other.latitude) { - core::cmp::Ordering::Equal => self.longitude.cmp(&other.longitude), - o => o, - }, - o => o, - }, - o => o, - } - } -} -impl pallet_identity::traits::IdtyDid for IdtyDid {} - #[cfg_attr(feature = "std", derive(Deserialize, Serialize))] #[derive(Encode, Decode, Default, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)] pub struct IdtyData { pub can_create_on: BlockNumber, } - -#[cfg_attr(feature = "std", derive(Deserialize, Serialize))] -#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, TypeInfo)] -pub enum Planet { - Earth, -} -impl Default for Planet { - fn default() -> Self { - Self::Earth - } -} diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index d1bde64f5cbbdaee5e9a9dfc2f3a0caa676b1081..795795b6d85142bf0bc0fb2d98e2ccc8bc97c986 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -60,3 +60,10 @@ pub type Index = u32; /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = sp_runtime::MultiSignature; + +pub struct IdtyNameValidatorImpl; +impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorImpl { + fn validate(idty_name: &pallet_identity::IdtyName) -> bool { + idty_name.0.len() <= 64 + } +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index c57a126ca6dc8b8ef81816ff466b5df57dd9903a..19c61e495ec5cf380045475ba1aef8feafe78a82 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -188,7 +188,7 @@ macro_rules! pallets_config { type EnsureIdtyCallAllowed = EnsureIdtyCallAllowedImpl<Runtime, StrongCert>; type IdtyDataProvider = IdtyDataProvider<Runtime, IDTY_CREATE_PERIOD>; type IdtyData = IdtyData; - type IdtyDid = IdtyDid; + type IdtyNameValidator = IdtyNameValidatorImpl; type IdtyIndex = IdtyIndex; type IdtyValidationOrigin = EnsureRoot<Self::AccountId>; type IdtyRight = IdtyRight; diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index 73ba6e9bf5e9b7c50a508925337a158b49c186f0..a48d9782e4d7af6349c952d6810ce093eb4791ea 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. -use crate::entities::{IdtyData, IdtyDid}; +use crate::entities::IdtyData; use crate::{AccountId, BlockNumber, IdtyIndex}; use frame_support::traits::Get; use sp_std::vec::Vec; @@ -27,11 +27,11 @@ impl<Runtime, const IDTY_CREATE_PERIOD: BlockNumber> for IdtyDataProvider<Runtime, IDTY_CREATE_PERIOD> where Runtime: frame_system::Config<AccountId = AccountId, BlockNumber = BlockNumber> - + pallet_identity::Config<IdtyData = IdtyData, IdtyDid = IdtyDid, IdtyIndex = IdtyIndex>, + + pallet_identity::Config<IdtyData = IdtyData, IdtyIndex = IdtyIndex>, { fn provide_identity_data( creator: IdtyIndex, - _idty_did: &IdtyDid, + _idty_name: &pallet_identity::IdtyName, _idty_owner_key: &AccountId, ) -> IdtyData { let block_number = frame_system::Pallet::<Runtime>::block_number(); diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index 1f6242d85e19333e1d5f0ae36d587f8dd0bb8e17..65824a34dc1d63462a1bedb1eb18630f10b04eec 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -27,8 +27,9 @@ mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, - entities::{IdtyData, IdtyDid, IdtyRight, Planet}, - AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, Index, Signature, + entities::{IdtyData, IdtyRight}, + AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, IdtyNameValidatorImpl, + Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue}; diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index a6c6f192db5cb8a36daa6c70fad7c596f35c82ef..e5bec7845fd68bc5e13372e9892a3c286030aa36 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -27,8 +27,9 @@ mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, - entities::{IdtyData, IdtyDid, IdtyRight, Planet}, - AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, Index, Signature, + entities::{IdtyData, IdtyRight}, + AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, IdtyNameValidatorImpl, + Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue}; diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs index e20e00ab14b6081f97dfe3dc9d2af7483fc1b0c1..152a27aafc65c96207ade046616c93f93653e2ee 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -27,8 +27,9 @@ mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, - entities::{IdtyData, IdtyDid, IdtyRight, Planet}, - AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, Index, Signature, + entities::{IdtyData, IdtyRight}, + AccountId, Address, Balance, BlockNumber, Hash, Header, IdtyIndex, IdtyNameValidatorImpl, + Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue};