diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index e0056bb56..bd228dadf 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -200,7 +200,7 @@ pub mod pallet { /// delegation hierarchy root. CTypeMismatch, /// The call origin is not authorized to change the attestation. - Unauthorized, + NotAuthorized, /// The maximum number of delegated attestations has already been /// reached for the corresponding delegation id such that another one /// cannot be added. @@ -320,8 +320,8 @@ pub mod pallet { ensure!(!attestation.revoked, Error::::AlreadyRevoked); if attestation.attester != who { - let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; - authorization.ok_or(Error::::Unauthorized)?.can_revoke( + let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::NotAuthorized)?; + authorization.ok_or(Error::::NotAuthorized)?.can_revoke( &who, &attestation.ctype_hash, &claim_hash, @@ -377,8 +377,8 @@ pub mod pallet { let attestation = Attestations::::get(claim_hash).ok_or(Error::::NotFound)?; if attestation.attester != who { - let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; - authorization.ok_or(Error::::Unauthorized)?.can_remove( + let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::NotAuthorized)?; + authorization.ok_or(Error::::NotAuthorized)?.can_remove( &who, &attestation.ctype_hash, &claim_hash, @@ -410,7 +410,7 @@ pub mod pallet { let who = ensure_signed(origin)?; let attestation = Attestations::::get(claim_hash).ok_or(Error::::NotFound)?; - ensure!(attestation.deposit.owner == who, Error::::Unauthorized); + ensure!(attestation.deposit.owner == who, Error::::NotAuthorized); // *** No Fail beyond this point *** @@ -436,7 +436,7 @@ pub mod pallet { let sender = source.sender(); let attestation = Attestations::::get(claim_hash).ok_or(Error::::NotFound)?; - ensure!(attestation.attester == subject, Error::::Unauthorized); + ensure!(attestation.attester == subject, Error::::NotAuthorized); AttestationStorageDepositCollector::::change_deposit_owner(&claim_hash, sender)?; @@ -451,7 +451,7 @@ pub mod pallet { let sender = ensure_signed(origin)?; let attestation = Attestations::::get(claim_hash).ok_or(Error::::NotFound)?; - ensure!(attestation.deposit.owner == sender, Error::::Unauthorized); + ensure!(attestation.deposit.owner == sender, Error::::NotAuthorized); AttestationStorageDepositCollector::::update_deposit(&claim_hash)?; diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 9968e6950..661f74877 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -374,7 +374,7 @@ fn test_remove_unauthorized() { claim_hash, authorization_info ), - attestation::Error::::Unauthorized + attestation::Error::::NotAuthorized ); }); } @@ -465,7 +465,7 @@ fn test_reclaim_unauthorized() { .execute_with(|| { assert_noop!( Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash), - attestation::Error::::Unauthorized, + attestation::Error::::NotAuthorized, ); }); } @@ -564,7 +564,7 @@ fn test_change_deposit_owner_unauthorized() { .execute_with(|| { assert_noop!( Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, evil_actor).into(), claim_hash), - attestation::Error::::Unauthorized, + attestation::Error::::NotAuthorized, ); }); } @@ -650,7 +650,7 @@ fn test_update_deposit_unauthorized() { ); assert_noop!( Attestation::update_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash), - Error::::Unauthorized + Error::::NotAuthorized ); }); } diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index a9be185a7..e7c394775 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -31,7 +31,7 @@ use sp_std::{convert::TryInto, vec::Vec}; use kilt_support::deposit::Deposit; use crate::{ - errors::{DidError, InputError, SignatureError, StorageError}, + errors::{Error, Input, Signature, Storage}, service_endpoints::DidEndpoint, utils, AccountIdOf, BalanceOf, BlockNumberOf, Config, DidCallableOf, DidIdentifierOf, KeyIdOf, Payload, }; @@ -49,22 +49,22 @@ pub enum DidVerificationKey { impl DidVerificationKey { /// Verify a DID signature using one of the DID keys. - pub fn verify_signature(&self, payload: &Payload, signature: &DidSignature) -> Result<(), SignatureError> { + pub fn verify_signature(&self, payload: &Payload, signature: &DidSignature) -> Result<(), Signature> { match (self, signature) { (DidVerificationKey::Ed25519(public_key), DidSignature::Ed25519(sig)) => { - ensure!(sig.verify(payload, public_key), SignatureError::InvalidSignature); + ensure!(sig.verify(payload, public_key), Signature::InvalidSignature); Ok(()) } // Follows same process as above, but using a Sr25519 instead (DidVerificationKey::Sr25519(public_key), DidSignature::Sr25519(sig)) => { - ensure!(sig.verify(payload, public_key), SignatureError::InvalidSignature); + ensure!(sig.verify(payload, public_key), Signature::InvalidSignature); Ok(()) } (DidVerificationKey::Ecdsa(public_key), DidSignature::Ecdsa(sig)) => { - ensure!(sig.verify(payload, public_key), SignatureError::InvalidSignature); + ensure!(sig.verify(payload, public_key), Signature::InvalidSignature); Ok(()) } - _ => Err(SignatureError::InvalidSignatureFormat), + _ => Err(Signature::InvalidSignatureFormat), } } } @@ -176,7 +176,7 @@ pub trait DidVerifiableIdentifier { &self, payload: &Payload, signature: &DidSignature, - ) -> Result; + ) -> Result; } impl> DidVerifiableIdentifier for I { @@ -184,7 +184,7 @@ impl> DidVerifiableIdentifier for I { &self, payload: &Payload, signature: &DidSignature, - ) -> Result { + ) -> Result { // So far, either the raw Ed25519/Sr25519 public key or the Blake2-256 hashed // ECDSA public key. let raw_public_key: &[u8; 32] = self.as_ref(); @@ -207,17 +207,17 @@ impl> DidVerifiableIdentifier for I { let ecdsa_signature: [u8; 65] = signature .encode() .try_into() - .map_err(|_| SignatureError::InvalidSignature)?; + .map_err(|_| Signature::InvalidSignature)?; // ECDSA uses blake2-256 hashing algorithm for signatures, so we hash the given // message to recover the public key. let hashed_message = sp_io::hashing::blake2_256(payload); let recovered_pk: [u8; 33] = sp_io::crypto::secp256k1_ecdsa_recover_compressed(&ecdsa_signature, &hashed_message) - .map_err(|_| SignatureError::InvalidSignature)?; + .map_err(|_| Signature::InvalidSignature)?; let hashed_recovered_pk = sp_io::hashing::blake2_256(&recovered_pk); // The hashed recovered public key must be equal to the AccountId32 value, which // is the hashed key. - ensure!(&hashed_recovered_pk == raw_public_key, SignatureError::InvalidSignature); + ensure!(&hashed_recovered_pk == raw_public_key, Signature::InvalidSignature); // Safe to reconstruct the public key using the recovered value from // secp256k1_ecdsa_recover_compressed Ok(DidVerificationKey::from(ecdsa::Public(recovered_pk))) @@ -287,7 +287,7 @@ impl DidDetails { authentication_key: DidVerificationKey, block_number: BlockNumberOf, deposit: Deposit, BalanceOf>, - ) -> Result { + ) -> Result { let mut public_keys = DidPublicKeyMap::::default(); let authentication_key_id = utils::calculate_key_id::(&authentication_key.clone().into()); public_keys @@ -298,7 +298,7 @@ impl DidDetails { block_number, }, ) - .map_err(|_| StorageError::MaxPublicKeysExceeded)?; + .map_err(|_| Storage::MaxPublicKeysExceeded)?; Ok(Self { authentication_key: authentication_key_id, key_agreement_keys: DidKeyAgreementKeySet::::default(), @@ -315,11 +315,11 @@ impl DidDetails { pub fn from_creation_details( details: DidCreationDetails, new_auth_key: DidVerificationKey, - ) -> Result { + ) -> Result { ensure!( details.new_key_agreement_keys.len() <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), - InputError::MaxKeyAgreementKeysLimitExceeded + Input::MaxKeyAgreementKeysLimitExceeded ); let current_block_number = frame_system::Pallet::::block_number(); @@ -354,7 +354,7 @@ impl DidDetails { &mut self, new_authentication_key: DidVerificationKey, block_number: BlockNumberOf, - ) -> Result<(), StorageError> { + ) -> Result<(), Storage> { let old_authentication_key_id = self.authentication_key; let new_authentication_key_id = utils::calculate_key_id::(&new_authentication_key.clone().into()); self.authentication_key = new_authentication_key_id; @@ -370,7 +370,7 @@ impl DidDetails { block_number, }, ) - .map_err(|_| StorageError::MaxPublicKeysExceeded)?; + .map_err(|_| Storage::MaxPublicKeysExceeded)?; Ok(()) } @@ -381,7 +381,7 @@ impl DidDetails { &mut self, new_key_agreement_keys: DidNewKeyAgreementKeySet, block_number: BlockNumberOf, - ) -> Result<(), StorageError> { + ) -> Result<(), Storage> { for new_key_agreement_key in new_key_agreement_keys { self.add_key_agreement_key(new_key_agreement_key, block_number)?; } @@ -395,7 +395,7 @@ impl DidDetails { &mut self, new_key_agreement_key: DidEncryptionKey, block_number: BlockNumberOf, - ) -> Result<(), StorageError> { + ) -> Result<(), Storage> { let new_key_agreement_id = utils::calculate_key_id::(&new_key_agreement_key.into()); self.public_keys .try_insert( @@ -405,17 +405,17 @@ impl DidDetails { block_number, }, ) - .map_err(|_| StorageError::MaxPublicKeysExceeded)?; + .map_err(|_| Storage::MaxPublicKeysExceeded)?; self.key_agreement_keys .try_insert(new_key_agreement_id) - .map_err(|_| StorageError::MaxTotalKeyAgreementKeysExceeded)?; + .map_err(|_| Storage::MaxTotalKeyAgreementKeysExceeded)?; Ok(()) } /// Remove a key agreement key from both the set of key agreement keys and /// the one of public keys. - pub fn remove_key_agreement_key(&mut self, key_id: KeyIdOf) -> Result<(), StorageError> { - ensure!(self.key_agreement_keys.remove(&key_id), StorageError::KeyNotFound); + pub fn remove_key_agreement_key(&mut self, key_id: KeyIdOf) -> Result<(), Storage> { + ensure!(self.key_agreement_keys.remove(&key_id), Storage::KeyNotFound); self.remove_key_if_unused(key_id); Ok(()) } @@ -429,7 +429,7 @@ impl DidDetails { &mut self, new_attestation_key: DidVerificationKey, block_number: BlockNumberOf, - ) -> Result<(), StorageError> { + ) -> Result<(), Storage> { let new_attestation_key_id = utils::calculate_key_id::(&new_attestation_key.clone().into()); if let Some(old_attestation_key_id) = self.attestation_key.take() { self.remove_key_if_unused(old_attestation_key_id); @@ -443,7 +443,7 @@ impl DidDetails { block_number, }, ) - .map_err(|_| StorageError::MaxPublicKeysExceeded)?; + .map_err(|_| Storage::MaxPublicKeysExceeded)?; Ok(()) } @@ -452,8 +452,8 @@ impl DidDetails { /// The old key is deleted from the set of public keys if it is /// not used in any other part of the DID. The new key is added to the /// set of public keys. - pub fn remove_attestation_key(&mut self) -> Result<(), StorageError> { - let old_key_id = self.attestation_key.take().ok_or(StorageError::KeyNotFound)?; + pub fn remove_attestation_key(&mut self) -> Result<(), Storage> { + let old_key_id = self.attestation_key.take().ok_or(Storage::KeyNotFound)?; self.remove_key_if_unused(old_key_id); Ok(()) } @@ -467,7 +467,7 @@ impl DidDetails { &mut self, new_delegation_key: DidVerificationKey, block_number: BlockNumberOf, - ) -> Result<(), StorageError> { + ) -> Result<(), Storage> { let new_delegation_key_id = utils::calculate_key_id::(&new_delegation_key.clone().into()); if let Some(old_delegation_key_id) = self.delegation_key.take() { self.remove_key_if_unused(old_delegation_key_id); @@ -481,7 +481,7 @@ impl DidDetails { block_number, }, ) - .map_err(|_| StorageError::MaxPublicKeysExceeded)?; + .map_err(|_| Storage::MaxPublicKeysExceeded)?; Ok(()) } @@ -490,8 +490,8 @@ impl DidDetails { /// The old key is deleted from the set of public keys if it is /// not used in any other part of the DID. The new key is added to the /// set of public keys. - pub fn remove_delegation_key(&mut self) -> Result<(), StorageError> { - let old_key_id = self.delegation_key.take().ok_or(StorageError::KeyNotFound)?; + pub fn remove_delegation_key(&mut self) -> Result<(), Storage> { + let old_key_id = self.delegation_key.take().ok_or(Storage::KeyNotFound)?; self.remove_key_if_unused(old_key_id); Ok(()) } diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index 2bfcd0550..bc9a8f3c9 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -22,32 +22,32 @@ use crate::did_details::DidVerificationKeyRelationship; /// All the errors that can be generated when validating a DID operation. #[derive(Debug, Eq, PartialEq, TypeInfo)] -pub enum DidError { +pub enum Error { /// See [StorageError]. - StorageError(StorageError), + Storage(Storage), /// See [SignatureError]. - SignatureError(SignatureError), + Signature(Signature), /// See [InputError]. - InputError(InputError), + Input(Input), /// An error that is not supposed to take place, yet it happened. Internal, } -impl From for DidError { - fn from(err: StorageError) -> Self { - DidError::StorageError(err) +impl From for Error { + fn from(err: Storage) -> Self { + Error::Storage(err) } } -impl From for DidError { - fn from(err: InputError) -> Self { - DidError::InputError(err) +impl From for Error { + fn from(err: Input) -> Self { + Error::Input(err) } } /// Error involving the pallet's storage. #[derive(Debug, Eq, PartialEq, TypeInfo)] -pub enum StorageError { +pub enum Storage { /// The DID being created is already present on chain. AlreadyExists, /// The expected DID cannot be found on chain. @@ -69,7 +69,7 @@ pub enum StorageError { /// Error generated when validating a DID operation. #[derive(Debug, Eq, PartialEq, TypeInfo)] -pub enum SignatureError { +pub enum Signature { /// The signature is not in the format the verification key expects. InvalidSignatureFormat, /// The signature is invalid for the payload and the verification key @@ -84,7 +84,7 @@ pub enum SignatureError { /// Error generated when some extrinsic input does not respect the pallet's /// constraints. #[derive(Debug, Eq, PartialEq, TypeInfo)] -pub enum InputError { +pub enum Input { /// A number of new key agreement keys greater than the maximum allowed has /// been provided. MaxKeyAgreementKeysLimitExceeded, diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 0adb0b05c..a67a08d60 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -150,7 +150,6 @@ pub mod pallet { DidDetails, DidEncryptionKey, DidSignature, DidVerifiableIdentifier, DidVerificationKey, RelationshipDeriveError, }, - errors::{DidError, InputError, SignatureError, StorageError}, service_endpoints::{DidEndpoint, ServiceEndpointId}, }; @@ -398,57 +397,57 @@ pub mod pallet { InvalidServiceEncoding, /// The number of service endpoints stored under the DID is larger than /// the number of endpoints to delete. - StoredEndpointsCountTooLarge, + MaxStoredEndpointsCountExceeded, /// An error that is not supposed to take place, yet it happened. Internal, } - impl From for Error { - fn from(error: DidError) -> Self { + impl From for Error { + fn from(error: errors::Error) -> Self { match error { - DidError::StorageError(storage_error) => Self::from(storage_error), - DidError::SignatureError(operation_error) => Self::from(operation_error), - DidError::InputError(input_error) => Self::from(input_error), - DidError::Internal => Self::Internal, + errors::Error::Storage(storage_error) => Self::from(storage_error), + errors::Error::Signature(operation_error) => Self::from(operation_error), + errors::Error::Input(input_error) => Self::from(input_error), + errors::Error::Internal => Self::Internal, } } } - impl From for Error { - fn from(error: StorageError) -> Self { + impl From for Error { + fn from(error: errors::Storage) -> Self { match error { - StorageError::NotFound => Self::NotFound, - StorageError::AlreadyExists => Self::AlreadyExists, - StorageError::DidKeyNotFound(_) | StorageError::KeyNotFound => Self::VerificationKeyNotFound, - StorageError::MaxPublicKeysExceeded => Self::MaxPublicKeysExceeded, - StorageError::MaxTotalKeyAgreementKeysExceeded => Self::MaxKeyAgreementKeysExceeded, - StorageError::AlreadyDeleted => Self::AlreadyDeleted, + errors::Storage::NotFound => Self::NotFound, + errors::Storage::AlreadyExists => Self::AlreadyExists, + errors::Storage::DidKeyNotFound(_) | errors::Storage::KeyNotFound => Self::VerificationKeyNotFound, + errors::Storage::MaxPublicKeysExceeded => Self::MaxPublicKeysExceeded, + errors::Storage::MaxTotalKeyAgreementKeysExceeded => Self::MaxKeyAgreementKeysExceeded, + errors::Storage::AlreadyDeleted => Self::AlreadyDeleted, } } } - impl From for Error { - fn from(error: SignatureError) -> Self { + impl From for Error { + fn from(error: errors::Signature) -> Self { match error { - SignatureError::InvalidSignature => Self::InvalidSignature, - SignatureError::InvalidSignatureFormat => Self::InvalidSignatureFormat, - SignatureError::InvalidNonce => Self::InvalidNonce, - SignatureError::TransactionExpired => Self::TransactionExpired, + errors::Signature::InvalidSignature => Self::InvalidSignature, + errors::Signature::InvalidSignatureFormat => Self::InvalidSignatureFormat, + errors::Signature::InvalidNonce => Self::InvalidNonce, + errors::Signature::TransactionExpired => Self::TransactionExpired, } } } - impl From for Error { - fn from(error: InputError) -> Self { + impl From for Error { + fn from(error: errors::Input) -> Self { match error { - InputError::MaxKeyAgreementKeysLimitExceeded => Self::MaxNewKeyAgreementKeysLimitExceeded, - InputError::MaxIdLengthExceeded => Self::MaxServiceIdLengthExceeded, - InputError::MaxServicesCountExceeded => Self::MaxNumberOfServicesExceeded, - InputError::MaxTypeCountExceeded => Self::MaxNumberOfTypesPerServiceExceeded, - InputError::MaxTypeLengthExceeded => Self::MaxServiceTypeLengthExceeded, - InputError::MaxUrlCountExceeded => Self::MaxNumberOfUrlsPerServiceExceeded, - InputError::MaxUrlLengthExceeded => Self::MaxServiceUrlLengthExceeded, - InputError::InvalidEncoding => Self::InvalidServiceEncoding, + errors::Input::MaxKeyAgreementKeysLimitExceeded => Self::MaxNewKeyAgreementKeysLimitExceeded, + errors::Input::MaxIdLengthExceeded => Self::MaxServiceIdLengthExceeded, + errors::Input::MaxServicesCountExceeded => Self::MaxNumberOfServicesExceeded, + errors::Input::MaxTypeCountExceeded => Self::MaxNumberOfTypesPerServiceExceeded, + errors::Input::MaxTypeLengthExceeded => Self::MaxServiceTypeLengthExceeded, + errors::Input::MaxUrlCountExceeded => Self::MaxNumberOfUrlsPerServiceExceeded, + errors::Input::MaxUrlLengthExceeded => Self::MaxServiceUrlLengthExceeded, + errors::Input::InvalidEncoding => Self::InvalidServiceEncoding, } } } @@ -1118,12 +1117,12 @@ pub mod pallet { pub fn verify_did_operation_signature_and_increase_nonce( operation: &DidAuthorizedCallOperationWithVerificationRelationship, signature: &DidSignature, - ) -> Result<(), DidError> { + ) -> Result<(), errors::Error> { // Check that the tx has not expired. Self::validate_block_number_value(operation.block_number)?; let mut did_details = - Did::::get(&operation.did).ok_or(DidError::StorageError(StorageError::NotFound))?; + Did::::get(&operation.did).ok_or(errors::Error::Storage(errors::Storage::NotFound))?; Self::validate_counter_value(operation.tx_counter, &did_details)?; // Increase the tx counter as soon as it is considered valid, no matter if the @@ -1145,13 +1144,13 @@ pub mod pallet { /// i.e., if the current blockchain block is in the inclusive range /// [operation_block_number, operation_block_number + /// MaxBlocksTxValidity]. - fn validate_block_number_value(block_number: BlockNumberOf) -> Result<(), DidError> { + fn validate_block_number_value(block_number: BlockNumberOf) -> Result<(), errors::Error> { let current_block_number = frame_system::Pallet::::block_number(); let allowed_range = block_number..=block_number.saturating_add(T::MaxBlocksTxValidity::get()); ensure!( allowed_range.contains(¤t_block_number), - DidError::SignatureError(SignatureError::TransactionExpired) + errors::Error::Signature(errors::Signature::TransactionExpired) ); Ok(()) @@ -1163,13 +1162,13 @@ pub mod pallet { /// the counter, as that would result in the DID being unusable, since /// we do not have yet any mechanism in place to wrap the counter value /// around when the limit is reached. - fn validate_counter_value(counter: u64, did_details: &DidDetails) -> Result<(), DidError> { + fn validate_counter_value(counter: u64, did_details: &DidDetails) -> Result<(), errors::Error> { // Verify that the operation counter is equal to the stored one + 1, // possibly wrapping around when u64::MAX is reached. let expected_nonce_value = did_details.last_tx_counter.wrapping_add(1); ensure!( counter == expected_nonce_value, - DidError::SignatureError(SignatureError::InvalidNonce) + errors::Error::Signature(errors::Signature::InvalidNonce) ); Ok(()) @@ -1182,18 +1181,18 @@ pub mod pallet { signature: &DidSignature, did_details: &DidDetails, key_type: DidVerificationKeyRelationship, - ) -> Result<(), DidError> { + ) -> Result<(), errors::Error> { // Retrieve the needed verification key from the DID details, or generate an // error if there is no key of the type required let verification_key = did_details .get_verification_key_for_key_type(key_type) - .ok_or(DidError::StorageError(StorageError::DidKeyNotFound(key_type)))?; + .ok_or(errors::Error::Storage(errors::Storage::DidKeyNotFound(key_type)))?; // Verify that the signature matches the expected format, otherwise generate // an error verification_key .verify_signature(payload, signature) - .map_err(DidError::SignatureError) + .map_err(errors::Error::Signature) } /// Deletes DID details from storage, including its linked service @@ -1203,7 +1202,7 @@ pub mod pallet { let current_endpoints_count = DidEndpointsCount::::get(&did_subject); ensure!( current_endpoints_count <= endpoints_to_remove, - Error::::StoredEndpointsCountTooLarge + Error::::MaxStoredEndpointsCountExceeded ); // *** No Fail beyond this point *** diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 87ffe1102..a8ed629aa 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -16,7 +16,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use crate::{errors::InputError, Config}; +use crate::{errors::Input, Config}; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ensure, traits::Get, BoundedVec, RuntimeDebug}; use scale_info::TypeInfo; @@ -59,36 +59,36 @@ pub struct DidEndpoint { impl DidEndpoint { /// Validates a given [DidEndpoint] instance against the constraint /// set in the pallet's [Config]. - pub(crate) fn validate_against_constraints(&self) -> Result<(), InputError> { + pub(crate) fn validate_against_constraints(&self) -> Result<(), Input> { // Check that the maximum number of service types is provided. ensure!( self.service_types.len() <= T::MaxNumberOfTypesPerService::get().saturated_into(), - InputError::MaxTypeCountExceeded + Input::MaxTypeCountExceeded ); // Check that the maximum number of URLs is provided. ensure!( self.urls.len() <= T::MaxNumberOfUrlsPerService::get().saturated_into(), - InputError::MaxUrlCountExceeded + Input::MaxUrlCountExceeded ); // Check that the ID is the maximum allowed length and only contain ASCII // characters. ensure!( self.id.len() <= T::MaxServiceIdLength::get().saturated_into(), - InputError::MaxIdLengthExceeded + Input::MaxIdLengthExceeded ); - let str_id = str::from_utf8(&self.id).map_err(|_| InputError::InvalidEncoding)?; - ensure!(crate_utils::is_valid_ascii_string(str_id), InputError::InvalidEncoding); + let str_id = str::from_utf8(&self.id).map_err(|_| Input::InvalidEncoding)?; + ensure!(crate_utils::is_valid_ascii_string(str_id), Input::InvalidEncoding); // Check that all types are the maximum allowed length and only contain ASCII // characters. self.service_types.iter().try_for_each(|s_type| { ensure!( s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), - InputError::MaxTypeLengthExceeded + Input::MaxTypeLengthExceeded ); - let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidEncoding)?; + let str_type = str::from_utf8(s_type).map_err(|_| Input::InvalidEncoding)?; ensure!( crate_utils::is_valid_ascii_string(str_type), - InputError::InvalidEncoding + Input::InvalidEncoding ); Ok(()) })?; @@ -97,10 +97,10 @@ impl DidEndpoint { for s_url in self.urls.iter() { ensure!( s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), - InputError::MaxUrlLengthExceeded + Input::MaxUrlLengthExceeded ); - let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidEncoding)?; - ensure!(crate_utils::is_valid_ascii_string(str_url), InputError::InvalidEncoding); + let str_url = str::from_utf8(s_url).map_err(|_| Input::InvalidEncoding)?; + ensure!(crate_utils::is_valid_ascii_string(str_url), Input::InvalidEncoding); } Ok(()) } @@ -134,11 +134,11 @@ impl DidEndpoint { pub mod utils { use super::*; - pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpoint]) -> Result<(), InputError> { + pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpoint]) -> Result<(), Input> { // Check if up the maximum number of endpoints is provided. ensure!( endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), - InputError::MaxServicesCountExceeded + Input::MaxServicesCountExceeded ); // Then validate each service. diff --git a/pallets/did/src/signature.rs b/pallets/did/src/signature.rs index 48a3e6d34..c25793a1b 100644 --- a/pallets/did/src/signature.rs +++ b/pallets/did/src/signature.rs @@ -23,7 +23,7 @@ use sp_std::{marker::PhantomData, vec::Vec}; use crate::{ did_details::{DidSignature, DidVerificationKeyRelationship}, - errors::DidError, + errors::Error, Config, Did, Pallet, WeightInfo, }; @@ -49,7 +49,7 @@ impl VerifySignature for DidSignatureVerify { ) .map_err(|err| match err { // Should never happen as a DID has always a valid authentication key and UrlErrors are never thrown here. - DidError::SignatureError(_) => SignatureVerificationError::SignatureInvalid, + Error::Signature(_) => SignatureVerificationError::SignatureInvalid, _ => SignatureVerificationError::SignerInformationNotPresent, }) } diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 489d63e8b..dde7a0aa1 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -31,7 +31,7 @@ use sp_std::{ use crate::{ self as did, did_details::{DidEncryptionKey, DidNewKeyAgreementKeySet, DidVerificationKey, DidVerificationKeyRelationship}, - errors::{DidError, SignatureError, StorageError}, + errors::Storage, mock::*, mock_utils::*, service_endpoints::DidEndpoint, @@ -2184,7 +2184,7 @@ fn check_service_count_too_small_deletion_error() { .execute_with(|| { assert_noop!( Did::delete(RuntimeOrigin::signed(alice_did.clone()), 0), - did::Error::::StoredEndpointsCountTooLarge + did::Error::::MaxStoredEndpointsCountExceeded ); }); } @@ -2291,7 +2291,7 @@ fn check_service_count_too_small_reclaim_error() { .execute_with(|| { assert_noop!( Did::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_00.clone()), alice_did.clone(), 0), - did::Error::::StoredEndpointsCountTooLarge + did::Error::::MaxStoredEndpointsCountExceeded ); }); } @@ -2953,7 +2953,7 @@ fn check_did_not_present_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::StorageError(StorageError::NotFound) + did::errors::Error::Storage(did::errors::Storage::NotFound) ); }); } @@ -3011,7 +3011,7 @@ fn check_smaller_counter_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::SignatureError(SignatureError::InvalidNonce) + did::errors::Error::Signature(did::errors::Signature::InvalidNonce) ); }); } @@ -3040,7 +3040,7 @@ fn check_equal_counter_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::SignatureError(SignatureError::InvalidNonce) + did::errors::Error::Signature(did::errors::Signature::InvalidNonce) ); }); } @@ -3069,7 +3069,7 @@ fn check_too_large_counter_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::SignatureError(SignatureError::InvalidNonce) + did::errors::Error::Signature(did::errors::Signature::InvalidNonce) ); }); } @@ -3094,7 +3094,7 @@ fn check_verification_key_not_present_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::StorageError(StorageError::DidKeyNotFound( + did::errors::Error::Storage(did::errors::Storage::DidKeyNotFound( DidVerificationKeyRelationship::AssertionMethod )) ); @@ -3123,7 +3123,7 @@ fn check_invalid_signature_format_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::SignatureError(SignatureError::InvalidSignatureFormat) + did::errors::Error::Signature(did::errors::Signature::InvalidSignatureFormat) ); }); } @@ -3150,7 +3150,7 @@ fn check_invalid_signature_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - DidError::SignatureError(SignatureError::InvalidSignature) + did::errors::Error::Signature(did::errors::Signature::InvalidSignature) ); }); }