Skip to content

Commit

Permalink
Update Identity pallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Jan 15, 2025
1 parent 297b4ab commit 316fceb
Show file tree
Hide file tree
Showing 21 changed files with 586 additions and 400 deletions.
3 changes: 1 addition & 2 deletions pallets/contracts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use codec::Encode;
use frame_benchmarking::{account, benchmarks};
use frame_support::StorageMap;
use frame_support::{storage::unhashed, traits::tokens::currency::Currency};
use frame_system::{Pallet as System, RawOrigin};
use pallet_contracts::benchmarking::code::body::DynInstr::{Counter, Regular};
Expand Down Expand Up @@ -71,7 +70,7 @@ where
// Check if contact is already linked.
match IdentityPallet::<T>::get_identity(&contract) {
Some(contract_did) => {
if contract_did != did && ParentDid::get(contract_did) != Some(did) {
if contract_did != did && ParentDid::<T>::get(contract_did) != Some(did) {
// Contract address already linked to a different identity.
Err(IdentityError::<T>::AlreadyLinked.into())
} else {
Expand Down
3 changes: 1 addition & 2 deletions pallets/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ use frame_support::ensure;
use frame_support::pallet_prelude::MaxEncodedLen;
use frame_support::traits::Get;
use frame_support::weights::Weight;
use frame_support::StorageMap as _;
use frame_system::ensure_root;
use frame_system::ensure_signed;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -194,7 +193,7 @@ where
// Check if contact is already linked.
match IdentityPallet::<T>::get_identity(&contract) {
Some(contract_did) => {
if contract_did != did && ParentDid::get(contract_did) != Some(did) {
if contract_did != did && ParentDid::<T>::get(contract_did) != Some(did) {
// Contract address already linked to a different identity.
Err(IdentityError::<T>::AlreadyLinked.into())
} else {
Expand Down
40 changes: 20 additions & 20 deletions pallets/identity/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::{
AuthorizationType, Authorizations, AuthorizationsGiven, Config, CurrentAuthId, Error,
KeyRecords, NumberOfGivenAuths, Pallet, RawEvent,
AuthorizationType, Authorizations, AuthorizationsGiven, Config, CurrentAuthId, Error, Event,
KeyRecords, NumberOfGivenAuths, Pallet,
};
use frame_support::dispatch::DispatchResult;
use frame_support::{ensure, StorageDoubleMap, StorageMap, StorageValue};
use frame_support::ensure;
use frame_system::ensure_signed;
use polymesh_primitives::{
Authorization, AuthorizationData, AuthorizationError, IdentityId, Signatory,
Expand Down Expand Up @@ -56,15 +56,15 @@ impl<T: Config> Pallet<T> {
authorization_data: AuthorizationData<T::AccountId>,
expiry: Option<T::Moment>,
) -> Result<u64, DispatchError> {
let number_of_given_auths = NumberOfGivenAuths::get(from);
let number_of_given_auths = NumberOfGivenAuths::<T>::get(from);
ensure!(
number_of_given_auths < T::MaxGivenAuths::get(),
Error::<T>::ExceededNumberOfGivenAuths
);
NumberOfGivenAuths::insert(from, number_of_given_auths.saturating_add(1));
NumberOfGivenAuths::<T>::insert(from, number_of_given_auths.saturating_add(1));

let new_auth_id = Self::current_auth_id().saturating_add(1);
CurrentAuthId::put(new_auth_id);
CurrentAuthId::<T>::put(new_auth_id);

let auth = Authorization {
authorization_data: authorization_data.clone(),
Expand All @@ -74,11 +74,11 @@ impl<T: Config> Pallet<T> {
count: 50,
};

<Authorizations<T>>::insert(target.clone(), new_auth_id, auth);
<AuthorizationsGiven<T>>::insert(from, new_auth_id, target.clone());
Authorizations::<T>::insert(target.clone(), new_auth_id, auth);
AuthorizationsGiven::<T>::insert(from, new_auth_id, target.clone());

// This event is split in order to help the event harvesters.
Self::deposit_event(RawEvent::AuthorizationAdded(
Self::deposit_event(Event::AuthorizationAdded(
from,
target.as_identity().cloned(),
target.as_account().cloned(),
Expand All @@ -97,7 +97,7 @@ impl<T: Config> Pallet<T> {
auth_id: u64,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let from_did = if <KeyRecords<T>>::contains_key(&sender) {
let from_did = if KeyRecords::<T>::contains_key(&sender) {
// If the sender is linked to an identity, ensure that it has relevant permissions
Some(pallet_permissions::Pallet::<T>::ensure_call_permissions(&sender)?.primary_did)
} else {
Expand All @@ -122,17 +122,17 @@ impl<T: Config> Pallet<T> {
authorizer: &IdentityId,
revoked: bool,
) {
<Authorizations<T>>::remove(target, auth_id);
<AuthorizationsGiven<T>>::remove(authorizer, auth_id);
NumberOfGivenAuths::mutate(authorizer, |number_of_given_auths| {
Authorizations::<T>::remove(target, auth_id);
AuthorizationsGiven::<T>::remove(authorizer, auth_id);
NumberOfGivenAuths::<T>::mutate(authorizer, |number_of_given_auths| {
*number_of_given_auths = number_of_given_auths.saturating_sub(1);
});
let id = target.as_identity().cloned();
let acc = target.as_account().cloned();
let event = if revoked {
RawEvent::AuthorizationRevoked
Event::AuthorizationRevoked
} else {
RawEvent::AuthorizationRejected
Event::AuthorizationRejected
};
Self::deposit_event(event(id, acc, auth_id))
}
Expand All @@ -147,7 +147,7 @@ impl<T: Config> Pallet<T> {
auth_type: Option<AuthorizationType>,
) -> Vec<Authorization<T::AccountId, T::Moment>> {
let now = <pallet_timestamp::Pallet<T>>::get();
let auths = <Authorizations<T>>::iter_prefix_values(signatory)
let auths = Authorizations::<T>::iter_prefix_values(signatory)
.filter(|auth| allow_expired || auth.expiry.filter(|&e| e < now).is_none());
if let Some(auth_type) = auth_type {
auths
Expand Down Expand Up @@ -197,12 +197,12 @@ impl<T: Config> Pallet<T> {
accepter(auth.authorization_data.clone(), auth.authorized_by)?;

// Remove authorization from storage and emit event.
<Authorizations<T>>::remove(&target, auth_id);
<AuthorizationsGiven<T>>::remove(auth.authorized_by, auth_id);
NumberOfGivenAuths::mutate(auth.authorized_by, |number_of_given_auths| {
Authorizations::<T>::remove(&target, auth_id);
AuthorizationsGiven::<T>::remove(auth.authorized_by, auth_id);
NumberOfGivenAuths::<T>::mutate(auth.authorized_by, |number_of_given_auths| {
*number_of_given_auths = number_of_given_auths.saturating_sub(1);
});
Self::deposit_event(RawEvent::AuthorizationConsumed(
Self::deposit_event(Event::AuthorizationConsumed(
target.as_identity().cloned(),
target.as_account().cloned(),
auth_id,
Expand Down
30 changes: 15 additions & 15 deletions pallets/identity/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

use crate::{
Claim1stKey, Claim2ndKey, Claims, Config, CustomClaimIdSequence, CustomClaims,
CustomClaimsInverse, DidRecords, Error, Event, Pallet, ParentDid, RawEvent,
CustomClaimsInverse, DidRecords, Error, Event, Pallet, ParentDid,
};
use frame_support::{
dispatch::{DispatchError, DispatchResult},
ensure, StorageDoubleMap, StorageMap, StorageValue,
ensure,
};
use frame_system::ensure_root;
use pallet_base::{ensure_string_limited, try_next_pre};
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<T: Config> Pallet<T> {
// we will initialize the `parent_claims` iterator.
if first_call {
first_call = false;
parent_claims = ParentDid::get(did).map(|parent_did| {
parent_claims = ParentDid::<T>::get(did).map(|parent_did| {
Self::fetch_base_claims(parent_did, ClaimType::CustomerDueDiligence)
});
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<T: Config> Pallet<T> {
target: IdentityId,
claim_type: ClaimType,
) -> impl Iterator<Item = IdentityClaim> + 'a {
Claims::iter_prefix_values(Claim1stKey { target, claim_type })
Claims::<T>::iter_prefix_values(Claim1stKey { target, claim_type })
}

/// It fetches an specific `claim_type` claim type for target identity `id`, which was issued
Expand All @@ -252,7 +252,7 @@ impl<T: Config> Pallet<T> {
) -> Option<IdentityClaim> {
let pk = Claim1stKey { target, claim_type };
let sk = Claim2ndKey { issuer, scope };
Claims::get(&pk, &sk)
Claims::<T>::get(&pk, &sk)
}

/// It adds a new claim without any previous security check.
Expand All @@ -265,7 +265,7 @@ impl<T: Config> Pallet<T> {
let inner_scope = claim.as_scope().cloned();
if let ClaimType::Custom(id) = claim.claim_type() {
ensure!(
CustomClaims::contains_key(id),
CustomClaims::<T>::contains_key(id),
Error::<T>::CustomClaimTypeDoesNotExist
);
}
Expand Down Expand Up @@ -296,8 +296,8 @@ impl<T: Config> Pallet<T> {
claim,
};

Claims::insert(&pk, &sk, id_claim.clone());
Self::deposit_event(RawEvent::ClaimAdded(target, id_claim));
Claims::<T>::insert(&pk, &sk, id_claim.clone());
Self::deposit_event(Event::ClaimAdded(target, id_claim));
}

/// Returns claim keys.
Expand Down Expand Up @@ -336,9 +336,9 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let (pk, sk) = Self::get_claim_keys(target, claim_type, issuer, scope);
// Remove the claim.
let claim = Claims::take(&pk, &sk).ok_or(Error::<T>::ClaimDoesNotExist)?;
let claim = Claims::<T>::take(&pk, &sk).ok_or(Error::<T>::ClaimDoesNotExist)?;
// Emit claim revoked event.
Self::deposit_event(RawEvent::ClaimRevoked(target, claim));
Self::deposit_event(Event::ClaimRevoked(target, claim));
Ok(())
}

Expand Down Expand Up @@ -422,7 +422,7 @@ impl<T: Config> Pallet<T> {
);

T::CddServiceProviders::disable_member(cdd, expiry, Some(disable_from))?;
Self::deposit_event(RawEvent::CddClaimsInvalidated(cdd, disable_from));
Self::deposit_event(Event::CddClaimsInvalidated(cdd, disable_from));
Ok(())
}

Expand Down Expand Up @@ -459,13 +459,13 @@ impl<T: Config> Pallet<T> {
fn unsafe_register_custom_claim_type(ty: Vec<u8>) -> Result<CustomClaimTypeId, DispatchError> {
ensure_string_limited::<T>(&ty)?;
ensure!(
!CustomClaimsInverse::contains_key(&ty),
!CustomClaimsInverse::<T>::contains_key(&ty),
Error::<T>::CustomClaimTypeAlreadyExists
);

let id = CustomClaimIdSequence::try_mutate(try_next_pre::<T, _>)?;
CustomClaimsInverse::insert(&ty, id);
CustomClaims::insert(id, ty);
let id = CustomClaimIdSequence::<T>::try_mutate(try_next_pre::<T, _>)?;
CustomClaimsInverse::<T>::insert(&ty, id);
CustomClaims::<T>::insert(id, ty);
Ok(id)
}

Expand Down
Loading

0 comments on commit 316fceb

Please sign in to comment.