From cd2efcb41fe5c7b97db8620f82fbb4bd5d2a420d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 2 Aug 2021 15:26:41 +0200 Subject: [PATCH 01/21] test: first unit tests passing after refactor --- pallets/did/src/benchmarking.rs | 19 +- pallets/did/src/did_details.rs | 135 +- pallets/did/src/lib.rs | 115 +- pallets/did/src/mock.rs | 45 +- pallets/did/src/tests.rs | 3876 +++++++++++++++---------------- runtimes/peregrine/src/lib.rs | 1 + runtimes/standalone/src/lib.rs | 1 + 7 files changed, 2039 insertions(+), 2153 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 886310648d..802f900b10 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -114,9 +114,8 @@ fn get_did_base_details(auth_key: DidVerificationKey) -> DidDetails::default()) } -fn generate_base_did_creation_operation(did: DidIdentifierOf) -> DidCreationOperation { +fn generate_base_did_creation_operation() -> DidCreationOperation { DidCreationOperation { - did, new_key_agreement_keys: BTreeSet::new(), new_attestation_key: None, new_delegation_key: None, @@ -169,7 +168,7 @@ benchmarks! { let did_public_del_key = get_ed25519_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(did_subject.clone()); + let mut did_creation_op = generate_base_did_creation_operation::(); did_creation_op.new_key_agreement_keys = did_key_agreement_keys; did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); @@ -218,7 +217,7 @@ benchmarks! { let did_public_del_key = get_sr25519_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(did_subject.clone()); + let mut did_creation_op = generate_base_did_creation_operation::(); did_creation_op.new_key_agreement_keys = did_key_agreement_keys; did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); @@ -267,7 +266,7 @@ benchmarks! { let did_public_del_key = get_ecdsa_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(did_subject.clone()); + let mut did_creation_op = generate_base_did_creation_operation::(); did_creation_op.new_key_agreement_keys = did_key_agreement_keys; did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key.clone())); did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key.clone())); @@ -552,8 +551,8 @@ benchmarks! { }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) } -impl_benchmark_test_suite! { - Pallet, - crate::mock::ExtBuilder::default().build_with_keystore(None), - crate::mock::Test -} +// impl_benchmark_test_suite! { +// Pallet, +// crate::mock::ExtBuilder::default().build_with_keystore(None), +// crate::mock::Test +// } diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 259aa0336d..601ea3c7f4 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -119,6 +119,15 @@ pub enum DidVerificationKeyRelationship { AssertionMethod, } +/// Key to be used to optionally verify a DID operation. +#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq, Eq)] +pub enum DidOperationAuthorizationKey { + /// A DID vrification key. + DidKey(DidVerificationKeyRelationship), + /// No key. The DID signature and nonce will not be verified. + NoKey +} + /// Types of signatures supported by this pallet. #[derive(Clone, Decode, Debug, Encode, Eq, PartialEq)] pub enum DidSignature { @@ -502,10 +511,10 @@ impl DidDetails { } } -impl TryFrom<(DidCreationOperation, DidVerificationKey)> for DidDetails { +impl TryFrom<(DidCreationOperation, DidVerificationKey)> for DidDetails { type Error = InputError; - fn try_from(new_details: (DidCreationOperation, DidVerificationKey)) -> Result { + fn try_from(new_details: (DidCreationOperation, DidVerificationKey)) -> Result { let (op, new_auth_key) = new_details; ensure!( @@ -619,41 +628,10 @@ impl TryFrom<(DidDetails, DidUpdateOperation)> for DidDetails: Encode { - /// The type of the verification key to be used to validate the - /// operation. - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship; - /// The DID identifier of the subject. - fn get_did(&self) -> &DidIdentifierOf; - /// The operation tx counter, used to protect against replay attacks. - fn get_tx_counter(&self) -> u64; -} - -/// Trait for extrinsic DID-based authorization. -/// -/// The trait allows -/// [DidAuthorizedCallOperations](DidAuthorizedCallOperation) wrapping an -/// extrinsic to specify what DID key to use to perform signature validation -/// over the byte-encoded operation. A result of None indicates that the -/// extrinsic does not support DID-based authorization. -pub trait DeriveDidCallAuthorizationVerificationKeyRelationship { - /// The type of the verification key to be used to validate the - /// wrapped extrinsic. - fn derive_verification_key_relationship(&self) -> Option; - - // Return a call to dispatch in order to test the pallet proxy feature. - #[cfg(feature = "runtime-benchmarks")] - fn get_call_for_did_call_benchmark() -> Self; -} - /// An operation to create a new DID. /// /// The struct implements the [DidOperation] trait, and as such it must @@ -661,9 +639,7 @@ pub trait DeriveDidCallAuthorizationVerificationKeyRelationship { /// required to verify the operation signature, and the tx counter to /// protect against replay attacks. #[derive(Clone, Debug, Decode, Encode, PartialEq)] -pub struct DidCreationOperation { - /// The DID identifier. It has to be unique. - pub did: DidIdentifierOf, +pub struct DidCreationOperation { /// The new key agreement keys. pub new_key_agreement_keys: BTreeSet, /// \[OPTIONAL\] The new attestation key. @@ -674,22 +650,6 @@ pub struct DidCreationOperation { pub new_endpoint_url: Option, } -impl DidOperation for DidCreationOperation { - // Not really used for creations. - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship { - DidVerificationKeyRelationship::Authentication - } - - fn get_did(&self) -> &DidIdentifierOf { - &self.did - } - - // Irrelevant for creation operations. - fn get_tx_counter(&self) -> u64 { - 0u64 - } -} - /// An operation to update a DID. /// /// The struct implements the [DidOperation] trait, and as such it must @@ -698,8 +658,6 @@ impl DidOperation for DidCreationOperation { /// protect against replay attacks. #[derive(Clone, Debug, Decode, Encode, PartialEq)] pub struct DidUpdateOperation { - /// The DID identifier. - pub did: DidIdentifierOf, /// \[OPTIONAL\] The new authentication key. pub new_authentication_key: Option, /// A new set of key agreement keys to add to the ones already stored. @@ -715,22 +673,6 @@ pub struct DidUpdateOperation { pub public_keys_to_remove: BTreeSet>, /// \[OPTIONAL\] The new endpoint URL. pub new_endpoint_url: Option, - /// The DID tx counter. - pub tx_counter: u64, -} - -impl DidOperation for DidUpdateOperation { - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship { - DidVerificationKeyRelationship::Authentication - } - - fn get_did(&self) -> &DidIdentifierOf { - &self.did - } - - fn get_tx_counter(&self) -> u64 { - self.tx_counter - } } /// Possible actions on a DID verification key within a @@ -752,32 +694,21 @@ impl Default for DidVerificationKeyUpdateAction { } } -/// An operation to delete a DID. +/// Trait for extrinsic DID-based authorization. /// -/// The struct implements the [DidOperation] trait, and as such it must -/// contain information about the caller's DID, the type of DID key -/// required to verify the operation signature, and the tx counter to -/// protect against replay attacks. -#[derive(Clone, Debug, Decode, Encode, PartialEq)] -pub struct DidDeletionOperation { - /// The DID identifier. - pub did: DidIdentifierOf, - /// The DID tx counter. - pub tx_counter: u64, -} - -impl DidOperation for DidDeletionOperation { - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship { - DidVerificationKeyRelationship::Authentication - } - - fn get_did(&self) -> &DidIdentifierOf { - &self.did - } +/// The trait allows +/// [DidAuthorizedCallOperations](DidAuthorizedCallOperation) wrapping an +/// extrinsic to specify what DID key to use to perform signature validation +/// over the byte-encoded operation. A result of None indicates that the +/// extrinsic does not support DID-based authorization. +pub trait DeriveDidCallAuthorizationVerificationKeyRelationship { + /// The type of the verification key to be used to validate the + /// wrapped extrinsic. + fn derive_verification_key_relationship(&self) -> Option; - fn get_tx_counter(&self) -> u64 { - self.tx_counter - } + // Return a call to dispatch in order to test the pallet proxy feature. + #[cfg(feature = "runtime-benchmarks")] + fn get_call_for_did_call_benchmark() -> Self; } /// A DID operation that wraps other extrinsic calls, allowing those @@ -802,21 +733,7 @@ pub struct DidAuthorizedCallOperationWithVerificationRelationship { /// The wrapped [DidAuthorizedCallOperation]. pub operation: DidAuthorizedCallOperation, /// The type of DID key to use for authorization. - pub verification_key_relationship: DidVerificationKeyRelationship, -} - -impl DidOperation for DidAuthorizedCallOperationWithVerificationRelationship { - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship { - self.verification_key_relationship - } - - fn get_did(&self) -> &DidIdentifierOf { - &self.did - } - - fn get_tx_counter(&self) -> u64 { - self.tx_counter - } + pub operation_authorization_key_type: DidOperationAuthorizationKey, } impl core::ops::Deref for DidAuthorizedCallOperationWithVerificationRelationship { diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 0bdfa3bc33..c9e33a95e3 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -116,6 +116,7 @@ mod tests; pub use crate::{default_weights::WeightInfo, did_details::*, errors::*, origin::*, pallet::*, url::*}; +use codec::Encode; use frame_support::{ dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, ensure, @@ -172,6 +173,7 @@ pub mod pallet { type Origin: From>>; #[cfg(feature = "runtime-benchmarks")] type Origin: From>>; + type EnsureOrigin: EnsureOrigin, ::Origin>; /// Overarching event type. type Event: From> + IsType<::Event>; /// Maximum number of key agreement keys that can be added in a creation @@ -208,13 +210,13 @@ pub mod pallet { pub enum Event { /// A new DID has been created. /// \[transaction signer, DID identifier\] - DidCreated(AccountIdentifierOf, DidIdentifierOf), + DidCreated(DidIdentifierOf), /// A DID has been updated. /// \[transaction signer, DID identifier\] - DidUpdated(AccountIdentifierOf, DidIdentifierOf), + DidUpdated(DidIdentifierOf), /// A DID has been deleted. /// \[transaction signer, DID identifier\] - DidDeleted(AccountIdentifierOf, DidIdentifierOf), + DidDeleted(DidIdentifierOf), /// A DID-authorised call has been executed. /// \[DID caller, dispatch result\] DidCallDispatched(DidIdentifierOf, DispatchResult), @@ -363,31 +365,29 @@ pub mod pallet { )] pub fn submit_did_create_operation( origin: OriginFor, - operation: DidCreationOperation, + operation: DidCreationOperation, signature: DidSignature, ) -> DispatchResult { - let sender = ensure_signed(origin)?; + let did_subject = T::EnsureOrigin::ensure_origin(origin)?; // There has to be no other DID with the same identifier already saved on chain, // otherwise generate a DidAlreadyPresent error. ensure!( - !>::contains_key(operation.get_did()), + !>::contains_key(&did_subject), >::DidAlreadyPresent ); - let account_did_auth_key = operation - .did + let account_did_auth_key = did_subject .verify_and_recover_signature(&operation.encode(), &signature) .map_err(>::from)?; let did_entry = DidDetails::try_from((operation.clone(), account_did_auth_key)).map_err(>::from)?; - let did_identifier = operation.get_did(); - log::debug!("Creating DID {:?}", did_identifier); - >::insert(did_identifier, did_entry); + log::debug!("Creating DID {:?}", did_subject.clone()); + >::insert(&did_subject, did_entry); - Self::deposit_event(Event::DidCreated(sender, did_identifier.clone())); + Self::deposit_event(Event::DidCreated(did_subject)); Ok(()) } @@ -475,26 +475,19 @@ pub mod pallet { pub fn submit_did_update_operation( origin: OriginFor, operation: DidUpdateOperation, - signature: DidSignature, ) -> DispatchResult { - let sender = ensure_signed(origin)?; - - // Saved here as it is consumed later when generating the new DidDetails object. - let did_identifier = operation.get_did().clone(); - - let did_details = >::get(&did_identifier).ok_or(>::DidNotPresent)?; + let did_subject = T::EnsureOrigin::ensure_origin(origin)?; - // Verify the signature and the nonce of the update operation. - Self::verify_operation_validity_for_did(&operation, &signature, &did_details).map_err(>::from)?; + let did_details = >::get(&did_subject).ok_or(>::DidNotPresent)?; // Generate a new DidDetails object by applying the changes in the update // operation to the old object (and consuming both). let new_did_details = DidDetails::try_from((did_details, operation)).map_err(>::from)?; - log::debug!("Updating DID {:?}", did_identifier); - >::insert(&did_identifier, new_did_details); + log::debug!("Updating DID {:?}", did_subject); + >::insert(&did_subject, new_did_details); - Self::deposit_event(Event::DidUpdated(sender, did_identifier)); + Self::deposit_event(Event::DidUpdated(did_subject)); Ok(()) } @@ -523,23 +516,19 @@ pub mod pallet { /// # #[pallet::weight(::WeightInfo::submit_did_delete_operation())] pub fn submit_did_delete_operation( - origin: OriginFor, - operation: DidDeletionOperation, - signature: DidSignature, + origin: OriginFor ) -> DispatchResult { - let sender = ensure_signed(origin)?; + let did_subject = T::EnsureOrigin::ensure_origin(origin)?; - let did_identifier = operation.get_did(); - - let did_details = >::get(&did_identifier).ok_or(>::DidNotPresent)?; - - // Verify the signature and the nonce of the delete operation. - Self::verify_operation_validity_for_did(&operation, &signature, &did_details).map_err(>::from)?; + ensure!( + // `take` calls `kill` internally + >::take(&did_subject).is_some(), + >::DidNotPresent + ); - log::debug!("Deleting DID {:?}", did_identifier); - >::remove(&did_identifier); + log::debug!("Deleted DID {:?}", did_subject); - Self::deposit_event(Event::DidDeleted(sender, did_identifier.clone())); + Self::deposit_event(Event::DidDeleted(did_subject)); Ok(()) } @@ -606,13 +595,12 @@ pub mod pallet { // Wrap the operation in the expected structure, specifying the key retrieved let wrapped_operation = DidAuthorizedCallOperationWithVerificationRelationship { operation: *did_call, - verification_key_relationship, + operation_authorization_key_type: verification_key_relationship, }; - // Verify if the DID exists, if the operation signature is valid, and if so - // increase the nonce if successful. - Self::verify_operation_validity_and_increase_did_nonce(&wrapped_operation, &signature) + Self::verify_did_operation_signature_and_increase_nonce(&wrapped_operation, &signature) .map_err(>::from)?; + log::debug!("Dispatch call from DID {:?}", did_identifier); // Dispatch the referenced [Call] instance and return its result @@ -642,45 +630,36 @@ impl Pallet { /// - Reads: Did /// - Writes: Did /// # - pub fn verify_operation_validity_and_increase_did_nonce>( - operation: &O, + pub fn verify_did_operation_signature_and_increase_nonce( + operation: &DidAuthorizedCallOperationWithVerificationRelationship, signature: &DidSignature, ) -> Result<(), DidError> { let mut did_details = - >::get(&operation.get_did()).ok_or(DidError::StorageError(StorageError::DidNotPresent))?; - - Self::verify_operation_validity_for_did(operation, signature, &did_details)?; - - // Update tx counter in DID details and save to DID pallet - did_details.increase_tx_counter().map_err(DidError::StorageError)?; - >::insert(&operation.get_did(), did_details); + >::get(&operation.did).ok_or(DidError::StorageError(StorageError::DidNotPresent))?; + + match operation.operation_authorization_key_type { + // Do nothing if no key is required. + DidOperationAuthorizationKey::NoKey => (), + DidOperationAuthorizationKey::DidKey(verification_key_type) => { + Self::validate_counter_value(operation.tx_counter, &did_details)?; + // Increase the tx counter as soon as it is considered valid, no matter if the signature is valid or not. + did_details.increase_tx_counter().map_err(DidError::StorageError)?; + Self::verify_payload_signature_with_did_key_type(operation.encode().as_ref(), signature, &did_details, verification_key_type)?; + >::insert(&operation.did, did_details); + }, + } Ok(()) } - // Internally verifies the validity of a DID operation nonce and signature. - fn verify_operation_validity_for_did>( - operation: &O, - signature: &DidSignature, - did_details: &DidDetails, - ) -> Result<(), DidError> { - Self::verify_operation_counter_for_did(operation, did_details)?; - Self::verify_payload_signature_with_did_key_type( - &operation.encode(), - signature, - did_details, - operation.get_verification_key_relationship(), - ) - } - // Verify the validity of a DID operation nonce. // To be valid, the nonce must be equal to the one currently stored + 1. // This is to avoid quickly "consuming" all the possible values for 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 verify_operation_counter_for_did>( - operation: &O, + fn validate_counter_value( + counter: u64, did_details: &DidDetails, ) -> Result<(), DidError> { // Verify that the DID has not reached the maximum tx counter value @@ -695,7 +674,7 @@ impl Pallet { .checked_add(1) .ok_or(DidError::InternalError)?; ensure!( - operation.get_tx_counter() == expected_nonce_value, + counter == expected_nonce_value, DidError::SignatureError(SignatureError::InvalidNonce) ); diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index a7d986b2d7..5178bd9157 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -21,6 +21,7 @@ use codec::{Decode, Encode}; use frame_support::{parameter_types, weights::constants::RocksDbWeight}; +use frame_system::EnsureSigned; #[cfg(feature = "runtime-benchmarks")] use frame_system::EnsureSigned; use sp_core::{ecdsa, ed25519, sr25519, Pair}; @@ -98,6 +99,7 @@ impl Config for Test { type DidIdentifier = TestDidIdentifier; type Origin = Origin; type Call = Call; + type EnsureOrigin = EnsureSigned; type Event = (); type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxUrlLength = MaxUrlLength; @@ -259,9 +261,8 @@ pub fn get_url_endpoint(length: u32) -> Url { ) } -pub fn generate_base_did_creation_operation(did: TestDidIdentifier) -> did::DidCreationOperation { +pub fn generate_base_did_creation_operation() -> did::DidCreationOperation { DidCreationOperation { - did, new_key_agreement_keys: BTreeSet::new(), new_attestation_key: None, new_delegation_key: None, @@ -271,21 +272,15 @@ pub fn generate_base_did_creation_operation(did: TestDidIdentifier) -> did::DidC pub fn generate_base_did_update_operation(did: TestDidIdentifier) -> did::DidUpdateOperation { DidUpdateOperation { - did, new_authentication_key: None, new_key_agreement_keys: BTreeSet::new(), attestation_key_update: DidVerificationKeyUpdateAction::default(), delegation_key_update: DidVerificationKeyUpdateAction::default(), new_endpoint_url: None, public_keys_to_remove: BTreeSet::new(), - tx_counter: 1u64, } } -pub fn generate_base_did_delete_operation(did: TestDidIdentifier) -> did::DidDeletionOperation { - DidDeletionOperation { did, tx_counter: 1u64 } -} - pub fn generate_base_did_details(authentication_key: did::DidVerificationKey) -> did::DidDetails { did::DidDetails::new(authentication_key, 0u64) } @@ -313,20 +308,28 @@ pub(crate) fn get_delegation_key_call() -> Call { Call::Ctype(ctype::Call::add(get_delegation_key_test_input())) } pub(crate) fn get_no_key_test_input() -> TestCtypeHash { - TestCtypeHash::from_slice(&[3u8; 32]) + TestCtypeHash::from_slice(&[4u8; 32]) } pub(crate) fn get_no_key_call() -> Call { Call::Ctype(ctype::Call::add(get_no_key_test_input())) } +pub(crate) fn get_none_key_test_input() -> TestCtypeHash { + TestCtypeHash::from_slice(&[5u8; 32]) +} +pub(crate) fn get_none_key_call() -> Call { + Call::Ctype(ctype::Call::add(get_none_key_test_input())) +} impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { if *self == get_attestation_key_call() { - Some(did::DidVerificationKeyRelationship::AssertionMethod) + Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)) } else if *self == get_authentication_key_call() { - Some(did::DidVerificationKeyRelationship::Authentication) + Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)) } else if *self == get_delegation_key_call() { - Some(did::DidVerificationKeyRelationship::CapabilityDelegation) + Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)) + } else if *self == get_no_key_call() { + Some(did::DidOperationAuthorizationKey::NoKey) } else { #[cfg(feature = "runtime-benchmarks")] if *self == Self::get_call_for_did_call_benchmark() { @@ -352,7 +355,7 @@ pub fn generate_test_did_call( DidVerificationKeyRelationship::AssertionMethod => get_attestation_key_call(), DidVerificationKeyRelationship::Authentication => get_authentication_key_call(), DidVerificationKeyRelationship::CapabilityDelegation => get_delegation_key_call(), - _ => get_no_key_call(), + _ => get_none_key_call(), }; did::DidAuthorizedCallOperation { did: caller, @@ -370,20 +373,6 @@ pub struct TestDidOperation { pub tx_counter: u64, } -impl DidOperation for TestDidOperation { - fn get_verification_key_relationship(&self) -> DidVerificationKeyRelationship { - self.verification_key_type - } - - fn get_did(&self) -> &TestDidIdentifier { - &self.did - } - - fn get_tx_counter(&self) -> u64 { - self.tx_counter - } -} - #[allow(dead_code)] pub fn initialize_logger() { env_logger::builder().is_test(true).try_init(); diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 58d7eb12ad..8fd05186b0 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -30,7 +30,7 @@ fn check_successful_simple_ed25519_creation() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(alice_did.clone()); + let operation = generate_base_did_creation_operation(); let signature = auth_key.sign(operation.encode().as_ref()); @@ -38,7 +38,7 @@ fn check_successful_simple_ed25519_creation() { ext.execute_with(|| { assert_ok!(Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), + Origin::signed(alice_did.clone()), operation.clone(), did::DidSignature::from(signature), )); @@ -65,7 +65,7 @@ fn check_successful_simple_sr25519_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(alice_did.clone()); + let operation = generate_base_did_creation_operation(); let signature = auth_key.sign(operation.encode().as_ref()); @@ -73,7 +73,7 @@ fn check_successful_simple_sr25519_creation() { ext.execute_with(|| { assert_ok!(Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), + Origin::signed(alice_did.clone()), operation.clone(), did::DidSignature::from(signature), )); @@ -100,7 +100,7 @@ fn check_successful_simple_ecdsa_creation() { let auth_key = get_ecdsa_authentication_key(true); let alice_did = get_did_identifier_from_ecdsa_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(alice_did.clone()); + let operation = generate_base_did_creation_operation(); let signature = auth_key.sign(operation.encode().as_ref()); @@ -108,7 +108,7 @@ fn check_successful_simple_ecdsa_creation() { ext.execute_with(|| { assert_ok!(Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), + Origin::signed(alice_did.clone()), operation.clone(), did::DidSignature::from(signature), )); @@ -146,7 +146,7 @@ fn check_successful_complete_creation() { did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), ); - let mut operation = generate_base_did_creation_operation(alice_did.clone()); + let mut operation = generate_base_did_creation_operation(); operation.new_key_agreement_keys = enc_keys.clone(); operation.new_attestation_key = Some(did::DidVerificationKey::from(att_key.public())); operation.new_delegation_key = Some(did::DidVerificationKey::from(del_key.public())); @@ -158,7 +158,7 @@ fn check_successful_complete_creation() { ext.execute_with(|| { assert_ok!(Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), + Origin::signed(alice_did.clone()), operation.clone(), did::DidSignature::from(signature), )); @@ -210,16 +210,16 @@ fn check_duplicate_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); let mock_did = generate_base_did_details(auth_did_key); - let operation = generate_base_did_creation_operation(alice_did.clone()); + let operation = generate_base_did_creation_operation(); let signature = auth_key.sign(operation.encode().as_ref()); - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(alice_did.clone(), mock_did)]).build(None); ext.execute_with(|| { assert_noop!( Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), + Origin::signed(alice_did), operation.clone(), did::DidSignature::from(signature), ), @@ -228,1945 +228,1945 @@ fn check_duplicate_did_creation() { }); } -#[test] -fn check_invalid_signature_format_did_creation() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Using an Ed25519 key where an Sr25519 is expected - let invalid_key = get_ed25519_authentication_key(true); - // DID creation contains auth_key, but signature is generated using invalid_key - let operation = generate_base_did_creation_operation(alice_did); - - let signature = invalid_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignature - ); - }); -} - -#[test] -fn check_invalid_signature_did_creation() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let alternative_key = get_sr25519_authentication_key(false); - let operation = generate_base_did_creation_operation(alice_did); - - let signature = alternative_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignature - ); - }); -} - -#[test] -fn check_max_limit_key_agreement_keys_did_creation() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Max keys allowed + 1 - let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - let mut operation = generate_base_did_creation_operation(alice_did); - operation.new_key_agreement_keys = enc_keys; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxKeyAgreementKeysLimitExceeded - ); - }); -} - -#[test] -fn check_url_too_long_did_creation() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - let mut operation = generate_base_did_creation_operation(alice_did); - // Max length allowed + 1 - operation.new_endpoint_url = Some(url_endpoint); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxUrlLengthExceeded - ); - }); -} - -// submit_did_update_operation - -#[test] -fn check_successful_complete_update() { - let old_auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(old_auth_key.public()); - let new_auth_key = get_ed25519_authentication_key(false); - let old_enc_key = get_x25519_encryption_key(true); - let new_enc_key = get_x25519_encryption_key(false); - let old_att_key = get_ed25519_attestation_key(true); - let new_att_key = get_ed25519_attestation_key(false); - let new_del_key = get_sr25519_delegation_key(true); - let new_url = did::Url::from( - did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) - .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), - ); - - let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(old_auth_key.public())); - old_did_details.add_key_agreement_keys( - vec![old_enc_key] - .iter() - .copied() - .collect::>(), - 0u64, - ); - old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); - - // Update all keys, URL endpoint and tx counter. The old key agreement key is - // removed. - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); - operation.new_key_agreement_keys = vec![new_enc_key] - .iter() - .copied() - .collect::>(); - operation.attestation_key_update = - did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); - operation.delegation_key_update = - did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_del_key.public())); - operation.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] - .iter() - .copied() - .collect::>(); - operation.new_endpoint_url = Some(new_url); - operation.tx_counter = old_did_details.last_tx_counter + 1u64; - - // Generate signature using the old authentication key - let signature = old_auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); - - let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); - assert_eq!( - new_did_details.get_authentication_key_id(), - generate_key_id(&did::DidVerificationKey::from(new_auth_key.public()).into()) - ); - // Old one deleted, new one added -> Total keys = 1 - assert_eq!(new_did_details.get_key_agreement_keys_ids().len(), 1); - assert_eq!( - new_did_details.get_key_agreement_keys_ids().iter().next().unwrap(), - &generate_key_id(&new_enc_key.into()) - ); - assert_eq!( - new_did_details.get_attestation_key_id(), - &Some(generate_key_id( - &did::DidVerificationKey::from(new_att_key.public()).into() - )) - ); - assert_eq!( - new_did_details.get_delegation_key_id(), - &Some(generate_key_id( - &did::DidVerificationKey::from(new_del_key.public()).into() - )) - ); - - // Total is +1 for the new auth key, -1 for the old auth key (replaced), +1 for - // the new key agreement key, -1 for the old key agreement key (deleted), +1 for - // the old attestation key, +1 for the new attestation key, +1 for the new - // delegation key = 5 - let public_keys = new_did_details.get_public_keys(); - assert_eq!(public_keys.len(), 5); - // Check for new authentication key - assert!(public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(new_att_key.public()).into() - ))); - // Check for new key agreement key - assert!(public_keys.contains_key(&generate_key_id(&new_enc_key.into()))); - // Check for old attestation key - assert!(public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(old_att_key.public()).into() - ))); - // Check for new attestation key - assert!(public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(new_att_key.public()).into() - ))); - // Check for new delegation key - assert!(public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(new_del_key.public()).into() - ))); - assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -} - -#[test] -fn check_successful_keys_deletion_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let att_key = get_ed25519_attestation_key(true); - let del_key = get_sr25519_delegation_key(true); - - let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); - old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - operation.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; - operation.tx_counter = old_did_details.last_tx_counter + 1u64; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); - - // Auth key and key agreement key unchanged - let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); - assert_eq!( - new_did_details.get_authentication_key_id(), - old_did_details.get_authentication_key_id() - ); - assert_eq!( - new_did_details.get_key_agreement_keys_ids(), - old_did_details.get_key_agreement_keys_ids() - ); - assert_eq!(new_did_details.get_attestation_key_id(), &None); - assert_eq!(new_did_details.get_delegation_key_id(), &None); - - // Public keys should now contain only the authentication key and the revoked - // attestation key - let stored_public_keys = new_did_details.get_public_keys(); - assert_eq!(stored_public_keys.len(), 2); - assert!(stored_public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(auth_key.public()).into() - ))); - assert!(stored_public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(att_key.public()).into() - ))); - assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -} - -#[test] -fn check_successful_keys_overwrite_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - // Same as the authentication key -> leads to two keys having the same ID - let new_att_key = auth_key.clone(); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = - did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); - - // Auth key unchanged - let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); - assert_eq!( - new_did_details.get_authentication_key_id(), - old_did_details.get_authentication_key_id() - ); - // New attestation key and authentication key should now have the same ID - assert_eq!( - new_did_details.get_attestation_key_id(), - &Some(old_did_details.get_authentication_key_id()) - ); - - // As the two keys have the same ID, public keys still contain only one element - let stored_public_keys = new_did_details.get_public_keys(); - assert_eq!(stored_public_keys.len(), 1); - assert!(stored_public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(auth_key.public()).into() - ))); - assert!(stored_public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(new_att_key.public()).into() - ))); - // The block number should be the updated to the latest one, even if the ID was - // also present before. - let stored_key_details = stored_public_keys - .get(&old_did_details.get_authentication_key_id()) - .expect("There should be a key with the given ID stored on chain."); - assert_eq!(stored_key_details.block_number, new_block_number); - assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -} - -#[test] -fn check_successful_keys_multiuse_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - // Same as the authentication key -> leads to two keys having the same ID - let old_att_key = auth_key.clone(); - - let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); - - // Remove attestation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); - - // Auth key unchanged - let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); - assert_eq!( - new_did_details.get_authentication_key_id(), - old_did_details.get_authentication_key_id() - ); - // Attestation key should now be set to None - assert_eq!(new_did_details.get_attestation_key_id(), &None); - - // As the two keys have the same ID, public keys still contain only one element - let stored_public_keys = new_did_details.get_public_keys(); - assert_eq!(stored_public_keys.len(), 1); - assert!(stored_public_keys.contains_key(&generate_key_id( - &did::DidVerificationKey::from(auth_key.public()).into() - ))); -} - -#[test] -fn check_did_not_present_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let bob_auth_key = get_ed25519_authentication_key(false); - let bob_did = get_did_identifier_from_ed25519_key(bob_auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_update_operation(alice_did); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(bob_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::DidNotPresent - ); - }); -} - -#[test] -fn check_did_max_counter_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = u64::MAX; - let operation = generate_base_did_update_operation(alice_did.clone()); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxTxCounterValue - ); - }); -} - -#[test] -fn check_smaller_tx_counter_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = 1; - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter - 1; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_equal_tx_counter_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_too_large_tx_counter_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter + 2; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_invalid_signature_format_did_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - // Using an Sr25519 key where an Ed25519 is expected - let invalid_key = get_sr25519_authentication_key(true); - // DID update contains auth_key, but signature is generated using invalid_key - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_update_operation(alice_did.clone()); - - let signature = invalid_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignatureFormat - ); - }); -} - -#[test] -fn check_invalid_signature_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Using an Sr25519 key as expected, but from a different seed (default = false) - let alternative_key = get_sr25519_authentication_key(false); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_update_operation(alice_did.clone()); - - let signature = alternative_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignature - ); - }); -} - -#[test] -fn check_max_limit_key_agreement_keys_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Max keys allowed + 1 - let new_enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_key_agreement_keys = new_enc_keys; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxKeyAgreementKeysLimitExceeded - ); - }); -} - -#[test] -fn check_max_limit_public_keys_to_remove_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Max keys allowed + 1 - let keys_ids_to_remove = - get_public_keys_to_remove(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.public_keys_to_remove = keys_ids_to_remove; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxVerificationKeysToRemoveLimitExceeded - ); - }); -} - -#[test] -fn check_url_too_long_did_update() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Max URL length allowed + 1 - let new_endpoint_url = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_endpoint_url = Some(new_endpoint_url); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxUrlLengthExceeded - ); - }); -} - -#[test] -fn check_currently_active_authentication_key_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - // Trying to remove the currently active authentication key - operation.public_keys_to_remove = vec![generate_key_id( - &did::DidVerificationKey::from(auth_key.public()).into(), - )] - .iter() - .copied() - .collect::>(); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::CurrentlyActiveKey - ); - }); -} - -#[test] -fn check_currently_active_delegation_key_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let del_key = get_ecdsa_delegation_key(true); - - let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - // Trying to remove the currently active delegation key - operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] - .iter() - .copied() - .collect::>(); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::CurrentlyActiveKey - ); - }); -} - -#[test] -fn check_currently_active_attestation_key_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let att_key = get_sr25519_attestation_key(true); - - let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - // Trying to remove the currently active attestation key - operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] - .iter() - .copied() - .collect::>(); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::CurrentlyActiveKey - ); - }); -} - -#[test] -fn check_verification_key_not_present_update() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let key_to_delete = get_sr25519_authentication_key(true); - - let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - // Trying to remove the currently active authentication key - operation.public_keys_to_remove = vec![generate_key_id( - &did::DidVerificationKey::from(key_to_delete.public()).into(), - )] - .iter() - .copied() - .collect::>(); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, old_did_details)]) - .build(None); - - let new_block_number: TestBlockNumber = 1; - - ext.execute_with(|| { - System::set_block_number(new_block_number); - assert_noop!( - Did::submit_did_update_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::VerificationKeyNotPresent - ); - }); -} - -// submit_did_delete_operation - -#[test] -fn check_successful_deletion() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - // Update all keys, URL endpoint and tx counter. No keys are removed in this - // test - let operation = generate_base_did_delete_operation(alice_did.clone()); - - // Generate signature using the old authentication key - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), did_details)]) - .build(None); - - ext.execute_with(|| { - assert_ok!(Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); - - assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); - - // Re-adding the same DID identifier, which should not fail. - let operation = generate_base_did_creation_operation(alice_did); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - )); - }); -} - -#[test] -fn check_did_not_present_deletion() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - - // Update all keys, URL endpoint and tx counter. No keys are removed in this - // test - let operation = generate_base_did_delete_operation(alice_did); - - // Generate signature using the old authentication key - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::DidNotPresent - ); - }); -} - -#[test] -fn check_max_tx_counter_did_deletion() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = u64::MAX; - let operation = generate_base_did_delete_operation(alice_did.clone()); - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::MaxTxCounterValue - ); - }); -} - -#[test] -fn check_smaller_tx_counter_did_deletion() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = 1; - let mut operation = generate_base_did_delete_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter - 1; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_equal_tx_counter_did_deletion() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = 1; - let mut operation = generate_base_did_delete_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_too_large_tx_counter_did_deletion() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut operation = generate_base_did_delete_operation(alice_did.clone()); - operation.tx_counter = mock_did.last_tx_counter + 2; - - let signature = auth_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_invalid_signature_format_did_deletion() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - // Using an Sr25519 key where an Ed25519 is expected - let invalid_key = get_sr25519_authentication_key(true); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_delete_operation(alice_did.clone()); - - let signature = invalid_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignatureFormat - ); - }); -} - -#[test] -fn check_invalid_signature_did_deletion() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Using an Sr25519 key as expected, but from a different seed (default = false) - let alternative_key = get_sr25519_authentication_key(false); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_delete_operation(alice_did.clone()); - - let signature = alternative_key.sign(operation.encode().as_ref()); - - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_delete_operation( - Origin::signed(DEFAULT_ACCOUNT), - operation.clone(), - did::DidSignature::from(signature), - ), - did::Error::::InvalidSignature - ); - }); -} - -// submit_did_call - -#[test] -fn check_no_key_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - - let mut ext = ExtBuilder::default().build(None); - - // CapabilityInvocation is not supported at the moment, so it should return no - // key and hence the operation fail. - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityInvocation, did); - let signature = did::DidSignature::from(ed25519::Signature::default()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call(Origin::signed(caller), Box::new(call_operation), signature), - did::Error::::UnsupportedDidAuthorizationCall - ); - }); -} - -#[test] -fn check_did_not_found_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - - // No DID added - let mut ext = ExtBuilder::default().build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::DidNotPresent - ); - }); -} - -#[test] -fn check_max_counter_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = u64::MAX; - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::MaxTxCounterValue - ); - }); -} - -#[test] -fn check_too_small_tx_counter_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = 1u64; - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - call_operation.tx_counter = 0u64; - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_equal_tx_counter_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) - .build(None); - - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - call_operation.tx_counter = mock_did.last_tx_counter; - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_too_large_tx_counter_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) - .build(None); - - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - call_operation.tx_counter = mock_did.last_tx_counter + 2u64; - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::InvalidNonce - ); - }); -} - -#[test] -fn check_verification_key_not_present_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - // The operation requires the delegation key that is currently not stored for - // the given DID. - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::VerificationKeyNotPresent - ); - }); -} - -#[test] -fn check_invalid_signature_format_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let alternative_auth_key = get_ed25519_authentication_key(true); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::InvalidSignatureFormat - ); - }); -} - -#[test] -fn check_invalid_signature_call_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let alternative_auth_key = get_sr25519_authentication_key(false); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - did::Error::::InvalidSignature - ); - }); -} - -#[test] -fn check_call_attestation_key_successful() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let attestation_key = get_ed25519_attestation_key(true); - - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); - let signature = attestation_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_ok!(Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - )); - }); -} - -#[test] -fn check_call_attestation_key_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let attestation_key = get_ed25519_attestation_key(true); - - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); - - let ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - // CType already added to storage - let mut ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(get_attestation_key_test_input(), did.clone())]) - .build(Some(ext)); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); - let signature = attestation_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_err!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - ctype::Error::::CTypeAlreadyExists - ); - }); -} - -#[test] -fn check_call_delegation_key_successful() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let delegation_key = get_ed25519_delegation_key(true); - - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); - let signature = delegation_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_ok!(Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - )); - }); -} - -#[test] -fn check_call_delegation_key_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - let delegation_key = get_ed25519_delegation_key(true); - - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); - - let ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - // CType already added to storage - let mut ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(get_delegation_key_test_input(), did.clone())]) - .build(Some(ext)); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); - let signature = delegation_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_err!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - ctype::Error::::CTypeAlreadyExists - ); - }); -} - -#[test] -fn check_call_authentication_key_successful() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_ok!(Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - )); - }); -} +// #[test] +// fn check_invalid_signature_format_did_creation() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Using an Ed25519 key where an Sr25519 is expected +// let invalid_key = get_ed25519_authentication_key(true); +// // DID creation contains auth_key, but signature is generated using invalid_key +// let operation = generate_base_did_creation_operation(); + +// let signature = invalid_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_create_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignature +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_did_creation() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let alternative_key = get_sr25519_authentication_key(false); +// let operation = generate_base_did_creation_operation(); + +// let signature = alternative_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_create_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignature +// ); +// }); +// } + +// #[test] +// fn check_max_limit_key_agreement_keys_did_creation() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Max keys allowed + 1 +// let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); +// let mut operation = generate_base_did_creation_operation(); +// operation.new_key_agreement_keys = enc_keys; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_create_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxKeyAgreementKeysLimitExceeded +// ); +// }); +// } + +// #[test] +// fn check_url_too_long_did_creation() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); +// let mut operation = generate_base_did_creation_operation(); +// // Max length allowed + 1 +// operation.new_endpoint_url = Some(url_endpoint); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_create_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxUrlLengthExceeded +// ); +// }); +// } + +// // submit_did_update_operation + +// #[test] +// fn check_successful_complete_update() { +// let old_auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(old_auth_key.public()); +// let new_auth_key = get_ed25519_authentication_key(false); +// let old_enc_key = get_x25519_encryption_key(true); +// let new_enc_key = get_x25519_encryption_key(false); +// let old_att_key = get_ed25519_attestation_key(true); +// let new_att_key = get_ed25519_attestation_key(false); +// let new_del_key = get_sr25519_delegation_key(true); +// let new_url = did::Url::from( +// did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) +// .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), +// ); + +// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(old_auth_key.public())); +// old_did_details.add_key_agreement_keys( +// vec![old_enc_key] +// .iter() +// .copied() +// .collect::>(), +// 0u64, +// ); +// old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); + +// // Update all keys, URL endpoint and tx counter. The old key agreement key is +// // removed. +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); +// operation.new_key_agreement_keys = vec![new_enc_key] +// .iter() +// .copied() +// .collect::>(); +// operation.attestation_key_update = +// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); +// operation.delegation_key_update = +// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_del_key.public())); +// operation.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] +// .iter() +// .copied() +// .collect::>(); +// operation.new_endpoint_url = Some(new_url); +// operation.tx_counter = old_did_details.last_tx_counter + 1u64; + +// // Generate signature using the old authentication key +// let signature = old_auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_ok!(Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); + +// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); +// assert_eq!( +// new_did_details.get_authentication_key_id(), +// generate_key_id(&did::DidVerificationKey::from(new_auth_key.public()).into()) +// ); +// // Old one deleted, new one added -> Total keys = 1 +// assert_eq!(new_did_details.get_key_agreement_keys_ids().len(), 1); +// assert_eq!( +// new_did_details.get_key_agreement_keys_ids().iter().next().unwrap(), +// &generate_key_id(&new_enc_key.into()) +// ); +// assert_eq!( +// new_did_details.get_attestation_key_id(), +// &Some(generate_key_id( +// &did::DidVerificationKey::from(new_att_key.public()).into() +// )) +// ); +// assert_eq!( +// new_did_details.get_delegation_key_id(), +// &Some(generate_key_id( +// &did::DidVerificationKey::from(new_del_key.public()).into() +// )) +// ); + +// // Total is +1 for the new auth key, -1 for the old auth key (replaced), +1 for +// // the new key agreement key, -1 for the old key agreement key (deleted), +1 for +// // the old attestation key, +1 for the new attestation key, +1 for the new +// // delegation key = 5 +// let public_keys = new_did_details.get_public_keys(); +// assert_eq!(public_keys.len(), 5); +// // Check for new authentication key +// assert!(public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(new_att_key.public()).into() +// ))); +// // Check for new key agreement key +// assert!(public_keys.contains_key(&generate_key_id(&new_enc_key.into()))); +// // Check for old attestation key +// assert!(public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(old_att_key.public()).into() +// ))); +// // Check for new attestation key +// assert!(public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(new_att_key.public()).into() +// ))); +// // Check for new delegation key +// assert!(public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(new_del_key.public()).into() +// ))); +// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); +// } + +// #[test] +// fn check_successful_keys_deletion_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let att_key = get_ed25519_attestation_key(true); +// let del_key = get_sr25519_delegation_key(true); + +// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); +// old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; +// operation.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; +// operation.tx_counter = old_did_details.last_tx_counter + 1u64; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_ok!(Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); + +// // Auth key and key agreement key unchanged +// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); +// assert_eq!( +// new_did_details.get_authentication_key_id(), +// old_did_details.get_authentication_key_id() +// ); +// assert_eq!( +// new_did_details.get_key_agreement_keys_ids(), +// old_did_details.get_key_agreement_keys_ids() +// ); +// assert_eq!(new_did_details.get_attestation_key_id(), &None); +// assert_eq!(new_did_details.get_delegation_key_id(), &None); + +// // Public keys should now contain only the authentication key and the revoked +// // attestation key +// let stored_public_keys = new_did_details.get_public_keys(); +// assert_eq!(stored_public_keys.len(), 2); +// assert!(stored_public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(auth_key.public()).into() +// ))); +// assert!(stored_public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(att_key.public()).into() +// ))); +// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); +// } + +// #[test] +// fn check_successful_keys_overwrite_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// // Same as the authentication key -> leads to two keys having the same ID +// let new_att_key = auth_key.clone(); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.attestation_key_update = +// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_ok!(Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); + +// // Auth key unchanged +// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); +// assert_eq!( +// new_did_details.get_authentication_key_id(), +// old_did_details.get_authentication_key_id() +// ); +// // New attestation key and authentication key should now have the same ID +// assert_eq!( +// new_did_details.get_attestation_key_id(), +// &Some(old_did_details.get_authentication_key_id()) +// ); + +// // As the two keys have the same ID, public keys still contain only one element +// let stored_public_keys = new_did_details.get_public_keys(); +// assert_eq!(stored_public_keys.len(), 1); +// assert!(stored_public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(auth_key.public()).into() +// ))); +// assert!(stored_public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(new_att_key.public()).into() +// ))); +// // The block number should be the updated to the latest one, even if the ID was +// // also present before. +// let stored_key_details = stored_public_keys +// .get(&old_did_details.get_authentication_key_id()) +// .expect("There should be a key with the given ID stored on chain."); +// assert_eq!(stored_key_details.block_number, new_block_number); +// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); +// } + +// #[test] +// fn check_successful_keys_multiuse_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// // Same as the authentication key -> leads to two keys having the same ID +// let old_att_key = auth_key.clone(); + +// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); + +// // Remove attestation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_ok!(Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); + +// // Auth key unchanged +// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); +// assert_eq!( +// new_did_details.get_authentication_key_id(), +// old_did_details.get_authentication_key_id() +// ); +// // Attestation key should now be set to None +// assert_eq!(new_did_details.get_attestation_key_id(), &None); + +// // As the two keys have the same ID, public keys still contain only one element +// let stored_public_keys = new_did_details.get_public_keys(); +// assert_eq!(stored_public_keys.len(), 1); +// assert!(stored_public_keys.contains_key(&generate_key_id( +// &did::DidVerificationKey::from(auth_key.public()).into() +// ))); +// } + +// #[test] +// fn check_did_not_present_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let bob_auth_key = get_ed25519_authentication_key(false); +// let bob_did = get_did_identifier_from_ed25519_key(bob_auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = generate_base_did_update_operation(alice_did); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(bob_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::DidNotPresent +// ); +// }); +// } + +// #[test] +// fn check_did_max_counter_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = u64::MAX; +// let operation = generate_base_did_update_operation(alice_did.clone()); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxTxCounterValue +// ); +// }); +// } + +// #[test] +// fn check_smaller_tx_counter_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = 1; +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter - 1; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_equal_tx_counter_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_too_large_tx_counter_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter + 2; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_format_did_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// // Using an Sr25519 key where an Ed25519 is expected +// let invalid_key = get_sr25519_authentication_key(true); +// // DID update contains auth_key, but signature is generated using invalid_key +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = generate_base_did_update_operation(alice_did.clone()); + +// let signature = invalid_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignatureFormat +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Using an Sr25519 key as expected, but from a different seed (default = false) +// let alternative_key = get_sr25519_authentication_key(false); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = generate_base_did_update_operation(alice_did.clone()); + +// let signature = alternative_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignature +// ); +// }); +// } + +// #[test] +// fn check_max_limit_key_agreement_keys_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Max keys allowed + 1 +// let new_enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); + +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.new_key_agreement_keys = new_enc_keys; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxKeyAgreementKeysLimitExceeded +// ); +// }); +// } + +// #[test] +// fn check_max_limit_public_keys_to_remove_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Max keys allowed + 1 +// let keys_ids_to_remove = +// get_public_keys_to_remove(::MaxNewKeyAgreementKeys::get().saturating_add(1)); + +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.public_keys_to_remove = keys_ids_to_remove; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxVerificationKeysToRemoveLimitExceeded +// ); +// }); +// } + +// #[test] +// fn check_url_too_long_did_update() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Max URL length allowed + 1 +// let new_endpoint_url = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); + +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// operation.new_endpoint_url = Some(new_endpoint_url); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxUrlLengthExceeded +// ); +// }); +// } + +// #[test] +// fn check_currently_active_authentication_key_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// // Trying to remove the currently active authentication key +// operation.public_keys_to_remove = vec![generate_key_id( +// &did::DidVerificationKey::from(auth_key.public()).into(), +// )] +// .iter() +// .copied() +// .collect::>(); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::CurrentlyActiveKey +// ); +// }); +// } + +// #[test] +// fn check_currently_active_delegation_key_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let del_key = get_ecdsa_delegation_key(true); + +// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// // Trying to remove the currently active delegation key +// operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] +// .iter() +// .copied() +// .collect::>(); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::CurrentlyActiveKey +// ); +// }); +// } + +// #[test] +// fn check_currently_active_attestation_key_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let att_key = get_sr25519_attestation_key(true); + +// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// // Trying to remove the currently active attestation key +// operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] +// .iter() +// .copied() +// .collect::>(); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::CurrentlyActiveKey +// ); +// }); +// } + +// #[test] +// fn check_verification_key_not_present_update() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let key_to_delete = get_sr25519_authentication_key(true); + +// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Remove both attestation and delegation key +// let mut operation = generate_base_did_update_operation(alice_did.clone()); +// // Trying to remove the currently active authentication key +// operation.public_keys_to_remove = vec![generate_key_id( +// &did::DidVerificationKey::from(key_to_delete.public()).into(), +// )] +// .iter() +// .copied() +// .collect::>(); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, old_did_details)]) +// .build(None); + +// let new_block_number: TestBlockNumber = 1; + +// ext.execute_with(|| { +// System::set_block_number(new_block_number); +// assert_noop!( +// Did::submit_did_update_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::VerificationKeyNotPresent +// ); +// }); +// } + +// // submit_did_delete_operation + +// #[test] +// fn check_successful_deletion() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// // Update all keys, URL endpoint and tx counter. No keys are removed in this +// // test +// let operation = generate_base_did_delete_operation(alice_did.clone()); + +// // Generate signature using the old authentication key +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did.clone(), did_details)]) +// .build(None); + +// ext.execute_with(|| { +// assert_ok!(Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); + +// assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); + +// // Re-adding the same DID identifier, which should not fail. +// let operation = generate_base_did_creation_operation(); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_ok!(Did::submit_did_create_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// )); +// }); +// } + +// #[test] +// fn check_did_not_present_deletion() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + +// // Update all keys, URL endpoint and tx counter. No keys are removed in this +// // test +// let operation = generate_base_did_delete_operation(alice_did); + +// // Generate signature using the old authentication key +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::DidNotPresent +// ); +// }); +// } + +// #[test] +// fn check_max_tx_counter_did_deletion() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = u64::MAX; +// let operation = generate_base_did_delete_operation(alice_did.clone()); + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::MaxTxCounterValue +// ); +// }); +// } + +// #[test] +// fn check_smaller_tx_counter_did_deletion() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = 1; +// let mut operation = generate_base_did_delete_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter - 1; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_equal_tx_counter_did_deletion() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = 1; +// let mut operation = generate_base_did_delete_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_too_large_tx_counter_did_deletion() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let mut operation = generate_base_did_delete_operation(alice_did.clone()); +// operation.tx_counter = mock_did.last_tx_counter + 2; + +// let signature = auth_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_format_did_deletion() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// // Using an Sr25519 key where an Ed25519 is expected +// let invalid_key = get_sr25519_authentication_key(true); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = generate_base_did_delete_operation(alice_did.clone()); + +// let signature = invalid_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignatureFormat +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_did_deletion() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Using an Sr25519 key as expected, but from a different seed (default = false) +// let alternative_key = get_sr25519_authentication_key(false); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = generate_base_did_delete_operation(alice_did.clone()); + +// let signature = alternative_key.sign(operation.encode().as_ref()); + +// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_delete_operation( +// Origin::signed(DEFAULT_ACCOUNT), +// operation.clone(), +// did::DidSignature::from(signature), +// ), +// did::Error::::InvalidSignature +// ); +// }); +// } + +// // submit_did_call #[test] -fn check_call_authentication_key_error() { +fn check_no_key_error() { let auth_key = get_sr25519_authentication_key(true); let did = get_did_identifier_from_sr25519_key(auth_key.public()); let caller = DEFAULT_ACCOUNT; - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - // CType already added to storage - let mut ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(get_authentication_key_test_input(), did.clone())]) - .build(Some(ext)); - - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_err!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation), - did::DidSignature::from(signature) - ), - ctype::Error::::CTypeAlreadyExists - ); - }); -} - -// Internal function: verify_operation_validity_and_increase_did_nonce - -#[test] -fn check_authentication_successful_operation_verification() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter + 1, - }; - - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_ok!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ) - ); - }); - - // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!( - did_details.get_tx_counter_value(), - mock_did.get_tx_counter_value() + 1u64 - ); -} - -#[test] -fn check_attestation_successful_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let att_key = get_ecdsa_attestation_key(true); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::AssertionMethod, - tx_counter: mock_did.last_tx_counter + 1, - }; - - let signature = att_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_ok!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ) - ); - }); - - // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!( - did_details.get_tx_counter_value(), - mock_did.get_tx_counter_value() + 1u64 - ); -} - -#[test] -fn check_delegation_successful_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let del_key = get_ed25519_delegation_key(true); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, - tx_counter: mock_did.last_tx_counter + 1, - }; - let signature = del_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_ok!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ) - ); - }); - - // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!( - did_details.get_tx_counter_value(), - mock_did.get_tx_counter_value() + 1u64 - ); -} - -#[test] -fn check_did_not_present_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let del_key = get_ed25519_delegation_key(true); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did, - verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, - tx_counter: mock_did.last_tx_counter + 1, - }; - let signature = del_key.sign(&operation.encode()); - let mut ext = ExtBuilder::default().build(None); - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::StorageError(did::StorageError::DidNotPresent) - ); - }); -} - -#[test] -fn check_max_tx_counter_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = u64::MAX; - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter, - }; - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::StorageError(did::StorageError::MaxTxCounterValue) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_smaller_counter_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - mock_did.last_tx_counter = 1; - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter - 1, - }; - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::SignatureError(did::SignatureError::InvalidNonce) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_equal_counter_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter, - }; - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::SignatureError(did::SignatureError::InvalidNonce) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_too_large_counter_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter + 2, - }; - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::SignatureError(did::SignatureError::InvalidNonce) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_verification_key_not_present_operation_verification() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let verification_key_required = did::DidVerificationKeyRelationship::CapabilityInvocation; - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: verification_key_required, - tx_counter: mock_did.last_tx_counter + 1, - }; - - let signature = auth_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::StorageError(did::StorageError::DidKeyNotPresent(verification_key_required)) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_invalid_signature_format_operation_verification() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Expected an Sr25519, given an Ed25519 - let invalid_key = get_ed25519_authentication_key(true); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter + 1, - }; - - let signature = invalid_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); - - ext.execute_with(|| { - assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::SignatureError(did::SignatureError::InvalidSignatureFormat) - ); - }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -#[test] -fn check_invalid_signature_operation_verification() { - let auth_key = get_sr25519_authentication_key(true); - let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - // Using same key type but different seed (default = false) - let alternative_key = get_sr25519_authentication_key(false); - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = TestDidOperation { - did: alice_did.clone(), - verification_key_type: did::DidVerificationKeyRelationship::Authentication, - tx_counter: mock_did.last_tx_counter + 1, - }; - - let signature = alternative_key.sign(&operation.encode()); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did.clone())]) - .build(None); + // CapabilityInvocation is not supported at the moment, so it should return no + // key and hence the operation fail. + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityInvocation, did); + let signature = did::DidSignature::from(ed25519::Signature::default()); ext.execute_with(|| { assert_noop!( - Did::verify_operation_validity_and_increase_did_nonce::( - &operation, - &did::DidSignature::from(signature) - ), - did::DidError::SignatureError(did::SignatureError::InvalidSignature) + Did::submit_did_call(Origin::signed(caller), Box::new(call_operation), signature), + did::Error::::UnsupportedDidAuthorizationCall ); }); - - // Verify that the DID tx counter has not increased - let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); - assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -} - -// Internal function: HttpUrl try_from - -#[test] -fn check_http_url() { - assert_ok!(did::HttpUrl::try_from("http://kilt.io".as_bytes())); - - assert_ok!(did::HttpUrl::try_from("https://kilt.io".as_bytes())); - - assert_ok!(did::HttpUrl::try_from( - "https://super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() - )); - - // All other valid ASCII characters - assert_ok!(did::HttpUrl::try_from("http://:/?#[]@!$&'()*+,;=-._~".as_bytes())); - - assert_eq!( - did::HttpUrl::try_from("".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - // Non-printable ASCII characters - assert_eq!( - did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - // Some invalid ASCII characters - assert_eq!( - did::HttpUrl::try_from("http://kilt.io/".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - // Non-ASCII characters - assert_eq!( - did::HttpUrl::try_from("http://¶.com".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - assert_eq!( - did::HttpUrl::try_from("htt://kilt.io".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - assert_eq!( - did::HttpUrl::try_from("httpss://kilt.io".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); -} - -// Internal function: FtpUrl try_from - -#[test] -fn check_ftp_url() { - assert_ok!(did::FtpUrl::try_from("ftp://kilt.io".as_bytes())); - - assert_ok!(did::FtpUrl::try_from("ftps://kilt.io".as_bytes())); - - assert_ok!(did::FtpUrl::try_from( - "ftps://user@super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() - )); - - // All other valid ASCII characters - assert_ok!(did::FtpUrl::try_from("ftps://:/?#[]@%!$&'()*+,;=-._~".as_bytes())); - - assert_eq!( - did::FtpUrl::try_from("".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - // Non-printable ASCII characters - assert_eq!( - did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - // Some invalid ASCII characters - assert_eq!( - did::FtpUrl::try_from("ftp://kilt.io/".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - // Non-ASCII characters - assert_eq!( - did::FtpUrl::try_from("ftps://¶.com".as_bytes()), - Err(did::UrlError::InvalidUrlEncoding) - ); - - assert_eq!( - did::FtpUrl::try_from("ft://kilt.io".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - assert_eq!( - did::HttpUrl::try_from("ftpss://kilt.io".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); } -// Internal function: IpfsUrl try_from - -#[test] -fn check_ipfs_url() { - // Base58 address - assert_ok!(did::IpfsUrl::try_from( - "ipfs://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes() - )); - - // Base32 address (at the moment, padding characters can appear anywhere in the - // string) - assert_ok!(did::IpfsUrl::try_from( - "ipfs://OQQHHHTGMMYDQQ364YB4GDE=HREJQL==".as_bytes() - )); - - assert_eq!( - did::IpfsUrl::try_from("".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - assert_eq!( - did::IpfsUrl::try_from( - "ipfs://¶ -QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" - .as_bytes() - ), - Err(did::UrlError::InvalidUrlEncoding) - ); - - assert_eq!( - did::IpfsUrl::try_from("ipf://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes()), - Err(did::UrlError::InvalidUrlScheme) - ); - - assert_eq!( - did::IpfsUrl::try_from( - "ipfss:// -QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" - .as_bytes() - ), - Err(did::UrlError::InvalidUrlScheme) - ); -} +// #[test] +// fn check_did_not_found_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; + +// // No DID added +// let mut ext = ExtBuilder::default().build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::DidNotPresent +// ); +// }); +// } + +// #[test] +// fn check_max_counter_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = u64::MAX; + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::MaxTxCounterValue +// ); +// }); +// } + +// #[test] +// fn check_too_small_tx_counter_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = 1u64; + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// call_operation.tx_counter = 0u64; +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_equal_tx_counter_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did.clone())]) +// .build(None); + +// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// call_operation.tx_counter = mock_did.last_tx_counter; +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_too_large_tx_counter_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did.clone())]) +// .build(None); + +// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// call_operation.tx_counter = mock_did.last_tx_counter + 2u64; +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::InvalidNonce +// ); +// }); +// } + +// #[test] +// fn check_verification_key_not_present_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// // The operation requires the delegation key that is currently not stored for +// // the given DID. +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::VerificationKeyNotPresent +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_format_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let alternative_auth_key = get_ed25519_authentication_key(true); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::InvalidSignatureFormat +// ); +// }); +// } + +// #[test] +// fn check_invalid_signature_call_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let alternative_auth_key = get_sr25519_authentication_key(false); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_noop!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// did::Error::::InvalidSignature +// ); +// }); +// } + +// #[test] +// fn check_call_attestation_key_successful() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let attestation_key = get_ed25519_attestation_key(true); + +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); +// let signature = attestation_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_ok!(Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// )); +// }); +// } + +// #[test] +// fn check_call_attestation_key_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let attestation_key = get_ed25519_attestation_key(true); + +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); + +// let ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); +// // CType already added to storage +// let mut ext = ctype_mock::ExtBuilder::default() +// .with_ctypes(vec![(get_attestation_key_test_input(), did.clone())]) +// .build(Some(ext)); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); +// let signature = attestation_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_err!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// ctype::Error::::CTypeAlreadyExists +// ); +// }); +// } + +// #[test] +// fn check_call_delegation_key_successful() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let delegation_key = get_ed25519_delegation_key(true); + +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); +// let signature = delegation_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_ok!(Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// )); +// }); +// } + +// #[test] +// fn check_call_delegation_key_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; +// let delegation_key = get_ed25519_delegation_key(true); + +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); + +// let ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); +// // CType already added to storage +// let mut ext = ctype_mock::ExtBuilder::default() +// .with_ctypes(vec![(get_delegation_key_test_input(), did.clone())]) +// .build(Some(ext)); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); +// let signature = delegation_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_err!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// ctype::Error::::CTypeAlreadyExists +// ); +// }); +// } + +// #[test] +// fn check_call_authentication_key_successful() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; + +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_ok!(Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// )); +// }); +// } + +// #[test] +// fn check_call_authentication_key_error() { +// let auth_key = get_sr25519_authentication_key(true); +// let did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let caller = DEFAULT_ACCOUNT; + +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + +// let ext = ExtBuilder::default() +// .with_dids(vec![(did.clone(), mock_did)]) +// .build(None); +// // CType already added to storage +// let mut ext = ctype_mock::ExtBuilder::default() +// .with_ctypes(vec![(get_authentication_key_test_input(), did.clone())]) +// .build(Some(ext)); + +// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); +// let signature = auth_key.sign(call_operation.encode().as_ref()); + +// ext.execute_with(|| { +// assert_err!( +// Did::submit_did_call( +// Origin::signed(caller), +// Box::new(call_operation), +// did::DidSignature::from(signature) +// ), +// ctype::Error::::CTypeAlreadyExists +// ); +// }); +// } + +// // Internal function: verify_operation_validity_and_increase_did_nonce + +// #[test] +// fn check_authentication_successful_operation_verification() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter + 1, +// }; + +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_ok!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ) +// ); +// }); + +// // Verify that the DID tx counter has increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!( +// did_details.get_tx_counter_value(), +// mock_did.get_tx_counter_value() + 1u64 +// ); +// } + +// #[test] +// fn check_attestation_successful_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let att_key = get_ecdsa_attestation_key(true); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::AssertionMethod, +// tx_counter: mock_did.last_tx_counter + 1, +// }; + +// let signature = att_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_ok!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ) +// ); +// }); + +// // Verify that the DID tx counter has increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!( +// did_details.get_tx_counter_value(), +// mock_did.get_tx_counter_value() + 1u64 +// ); +// } + +// #[test] +// fn check_delegation_successful_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let del_key = get_ed25519_delegation_key(true); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, +// tx_counter: mock_did.last_tx_counter + 1, +// }; +// let signature = del_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_ok!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ) +// ); +// }); + +// // Verify that the DID tx counter has increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!( +// did_details.get_tx_counter_value(), +// mock_did.get_tx_counter_value() + 1u64 +// ); +// } + +// #[test] +// fn check_did_not_present_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let del_key = get_ed25519_delegation_key(true); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did, +// verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, +// tx_counter: mock_did.last_tx_counter + 1, +// }; +// let signature = del_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default().build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::StorageError(did::StorageError::DidNotPresent) +// ); +// }); +// } + +// #[test] +// fn check_max_tx_counter_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = u64::MAX; +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter, +// }; +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::StorageError(did::StorageError::MaxTxCounterValue) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_smaller_counter_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// mock_did.last_tx_counter = 1; +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter - 1, +// }; +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::SignatureError(did::SignatureError::InvalidNonce) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_equal_counter_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter, +// }; +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::SignatureError(did::SignatureError::InvalidNonce) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_too_large_counter_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter + 2, +// }; +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::SignatureError(did::SignatureError::InvalidNonce) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_verification_key_not_present_operation_verification() { +// let auth_key = get_ed25519_authentication_key(true); +// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let verification_key_required = did::DidVerificationKeyRelationship::CapabilityInvocation; +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: verification_key_required, +// tx_counter: mock_did.last_tx_counter + 1, +// }; + +// let signature = auth_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::StorageError(did::StorageError::DidKeyNotPresent(verification_key_required)) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_invalid_signature_format_operation_verification() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Expected an Sr25519, given an Ed25519 +// let invalid_key = get_ed25519_authentication_key(true); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter + 1, +// }; + +// let signature = invalid_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::SignatureError(did::SignatureError::InvalidSignatureFormat) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// #[test] +// fn check_invalid_signature_operation_verification() { +// let auth_key = get_sr25519_authentication_key(true); +// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); +// // Using same key type but different seed (default = false) +// let alternative_key = get_sr25519_authentication_key(false); +// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); +// let operation = TestDidOperation { +// did: alice_did.clone(), +// verification_key_type: did::DidVerificationKeyRelationship::Authentication, +// tx_counter: mock_did.last_tx_counter + 1, +// }; + +// let signature = alternative_key.sign(&operation.encode()); + +// let mut ext = ExtBuilder::default() +// .with_dids(vec![(alice_did, mock_did.clone())]) +// .build(None); + +// ext.execute_with(|| { +// assert_noop!( +// Did::verify_signature_and_increase_nonce::( +// &operation, +// &did::DidSignature::from(signature) +// ), +// did::DidError::SignatureError(did::SignatureError::InvalidSignature) +// ); +// }); + +// // Verify that the DID tx counter has not increased +// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); +// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); +// } + +// // Internal function: HttpUrl try_from + +// #[test] +// fn check_http_url() { +// assert_ok!(did::HttpUrl::try_from("http://kilt.io".as_bytes())); + +// assert_ok!(did::HttpUrl::try_from("https://kilt.io".as_bytes())); + +// assert_ok!(did::HttpUrl::try_from( +// "https://super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() +// )); + +// // All other valid ASCII characters +// assert_ok!(did::HttpUrl::try_from("http://:/?#[]@!$&'()*+,;=-._~".as_bytes())); + +// assert_eq!( +// did::HttpUrl::try_from("".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// // Non-printable ASCII characters +// assert_eq!( +// did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// // Some invalid ASCII characters +// assert_eq!( +// did::HttpUrl::try_from("http://kilt.io/".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// // Non-ASCII characters +// assert_eq!( +// did::HttpUrl::try_from("http://¶.com".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// assert_eq!( +// did::HttpUrl::try_from("htt://kilt.io".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// assert_eq!( +// did::HttpUrl::try_from("httpss://kilt.io".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); +// } + +// // Internal function: FtpUrl try_from + +// #[test] +// fn check_ftp_url() { +// assert_ok!(did::FtpUrl::try_from("ftp://kilt.io".as_bytes())); + +// assert_ok!(did::FtpUrl::try_from("ftps://kilt.io".as_bytes())); + +// assert_ok!(did::FtpUrl::try_from( +// "ftps://user@super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() +// )); + +// // All other valid ASCII characters +// assert_ok!(did::FtpUrl::try_from("ftps://:/?#[]@%!$&'()*+,;=-._~".as_bytes())); + +// assert_eq!( +// did::FtpUrl::try_from("".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// // Non-printable ASCII characters +// assert_eq!( +// did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// // Some invalid ASCII characters +// assert_eq!( +// did::FtpUrl::try_from("ftp://kilt.io/".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// // Non-ASCII characters +// assert_eq!( +// did::FtpUrl::try_from("ftps://¶.com".as_bytes()), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// assert_eq!( +// did::FtpUrl::try_from("ft://kilt.io".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// assert_eq!( +// did::HttpUrl::try_from("ftpss://kilt.io".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); +// } + +// // Internal function: IpfsUrl try_from + +// #[test] +// fn check_ipfs_url() { +// // Base58 address +// assert_ok!(did::IpfsUrl::try_from( +// "ipfs://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes() +// )); + +// // Base32 address (at the moment, padding characters can appear anywhere in the +// // string) +// assert_ok!(did::IpfsUrl::try_from( +// "ipfs://OQQHHHTGMMYDQQ364YB4GDE=HREJQL==".as_bytes() +// )); + +// assert_eq!( +// did::IpfsUrl::try_from("".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// assert_eq!( +// did::IpfsUrl::try_from( +// "ipfs://¶ +// QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" +// .as_bytes() +// ), +// Err(did::UrlError::InvalidUrlEncoding) +// ); + +// assert_eq!( +// did::IpfsUrl::try_from("ipf://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes()), +// Err(did::UrlError::InvalidUrlScheme) +// ); + +// assert_eq!( +// did::IpfsUrl::try_from( +// "ipfss:// +// QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" +// .as_bytes() +// ), +// Err(did::UrlError::InvalidUrlScheme) +// ); +// } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 4673a03ee7..9a57e188dc 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -693,6 +693,7 @@ impl did::Config for Runtime { type Event = Event; type Call = Call; type Origin = Origin; + type EnsureOrigin = did::EnsureDidOrigin; type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index ff16c6bd31..e6bc5b6658 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -371,6 +371,7 @@ impl did::Config for Runtime { type Event = Event; type Call = Call; type Origin = Origin; + type EnsureOrigin = did::EnsureDidOrigin; type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; From 99ba36229d567a0ec83bbd06a22e690c1eafe5fb Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Tue, 3 Aug 2021 10:52:39 +0200 Subject: [PATCH 02/21] feat: refactor complete --- Cargo.lock | 330 +-- pallets/did/src/benchmarking.rs | 27 +- pallets/did/src/errors.rs | 2 + pallets/did/src/lib.rs | 8 +- pallets/did/src/mock.rs | 37 +- pallets/did/src/tests.rs | 3509 ++++++++++++++----------------- runtimes/peregrine/src/lib.rs | 12 +- runtimes/standalone/src/lib.rs | 12 +- 8 files changed, 1794 insertions(+), 2143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 131445d772..e8e3039cd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2314,7 +2314,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "fork-tree" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", ] @@ -2332,7 +2332,7 @@ dependencies = [ [[package]] name = "frame-benchmarking" version = "3.1.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -2351,7 +2351,7 @@ dependencies = [ [[package]] name = "frame-benchmarking-cli" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "Inflector", "chrono", @@ -2374,7 +2374,7 @@ dependencies = [ [[package]] name = "frame-election-provider-support" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -2387,7 +2387,7 @@ dependencies = [ [[package]] name = "frame-executive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -2402,7 +2402,7 @@ dependencies = [ [[package]] name = "frame-metadata" version = "13.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "serde", @@ -2413,7 +2413,7 @@ dependencies = [ [[package]] name = "frame-support" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "bitflags", "frame-metadata", @@ -2440,7 +2440,7 @@ dependencies = [ [[package]] name = "frame-support-procedural" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "Inflector", "frame-support-procedural-tools", @@ -2452,7 +2452,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support-procedural-tools-derive", "proc-macro-crate 1.0.0", @@ -2464,7 +2464,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools-derive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro2 1.0.27", "quote 1.0.9", @@ -2474,7 +2474,7 @@ dependencies = [ [[package]] name = "frame-system" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "impl-trait-for-tuples", @@ -2491,7 +2491,7 @@ dependencies = [ [[package]] name = "frame-system-benchmarking" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -2505,7 +2505,7 @@ dependencies = [ [[package]] name = "frame-system-rpc-runtime-api" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-api", @@ -2514,7 +2514,7 @@ dependencies = [ [[package]] name = "frame-try-runtime" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "parity-scale-codec", @@ -4515,7 +4515,7 @@ dependencies = [ [[package]] name = "max-encoded-len" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-trait-for-tuples", "max-encoded-len-derive", @@ -4526,7 +4526,7 @@ dependencies = [ [[package]] name = "max-encoded-len-derive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro-crate 1.0.0", "proc-macro2 1.0.27", @@ -4870,7 +4870,7 @@ dependencies = [ [[package]] name = "node-executor" version = "2.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "node-primitives", @@ -4888,7 +4888,7 @@ dependencies = [ [[package]] name = "node-primitives" version = "2.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-system", "parity-scale-codec", @@ -4900,7 +4900,7 @@ dependencies = [ [[package]] name = "node-runtime" version = "2.0.1" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-election-provider-support", @@ -5135,7 +5135,7 @@ dependencies = [ [[package]] name = "pallet-assets" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5149,7 +5149,7 @@ dependencies = [ [[package]] name = "pallet-aura" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5165,7 +5165,7 @@ dependencies = [ [[package]] name = "pallet-authority-discovery" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5180,7 +5180,7 @@ dependencies = [ [[package]] name = "pallet-authorship" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5194,7 +5194,7 @@ dependencies = [ [[package]] name = "pallet-babe" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5217,7 +5217,7 @@ dependencies = [ [[package]] name = "pallet-balances" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5247,7 +5247,7 @@ dependencies = [ [[package]] name = "pallet-bounties" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5281,7 +5281,7 @@ dependencies = [ [[package]] name = "pallet-collective" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5297,7 +5297,7 @@ dependencies = [ [[package]] name = "pallet-contracts" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "bitflags", "frame-support", @@ -5320,7 +5320,7 @@ dependencies = [ [[package]] name = "pallet-contracts-primitives" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "bitflags", "parity-scale-codec", @@ -5333,7 +5333,7 @@ dependencies = [ [[package]] name = "pallet-contracts-proc-macro" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro2 1.0.27", "quote 1.0.9", @@ -5343,7 +5343,7 @@ dependencies = [ [[package]] name = "pallet-contracts-rpc-runtime-api" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "pallet-contracts-primitives", "parity-scale-codec", @@ -5355,7 +5355,7 @@ dependencies = [ [[package]] name = "pallet-democracy" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5370,7 +5370,7 @@ dependencies = [ [[package]] name = "pallet-election-provider-multi-phase" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-election-provider-support", "frame-support", @@ -5389,7 +5389,7 @@ dependencies = [ [[package]] name = "pallet-elections-phragmen" version = "4.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5405,7 +5405,7 @@ dependencies = [ [[package]] name = "pallet-gilt" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5419,7 +5419,7 @@ dependencies = [ [[package]] name = "pallet-grandpa" version = "3.1.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5441,7 +5441,7 @@ dependencies = [ [[package]] name = "pallet-identity" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "enumflags2", "frame-benchmarking", @@ -5456,7 +5456,7 @@ dependencies = [ [[package]] name = "pallet-im-online" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5475,7 +5475,7 @@ dependencies = [ [[package]] name = "pallet-indices" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5491,7 +5491,7 @@ dependencies = [ [[package]] name = "pallet-lottery" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5503,7 +5503,7 @@ dependencies = [ [[package]] name = "pallet-membership" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5518,7 +5518,7 @@ dependencies = [ [[package]] name = "pallet-mmr" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "ckb-merkle-mountain-range", "frame-benchmarking", @@ -5535,7 +5535,7 @@ dependencies = [ [[package]] name = "pallet-mmr-primitives" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5551,7 +5551,7 @@ dependencies = [ [[package]] name = "pallet-mmr-rpc" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "jsonrpc-core", "jsonrpc-core-client", @@ -5569,7 +5569,7 @@ dependencies = [ [[package]] name = "pallet-multisig" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5583,7 +5583,7 @@ dependencies = [ [[package]] name = "pallet-nicks" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5596,7 +5596,7 @@ dependencies = [ [[package]] name = "pallet-offences" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5612,7 +5612,7 @@ dependencies = [ [[package]] name = "pallet-proxy" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5627,7 +5627,7 @@ dependencies = [ [[package]] name = "pallet-randomness-collective-flip" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5640,7 +5640,7 @@ dependencies = [ [[package]] name = "pallet-recovery" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "enumflags2", "frame-support", @@ -5654,7 +5654,7 @@ dependencies = [ [[package]] name = "pallet-scheduler" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5669,7 +5669,7 @@ dependencies = [ [[package]] name = "pallet-session" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5689,7 +5689,7 @@ dependencies = [ [[package]] name = "pallet-society" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5702,7 +5702,7 @@ dependencies = [ [[package]] name = "pallet-staking" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-election-provider-support", @@ -5726,7 +5726,7 @@ dependencies = [ [[package]] name = "pallet-staking-reward-curve" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro-crate 1.0.0", "proc-macro2 1.0.27", @@ -5737,7 +5737,7 @@ dependencies = [ [[package]] name = "pallet-sudo" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5750,7 +5750,7 @@ dependencies = [ [[package]] name = "pallet-timestamp" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5768,7 +5768,7 @@ dependencies = [ [[package]] name = "pallet-tips" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5782,7 +5782,7 @@ dependencies = [ [[package]] name = "pallet-transaction-payment" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5798,7 +5798,7 @@ dependencies = [ [[package]] name = "pallet-transaction-payment-rpc" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "jsonrpc-core", "jsonrpc-core-client", @@ -5815,7 +5815,7 @@ dependencies = [ [[package]] name = "pallet-transaction-payment-rpc-runtime-api" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "pallet-transaction-payment", "parity-scale-codec", @@ -5826,7 +5826,7 @@ dependencies = [ [[package]] name = "pallet-transaction-storage" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-support", "frame-system", @@ -5843,7 +5843,7 @@ dependencies = [ [[package]] name = "pallet-treasury" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5859,7 +5859,7 @@ dependencies = [ [[package]] name = "pallet-uniques" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5873,7 +5873,7 @@ dependencies = [ [[package]] name = "pallet-utility" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-benchmarking", "frame-support", @@ -5888,7 +5888,7 @@ dependencies = [ [[package]] name = "pallet-vesting" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "enumflags2", "frame-benchmarking", @@ -8167,7 +8167,7 @@ dependencies = [ [[package]] name = "remote-externalities" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "env_logger 0.8.4", "hex", @@ -8471,7 +8471,7 @@ dependencies = [ [[package]] name = "sc-allocator" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "log", "sp-core", @@ -8483,7 +8483,7 @@ dependencies = [ [[package]] name = "sc-authority-discovery" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -8512,7 +8512,7 @@ dependencies = [ [[package]] name = "sc-basic-authorship" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "futures-timer 3.0.2", @@ -8535,7 +8535,7 @@ dependencies = [ [[package]] name = "sc-block-builder" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sc-client-api", @@ -8551,7 +8551,7 @@ dependencies = [ [[package]] name = "sc-chain-spec" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", @@ -8571,7 +8571,7 @@ dependencies = [ [[package]] name = "sc-chain-spec-derive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro-crate 1.0.0", "proc-macro2 1.0.27", @@ -8582,7 +8582,7 @@ dependencies = [ [[package]] name = "sc-cli" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "chrono", "fdlimit", @@ -8620,7 +8620,7 @@ dependencies = [ [[package]] name = "sc-client-api" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "fnv", @@ -8654,7 +8654,7 @@ dependencies = [ [[package]] name = "sc-client-db" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "blake2-rfc", "hash-db", @@ -8684,7 +8684,7 @@ dependencies = [ [[package]] name = "sc-consensus" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "parking_lot 0.11.1", @@ -8697,7 +8697,7 @@ dependencies = [ [[package]] name = "sc-consensus-aura" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -8728,7 +8728,7 @@ dependencies = [ [[package]] name = "sc-consensus-babe" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -8774,7 +8774,7 @@ dependencies = [ [[package]] name = "sc-consensus-babe-rpc" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "futures 0.3.15", @@ -8798,7 +8798,7 @@ dependencies = [ [[package]] name = "sc-consensus-epochs" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "fork-tree", "parity-scale-codec", @@ -8811,7 +8811,7 @@ dependencies = [ [[package]] name = "sc-consensus-slots" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "futures 0.3.15", @@ -8839,7 +8839,7 @@ dependencies = [ [[package]] name = "sc-consensus-uncles" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "sc-client-api", "sp-authorship", @@ -8850,7 +8850,7 @@ dependencies = [ [[package]] name = "sc-executor" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "lazy_static", @@ -8879,7 +8879,7 @@ dependencies = [ [[package]] name = "sc-executor-common" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "parity-scale-codec", @@ -8896,7 +8896,7 @@ dependencies = [ [[package]] name = "sc-executor-wasmi" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "log", "parity-scale-codec", @@ -8911,7 +8911,7 @@ dependencies = [ [[package]] name = "sc-executor-wasmtime" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "cfg-if 1.0.0", "libc", @@ -8930,7 +8930,7 @@ dependencies = [ [[package]] name = "sc-finality-grandpa" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -8971,7 +8971,7 @@ dependencies = [ [[package]] name = "sc-finality-grandpa-rpc" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "finality-grandpa", @@ -8995,7 +8995,7 @@ dependencies = [ [[package]] name = "sc-finality-grandpa-warp-sync" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "futures 0.3.15", @@ -9016,7 +9016,7 @@ dependencies = [ [[package]] name = "sc-informant" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "ansi_term 0.12.1", "futures 0.3.15", @@ -9034,7 +9034,7 @@ dependencies = [ [[package]] name = "sc-keystore" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -9054,7 +9054,7 @@ dependencies = [ [[package]] name = "sc-light" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "hash-db", "lazy_static", @@ -9073,7 +9073,7 @@ dependencies = [ [[package]] name = "sc-network" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-std", "async-trait", @@ -9126,7 +9126,7 @@ dependencies = [ [[package]] name = "sc-network-gossip" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "futures-timer 3.0.2", @@ -9143,7 +9143,7 @@ dependencies = [ [[package]] name = "sc-offchain" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "bytes 0.5.6", "fnv", @@ -9171,7 +9171,7 @@ dependencies = [ [[package]] name = "sc-peerset" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "libp2p", @@ -9184,7 +9184,7 @@ dependencies = [ [[package]] name = "sc-proposer-metrics" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "log", "substrate-prometheus-endpoint", @@ -9193,7 +9193,7 @@ dependencies = [ [[package]] name = "sc-rpc" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "hash-db", @@ -9228,7 +9228,7 @@ dependencies = [ [[package]] name = "sc-rpc-api" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "futures 0.3.15", @@ -9253,7 +9253,7 @@ dependencies = [ [[package]] name = "sc-rpc-server" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.1.31", "jsonrpc-core", @@ -9271,7 +9271,7 @@ dependencies = [ [[package]] name = "sc-service" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "directories", @@ -9337,7 +9337,7 @@ dependencies = [ [[package]] name = "sc-state-db" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "log", "parity-scale-codec", @@ -9352,7 +9352,7 @@ dependencies = [ [[package]] name = "sc-sync-state-rpc" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "jsonrpc-core", "jsonrpc-core-client", @@ -9372,7 +9372,7 @@ dependencies = [ [[package]] name = "sc-telemetry" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "chrono", "futures 0.3.15", @@ -9392,7 +9392,7 @@ dependencies = [ [[package]] name = "sc-tracing" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "ansi_term 0.12.1", "atty", @@ -9429,7 +9429,7 @@ dependencies = [ [[package]] name = "sc-tracing-proc-macro" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro-crate 1.0.0", "proc-macro2 1.0.27", @@ -9440,7 +9440,7 @@ dependencies = [ [[package]] name = "sc-transaction-graph" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "futures 0.3.15", @@ -9462,7 +9462,7 @@ dependencies = [ [[package]] name = "sc-transaction-pool" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "intervalier", @@ -9933,7 +9933,7 @@ dependencies = [ [[package]] name = "sp-api" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "hash-db", "log", @@ -9950,7 +9950,7 @@ dependencies = [ [[package]] name = "sp-api-proc-macro" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "blake2-rfc", "proc-macro-crate 1.0.0", @@ -9962,7 +9962,7 @@ dependencies = [ [[package]] name = "sp-application-crypto" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "max-encoded-len", "parity-scale-codec", @@ -9975,7 +9975,7 @@ dependencies = [ [[package]] name = "sp-arithmetic" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "integer-sqrt", "num-traits", @@ -9989,7 +9989,7 @@ dependencies = [ [[package]] name = "sp-authority-discovery" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-api", @@ -10001,7 +10001,7 @@ dependencies = [ [[package]] name = "sp-authorship" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "parity-scale-codec", @@ -10013,7 +10013,7 @@ dependencies = [ [[package]] name = "sp-block-builder" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-api", @@ -10025,7 +10025,7 @@ dependencies = [ [[package]] name = "sp-blockchain" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "log", @@ -10043,7 +10043,7 @@ dependencies = [ [[package]] name = "sp-consensus" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "futures 0.3.15", @@ -10070,7 +10070,7 @@ dependencies = [ [[package]] name = "sp-consensus-aura" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "parity-scale-codec", @@ -10087,7 +10087,7 @@ dependencies = [ [[package]] name = "sp-consensus-babe" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "merlin", @@ -10109,7 +10109,7 @@ dependencies = [ [[package]] name = "sp-consensus-slots" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-arithmetic", @@ -10119,7 +10119,7 @@ dependencies = [ [[package]] name = "sp-consensus-vrf" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "schnorrkel", @@ -10131,7 +10131,7 @@ dependencies = [ [[package]] name = "sp-core" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "base58", "blake2-rfc", @@ -10176,7 +10176,7 @@ dependencies = [ [[package]] name = "sp-database" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "kvdb", "parking_lot 0.11.1", @@ -10185,7 +10185,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro2 1.0.27", "quote 1.0.9", @@ -10195,7 +10195,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "environmental", "parity-scale-codec", @@ -10206,7 +10206,7 @@ dependencies = [ [[package]] name = "sp-finality-grandpa" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "finality-grandpa", "log", @@ -10223,7 +10223,7 @@ dependencies = [ [[package]] name = "sp-inherents" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "impl-trait-for-tuples", @@ -10237,7 +10237,7 @@ dependencies = [ [[package]] name = "sp-io" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "hash-db", @@ -10262,7 +10262,7 @@ dependencies = [ [[package]] name = "sp-keyring" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "lazy_static", "sp-core", @@ -10273,7 +10273,7 @@ dependencies = [ [[package]] name = "sp-keystore" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "derive_more 0.99.16", @@ -10290,7 +10290,7 @@ dependencies = [ [[package]] name = "sp-maybe-compressed-blob" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "ruzstd", "zstd", @@ -10299,7 +10299,7 @@ dependencies = [ [[package]] name = "sp-npos-elections" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "serde", @@ -10312,7 +10312,7 @@ dependencies = [ [[package]] name = "sp-npos-elections-compact" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "proc-macro-crate 1.0.0", "proc-macro2 1.0.27", @@ -10323,7 +10323,7 @@ dependencies = [ [[package]] name = "sp-offchain" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "sp-api", "sp-core", @@ -10333,7 +10333,7 @@ dependencies = [ [[package]] name = "sp-panic-handler" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "backtrace", ] @@ -10341,7 +10341,7 @@ dependencies = [ [[package]] name = "sp-rpc" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "rustc-hash", "serde", @@ -10352,7 +10352,7 @@ dependencies = [ [[package]] name = "sp-runtime" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "either", "hash256-std-hasher", @@ -10374,7 +10374,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", @@ -10391,7 +10391,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "Inflector", "proc-macro-crate 1.0.0", @@ -10403,7 +10403,7 @@ dependencies = [ [[package]] name = "sp-sandbox" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-core", @@ -10416,7 +10416,7 @@ dependencies = [ [[package]] name = "sp-serializer" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "serde", "serde_json", @@ -10425,7 +10425,7 @@ dependencies = [ [[package]] name = "sp-session" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-api", @@ -10438,7 +10438,7 @@ dependencies = [ [[package]] name = "sp-staking" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "sp-runtime", @@ -10448,7 +10448,7 @@ dependencies = [ [[package]] name = "sp-state-machine" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "hash-db", "log", @@ -10471,12 +10471,12 @@ dependencies = [ [[package]] name = "sp-std" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" [[package]] name = "sp-storage" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-serde", "parity-scale-codec", @@ -10489,7 +10489,7 @@ dependencies = [ [[package]] name = "sp-tasks" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "log", "sp-core", @@ -10502,7 +10502,7 @@ dependencies = [ [[package]] name = "sp-timestamp" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "futures-timer 3.0.2", @@ -10519,7 +10519,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "erased-serde", "log", @@ -10537,7 +10537,7 @@ dependencies = [ [[package]] name = "sp-transaction-pool" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "derive_more 0.99.16", "futures 0.3.15", @@ -10553,7 +10553,7 @@ dependencies = [ [[package]] name = "sp-transaction-storage-proof" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "log", @@ -10568,7 +10568,7 @@ dependencies = [ [[package]] name = "sp-trie" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "hash-db", "memory-db", @@ -10582,7 +10582,7 @@ dependencies = [ [[package]] name = "sp-utils" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "futures-core", @@ -10594,7 +10594,7 @@ dependencies = [ [[package]] name = "sp-version" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-serde", "parity-scale-codec", @@ -10607,7 +10607,7 @@ dependencies = [ [[package]] name = "sp-version-proc-macro" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "parity-scale-codec", "proc-macro-crate 1.0.0", @@ -10619,7 +10619,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", @@ -10828,7 +10828,7 @@ dependencies = [ [[package]] name = "substrate-build-script-utils" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "platforms", ] @@ -10836,7 +10836,7 @@ dependencies = [ [[package]] name = "substrate-frame-rpc-system" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-system-rpc-runtime-api", "futures 0.3.15", @@ -10859,7 +10859,7 @@ dependencies = [ [[package]] name = "substrate-prometheus-endpoint" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-std", "derive_more 0.99.16", @@ -10873,7 +10873,7 @@ dependencies = [ [[package]] name = "substrate-test-client" version = "2.0.1" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "async-trait", "futures 0.1.31", @@ -10902,7 +10902,7 @@ dependencies = [ [[package]] name = "substrate-test-runtime" version = "2.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "cfg-if 1.0.0", "frame-support", @@ -10943,7 +10943,7 @@ dependencies = [ [[package]] name = "substrate-test-runtime-client" version = "2.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "futures 0.3.15", "parity-scale-codec", @@ -10964,7 +10964,7 @@ dependencies = [ [[package]] name = "substrate-wasm-builder" version = "4.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "ansi_term 0.12.1", "atty", @@ -11671,7 +11671,7 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "try-runtime-cli" version = "0.9.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8#1b758b2a8d151d97d2242260c465b6df9cb8a7a4" dependencies = [ "frame-try-runtime", "log", diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 802f900b10..452c654b32 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -125,21 +125,15 @@ fn generate_base_did_creation_operation() -> DidCreationOperation { fn generate_base_did_update_operation(did: DidIdentifierOf) -> DidUpdateOperation { DidUpdateOperation { - did, new_authentication_key: None, new_key_agreement_keys: BTreeSet::new(), attestation_key_update: DidVerificationKeyUpdateAction::default(), delegation_key_update: DidVerificationKeyUpdateAction::default(), new_endpoint_url: None, public_keys_to_remove: BTreeSet::new(), - tx_counter: 1u64, } } -fn generate_base_did_deletion_operation(did: DidIdentifierOf) -> DidDeletionOperation { - DidDeletionOperation { did, tx_counter: 1u64 } -} - // Must always be dispatched with the DID authentication key fn generate_base_did_call_operation(did: DidIdentifierOf) -> DidAuthorizedCallOperation { let test_call = ::Call::get_call_for_did_call_benchmark(); @@ -335,7 +329,7 @@ benchmarks! { did_update_op.new_endpoint_url = Some(new_url); let did_update_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone(), DidSignature::from(did_update_signature)) + }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -361,7 +355,6 @@ benchmarks! { &Some(expected_attestation_key_id) ); assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); - assert_eq!(stored_did.last_tx_counter, did_update_op.tx_counter); } submit_did_update_operation_sr25519_keys { @@ -397,7 +390,7 @@ benchmarks! { did_update_op.new_endpoint_url = Some(new_url); let did_update_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone(), DidSignature::from(did_update_signature)) + }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -423,7 +416,6 @@ benchmarks! { &Some(expected_attestation_key_id) ); assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); - assert_eq!(stored_did.last_tx_counter, did_update_op.tx_counter); } submit_did_update_operation_ecdsa_keys { @@ -459,7 +451,7 @@ benchmarks! { did_update_op.new_endpoint_url = Some(new_url); let did_update_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone(), DidSignature::from(did_update_signature)) + }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -485,7 +477,6 @@ benchmarks! { &Some(expected_attestation_key_id) ); assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); - assert_eq!(stored_did.last_tx_counter, did_update_op.tx_counter); } submit_did_delete_operation { @@ -496,11 +487,7 @@ benchmarks! { let did_details = get_did_base_details(DidVerificationKey::from(did_public_auth_key)); Did::::insert(&did_subject, did_details); - - let did_deletion_op = generate_base_did_deletion_operation::(did_subject.clone()); - - let did_deletion_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_deletion_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: _(RawOrigin::Signed(submitter), did_deletion_op.clone(), DidSignature::from(did_deletion_signature)) + }: _(RawOrigin::Signed(submitter)) verify { assert_eq!( Did::::get(&did_subject), @@ -520,7 +507,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) submit_did_call_sr25519_key { let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -534,7 +521,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) submit_did_call_ecdsa_key { let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -548,7 +535,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) } // impl_benchmark_test_suite! { diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index 8b37443171..0ce10ad6a6 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -64,6 +64,8 @@ pub enum SignatureError { InvalidSignature, /// The operation nonce is not equal to the current DID nonce + 1. InvalidNonce, + /// The operation required a signature which has not been provided. + SignatureRequired, } /// Error generated when validating a byte-encoded endpoint URL. diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index c9e33a95e3..bedc5393bb 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -230,6 +230,8 @@ pub mod pallet { /// The DID operation signature is invalid for the payload and the /// verification key provided. InvalidSignature, + /// The operation required a signature which has not been provided. + SignatureRequired, /// The DID with the given identifier is already present on chain. DidAlreadyPresent, /// No DID with the given identifier is present on chain. @@ -296,6 +298,7 @@ pub mod pallet { SignatureError::InvalidSignature => Self::InvalidSignature, SignatureError::InvalidSignatureFormat => Self::InvalidSignatureFormat, SignatureError::InvalidNonce => Self::InvalidNonce, + SignatureError::SignatureRequired => Self::SignatureRequired, } } } @@ -579,7 +582,7 @@ pub mod pallet { pub fn submit_did_call( origin: OriginFor, did_call: Box>, - signature: DidSignature, + signature: Option, ) -> DispatchResultWithPostInfo { ensure_signed(origin)?; @@ -632,7 +635,7 @@ impl Pallet { /// # pub fn verify_did_operation_signature_and_increase_nonce( operation: &DidAuthorizedCallOperationWithVerificationRelationship, - signature: &DidSignature, + signature: &Option, ) -> Result<(), DidError> { let mut did_details = >::get(&operation.did).ok_or(DidError::StorageError(StorageError::DidNotPresent))?; @@ -641,6 +644,7 @@ impl Pallet { // Do nothing if no key is required. DidOperationAuthorizationKey::NoKey => (), DidOperationAuthorizationKey::DidKey(verification_key_type) => { + let signature = signature.as_ref().ok_or(DidError::SignatureError(SignatureError::SignatureRequired))?; Self::validate_counter_value(operation.tx_counter, &did_details)?; // Increase the tx counter as soon as it is considered valid, no matter if the signature is valid or not. did_details.increase_tx_counter().map_err(DidError::StorageError)?; diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 5178bd9157..69baec3f0a 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -22,8 +22,6 @@ use codec::{Decode, Encode}; use frame_support::{parameter_types, weights::constants::RocksDbWeight}; use frame_system::EnsureSigned; -#[cfg(feature = "runtime-benchmarks")] -use frame_system::EnsureSigned; use sp_core::{ecdsa, ed25519, sr25519, Pair}; use sp_keystore::{testing::KeyStore, KeystoreExt}; use sp_runtime::{ @@ -334,7 +332,7 @@ impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { #[cfg(feature = "runtime-benchmarks")] if *self == Self::get_call_for_did_call_benchmark() { // Always require an authentication key to dispatch calls during benchmarking - return Some(did::DidVerificationKeyRelationship::Authentication); + return Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)); } None } @@ -348,31 +346,26 @@ impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { } pub fn generate_test_did_call( - verification_key_required: did::DidVerificationKeyRelationship, + verification_key_required: did::DidOperationAuthorizationKey, caller: TestDidIdentifier, -) -> did::DidAuthorizedCallOperation { +) -> did::DidAuthorizedCallOperationWithVerificationRelationship { let call = match verification_key_required { - DidVerificationKeyRelationship::AssertionMethod => get_attestation_key_call(), - DidVerificationKeyRelationship::Authentication => get_authentication_key_call(), - DidVerificationKeyRelationship::CapabilityDelegation => get_delegation_key_call(), - _ => get_none_key_call(), + DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::AssertionMethod) => get_attestation_key_call(), + DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::Authentication) => get_authentication_key_call(), + DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::CapabilityDelegation) => get_delegation_key_call(), + DidOperationAuthorizationKey::DidKey(_) => get_none_key_call(), + DidOperationAuthorizationKey::NoKey => get_no_key_call(), }; - did::DidAuthorizedCallOperation { - did: caller, - call, - tx_counter: 1u64, + did::DidAuthorizedCallOperationWithVerificationRelationship { + operation: did::DidAuthorizedCallOperation { + did: caller, + call, + tx_counter: 1u64 + }, + operation_authorization_key_type: verification_key_required } } -// A test DID operation which can be crated to require any DID verification key -// type. -#[derive(Clone, Decode, Debug, Encode, PartialEq)] -pub struct TestDidOperation { - pub did: TestDidIdentifier, - pub verification_key_type: DidVerificationKeyRelationship, - pub tx_counter: u64, -} - #[allow(dead_code)] pub fn initialize_logger() { env_logger::builder().is_test(true).try_init(); diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 8fd05186b0..bc73a2e6c4 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -20,7 +20,7 @@ use frame_support::{assert_err, assert_noop, assert_ok}; use sp_core::*; use sp_std::{collections::btree_set::BTreeSet, convert::TryFrom}; -use crate::{self as did, mock::*}; +use crate::{self as did, DidAuthorizedCallOperationWithVerificationRelationship, mock::*}; use ctype::mock as ctype_mock; // submit_did_create_operation @@ -228,1040 +228,1122 @@ fn check_duplicate_did_creation() { }); } -// #[test] -// fn check_invalid_signature_format_did_creation() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Using an Ed25519 key where an Sr25519 is expected -// let invalid_key = get_ed25519_authentication_key(true); -// // DID creation contains auth_key, but signature is generated using invalid_key -// let operation = generate_base_did_creation_operation(); - -// let signature = invalid_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_create_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignature -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_did_creation() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let alternative_key = get_sr25519_authentication_key(false); -// let operation = generate_base_did_creation_operation(); - -// let signature = alternative_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_create_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignature -// ); -// }); -// } - -// #[test] -// fn check_max_limit_key_agreement_keys_did_creation() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Max keys allowed + 1 -// let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); -// let mut operation = generate_base_did_creation_operation(); -// operation.new_key_agreement_keys = enc_keys; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_create_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxKeyAgreementKeysLimitExceeded -// ); -// }); -// } - -// #[test] -// fn check_url_too_long_did_creation() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); -// let mut operation = generate_base_did_creation_operation(); -// // Max length allowed + 1 -// operation.new_endpoint_url = Some(url_endpoint); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_create_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxUrlLengthExceeded -// ); -// }); -// } - -// // submit_did_update_operation - -// #[test] -// fn check_successful_complete_update() { -// let old_auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(old_auth_key.public()); -// let new_auth_key = get_ed25519_authentication_key(false); -// let old_enc_key = get_x25519_encryption_key(true); -// let new_enc_key = get_x25519_encryption_key(false); -// let old_att_key = get_ed25519_attestation_key(true); -// let new_att_key = get_ed25519_attestation_key(false); -// let new_del_key = get_sr25519_delegation_key(true); -// let new_url = did::Url::from( -// did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) -// .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), -// ); - -// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(old_auth_key.public())); -// old_did_details.add_key_agreement_keys( -// vec![old_enc_key] -// .iter() -// .copied() -// .collect::>(), -// 0u64, -// ); -// old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); - -// // Update all keys, URL endpoint and tx counter. The old key agreement key is -// // removed. -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); -// operation.new_key_agreement_keys = vec![new_enc_key] -// .iter() -// .copied() -// .collect::>(); -// operation.attestation_key_update = -// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); -// operation.delegation_key_update = -// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_del_key.public())); -// operation.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] -// .iter() -// .copied() -// .collect::>(); -// operation.new_endpoint_url = Some(new_url); -// operation.tx_counter = old_did_details.last_tx_counter + 1u64; - -// // Generate signature using the old authentication key -// let signature = old_auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_ok!(Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); - -// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); -// assert_eq!( -// new_did_details.get_authentication_key_id(), -// generate_key_id(&did::DidVerificationKey::from(new_auth_key.public()).into()) -// ); -// // Old one deleted, new one added -> Total keys = 1 -// assert_eq!(new_did_details.get_key_agreement_keys_ids().len(), 1); -// assert_eq!( -// new_did_details.get_key_agreement_keys_ids().iter().next().unwrap(), -// &generate_key_id(&new_enc_key.into()) -// ); -// assert_eq!( -// new_did_details.get_attestation_key_id(), -// &Some(generate_key_id( -// &did::DidVerificationKey::from(new_att_key.public()).into() -// )) -// ); -// assert_eq!( -// new_did_details.get_delegation_key_id(), -// &Some(generate_key_id( -// &did::DidVerificationKey::from(new_del_key.public()).into() -// )) -// ); - -// // Total is +1 for the new auth key, -1 for the old auth key (replaced), +1 for -// // the new key agreement key, -1 for the old key agreement key (deleted), +1 for -// // the old attestation key, +1 for the new attestation key, +1 for the new -// // delegation key = 5 -// let public_keys = new_did_details.get_public_keys(); -// assert_eq!(public_keys.len(), 5); -// // Check for new authentication key -// assert!(public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(new_att_key.public()).into() -// ))); -// // Check for new key agreement key -// assert!(public_keys.contains_key(&generate_key_id(&new_enc_key.into()))); -// // Check for old attestation key -// assert!(public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(old_att_key.public()).into() -// ))); -// // Check for new attestation key -// assert!(public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(new_att_key.public()).into() -// ))); -// // Check for new delegation key -// assert!(public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(new_del_key.public()).into() -// ))); -// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -// } - -// #[test] -// fn check_successful_keys_deletion_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let att_key = get_ed25519_attestation_key(true); -// let del_key = get_sr25519_delegation_key(true); - -// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); -// old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; -// operation.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; -// operation.tx_counter = old_did_details.last_tx_counter + 1u64; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_ok!(Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); - -// // Auth key and key agreement key unchanged -// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); -// assert_eq!( -// new_did_details.get_authentication_key_id(), -// old_did_details.get_authentication_key_id() -// ); -// assert_eq!( -// new_did_details.get_key_agreement_keys_ids(), -// old_did_details.get_key_agreement_keys_ids() -// ); -// assert_eq!(new_did_details.get_attestation_key_id(), &None); -// assert_eq!(new_did_details.get_delegation_key_id(), &None); - -// // Public keys should now contain only the authentication key and the revoked -// // attestation key -// let stored_public_keys = new_did_details.get_public_keys(); -// assert_eq!(stored_public_keys.len(), 2); -// assert!(stored_public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(auth_key.public()).into() -// ))); -// assert!(stored_public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(att_key.public()).into() -// ))); -// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -// } - -// #[test] -// fn check_successful_keys_overwrite_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// // Same as the authentication key -> leads to two keys having the same ID -// let new_att_key = auth_key.clone(); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.attestation_key_update = -// did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_ok!(Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); - -// // Auth key unchanged -// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); -// assert_eq!( -// new_did_details.get_authentication_key_id(), -// old_did_details.get_authentication_key_id() -// ); -// // New attestation key and authentication key should now have the same ID -// assert_eq!( -// new_did_details.get_attestation_key_id(), -// &Some(old_did_details.get_authentication_key_id()) -// ); - -// // As the two keys have the same ID, public keys still contain only one element -// let stored_public_keys = new_did_details.get_public_keys(); -// assert_eq!(stored_public_keys.len(), 1); -// assert!(stored_public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(auth_key.public()).into() -// ))); -// assert!(stored_public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(new_att_key.public()).into() -// ))); -// // The block number should be the updated to the latest one, even if the ID was -// // also present before. -// let stored_key_details = stored_public_keys -// .get(&old_did_details.get_authentication_key_id()) -// .expect("There should be a key with the given ID stored on chain."); -// assert_eq!(stored_key_details.block_number, new_block_number); -// assert_eq!(new_did_details.last_tx_counter, old_did_details.last_tx_counter + 1u64); -// } - -// #[test] -// fn check_successful_keys_multiuse_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// // Same as the authentication key -> leads to two keys having the same ID -// let old_att_key = auth_key.clone(); - -// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); - -// // Remove attestation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_ok!(Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); - -// // Auth key unchanged -// let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); -// assert_eq!( -// new_did_details.get_authentication_key_id(), -// old_did_details.get_authentication_key_id() -// ); -// // Attestation key should now be set to None -// assert_eq!(new_did_details.get_attestation_key_id(), &None); - -// // As the two keys have the same ID, public keys still contain only one element -// let stored_public_keys = new_did_details.get_public_keys(); -// assert_eq!(stored_public_keys.len(), 1); -// assert!(stored_public_keys.contains_key(&generate_key_id( -// &did::DidVerificationKey::from(auth_key.public()).into() -// ))); -// } - -// #[test] -// fn check_did_not_present_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let bob_auth_key = get_ed25519_authentication_key(false); -// let bob_did = get_did_identifier_from_ed25519_key(bob_auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = generate_base_did_update_operation(alice_did); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(bob_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::DidNotPresent -// ); -// }); -// } - -// #[test] -// fn check_did_max_counter_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = u64::MAX; -// let operation = generate_base_did_update_operation(alice_did.clone()); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxTxCounterValue -// ); -// }); -// } - -// #[test] -// fn check_smaller_tx_counter_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = 1; -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter - 1; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_equal_tx_counter_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_too_large_tx_counter_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter + 2; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_format_did_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// // Using an Sr25519 key where an Ed25519 is expected -// let invalid_key = get_sr25519_authentication_key(true); -// // DID update contains auth_key, but signature is generated using invalid_key -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = generate_base_did_update_operation(alice_did.clone()); - -// let signature = invalid_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignatureFormat -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Using an Sr25519 key as expected, but from a different seed (default = false) -// let alternative_key = get_sr25519_authentication_key(false); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = generate_base_did_update_operation(alice_did.clone()); - -// let signature = alternative_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignature -// ); -// }); -// } - -// #[test] -// fn check_max_limit_key_agreement_keys_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Max keys allowed + 1 -// let new_enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.new_key_agreement_keys = new_enc_keys; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxKeyAgreementKeysLimitExceeded -// ); -// }); -// } - -// #[test] -// fn check_max_limit_public_keys_to_remove_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Max keys allowed + 1 -// let keys_ids_to_remove = -// get_public_keys_to_remove(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.public_keys_to_remove = keys_ids_to_remove; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxVerificationKeysToRemoveLimitExceeded -// ); -// }); -// } - -// #[test] -// fn check_url_too_long_did_update() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Max URL length allowed + 1 -// let new_endpoint_url = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// operation.new_endpoint_url = Some(new_endpoint_url); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxUrlLengthExceeded -// ); -// }); -// } - -// #[test] -// fn check_currently_active_authentication_key_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// // Trying to remove the currently active authentication key -// operation.public_keys_to_remove = vec![generate_key_id( -// &did::DidVerificationKey::from(auth_key.public()).into(), -// )] -// .iter() -// .copied() -// .collect::>(); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::CurrentlyActiveKey -// ); -// }); -// } - -// #[test] -// fn check_currently_active_delegation_key_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let del_key = get_ecdsa_delegation_key(true); - -// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// // Trying to remove the currently active delegation key -// operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] -// .iter() -// .copied() -// .collect::>(); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::CurrentlyActiveKey -// ); -// }); -// } - -// #[test] -// fn check_currently_active_attestation_key_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let att_key = get_sr25519_attestation_key(true); - -// let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// // Trying to remove the currently active attestation key -// operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] -// .iter() -// .copied() -// .collect::>(); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::CurrentlyActiveKey -// ); -// }); -// } - -// #[test] -// fn check_verification_key_not_present_update() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let key_to_delete = get_sr25519_authentication_key(true); - -// let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Remove both attestation and delegation key -// let mut operation = generate_base_did_update_operation(alice_did.clone()); -// // Trying to remove the currently active authentication key -// operation.public_keys_to_remove = vec![generate_key_id( -// &did::DidVerificationKey::from(key_to_delete.public()).into(), -// )] -// .iter() -// .copied() -// .collect::>(); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, old_did_details)]) -// .build(None); - -// let new_block_number: TestBlockNumber = 1; - -// ext.execute_with(|| { -// System::set_block_number(new_block_number); -// assert_noop!( -// Did::submit_did_update_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::VerificationKeyNotPresent -// ); -// }); -// } - -// // submit_did_delete_operation - -// #[test] -// fn check_successful_deletion() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// // Update all keys, URL endpoint and tx counter. No keys are removed in this -// // test -// let operation = generate_base_did_delete_operation(alice_did.clone()); - -// // Generate signature using the old authentication key -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did.clone(), did_details)]) -// .build(None); - -// ext.execute_with(|| { -// assert_ok!(Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); - -// assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); - -// // Re-adding the same DID identifier, which should not fail. -// let operation = generate_base_did_creation_operation(); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_ok!(Did::submit_did_create_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// )); -// }); -// } - -// #[test] -// fn check_did_not_present_deletion() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - -// // Update all keys, URL endpoint and tx counter. No keys are removed in this -// // test -// let operation = generate_base_did_delete_operation(alice_did); - -// // Generate signature using the old authentication key -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::DidNotPresent -// ); -// }); -// } - -// #[test] -// fn check_max_tx_counter_did_deletion() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = u64::MAX; -// let operation = generate_base_did_delete_operation(alice_did.clone()); - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::MaxTxCounterValue -// ); -// }); -// } - -// #[test] -// fn check_smaller_tx_counter_did_deletion() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = 1; -// let mut operation = generate_base_did_delete_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter - 1; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_equal_tx_counter_did_deletion() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = 1; -// let mut operation = generate_base_did_delete_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_too_large_tx_counter_did_deletion() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let mut operation = generate_base_did_delete_operation(alice_did.clone()); -// operation.tx_counter = mock_did.last_tx_counter + 2; - -// let signature = auth_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_format_did_deletion() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// // Using an Sr25519 key where an Ed25519 is expected -// let invalid_key = get_sr25519_authentication_key(true); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = generate_base_did_delete_operation(alice_did.clone()); - -// let signature = invalid_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignatureFormat -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_did_deletion() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Using an Sr25519 key as expected, but from a different seed (default = false) -// let alternative_key = get_sr25519_authentication_key(false); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = generate_base_did_delete_operation(alice_did.clone()); - -// let signature = alternative_key.sign(operation.encode().as_ref()); - -// let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_delete_operation( -// Origin::signed(DEFAULT_ACCOUNT), -// operation.clone(), -// did::DidSignature::from(signature), -// ), -// did::Error::::InvalidSignature -// ); -// }); -// } - -// // submit_did_call +#[test] +fn check_invalid_signature_format_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + // Using an Ed25519 key where an Sr25519 is expected + let invalid_key = get_ed25519_authentication_key(true); + // DID creation contains auth_key, but signature is generated using invalid_key + let operation = generate_base_did_creation_operation(); + + let signature = invalid_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_create_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + did::DidSignature::from(signature), + ), + did::Error::::InvalidSignature + ); + }); +} + +#[test] +fn check_invalid_signature_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let alternative_key = get_sr25519_authentication_key(false); + let operation = generate_base_did_creation_operation(); + + let signature = alternative_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_create_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + did::DidSignature::from(signature), + ), + did::Error::::InvalidSignature + ); + }); +} + +#[test] +fn check_max_limit_key_agreement_keys_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + // Max keys allowed + 1 + let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); + let mut operation = generate_base_did_creation_operation(); + operation.new_key_agreement_keys = enc_keys; + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_create_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + did::DidSignature::from(signature), + ), + did::Error::::MaxKeyAgreementKeysLimitExceeded + ); + }); +} + +#[test] +fn check_url_too_long_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); + let mut operation = generate_base_did_creation_operation(); + // Max length allowed + 1 + operation.new_endpoint_url = Some(url_endpoint); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_create_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + did::DidSignature::from(signature), + ), + did::Error::::MaxUrlLengthExceeded + ); + }); +} + +// submit_did_update_operation + +#[test] +fn check_successful_complete_update() { + let old_auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(old_auth_key.public()); + let new_auth_key = get_ed25519_authentication_key(false); + let old_enc_key = get_x25519_encryption_key(true); + let new_enc_key = get_x25519_encryption_key(false); + let old_att_key = get_ed25519_attestation_key(true); + let new_att_key = get_ed25519_attestation_key(false); + let new_del_key = get_sr25519_delegation_key(true); + let new_url = did::Url::from( + did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) + .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), + ); + + let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(old_auth_key.public())); + old_did_details.add_key_agreement_keys( + vec![old_enc_key] + .iter() + .copied() + .collect::>(), + 0u64, + ); + old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); + + // Update all keys, URL endpoint and tx counter. The old key agreement key is + // removed. + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); + operation.new_key_agreement_keys = vec![new_enc_key] + .iter() + .copied() + .collect::>(); + operation.attestation_key_update = + did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); + operation.delegation_key_update = + did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_del_key.public())); + operation.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] + .iter() + .copied() + .collect::>(); + operation.new_endpoint_url = Some(new_url); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_ok!(Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + )); + }); + + let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); + assert_eq!( + new_did_details.get_authentication_key_id(), + generate_key_id(&did::DidVerificationKey::from(new_auth_key.public()).into()) + ); + // Old one deleted, new one added -> Total keys = 1 + assert_eq!(new_did_details.get_key_agreement_keys_ids().len(), 1); + assert_eq!( + new_did_details.get_key_agreement_keys_ids().iter().next().unwrap(), + &generate_key_id(&new_enc_key.into()) + ); + assert_eq!( + new_did_details.get_attestation_key_id(), + &Some(generate_key_id( + &did::DidVerificationKey::from(new_att_key.public()).into() + )) + ); + assert_eq!( + new_did_details.get_delegation_key_id(), + &Some(generate_key_id( + &did::DidVerificationKey::from(new_del_key.public()).into() + )) + ); + + // Total is +1 for the new auth key, -1 for the old auth key (replaced), +1 for + // the new key agreement key, -1 for the old key agreement key (deleted), +1 for + // the old attestation key, +1 for the new attestation key, +1 for the new + // delegation key = 5 + let public_keys = new_did_details.get_public_keys(); + assert_eq!(public_keys.len(), 5); + // Check for new authentication key + assert!(public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(new_att_key.public()).into() + ))); + // Check for new key agreement key + assert!(public_keys.contains_key(&generate_key_id(&new_enc_key.into()))); + // Check for old attestation key + assert!(public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(old_att_key.public()).into() + ))); + // Check for new attestation key + assert!(public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(new_att_key.public()).into() + ))); + // Check for new delegation key + assert!(public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(new_del_key.public()).into() + ))); +} + +#[test] +fn check_successful_keys_deletion_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let att_key = get_ed25519_attestation_key(true); + let del_key = get_sr25519_delegation_key(true); + + let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); + old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; + operation.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_ok!(Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + )); + }); + + // Auth key and key agreement key unchanged + let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); + assert_eq!( + new_did_details.get_authentication_key_id(), + old_did_details.get_authentication_key_id() + ); + assert_eq!( + new_did_details.get_key_agreement_keys_ids(), + old_did_details.get_key_agreement_keys_ids() + ); + assert_eq!(new_did_details.get_attestation_key_id(), &None); + assert_eq!(new_did_details.get_delegation_key_id(), &None); + + // Public keys should now contain only the authentication key and the revoked + // attestation key + let stored_public_keys = new_did_details.get_public_keys(); + assert_eq!(stored_public_keys.len(), 2); + assert!(stored_public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(auth_key.public()).into() + ))); + assert!(stored_public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(att_key.public()).into() + ))); +} + +#[test] +fn check_successful_keys_overwrite_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + // Same as the authentication key -> leads to two keys having the same ID + let new_att_key = auth_key.clone(); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.attestation_key_update = + did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_ok!(Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + )); + }); + + // Auth key unchanged + let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); + assert_eq!( + new_did_details.get_authentication_key_id(), + old_did_details.get_authentication_key_id() + ); + // New attestation key and authentication key should now have the same ID + assert_eq!( + new_did_details.get_attestation_key_id(), + &Some(old_did_details.get_authentication_key_id()) + ); + + // As the two keys have the same ID, public keys still contain only one element + let stored_public_keys = new_did_details.get_public_keys(); + assert_eq!(stored_public_keys.len(), 1); + assert!(stored_public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(auth_key.public()).into() + ))); + assert!(stored_public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(new_att_key.public()).into() + ))); + // The block number should be the updated to the latest one, even if the ID was + // also present before. + let stored_key_details = stored_public_keys + .get(&old_did_details.get_authentication_key_id()) + .expect("There should be a key with the given ID stored on chain."); + assert_eq!(stored_key_details.block_number, new_block_number); +} + +#[test] +fn check_successful_keys_multiuse_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + // Same as the authentication key -> leads to two keys having the same ID + let old_att_key = auth_key.clone(); + + let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); + + // Remove attestation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_ok!(Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + )); + }); + + // Auth key unchanged + let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); + assert_eq!( + new_did_details.get_authentication_key_id(), + old_did_details.get_authentication_key_id() + ); + // Attestation key should now be set to None + assert_eq!(new_did_details.get_attestation_key_id(), &None); + + // As the two keys have the same ID, public keys still contain only one element + let stored_public_keys = new_did_details.get_public_keys(); + assert_eq!(stored_public_keys.len(), 1); + assert!(stored_public_keys.contains_key(&generate_key_id( + &did::DidVerificationKey::from(auth_key.public()).into() + ))); +} + +#[test] +fn check_did_not_present_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let bob_auth_key = get_ed25519_authentication_key(false); + let bob_did = get_did_identifier_from_ed25519_key(bob_auth_key.public()); + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + let operation = generate_base_did_update_operation(alice_did.clone()); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().with_dids(vec![(bob_did, mock_did)]).build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::DidNotPresent + ); + }); +} + +#[test] +fn check_max_limit_key_agreement_keys_did_update() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Max keys allowed + 1 + let new_enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); + + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.new_key_agreement_keys = new_enc_keys; + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::MaxKeyAgreementKeysLimitExceeded + ); + }); +} + +#[test] +fn check_max_limit_public_keys_to_remove_did_update() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Max keys allowed + 1 + let keys_ids_to_remove = + get_public_keys_to_remove(::MaxNewKeyAgreementKeys::get().saturating_add(1)); + + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.public_keys_to_remove = keys_ids_to_remove; + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::MaxVerificationKeysToRemoveLimitExceeded + ); + }); +} + +#[test] +fn check_url_too_long_did_update() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Max URL length allowed + 1 + let new_endpoint_url = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); + + let mut operation = generate_base_did_update_operation(alice_did.clone()); + operation.new_endpoint_url = Some(new_endpoint_url); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::MaxUrlLengthExceeded + ); + }); +} + +#[test] +fn check_currently_active_authentication_key_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + // Trying to remove the currently active authentication key + operation.public_keys_to_remove = vec![generate_key_id( + &did::DidVerificationKey::from(auth_key.public()).into(), + )] + .iter() + .copied() + .collect::>(); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::CurrentlyActiveKey + ); + }); +} + +#[test] +fn check_currently_active_delegation_key_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let del_key = get_ecdsa_delegation_key(true); + + let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + // Trying to remove the currently active delegation key + operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] + .iter() + .copied() + .collect::>(); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::CurrentlyActiveKey + ); + }); +} + +#[test] +fn check_currently_active_attestation_key_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let att_key = get_sr25519_attestation_key(true); + + let mut old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + // Trying to remove the currently active attestation key + operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] + .iter() + .copied() + .collect::>(); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::CurrentlyActiveKey + ); + }); +} + +#[test] +fn check_verification_key_not_present_update() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let key_to_delete = get_sr25519_authentication_key(true); + + let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + // Remove both attestation and delegation key + let mut operation = generate_base_did_update_operation(alice_did.clone()); + // Trying to remove the currently active authentication key + operation.public_keys_to_remove = vec![generate_key_id( + &did::DidVerificationKey::from(key_to_delete.public()).into(), + )] + .iter() + .copied() + .collect::>(); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None); + + let new_block_number: TestBlockNumber = 1; + + ext.execute_with(|| { + System::set_block_number(new_block_number); + assert_noop!( + Did::submit_did_update_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + ), + did::Error::::VerificationKeyNotPresent + ); + }); +} + +// submit_did_delete_operation + +#[test] +fn check_successful_deletion() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), did_details)]) + .build(None); + + ext.execute_with(|| { + assert_ok!(Did::submit_did_delete_operation( + Origin::signed(alice_did.clone()), + )); + }); + + assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); + + // Re-adding the same DID identifier, which should not fail. + let operation = generate_base_did_creation_operation(); + + let signature = auth_key.sign(operation.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_ok!(Did::submit_did_create_operation( + Origin::signed(alice_did.clone()), + operation.clone(), + did::DidSignature::from(signature), + )); + }); +} + +#[test] +fn check_did_not_present_deletion() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_delete_operation( + Origin::signed(alice_did.clone()), + ), + did::Error::::DidNotPresent + ); + }); +} + +// submit_did_call + +#[test] +fn check_did_not_found_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + + // No DID added + let mut ext = ExtBuilder::default().build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::DidNotPresent + ); + }); +} + +#[test] +fn check_signature_not_provided_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + // Operation requires an authentication key for verification, but no signature is provided. + None + ), + did::Error::::SignatureRequired + ); + }); +} + +#[test] +fn check_max_counter_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.last_tx_counter = u64::MAX; + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::MaxTxCounterValue + ); + }); +} + +#[test] +fn check_too_small_tx_counter_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.last_tx_counter = 1u64; + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + call_operation.operation.tx_counter = 0u64; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::InvalidNonce + ); + }); +} + +#[test] +fn check_equal_tx_counter_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + call_operation.operation.tx_counter = mock_did.last_tx_counter; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::InvalidNonce + ); + }); +} + +#[test] +fn check_too_large_tx_counter_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + call_operation.operation.tx_counter = mock_did.last_tx_counter + 2u64; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::InvalidNonce + ); + }); +} + +#[test] +fn check_verification_key_not_present_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + // The operation requires the delegation key that is currently not stored for + // the given DID. + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::VerificationKeyNotPresent + ); + }); +} + +#[test] +fn check_invalid_signature_format_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let alternative_auth_key = get_ed25519_authentication_key(true); + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::InvalidSignatureFormat + ); + }); +} + +#[test] +fn check_invalid_signature_call_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let alternative_auth_key = get_sr25519_authentication_key(false); + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + did::Error::::InvalidSignature + ); + }); +} + +#[test] +fn check_call_attestation_key_successful() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let attestation_key = get_ed25519_attestation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let signature = attestation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!(Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + )); + }); +} + +#[test] +fn check_call_attestation_key_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let attestation_key = get_ed25519_attestation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); + + let ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + // CType already added to storage + let mut ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(get_attestation_key_test_input(), did.clone())]) + .build(Some(ext)); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let signature = attestation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_err!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + ctype::Error::::CTypeAlreadyExists + ); + }); +} + +#[test] +fn check_call_delegation_key_successful() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let delegation_key = get_ed25519_delegation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let signature = delegation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!(Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + )); + }); +} + +#[test] +fn check_call_delegation_key_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + let delegation_key = get_ed25519_delegation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); + + let ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + // CType already added to storage + let mut ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(get_delegation_key_test_input(), did.clone())]) + .build(Some(ext)); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let signature = delegation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_err!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + ctype::Error::::CTypeAlreadyExists + ); + }); +} + +#[test] +fn check_call_authentication_key_successful() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!(Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + )); + }); +} + +#[test] +fn check_call_authentication_key_error() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + // CType already added to storage + let mut ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(get_authentication_key_test_input(), did.clone())]) + .build(Some(ext)); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_err!( + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + Some(did::DidSignature::from(signature)) + ), + ctype::Error::::CTypeAlreadyExists + ); + }); +} #[test] -fn check_no_key_error() { +fn check_null_key_error() { let auth_key = get_sr25519_authentication_key(true); let did = get_did_identifier_from_sr25519_key(auth_key.public()); let caller = DEFAULT_ACCOUNT; @@ -1270,903 +1352,482 @@ fn check_no_key_error() { // CapabilityInvocation is not supported at the moment, so it should return no // key and hence the operation fail. - let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityInvocation, did); + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityInvocation), did); let signature = did::DidSignature::from(ed25519::Signature::default()); ext.execute_with(|| { assert_noop!( - Did::submit_did_call(Origin::signed(caller), Box::new(call_operation), signature), + Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), Some(signature)), did::Error::::UnsupportedDidAuthorizationCall ); }); } -// #[test] -// fn check_did_not_found_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; - -// // No DID added -// let mut ext = ExtBuilder::default().build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::DidNotPresent -// ); -// }); -// } - -// #[test] -// fn check_max_counter_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = u64::MAX; - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::MaxTxCounterValue -// ); -// }); -// } - -// #[test] -// fn check_too_small_tx_counter_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = 1u64; - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// call_operation.tx_counter = 0u64; -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_equal_tx_counter_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did.clone())]) -// .build(None); - -// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// call_operation.tx_counter = mock_did.last_tx_counter; -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_too_large_tx_counter_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did.clone())]) -// .build(None); - -// let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// call_operation.tx_counter = mock_did.last_tx_counter + 2u64; -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::InvalidNonce -// ); -// }); -// } - -// #[test] -// fn check_verification_key_not_present_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// // The operation requires the delegation key that is currently not stored for -// // the given DID. -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::VerificationKeyNotPresent -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_format_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let alternative_auth_key = get_ed25519_authentication_key(true); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::InvalidSignatureFormat -// ); -// }); -// } - -// #[test] -// fn check_invalid_signature_call_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let alternative_auth_key = get_sr25519_authentication_key(false); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_noop!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// did::Error::::InvalidSignature -// ); -// }); -// } - -// #[test] -// fn check_call_attestation_key_successful() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let attestation_key = get_ed25519_attestation_key(true); - -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); -// let signature = attestation_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_ok!(Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// )); -// }); -// } - -// #[test] -// fn check_call_attestation_key_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let attestation_key = get_ed25519_attestation_key(true); - -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); - -// let ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); -// // CType already added to storage -// let mut ext = ctype_mock::ExtBuilder::default() -// .with_ctypes(vec![(get_attestation_key_test_input(), did.clone())]) -// .build(Some(ext)); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); -// let signature = attestation_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_err!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// ctype::Error::::CTypeAlreadyExists -// ); -// }); -// } - -// #[test] -// fn check_call_delegation_key_successful() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let delegation_key = get_ed25519_delegation_key(true); - -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); -// let signature = delegation_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_ok!(Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// )); -// }); -// } - -// #[test] -// fn check_call_delegation_key_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; -// let delegation_key = get_ed25519_delegation_key(true); - -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); - -// let ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); -// // CType already added to storage -// let mut ext = ctype_mock::ExtBuilder::default() -// .with_ctypes(vec![(get_delegation_key_test_input(), did.clone())]) -// .build(Some(ext)); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); -// let signature = delegation_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_err!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// ctype::Error::::CTypeAlreadyExists -// ); -// }); -// } - -// #[test] -// fn check_call_authentication_key_successful() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; - -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_ok!(Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// )); -// }); -// } - -// #[test] -// fn check_call_authentication_key_error() { -// let auth_key = get_sr25519_authentication_key(true); -// let did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let caller = DEFAULT_ACCOUNT; - -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - -// let ext = ExtBuilder::default() -// .with_dids(vec![(did.clone(), mock_did)]) -// .build(None); -// // CType already added to storage -// let mut ext = ctype_mock::ExtBuilder::default() -// .with_ctypes(vec![(get_authentication_key_test_input(), did.clone())]) -// .build(Some(ext)); - -// let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); -// let signature = auth_key.sign(call_operation.encode().as_ref()); - -// ext.execute_with(|| { -// assert_err!( -// Did::submit_did_call( -// Origin::signed(caller), -// Box::new(call_operation), -// did::DidSignature::from(signature) -// ), -// ctype::Error::::CTypeAlreadyExists -// ); -// }); -// } - -// // Internal function: verify_operation_validity_and_increase_did_nonce - -// #[test] -// fn check_authentication_successful_operation_verification() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter + 1, -// }; - -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_ok!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ) -// ); -// }); - -// // Verify that the DID tx counter has increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!( -// did_details.get_tx_counter_value(), -// mock_did.get_tx_counter_value() + 1u64 -// ); -// } - -// #[test] -// fn check_attestation_successful_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let att_key = get_ecdsa_attestation_key(true); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::AssertionMethod, -// tx_counter: mock_did.last_tx_counter + 1, -// }; - -// let signature = att_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_ok!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ) -// ); -// }); - -// // Verify that the DID tx counter has increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!( -// did_details.get_tx_counter_value(), -// mock_did.get_tx_counter_value() + 1u64 -// ); -// } - -// #[test] -// fn check_delegation_successful_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let del_key = get_ed25519_delegation_key(true); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, -// tx_counter: mock_did.last_tx_counter + 1, -// }; -// let signature = del_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_ok!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ) -// ); -// }); - -// // Verify that the DID tx counter has increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!( -// did_details.get_tx_counter_value(), -// mock_did.get_tx_counter_value() + 1u64 -// ); -// } - -// #[test] -// fn check_did_not_present_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let del_key = get_ed25519_delegation_key(true); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did, -// verification_key_type: did::DidVerificationKeyRelationship::CapabilityDelegation, -// tx_counter: mock_did.last_tx_counter + 1, -// }; -// let signature = del_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default().build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::StorageError(did::StorageError::DidNotPresent) -// ); -// }); -// } - -// #[test] -// fn check_max_tx_counter_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = u64::MAX; -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter, -// }; -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::StorageError(did::StorageError::MaxTxCounterValue) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_smaller_counter_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// mock_did.last_tx_counter = 1; -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter - 1, -// }; -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::SignatureError(did::SignatureError::InvalidNonce) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_equal_counter_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter, -// }; -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::SignatureError(did::SignatureError::InvalidNonce) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_too_large_counter_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter + 2, -// }; -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::SignatureError(did::SignatureError::InvalidNonce) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_verification_key_not_present_operation_verification() { -// let auth_key = get_ed25519_authentication_key(true); -// let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let verification_key_required = did::DidVerificationKeyRelationship::CapabilityInvocation; -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: verification_key_required, -// tx_counter: mock_did.last_tx_counter + 1, -// }; - -// let signature = auth_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::StorageError(did::StorageError::DidKeyNotPresent(verification_key_required)) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_invalid_signature_format_operation_verification() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Expected an Sr25519, given an Ed25519 -// let invalid_key = get_ed25519_authentication_key(true); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter + 1, -// }; - -// let signature = invalid_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::SignatureError(did::SignatureError::InvalidSignatureFormat) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// #[test] -// fn check_invalid_signature_operation_verification() { -// let auth_key = get_sr25519_authentication_key(true); -// let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); -// // Using same key type but different seed (default = false) -// let alternative_key = get_sr25519_authentication_key(false); -// let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); -// let operation = TestDidOperation { -// did: alice_did.clone(), -// verification_key_type: did::DidVerificationKeyRelationship::Authentication, -// tx_counter: mock_did.last_tx_counter + 1, -// }; - -// let signature = alternative_key.sign(&operation.encode()); - -// let mut ext = ExtBuilder::default() -// .with_dids(vec![(alice_did, mock_did.clone())]) -// .build(None); - -// ext.execute_with(|| { -// assert_noop!( -// Did::verify_signature_and_increase_nonce::( -// &operation, -// &did::DidSignature::from(signature) -// ), -// did::DidError::SignatureError(did::SignatureError::InvalidSignature) -// ); -// }); - -// // Verify that the DID tx counter has not increased -// let did_details = ext.execute_with(|| Did::get_did(&operation.did).expect("DID should be present on chain.")); -// assert_eq!(did_details.get_tx_counter_value(), mock_did.get_tx_counter_value()); -// } - -// // Internal function: HttpUrl try_from - -// #[test] -// fn check_http_url() { -// assert_ok!(did::HttpUrl::try_from("http://kilt.io".as_bytes())); - -// assert_ok!(did::HttpUrl::try_from("https://kilt.io".as_bytes())); - -// assert_ok!(did::HttpUrl::try_from( -// "https://super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() -// )); - -// // All other valid ASCII characters -// assert_ok!(did::HttpUrl::try_from("http://:/?#[]@!$&'()*+,;=-._~".as_bytes())); - -// assert_eq!( -// did::HttpUrl::try_from("".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// // Non-printable ASCII characters -// assert_eq!( -// did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// // Some invalid ASCII characters -// assert_eq!( -// did::HttpUrl::try_from("http://kilt.io/".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// // Non-ASCII characters -// assert_eq!( -// did::HttpUrl::try_from("http://¶.com".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// assert_eq!( -// did::HttpUrl::try_from("htt://kilt.io".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// assert_eq!( -// did::HttpUrl::try_from("httpss://kilt.io".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); -// } - -// // Internal function: FtpUrl try_from - -// #[test] -// fn check_ftp_url() { -// assert_ok!(did::FtpUrl::try_from("ftp://kilt.io".as_bytes())); - -// assert_ok!(did::FtpUrl::try_from("ftps://kilt.io".as_bytes())); - -// assert_ok!(did::FtpUrl::try_from( -// "ftps://user@super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() -// )); - -// // All other valid ASCII characters -// assert_ok!(did::FtpUrl::try_from("ftps://:/?#[]@%!$&'()*+,;=-._~".as_bytes())); - -// assert_eq!( -// did::FtpUrl::try_from("".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// // Non-printable ASCII characters -// assert_eq!( -// did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// // Some invalid ASCII characters -// assert_eq!( -// did::FtpUrl::try_from("ftp://kilt.io/".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// // Non-ASCII characters -// assert_eq!( -// did::FtpUrl::try_from("ftps://¶.com".as_bytes()), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// assert_eq!( -// did::FtpUrl::try_from("ft://kilt.io".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// assert_eq!( -// did::HttpUrl::try_from("ftpss://kilt.io".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); -// } - -// // Internal function: IpfsUrl try_from - -// #[test] -// fn check_ipfs_url() { -// // Base58 address -// assert_ok!(did::IpfsUrl::try_from( -// "ipfs://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes() -// )); - -// // Base32 address (at the moment, padding characters can appear anywhere in the -// // string) -// assert_ok!(did::IpfsUrl::try_from( -// "ipfs://OQQHHHTGMMYDQQ364YB4GDE=HREJQL==".as_bytes() -// )); - -// assert_eq!( -// did::IpfsUrl::try_from("".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// assert_eq!( -// did::IpfsUrl::try_from( -// "ipfs://¶ -// QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" -// .as_bytes() -// ), -// Err(did::UrlError::InvalidUrlEncoding) -// ); - -// assert_eq!( -// did::IpfsUrl::try_from("ipf://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes()), -// Err(did::UrlError::InvalidUrlScheme) -// ); - -// assert_eq!( -// did::IpfsUrl::try_from( -// "ipfss:// -// QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" -// .as_bytes() -// ), -// Err(did::UrlError::InvalidUrlScheme) -// ); -// } +#[test] +fn check_no_key_success() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let caller = DEFAULT_ACCOUNT; + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did)]) + .build(None); + + // If a DID operation specifies that no key is needed, the signature is not verified hence the call will always be dispatched. + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::NoKey, did); + + ext.execute_with(|| { + assert_ok!( + Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), None), + ); + }); +} + +// Internal function: verify_did_operation_signature_and_increase_nonce + +#[test] +fn check_authentication_successful_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ) + ); + }); + + // Verify that the DID tx counter has increased + let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + assert_eq!( + did_details.get_tx_counter_value(), + mock_did.get_tx_counter_value() + 1u64 + ); +} + +#[test] +fn check_attestation_successful_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let attestation_key = get_ed25519_attestation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_attestation_key(did::DidVerificationKey::from(attestation_key.public()), 0); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let signature = attestation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ) + ); + }); + + // Verify that the DID tx counter has increased + let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + assert_eq!( + did_details.get_tx_counter_value(), + mock_did.get_tx_counter_value() + 1u64 + ); +} + +#[test] +fn check_delegation_successful_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + let delegation_key = get_ecdsa_delegation_key(true); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.update_delegation_key(did::DidVerificationKey::from(delegation_key.public()), 0); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let signature = delegation_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_ok!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ) + ); + }); + + // Verify that the DID tx counter has increased + let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + assert_eq!( + did_details.get_tx_counter_value(), + mock_did.get_tx_counter_value() + 1u64 + ); +} + +#[test] +fn check_did_not_present_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let mut ext = ExtBuilder::default().build(None); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::StorageError(did::StorageError::DidNotPresent) + ); + }); +} + +#[test] +fn check_max_tx_counter_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.set_tx_counter(u64::MAX); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + call_operation.operation.tx_counter = mock_did.last_tx_counter; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::StorageError(did::StorageError::MaxTxCounterValue) + ); + }); +} + +#[test] +fn check_smaller_counter_operation_verification() { + let auth_key = get_ed25519_authentication_key(true); + let did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + mock_did.last_tx_counter = 1; + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + call_operation.operation.tx_counter = 0u64; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::SignatureError(did::SignatureError::InvalidNonce) + ); + }); +} + +#[test] +fn check_equal_counter_operation_verification() { + let auth_key = get_ed25519_authentication_key(true); + let did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + call_operation.operation.tx_counter = mock_did.last_tx_counter; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::SignatureError(did::SignatureError::InvalidNonce) + ); + }); +} + +#[test] +fn check_too_large_counter_operation_verification() { + let auth_key = get_ed25519_authentication_key(true); + let did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + call_operation.operation.tx_counter = mock_did.last_tx_counter + 2; + let signature = auth_key.sign(call_operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::SignatureError(did::SignatureError::InvalidNonce) + ); + }); +} + +#[test] +fn check_verification_key_not_present_operation_verification() { + let auth_key = get_ed25519_authentication_key(true); + let did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did.clone()); + let signature = auth_key.sign(call_operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did, mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::StorageError(did::StorageError::DidKeyNotPresent(did::DidVerificationKeyRelationship::AssertionMethod)) + ); + }); +} + +#[test] +fn check_invalid_signature_format_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + // Expected an Sr25519, given an Ed25519 + let invalid_key = get_ed25519_authentication_key(true); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did.clone()); + let signature = invalid_key.sign(call_operation.encode().as_ref()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did, mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::SignatureError(did::SignatureError::InvalidSignatureFormat) + ); + }); +} + +#[test] +fn check_invalid_signature_operation_verification() { + let auth_key = get_sr25519_authentication_key(true); + let did = get_did_identifier_from_sr25519_key(auth_key.public()); + // Using same key type but different seed (default = false) + let alternative_key = get_sr25519_authentication_key(false); + + let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); + + let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did.clone()); + let signature = alternative_key.sign(&call_operation.encode()); + + let mut ext = ExtBuilder::default() + .with_dids(vec![(did.clone(), mock_did.clone())]) + .build(None); + + ext.execute_with(|| { + assert_noop!( + Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &Some(did::DidSignature::from(signature)) + ), + did::DidError::SignatureError(did::SignatureError::InvalidSignature) + ); + }); +} + +// Internal function: HttpUrl try_from + +#[test] +fn check_http_url() { + assert_ok!(did::HttpUrl::try_from("http://kilt.io".as_bytes())); + + assert_ok!(did::HttpUrl::try_from("https://kilt.io".as_bytes())); + + assert_ok!(did::HttpUrl::try_from( + "https://super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() + )); + + // All other valid ASCII characters + assert_ok!(did::HttpUrl::try_from("http://:/?#[]@!$&'()*+,;=-._~".as_bytes())); + + assert_eq!( + did::HttpUrl::try_from("".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + // Non-printable ASCII characters + assert_eq!( + did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + // Some invalid ASCII characters + assert_eq!( + did::HttpUrl::try_from("http://kilt.io/".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + // Non-ASCII characters + assert_eq!( + did::HttpUrl::try_from("http://¶.com".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + assert_eq!( + did::HttpUrl::try_from("htt://kilt.io".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + assert_eq!( + did::HttpUrl::try_from("httpss://kilt.io".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); +} + +// Internal function: FtpUrl try_from + +#[test] +fn check_ftp_url() { + assert_ok!(did::FtpUrl::try_from("ftp://kilt.io".as_bytes())); + + assert_ok!(did::FtpUrl::try_from("ftps://kilt.io".as_bytes())); + + assert_ok!(did::FtpUrl::try_from( + "ftps://user@super.long.domain.kilt.io:12345/public/files/test.txt".as_bytes() + )); + + // All other valid ASCII characters + assert_ok!(did::FtpUrl::try_from("ftps://:/?#[]@%!$&'()*+,;=-._~".as_bytes())); + + assert_eq!( + did::FtpUrl::try_from("".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + // Non-printable ASCII characters + assert_eq!( + did::HttpUrl::try_from("http://kilt.io/\x00".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + // Some invalid ASCII characters + assert_eq!( + did::FtpUrl::try_from("ftp://kilt.io/".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + // Non-ASCII characters + assert_eq!( + did::FtpUrl::try_from("ftps://¶.com".as_bytes()), + Err(did::UrlError::InvalidUrlEncoding) + ); + + assert_eq!( + did::FtpUrl::try_from("ft://kilt.io".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + assert_eq!( + did::HttpUrl::try_from("ftpss://kilt.io".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); +} + +// Internal function: IpfsUrl try_from + +#[test] +fn check_ipfs_url() { + // Base58 address + assert_ok!(did::IpfsUrl::try_from( + "ipfs://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes() + )); + + // Base32 address (at the moment, padding characters can appear anywhere in the + // string) + assert_ok!(did::IpfsUrl::try_from( + "ipfs://OQQHHHTGMMYDQQ364YB4GDE=HREJQL==".as_bytes() + )); + + assert_eq!( + did::IpfsUrl::try_from("".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + assert_eq!( + did::IpfsUrl::try_from( + "ipfs://¶ +QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" + .as_bytes() + ), + Err(did::UrlError::InvalidUrlEncoding) + ); + + assert_eq!( + did::IpfsUrl::try_from("ipf://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes()), + Err(did::UrlError::InvalidUrlScheme) + ); + + assert_eq!( + did::IpfsUrl::try_from( + "ipfss:// +QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" + .as_bytes() + ), + Err(did::UrlError::InvalidUrlScheme) + ); +} diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 9a57e188dc..24d980d5cd 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -842,16 +842,18 @@ construct_runtime! { } impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { match self { - Call::Attestation(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), - Call::Ctype(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), - Call::Delegation(_) => Some(did::DidVerificationKeyRelationship::CapabilityDelegation), + Call::Attestation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), + Call::Ctype(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), + Call::Delegation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)), + Call::Did(did::Call::submit_did_create_operation(..)) => Some(did::DidOperationAuthorizationKey::NoKey), + Call::Did(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), #[cfg(not(feature = "runtime-benchmarks"))] _ => None, // By default, returns the authentication key #[cfg(feature = "runtime-benchmarks")] - _ => Some(did::DidVerificationKeyRelationship::Authentication), + _ => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), } } diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index e6bc5b6658..7f421c7e4e 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -476,16 +476,18 @@ construct_runtime!( ); impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { match self { - Call::Attestation(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), - Call::Ctype(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), - Call::Delegation(_) => Some(did::DidVerificationKeyRelationship::CapabilityDelegation), + Call::Attestation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), + Call::Ctype(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), + Call::Delegation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)), + Call::Did(did::Call::submit_did_create_operation(..)) => Some(did::DidOperationAuthorizationKey::NoKey), + Call::Did(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), #[cfg(not(feature = "runtime-benchmarks"))] _ => None, // By default, returns the authentication key #[cfg(feature = "runtime-benchmarks")] - _ => Some(did::DidVerificationKeyRelationship::Authentication), + _ => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), } } From ce9ade22289b3b599ab96f23851a351027584d1a Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Tue, 3 Aug 2021 14:59:20 +0200 Subject: [PATCH 03/21] feat: revert DID creation to old version and update test cases --- pallets/did/src/did_details.rs | 27 +-- pallets/did/src/errors.rs | 2 - pallets/did/src/lib.rs | 91 ++++---- pallets/did/src/mock.rs | 38 ++-- pallets/did/src/tests.rs | 404 ++++++++++++++------------------- 5 files changed, 240 insertions(+), 322 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 601ea3c7f4..c7a613b39f 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -119,15 +119,6 @@ pub enum DidVerificationKeyRelationship { AssertionMethod, } -/// Key to be used to optionally verify a DID operation. -#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq, Eq)] -pub enum DidOperationAuthorizationKey { - /// A DID vrification key. - DidKey(DidVerificationKeyRelationship), - /// No key. The DID signature and nonce will not be verified. - NoKey -} - /// Types of signatures supported by this pallet. #[derive(Clone, Decode, Debug, Encode, Eq, PartialEq)] pub enum DidSignature { @@ -511,10 +502,10 @@ impl DidDetails { } } -impl TryFrom<(DidCreationOperation, DidVerificationKey)> for DidDetails { +impl TryFrom<(DidCreationDetails, DidVerificationKey)> for DidDetails { type Error = InputError; - fn try_from(new_details: (DidCreationOperation, DidVerificationKey)) -> Result { + fn try_from(new_details: (DidCreationDetails, DidVerificationKey)) -> Result { let (op, new_auth_key) = new_details; ensure!( @@ -559,10 +550,10 @@ impl TryFrom<(DidCreationOperation, DidVerificationKey)> for DidDetai // Please note that this method does not perform any checks regarding // the validity of the [DidUpdateOperation] signature nor whether the nonce // provided is valid. -impl TryFrom<(DidDetails, DidUpdateOperation)> for DidDetails { +impl TryFrom<(DidDetails, DidUpdateDetails)> for DidDetails { type Error = DidError; - fn try_from((old_details, update_operation): (DidDetails, DidUpdateOperation)) -> Result { + fn try_from((old_details, update_operation): (DidDetails, DidUpdateDetails)) -> Result { ensure!( update_operation.new_key_agreement_keys.len() <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), @@ -639,7 +630,9 @@ impl TryFrom<(DidDetails, DidUpdateOperation)> for DidDetails { + /// The DID identifier. It has to be unique. + pub did: DidIdentifierOf, /// The new key agreement keys. pub new_key_agreement_keys: BTreeSet, /// \[OPTIONAL\] The new attestation key. @@ -657,7 +650,7 @@ pub struct DidCreationOperation { /// required to verify the operation signature, and the tx counter to /// protect against replay attacks. #[derive(Clone, Debug, Decode, Encode, PartialEq)] -pub struct DidUpdateOperation { +pub struct DidUpdateDetails { /// \[OPTIONAL\] The new authentication key. pub new_authentication_key: Option, /// A new set of key agreement keys to add to the ones already stored. @@ -704,7 +697,7 @@ impl Default for DidVerificationKeyUpdateAction { pub trait DeriveDidCallAuthorizationVerificationKeyRelationship { /// The type of the verification key to be used to validate the /// wrapped extrinsic. - fn derive_verification_key_relationship(&self) -> Option; + fn derive_verification_key_relationship(&self) -> Option; // Return a call to dispatch in order to test the pallet proxy feature. #[cfg(feature = "runtime-benchmarks")] @@ -733,7 +726,7 @@ pub struct DidAuthorizedCallOperationWithVerificationRelationship { /// The wrapped [DidAuthorizedCallOperation]. pub operation: DidAuthorizedCallOperation, /// The type of DID key to use for authorization. - pub operation_authorization_key_type: DidOperationAuthorizationKey, + pub operation_authorization_key_type: DidVerificationKeyRelationship, } impl core::ops::Deref for DidAuthorizedCallOperationWithVerificationRelationship { diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index 0ce10ad6a6..8b37443171 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -64,8 +64,6 @@ pub enum SignatureError { InvalidSignature, /// The operation nonce is not equal to the current DID nonce + 1. InvalidNonce, - /// The operation required a signature which has not been provided. - SignatureRequired, } /// Error generated when validating a byte-encoded endpoint URL. diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index bedc5393bb..9e85ad63e4 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -210,12 +210,12 @@ pub mod pallet { pub enum Event { /// A new DID has been created. /// \[transaction signer, DID identifier\] - DidCreated(DidIdentifierOf), + DidCreated(AccountIdentifierOf, DidIdentifierOf), /// A DID has been updated. - /// \[transaction signer, DID identifier\] + /// \[DID identifier\] DidUpdated(DidIdentifierOf), /// A DID has been deleted. - /// \[transaction signer, DID identifier\] + /// \[DID identifier\] DidDeleted(DidIdentifierOf), /// A DID-authorised call has been executed. /// \[DID caller, dispatch result\] @@ -230,8 +230,6 @@ pub mod pallet { /// The DID operation signature is invalid for the payload and the /// verification key provided. InvalidSignature, - /// The operation required a signature which has not been provided. - SignatureRequired, /// The DID with the given identifier is already present on chain. DidAlreadyPresent, /// No DID with the given identifier is present on chain. @@ -298,7 +296,6 @@ pub mod pallet { SignatureError::InvalidSignature => Self::InvalidSignature, SignatureError::InvalidSignatureFormat => Self::InvalidSignatureFormat, SignatureError::InvalidNonce => Self::InvalidNonce, - SignatureError::SignatureRequired => Self::SignatureRequired, } } } @@ -354,43 +351,45 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::submit_did_create_operation_ed25519_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) ) .max(::WeightInfo::submit_did_create_operation_sr25519_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) .max(::WeightInfo::submit_did_create_operation_ecdsa_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) )] - pub fn submit_did_create_operation( + pub fn create( origin: OriginFor, - operation: DidCreationOperation, + details: DidCreationDetails, signature: DidSignature, ) -> DispatchResult { - let did_subject = T::EnsureOrigin::ensure_origin(origin)?; + let sender = ensure_signed(origin)?; + + let did_identifier = details.did.clone(); // There has to be no other DID with the same identifier already saved on chain, // otherwise generate a DidAlreadyPresent error. ensure!( - !>::contains_key(&did_subject), + !>::contains_key(&did_identifier), >::DidAlreadyPresent ); - let account_did_auth_key = did_subject - .verify_and_recover_signature(&operation.encode(), &signature) + let account_did_auth_key = did_identifier + .verify_and_recover_signature(&details.encode(), &signature) .map_err(>::from)?; let did_entry = - DidDetails::try_from((operation.clone(), account_did_auth_key)).map_err(>::from)?; + DidDetails::try_from((details.clone(), account_did_auth_key)).map_err(>::from)?; - log::debug!("Creating DID {:?}", did_subject.clone()); - >::insert(&did_subject, did_entry); + log::debug!("Creating DID {:?}", &did_identifier); + >::insert(&did_identifier, did_entry); - Self::deposit_event(Event::DidCreated(did_subject)); + Self::deposit_event(Event::DidCreated(sender, did_identifier)); Ok(()) } @@ -460,24 +459,24 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::submit_did_update_operation_ed25519_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.public_keys_to_remove.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) ) .max(::WeightInfo::submit_did_update_operation_sr25519_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.public_keys_to_remove.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) .max(::WeightInfo::submit_did_update_operation_ecdsa_keys( - operation.new_key_agreement_keys.len().saturated_into::(), - operation.public_keys_to_remove.len().saturated_into::(), - operation.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) )] - pub fn submit_did_update_operation( + pub fn update( origin: OriginFor, - operation: DidUpdateOperation, + details: DidUpdateDetails, ) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; @@ -485,7 +484,7 @@ pub mod pallet { // Generate a new DidDetails object by applying the changes in the update // operation to the old object (and consuming both). - let new_did_details = DidDetails::try_from((did_details, operation)).map_err(>::from)?; + let new_did_details = DidDetails::try_from((did_details, details)).map_err(>::from)?; log::debug!("Updating DID {:?}", did_subject); >::insert(&did_subject, new_did_details); @@ -518,7 +517,7 @@ pub mod pallet { /// - Kills: Did entry associated to the DID identifier /// # #[pallet::weight(::WeightInfo::submit_did_delete_operation())] - pub fn submit_did_delete_operation( + pub fn delete( origin: OriginFor ) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; @@ -582,7 +581,7 @@ pub mod pallet { pub fn submit_did_call( origin: OriginFor, did_call: Box>, - signature: Option, + signature: DidSignature, ) -> DispatchResultWithPostInfo { ensure_signed(origin)?; @@ -635,23 +634,17 @@ impl Pallet { /// # pub fn verify_did_operation_signature_and_increase_nonce( operation: &DidAuthorizedCallOperationWithVerificationRelationship, - signature: &Option, + signature: &DidSignature, ) -> Result<(), DidError> { let mut did_details = >::get(&operation.did).ok_or(DidError::StorageError(StorageError::DidNotPresent))?; - match operation.operation_authorization_key_type { - // Do nothing if no key is required. - DidOperationAuthorizationKey::NoKey => (), - DidOperationAuthorizationKey::DidKey(verification_key_type) => { - let signature = signature.as_ref().ok_or(DidError::SignatureError(SignatureError::SignatureRequired))?; - Self::validate_counter_value(operation.tx_counter, &did_details)?; - // Increase the tx counter as soon as it is considered valid, no matter if the signature is valid or not. - did_details.increase_tx_counter().map_err(DidError::StorageError)?; - Self::verify_payload_signature_with_did_key_type(operation.encode().as_ref(), signature, &did_details, verification_key_type)?; - >::insert(&operation.did, did_details); - }, - } + Self::validate_counter_value(operation.tx_counter, &did_details)?; + // Increase the tx counter as soon as it is considered valid, no matter if the signature is valid or not. + did_details.increase_tx_counter().map_err(DidError::StorageError)?; + Self::verify_payload_signature_with_did_key_type(operation.encode().as_ref(), signature, &did_details, operation.operation_authorization_key_type)?; + + >::insert(&operation.did, did_details); Ok(()) } diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 69baec3f0a..cd2eb60251 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -259,8 +259,9 @@ pub fn get_url_endpoint(length: u32) -> Url { ) } -pub fn generate_base_did_creation_operation() -> did::DidCreationOperation { - DidCreationOperation { +pub fn generate_base_did_creation_details(did: TestDidIdentifier) -> did::DidCreationDetails { + DidCreationDetails { + did, new_key_agreement_keys: BTreeSet::new(), new_attestation_key: None, new_delegation_key: None, @@ -268,8 +269,8 @@ pub fn generate_base_did_creation_operation() -> did::DidCreationOperation { } } -pub fn generate_base_did_update_operation(did: TestDidIdentifier) -> did::DidUpdateOperation { - DidUpdateOperation { +pub fn generate_base_did_update_details() -> did::DidUpdateDetails { + DidUpdateDetails { new_authentication_key: None, new_key_agreement_keys: BTreeSet::new(), attestation_key_update: DidVerificationKeyUpdateAction::default(), @@ -305,29 +306,21 @@ pub(crate) fn get_delegation_key_test_input() -> TestCtypeHash { pub(crate) fn get_delegation_key_call() -> Call { Call::Ctype(ctype::Call::add(get_delegation_key_test_input())) } -pub(crate) fn get_no_key_test_input() -> TestCtypeHash { - TestCtypeHash::from_slice(&[4u8; 32]) -} -pub(crate) fn get_no_key_call() -> Call { - Call::Ctype(ctype::Call::add(get_no_key_test_input())) -} pub(crate) fn get_none_key_test_input() -> TestCtypeHash { - TestCtypeHash::from_slice(&[5u8; 32]) + TestCtypeHash::from_slice(&[4u8; 32]) } pub(crate) fn get_none_key_call() -> Call { Call::Ctype(ctype::Call::add(get_none_key_test_input())) } impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { if *self == get_attestation_key_call() { - Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)) + Some(did::DidVerificationKeyRelationship::AssertionMethod) } else if *self == get_authentication_key_call() { - Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)) + Some(did::DidVerificationKeyRelationship::Authentication) } else if *self == get_delegation_key_call() { - Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)) - } else if *self == get_no_key_call() { - Some(did::DidOperationAuthorizationKey::NoKey) + Some(did::DidVerificationKeyRelationship::CapabilityDelegation) } else { #[cfg(feature = "runtime-benchmarks")] if *self == Self::get_call_for_did_call_benchmark() { @@ -346,15 +339,14 @@ impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { } pub fn generate_test_did_call( - verification_key_required: did::DidOperationAuthorizationKey, + verification_key_required: did::DidVerificationKeyRelationship, caller: TestDidIdentifier, ) -> did::DidAuthorizedCallOperationWithVerificationRelationship { let call = match verification_key_required { - DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::AssertionMethod) => get_attestation_key_call(), - DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::Authentication) => get_authentication_key_call(), - DidOperationAuthorizationKey::DidKey(DidVerificationKeyRelationship::CapabilityDelegation) => get_delegation_key_call(), - DidOperationAuthorizationKey::DidKey(_) => get_none_key_call(), - DidOperationAuthorizationKey::NoKey => get_no_key_call(), + DidVerificationKeyRelationship::AssertionMethod => get_attestation_key_call(), + DidVerificationKeyRelationship::Authentication => get_authentication_key_call(), + DidVerificationKeyRelationship::CapabilityDelegation => get_delegation_key_call(), + _ => get_none_key_call() }; did::DidAuthorizedCallOperationWithVerificationRelationship { operation: did::DidAuthorizedCallOperation { diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index bc73a2e6c4..31234f7bc3 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -20,26 +20,26 @@ use frame_support::{assert_err, assert_noop, assert_ok}; use sp_core::*; use sp_std::{collections::btree_set::BTreeSet, convert::TryFrom}; -use crate::{self as did, DidAuthorizedCallOperationWithVerificationRelationship, mock::*}; +use crate::{self as did, mock::*}; use ctype::mock as ctype_mock; -// submit_did_create_operation +// create #[test] fn check_successful_simple_ed25519_creation() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( - Origin::signed(alice_did.clone()), - operation.clone(), + assert_ok!(Did::create( + Origin::signed(DEFAULT_ACCOUNT), + details.clone(), did::DidSignature::from(signature), )); }); @@ -65,16 +65,16 @@ fn check_successful_simple_sr25519_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( - Origin::signed(alice_did.clone()), - operation.clone(), + assert_ok!(Did::create( + Origin::signed(DEFAULT_ACCOUNT), + details.clone(), did::DidSignature::from(signature), )); }); @@ -100,16 +100,16 @@ fn check_successful_simple_ecdsa_creation() { let auth_key = get_ecdsa_authentication_key(true); let alice_did = get_did_identifier_from_ecdsa_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( - Origin::signed(alice_did.clone()), - operation.clone(), + assert_ok!(Did::create( + Origin::signed(DEFAULT_ACCOUNT), + details.clone(), did::DidSignature::from(signature), )); }); @@ -146,20 +146,20 @@ fn check_successful_complete_creation() { did::HttpUrl::try_from("https://new_kilt.io".as_bytes()) .expect("https://new_kilt.io should not be considered an invalid HTTP URL."), ); - let mut operation = generate_base_did_creation_operation(); - operation.new_key_agreement_keys = enc_keys.clone(); - operation.new_attestation_key = Some(did::DidVerificationKey::from(att_key.public())); - operation.new_delegation_key = Some(did::DidVerificationKey::from(del_key.public())); - operation.new_endpoint_url = Some(new_url); + let mut details = generate_base_did_creation_details(alice_did.clone()); + details.new_key_agreement_keys = enc_keys.clone(); + details.new_attestation_key = Some(did::DidVerificationKey::from(att_key.public())); + details.new_delegation_key = Some(did::DidVerificationKey::from(del_key.public())); + details.new_endpoint_url = Some(new_url); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( + assert_ok!(Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), )); }); @@ -177,11 +177,11 @@ fn check_successful_complete_creation() { } assert_eq!( stored_did.get_delegation_key_id(), - &Some(generate_key_id(&operation.new_delegation_key.clone().unwrap().into())) + &Some(generate_key_id(&details.new_delegation_key.clone().unwrap().into())) ); assert_eq!( stored_did.get_attestation_key_id(), - &Some(generate_key_id(&operation.new_attestation_key.clone().unwrap().into())) + &Some(generate_key_id(&details.new_attestation_key.clone().unwrap().into())) ); // Authentication key + 2 * Encryption key + Delegation key + Attestation key = // 5 @@ -189,7 +189,7 @@ fn check_successful_complete_creation() { assert!(stored_did .get_public_keys() .contains_key(&generate_key_id(&auth_did_key.into()))); - let mut key_agreement_keys_iterator = operation.new_key_agreement_keys.iter().copied(); + let mut key_agreement_keys_iterator = details.new_key_agreement_keys.iter().copied(); assert!(stored_did .get_public_keys() .contains_key(&generate_key_id(&key_agreement_keys_iterator.next().unwrap().into()))); @@ -198,10 +198,10 @@ fn check_successful_complete_creation() { .contains_key(&generate_key_id(&key_agreement_keys_iterator.next().unwrap().into()))); assert!(stored_did .get_public_keys() - .contains_key(&generate_key_id(&operation.new_attestation_key.clone().unwrap().into()))); + .contains_key(&generate_key_id(&details.new_attestation_key.clone().unwrap().into()))); assert!(stored_did .get_public_keys() - .contains_key(&generate_key_id(&operation.new_delegation_key.clone().unwrap().into()))); + .contains_key(&generate_key_id(&details.new_delegation_key.clone().unwrap().into()))); } #[test] @@ -210,17 +210,17 @@ fn check_duplicate_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let auth_did_key = did::DidVerificationKey::from(auth_key.public()); let mock_did = generate_base_did_details(auth_did_key); - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().with_dids(vec![(alice_did.clone(), mock_did)]).build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_create_operation( + Did::create( Origin::signed(alice_did), - operation.clone(), + details.clone(), did::DidSignature::from(signature), ), did::Error::::DidAlreadyPresent @@ -235,17 +235,17 @@ fn check_invalid_signature_format_did_creation() { // Using an Ed25519 key where an Sr25519 is expected let invalid_key = get_ed25519_authentication_key(true); // DID creation contains auth_key, but signature is generated using invalid_key - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = invalid_key.sign(operation.encode().as_ref()); + let signature = invalid_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_create_operation( + Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), ), did::Error::::InvalidSignature @@ -258,17 +258,17 @@ fn check_invalid_signature_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let alternative_key = get_sr25519_authentication_key(false); - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = alternative_key.sign(operation.encode().as_ref()); + let signature = alternative_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_create_operation( + Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), ), did::Error::::InvalidSignature @@ -282,18 +282,18 @@ fn check_max_limit_key_agreement_keys_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); // Max keys allowed + 1 let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - let mut operation = generate_base_did_creation_operation(); - operation.new_key_agreement_keys = enc_keys; + let mut details = generate_base_did_creation_details(alice_did.clone()); + details.new_key_agreement_keys = enc_keys; - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_create_operation( + Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), ), did::Error::::MaxKeyAgreementKeysLimitExceeded @@ -306,19 +306,19 @@ fn check_url_too_long_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - let mut operation = generate_base_did_creation_operation(); + let mut details = generate_base_did_creation_details(alice_did.clone()); // Max length allowed + 1 - operation.new_endpoint_url = Some(url_endpoint); + details.new_endpoint_url = Some(url_endpoint); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_create_operation( + Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), ), did::Error::::MaxUrlLengthExceeded @@ -326,7 +326,7 @@ fn check_url_too_long_did_creation() { }); } -// submit_did_update_operation +// update #[test] fn check_successful_complete_update() { @@ -355,21 +355,21 @@ fn check_successful_complete_update() { // Update all keys, URL endpoint and tx counter. The old key agreement key is // removed. - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); - operation.new_key_agreement_keys = vec![new_enc_key] + let mut details = generate_base_did_update_details(); + details.new_authentication_key = Some(did::DidVerificationKey::from(new_auth_key.public())); + details.new_key_agreement_keys = vec![new_enc_key] .iter() .copied() .collect::>(); - operation.attestation_key_update = + details.attestation_key_update = did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); - operation.delegation_key_update = + details.delegation_key_update = did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_del_key.public())); - operation.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] + details.public_keys_to_remove = vec![generate_key_id(&old_enc_key.into())] .iter() .copied() .collect::>(); - operation.new_endpoint_url = Some(new_url); + details.new_endpoint_url = Some(new_url); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) @@ -379,9 +379,9 @@ fn check_successful_complete_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( + assert_ok!(Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), )); }); @@ -447,11 +447,11 @@ fn check_successful_keys_deletion_update() { old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - operation.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; + let mut details = generate_base_did_update_details(); + details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; + details.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) @@ -461,9 +461,9 @@ fn check_successful_keys_deletion_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( + assert_ok!(Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), )); }); @@ -502,12 +502,10 @@ fn check_successful_keys_overwrite_update() { let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = + let mut details = generate_base_did_update_details(); + details.attestation_key_update = did::DidVerificationKeyUpdateAction::Change(did::DidVerificationKey::from(new_att_key.public())); - let signature = auth_key.sign(operation.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) .build(None); @@ -516,9 +514,9 @@ fn check_successful_keys_overwrite_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( + assert_ok!(Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), )); }); @@ -562,10 +560,10 @@ fn check_successful_keys_multiuse_update() { old_did_details.update_attestation_key(did::DidVerificationKey::from(old_att_key.public()), 0u64); // Remove attestation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; + let mut details = generate_base_did_update_details(); + details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) @@ -575,9 +573,9 @@ fn check_successful_keys_multiuse_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::submit_did_update_operation( + assert_ok!(Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), )); }); @@ -605,17 +603,15 @@ fn check_did_not_present_update() { let bob_auth_key = get_ed25519_authentication_key(false); let bob_did = get_did_identifier_from_ed25519_key(bob_auth_key.public()); let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let operation = generate_base_did_update_operation(alice_did.clone()); - - let signature = auth_key.sign(operation.encode().as_ref()); + let details = generate_base_did_update_details(); let mut ext = ExtBuilder::default().with_dids(vec![(bob_did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::DidNotPresent ); @@ -632,10 +628,8 @@ fn check_max_limit_key_agreement_keys_did_update() { // Max keys allowed + 1 let new_enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_key_agreement_keys = new_enc_keys; - - let signature = auth_key.sign(operation.encode().as_ref()); + let mut details = generate_base_did_update_details(); + details.new_key_agreement_keys = new_enc_keys; let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -646,9 +640,9 @@ fn check_max_limit_key_agreement_keys_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::MaxKeyAgreementKeysLimitExceeded ); @@ -666,10 +660,10 @@ fn check_max_limit_public_keys_to_remove_did_update() { let keys_ids_to_remove = get_public_keys_to_remove(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.public_keys_to_remove = keys_ids_to_remove; + let mut details = generate_base_did_update_details(); + details.public_keys_to_remove = keys_ids_to_remove; - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -680,9 +674,9 @@ fn check_max_limit_public_keys_to_remove_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::MaxVerificationKeysToRemoveLimitExceeded ); @@ -699,10 +693,10 @@ fn check_url_too_long_did_update() { // Max URL length allowed + 1 let new_endpoint_url = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - let mut operation = generate_base_did_update_operation(alice_did.clone()); - operation.new_endpoint_url = Some(new_endpoint_url); + let mut details = generate_base_did_update_details(); + details.new_endpoint_url = Some(new_endpoint_url); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -713,9 +707,9 @@ fn check_url_too_long_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::MaxUrlLengthExceeded ); @@ -730,16 +724,16 @@ fn check_currently_active_authentication_key_update() { let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); + let mut details = generate_base_did_update_details(); // Trying to remove the currently active authentication key - operation.public_keys_to_remove = vec![generate_key_id( + details.public_keys_to_remove = vec![generate_key_id( &did::DidVerificationKey::from(auth_key.public()).into(), )] .iter() .copied() .collect::>(); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -750,9 +744,9 @@ fn check_currently_active_authentication_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::CurrentlyActiveKey ); @@ -769,14 +763,14 @@ fn check_currently_active_delegation_key_update() { old_did_details.update_delegation_key(did::DidVerificationKey::from(del_key.public()), 0u64); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); + let mut details = generate_base_did_update_details(); // Trying to remove the currently active delegation key - operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] + details.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(del_key.public()).into())] .iter() .copied() .collect::>(); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -787,9 +781,9 @@ fn check_currently_active_delegation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::CurrentlyActiveKey ); @@ -806,14 +800,14 @@ fn check_currently_active_attestation_key_update() { old_did_details.update_attestation_key(did::DidVerificationKey::from(att_key.public()), 0u64); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); + let mut details = generate_base_did_update_details(); // Trying to remove the currently active attestation key - operation.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] + details.public_keys_to_remove = vec![generate_key_id(&did::DidVerificationKey::from(att_key.public()).into())] .iter() .copied() .collect::>(); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -824,9 +818,9 @@ fn check_currently_active_attestation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::CurrentlyActiveKey ); @@ -842,17 +836,15 @@ fn check_verification_key_not_present_update() { let old_did_details = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); // Remove both attestation and delegation key - let mut operation = generate_base_did_update_operation(alice_did.clone()); + let mut details = generate_base_did_update_details(); // Trying to remove the currently active authentication key - operation.public_keys_to_remove = vec![generate_key_id( + details.public_keys_to_remove = vec![generate_key_id( &did::DidVerificationKey::from(key_to_delete.public()).into(), )] .iter() .copied() .collect::>(); - let signature = auth_key.sign(operation.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); @@ -862,16 +854,16 @@ fn check_verification_key_not_present_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::submit_did_update_operation( + Did::update( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), ), did::Error::::VerificationKeyNotPresent ); }); } -// submit_did_delete_operation +// delete #[test] fn check_successful_deletion() { @@ -884,7 +876,7 @@ fn check_successful_deletion() { .build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_delete_operation( + assert_ok!(Did::delete( Origin::signed(alice_did.clone()), )); }); @@ -892,16 +884,16 @@ fn check_successful_deletion() { assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); // Re-adding the same DID identifier, which should not fail. - let operation = generate_base_did_creation_operation(); + let details = generate_base_did_creation_details(alice_did.clone()); - let signature = auth_key.sign(operation.encode().as_ref()); + let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_ok!(Did::submit_did_create_operation( + assert_ok!(Did::create( Origin::signed(alice_did.clone()), - operation.clone(), + details.clone(), did::DidSignature::from(signature), )); }); @@ -916,7 +908,7 @@ fn check_did_not_present_deletion() { ext.execute_with(|| { assert_noop!( - Did::submit_did_delete_operation( + Did::delete( Origin::signed(alice_did.clone()), ), did::Error::::DidNotPresent @@ -935,7 +927,7 @@ fn check_did_not_found_call_error() { // No DID added let mut ext = ExtBuilder::default().build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -943,41 +935,13 @@ fn check_did_not_found_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::DidNotPresent ); }); } -#[test] -fn check_signature_not_provided_error() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); - let signature = auth_key.sign(call_operation.encode().as_ref()); - - ext.execute_with(|| { - assert_noop!( - Did::submit_did_call( - Origin::signed(caller), - Box::new(call_operation.operation), - // Operation requires an authentication key for verification, but no signature is provided. - None - ), - did::Error::::SignatureRequired - ); - }); -} - #[test] fn check_max_counter_call_error() { let auth_key = get_sr25519_authentication_key(true); @@ -990,7 +954,7 @@ fn check_max_counter_call_error() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -998,7 +962,7 @@ fn check_max_counter_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::MaxTxCounterValue ); @@ -1017,7 +981,7 @@ fn check_too_small_tx_counter_call_error() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); call_operation.operation.tx_counter = 0u64; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1026,7 +990,7 @@ fn check_too_small_tx_counter_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::InvalidNonce ); @@ -1044,7 +1008,7 @@ fn check_equal_tx_counter_call_error() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); call_operation.operation.tx_counter = mock_did.last_tx_counter; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1053,7 +1017,7 @@ fn check_equal_tx_counter_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::InvalidNonce ); @@ -1071,7 +1035,7 @@ fn check_too_large_tx_counter_call_error() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); call_operation.operation.tx_counter = mock_did.last_tx_counter + 2u64; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1080,7 +1044,7 @@ fn check_too_large_tx_counter_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::InvalidNonce ); @@ -1100,7 +1064,7 @@ fn check_verification_key_not_present_call_error() { // The operation requires the delegation key that is currently not stored for // the given DID. - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1108,7 +1072,7 @@ fn check_verification_key_not_present_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::VerificationKeyNotPresent ); @@ -1127,7 +1091,7 @@ fn check_invalid_signature_format_call_error() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1135,7 +1099,7 @@ fn check_invalid_signature_format_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::InvalidSignatureFormat ); @@ -1154,7 +1118,7 @@ fn check_invalid_signature_call_error() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = alternative_auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1162,7 +1126,7 @@ fn check_invalid_signature_call_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), did::Error::::InvalidSignature ); @@ -1183,14 +1147,14 @@ fn check_call_attestation_key_successful() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); let signature = attestation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!(Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) )); }); } @@ -1213,7 +1177,7 @@ fn check_call_attestation_key_error() { .with_ctypes(vec![(get_attestation_key_test_input(), did.clone())]) .build(Some(ext)); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); let signature = attestation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1221,7 +1185,7 @@ fn check_call_attestation_key_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), ctype::Error::::CTypeAlreadyExists ); @@ -1242,14 +1206,14 @@ fn check_call_delegation_key_successful() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); let signature = delegation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!(Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) )); }); } @@ -1272,7 +1236,7 @@ fn check_call_delegation_key_error() { .with_ctypes(vec![(get_delegation_key_test_input(), did.clone())]) .build(Some(ext)); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); let signature = delegation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1280,7 +1244,7 @@ fn check_call_delegation_key_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), ctype::Error::::CTypeAlreadyExists ); @@ -1299,14 +1263,14 @@ fn check_call_authentication_key_successful() { .with_dids(vec![(did.clone(), mock_did)]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!(Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) )); }); } @@ -1327,7 +1291,7 @@ fn check_call_authentication_key_error() { .with_ctypes(vec![(get_authentication_key_test_input(), did.clone())]) .build(Some(ext)); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { @@ -1335,7 +1299,7 @@ fn check_call_authentication_key_error() { Did::submit_did_call( Origin::signed(caller), Box::new(call_operation.operation), - Some(did::DidSignature::from(signature)) + did::DidSignature::from(signature) ), ctype::Error::::CTypeAlreadyExists ); @@ -1352,39 +1316,17 @@ fn check_null_key_error() { // CapabilityInvocation is not supported at the moment, so it should return no // key and hence the operation fail. - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityInvocation), did); - let signature = did::DidSignature::from(ed25519::Signature::default()); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityInvocation, did); + let signature = ed25519::Signature::default(); ext.execute_with(|| { assert_noop!( - Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), Some(signature)), + Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), did::DidSignature::from(signature)), did::Error::::UnsupportedDidAuthorizationCall ); }); } -#[test] -fn check_no_key_success() { - let auth_key = get_sr25519_authentication_key(true); - let did = get_did_identifier_from_sr25519_key(auth_key.public()); - let caller = DEFAULT_ACCOUNT; - - let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - - let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did)]) - .build(None); - - // If a DID operation specifies that no key is needed, the signature is not verified hence the call will always be dispatched. - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::NoKey, did); - - ext.execute_with(|| { - assert_ok!( - Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), None), - ); - }); -} - // Internal function: verify_did_operation_signature_and_increase_nonce #[test] @@ -1398,14 +1340,14 @@ fn check_authentication_successful_operation_verification() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ) ); }); @@ -1431,14 +1373,14 @@ fn check_attestation_successful_operation_verification() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did); let signature = attestation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ) ); }); @@ -1464,14 +1406,14 @@ fn check_delegation_successful_operation_verification() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); let signature = delegation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_ok!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ) ); }); @@ -1491,14 +1433,14 @@ fn check_did_not_present_operation_verification() { let mut ext = ExtBuilder::default().build(None); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::StorageError(did::StorageError::DidNotPresent) ); @@ -1517,7 +1459,7 @@ fn check_max_tx_counter_operation_verification() { .with_dids(vec![(did.clone(), mock_did.clone())]) .build(None); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did); call_operation.operation.tx_counter = mock_did.last_tx_counter; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1525,7 +1467,7 @@ fn check_max_tx_counter_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::StorageError(did::StorageError::MaxTxCounterValue) ); @@ -1540,7 +1482,7 @@ fn check_smaller_counter_operation_verification() { let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); mock_did.last_tx_counter = 1; - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = 0u64; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1552,7 +1494,7 @@ fn check_smaller_counter_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::SignatureError(did::SignatureError::InvalidNonce) ); @@ -1566,7 +1508,7 @@ fn check_equal_counter_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = mock_did.last_tx_counter; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1578,7 +1520,7 @@ fn check_equal_counter_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::SignatureError(did::SignatureError::InvalidNonce) ); @@ -1592,7 +1534,7 @@ fn check_too_large_counter_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation), did.clone()); + let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = mock_did.last_tx_counter + 2; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1604,7 +1546,7 @@ fn check_too_large_counter_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::SignatureError(did::SignatureError::InvalidNonce) ); @@ -1618,7 +1560,7 @@ fn check_verification_key_not_present_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod), did.clone()); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did.clone()); let signature = auth_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() @@ -1629,7 +1571,7 @@ fn check_verification_key_not_present_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::StorageError(did::StorageError::DidKeyNotPresent(did::DidVerificationKeyRelationship::AssertionMethod)) ); @@ -1645,7 +1587,7 @@ fn check_invalid_signature_format_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did.clone()); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did.clone()); let signature = invalid_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() @@ -1656,7 +1598,7 @@ fn check_invalid_signature_format_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::SignatureError(did::SignatureError::InvalidSignatureFormat) ); @@ -1672,7 +1614,7 @@ fn check_invalid_signature_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let call_operation = generate_test_did_call(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication), did.clone()); + let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did.clone()); let signature = alternative_key.sign(&call_operation.encode()); let mut ext = ExtBuilder::default() @@ -1683,7 +1625,7 @@ fn check_invalid_signature_operation_verification() { assert_noop!( Did::verify_did_operation_signature_and_increase_nonce( &call_operation, - &Some(did::DidSignature::from(signature)) + &did::DidSignature::from(signature) ), did::DidError::SignatureError(did::SignatureError::InvalidSignature) ); From 77521373700275c753f3d7bbdfbbfc27319d8111 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 08:18:03 +0200 Subject: [PATCH 04/21] fix: update test cases and benchmarks --- pallets/did/src/benchmarking.rs | 149 ++++++++++++++------------- pallets/did/src/lib.rs | 38 +++---- pallets/did/src/mock.rs | 8 +- pallets/did/src/tests.rs | 177 +++++++++++++++----------------- runtimes/peregrine/src/lib.rs | 15 +-- runtimes/standalone/src/lib.rs | 15 +-- 6 files changed, 189 insertions(+), 213 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 452c654b32..44e8cc255d 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -114,8 +114,9 @@ fn get_did_base_details(auth_key: DidVerificationKey) -> DidDetails::default()) } -fn generate_base_did_creation_operation() -> DidCreationOperation { - DidCreationOperation { +fn generate_base_did_creation_details(did: DidIdentifierOf) -> DidCreationDetails { + DidCreationDetails { + did, new_key_agreement_keys: BTreeSet::new(), new_attestation_key: None, new_delegation_key: None, @@ -123,8 +124,8 @@ fn generate_base_did_creation_operation() -> DidCreationOperation { } } -fn generate_base_did_update_operation(did: DidIdentifierOf) -> DidUpdateOperation { - DidUpdateOperation { +fn generate_base_did_update_details(did: DidIdentifierOf) -> DidUpdateDetails { + DidUpdateDetails { new_authentication_key: None, new_key_agreement_keys: BTreeSet::new(), attestation_key_update: DidVerificationKeyUpdateAction::default(), @@ -149,7 +150,7 @@ benchmarks! { where_clause { where T::DidIdentifier: From, ::Origin: From>} - submit_did_create_operation_ed25519_keys { + create_ed25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -162,14 +163,14 @@ benchmarks! { let did_public_del_key = get_ed25519_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(); - did_creation_op.new_key_agreement_keys = did_key_agreement_keys; - did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); - did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); - did_creation_op.new_endpoint_url = Some(did_endpoint); + let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone()); + did_creation_details.new_key_agreement_keys = did_key_agreement_keys; + did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); + did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); + did_creation_details.new_endpoint_url = Some(did_endpoint); - let did_creation_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: submit_did_create_operation(RawOrigin::Signed(submitter), did_creation_op.clone(), DidSignature::from(did_creation_signature)) + let did_creation_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); + }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -182,7 +183,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_creation_op.new_key_agreement_keys.iter().copied() { + for new_key in did_creation_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -194,11 +195,11 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_creation_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url); assert_eq!(stored_did.last_tx_counter, 0u64); } - submit_did_create_operation_sr25519_keys { + create_sr25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -211,14 +212,14 @@ benchmarks! { let did_public_del_key = get_sr25519_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(); - did_creation_op.new_key_agreement_keys = did_key_agreement_keys; - did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); - did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); - did_creation_op.new_endpoint_url = Some(did_endpoint); + let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone()); + did_creation_details.new_key_agreement_keys = did_key_agreement_keys; + did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); + did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); + did_creation_details.new_endpoint_url = Some(did_endpoint); - let did_creation_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_op.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: submit_did_create_operation(RawOrigin::Signed(submitter), did_creation_op.clone(), DidSignature::from(did_creation_signature)) + let did_creation_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); + }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -231,7 +232,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_creation_op.new_key_agreement_keys.iter().copied() { + for new_key in did_creation_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -243,11 +244,11 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_creation_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url); assert_eq!(stored_did.last_tx_counter, 0u64); } - submit_did_create_operation_ecdsa_keys { + create_ecdsa_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -260,14 +261,14 @@ benchmarks! { let did_public_del_key = get_ecdsa_public_delegation_key(); let did_endpoint = get_url_endpoint(u); - let mut did_creation_op = generate_base_did_creation_operation::(); - did_creation_op.new_key_agreement_keys = did_key_agreement_keys; - did_creation_op.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key.clone())); - did_creation_op.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key.clone())); - did_creation_op.new_endpoint_url = Some(did_endpoint); + let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone()); + did_creation_details.new_key_agreement_keys = did_key_agreement_keys; + did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key.clone())); + did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key.clone())); + did_creation_details.new_endpoint_url = Some(did_endpoint); - let did_creation_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_op.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: submit_did_create_operation(RawOrigin::Signed(submitter), did_creation_op.clone(), DidSignature::from(did_creation_signature)) + let did_creation_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); + }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -280,7 +281,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_creation_op.new_key_agreement_keys.iter().copied() { + for new_key in did_creation_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -292,11 +293,11 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_creation_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url); assert_eq!(stored_did.last_tx_counter, 0u64); } - submit_did_update_operation_ed25519_keys { + update_ed25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let m in 1 .. T::MaxVerificationKeysToRevoke::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -320,16 +321,16 @@ benchmarks! { let public_keys_to_remove = get_public_keys::(m); let new_url = get_url_endpoint(u); - let mut did_update_op = generate_base_did_update_operation::(did_subject.clone()); - did_update_op.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key)); - did_update_op.new_key_agreement_keys = new_key_agreement_keys; - did_update_op.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key)); - did_update_op.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key)); - did_update_op.public_keys_to_remove = public_keys_to_remove; - did_update_op.new_endpoint_url = Some(new_url); + let mut did_update_details = generate_base_did_update_details::(did_subject.clone()); + did_update_details.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key)); + did_update_details.new_key_agreement_keys = new_key_agreement_keys; + did_update_details.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key)); + did_update_details.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key)); + did_update_details.public_keys_to_remove = public_keys_to_remove; + did_update_details.new_endpoint_url = Some(new_url); - let did_update_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) + let did_update_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); + }: update(RawOrigin::Signed(submitter), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -342,7 +343,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_update_op.new_key_agreement_keys.iter().copied() { + for new_key in did_update_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -354,10 +355,10 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url); } - submit_did_update_operation_sr25519_keys { + update_sr25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let m in 1 .. T::MaxVerificationKeysToRevoke::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -381,16 +382,16 @@ benchmarks! { let public_keys_to_remove = get_public_keys::(m); let new_url = get_url_endpoint(u); - let mut did_update_op = generate_base_did_update_operation::(did_subject.clone()); - did_update_op.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key)); - did_update_op.new_key_agreement_keys = new_key_agreement_keys; - did_update_op.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key)); - did_update_op.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key)); - did_update_op.public_keys_to_remove = public_keys_to_remove; - did_update_op.new_endpoint_url = Some(new_url); + let mut did_update_details = generate_base_did_update_details::(did_subject.clone()); + did_update_details.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key)); + did_update_details.new_key_agreement_keys = new_key_agreement_keys; + did_update_details.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key)); + did_update_details.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key)); + did_update_details.public_keys_to_remove = public_keys_to_remove; + did_update_details.new_endpoint_url = Some(new_url); - let did_update_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) + let did_update_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); + }: update(RawOrigin::Signed(submitter), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -403,7 +404,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_update_op.new_key_agreement_keys.iter().copied() { + for new_key in did_update_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -415,10 +416,10 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url); } - submit_did_update_operation_ecdsa_keys { + update_ecdsa_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); let m in 1 .. T::MaxVerificationKeysToRevoke::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); @@ -442,16 +443,16 @@ benchmarks! { let public_keys_to_remove = get_public_keys::(m); let new_url = get_url_endpoint(u); - let mut did_update_op = generate_base_did_update_operation::(did_subject.clone()); - did_update_op.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key.clone())); - did_update_op.new_key_agreement_keys = new_key_agreement_keys; - did_update_op.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key.clone())); - did_update_op.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key.clone())); - did_update_op.public_keys_to_remove = public_keys_to_remove; - did_update_op.new_endpoint_url = Some(new_url); + let mut did_update_details = generate_base_did_update_details::(did_subject.clone()); + did_update_details.new_authentication_key = Some(DidVerificationKey::from(new_did_public_auth_key.clone())); + did_update_details.new_key_agreement_keys = new_key_agreement_keys; + did_update_details.attestation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key.clone())); + did_update_details.delegation_key_update = DidVerificationKeyUpdateAction::Change(DidVerificationKey::from(new_did_public_del_key.clone())); + did_update_details.public_keys_to_remove = public_keys_to_remove; + did_update_details.new_endpoint_url = Some(new_url); - let did_update_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_op.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: submit_did_update_operation(RawOrigin::Signed(submitter), did_update_op.clone()) + let did_update_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); + }: update(RawOrigin::Signed(submitter), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -464,7 +465,7 @@ benchmarks! { stored_did.get_authentication_key_id(), expected_authentication_key_id ); - for new_key in did_update_op.new_key_agreement_keys.iter().copied() { + for new_key in did_update_details.new_key_agreement_keys.iter().copied() { assert!( stored_key_agreement_keys_ids.contains(&utils::calculate_key_id::(&new_key.into()))) } @@ -476,10 +477,10 @@ benchmarks! { stored_did.get_attestation_key_id(), &Some(expected_attestation_key_id) ); - assert_eq!(stored_did.endpoint_url, did_update_op.new_endpoint_url); + assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url); } - submit_did_delete_operation { + delete { let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); let did_public_auth_key = get_ed25519_public_authentication_key(); @@ -507,7 +508,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) submit_did_call_sr25519_key { let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -521,7 +522,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) submit_did_call_ecdsa_key { let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -535,7 +536,7 @@ benchmarks! { let did_call_op = generate_base_did_call_operation::(did_subject); let did_call_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_call_op.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), Some(DidSignature::from(did_call_signature))) + }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) } // impl_benchmark_test_suite! { diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 9e85ad63e4..914d9456a0 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -363,28 +363,20 @@ pub mod pallet { details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) )] - pub fn create( - origin: OriginFor, - details: DidCreationDetails, - signature: DidSignature, - ) -> DispatchResult { + pub fn create(origin: OriginFor, details: DidCreationDetails, signature: DidSignature) -> DispatchResult { let sender = ensure_signed(origin)?; let did_identifier = details.did.clone(); // There has to be no other DID with the same identifier already saved on chain, // otherwise generate a DidAlreadyPresent error. - ensure!( - !>::contains_key(&did_identifier), - >::DidAlreadyPresent - ); + ensure!(!>::contains_key(&did_identifier), >::DidAlreadyPresent); let account_did_auth_key = did_identifier .verify_and_recover_signature(&details.encode(), &signature) .map_err(>::from)?; - let did_entry = - DidDetails::try_from((details.clone(), account_did_auth_key)).map_err(>::from)?; + let did_entry = DidDetails::try_from((details.clone(), account_did_auth_key)).map_err(>::from)?; log::debug!("Creating DID {:?}", &did_identifier); >::insert(&did_identifier, did_entry); @@ -474,10 +466,7 @@ pub mod pallet { details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) )) )] - pub fn update( - origin: OriginFor, - details: DidUpdateDetails, - ) -> DispatchResult { + pub fn update(origin: OriginFor, details: DidUpdateDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; let did_details = >::get(&did_subject).ok_or(>::DidNotPresent)?; @@ -517,9 +506,7 @@ pub mod pallet { /// - Kills: Did entry associated to the DID identifier /// # #[pallet::weight(::WeightInfo::submit_did_delete_operation())] - pub fn delete( - origin: OriginFor - ) -> DispatchResult { + pub fn delete(origin: OriginFor) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; ensure!( @@ -640,9 +627,15 @@ impl Pallet { >::get(&operation.did).ok_or(DidError::StorageError(StorageError::DidNotPresent))?; Self::validate_counter_value(operation.tx_counter, &did_details)?; - // Increase the tx counter as soon as it is considered valid, no matter if the signature is valid or not. + // Increase the tx counter as soon as it is considered valid, no matter if the + // signature is valid or not. did_details.increase_tx_counter().map_err(DidError::StorageError)?; - Self::verify_payload_signature_with_did_key_type(operation.encode().as_ref(), signature, &did_details, operation.operation_authorization_key_type)?; + Self::verify_payload_signature_with_did_key_type( + operation.encode().as_ref(), + signature, + &did_details, + operation.operation_authorization_key_type, + )?; >::insert(&operation.did, did_details); @@ -655,10 +648,7 @@ impl Pallet { // 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<(), DidError> { // Verify that the DID has not reached the maximum tx counter value ensure!( did_details.get_tx_counter_value() < u64::MAX, diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index cd2eb60251..dea845693c 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -325,7 +325,7 @@ impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { #[cfg(feature = "runtime-benchmarks")] if *self == Self::get_call_for_did_call_benchmark() { // Always require an authentication key to dispatch calls during benchmarking - return Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)); + return Some(did::DidVerificationKeyRelationship::Authentication); } None } @@ -346,15 +346,15 @@ pub fn generate_test_did_call( DidVerificationKeyRelationship::AssertionMethod => get_attestation_key_call(), DidVerificationKeyRelationship::Authentication => get_authentication_key_call(), DidVerificationKeyRelationship::CapabilityDelegation => get_delegation_key_call(), - _ => get_none_key_call() + _ => get_none_key_call(), }; did::DidAuthorizedCallOperationWithVerificationRelationship { operation: did::DidAuthorizedCallOperation { did: caller, call, - tx_counter: 1u64 + tx_counter: 1u64, }, - operation_authorization_key_type: verification_key_required + operation_authorization_key_type: verification_key_required, } } diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 31234f7bc3..9b9cbbffd6 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -158,7 +158,7 @@ fn check_successful_complete_creation() { ext.execute_with(|| { assert_ok!(Did::create( - Origin::signed(alice_did.clone()), + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), )); @@ -214,12 +214,14 @@ fn check_duplicate_did_creation() { let signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default().with_dids(vec![(alice_did.clone(), mock_did)]).build(None); + let mut ext = ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), mock_did)]) + .build(None); ext.execute_with(|| { assert_noop!( Did::create( - Origin::signed(alice_did), + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), ), @@ -244,7 +246,7 @@ fn check_invalid_signature_format_did_creation() { ext.execute_with(|| { assert_noop!( Did::create( - Origin::signed(alice_did.clone()), + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), ), @@ -267,7 +269,30 @@ fn check_invalid_signature_did_creation() { ext.execute_with(|| { assert_noop!( Did::create( - Origin::signed(alice_did.clone()), + Origin::signed(DEFAULT_ACCOUNT), + details.clone(), + did::DidSignature::from(signature), + ), + did::Error::::InvalidSignature + ); + }); +} + +#[test] +fn check_swapped_did_subject_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let swapped_key = get_sr25519_authentication_key(false); + let swapped_did = get_did_identifier_from_sr25519_key(swapped_key.public()); + let details = generate_base_did_creation_details(swapped_did.clone()); + + let signature = auth_key.sign(details.encode().as_ref()); + + let mut ext = ExtBuilder::default().build(None); + + ext.execute_with(|| { + assert_noop!( + Did::create( + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), ), @@ -292,7 +317,7 @@ fn check_max_limit_key_agreement_keys_did_creation() { ext.execute_with(|| { assert_noop!( Did::create( - Origin::signed(alice_did.clone()), + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), ), @@ -317,7 +342,7 @@ fn check_url_too_long_did_creation() { ext.execute_with(|| { assert_noop!( Did::create( - Origin::signed(alice_did.clone()), + Origin::signed(DEFAULT_ACCOUNT), details.clone(), did::DidSignature::from(signature), ), @@ -379,10 +404,7 @@ fn check_successful_complete_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - )); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); }); let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); @@ -461,10 +483,7 @@ fn check_successful_keys_deletion_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - )); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); }); // Auth key and key agreement key unchanged @@ -514,10 +533,7 @@ fn check_successful_keys_overwrite_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - )); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); }); // Auth key unchanged @@ -573,10 +589,7 @@ fn check_successful_keys_multiuse_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - )); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); }); // Auth key unchanged @@ -609,10 +622,7 @@ fn check_did_not_present_update() { ext.execute_with(|| { assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::DidNotPresent ); }); @@ -640,10 +650,7 @@ fn check_max_limit_key_agreement_keys_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::MaxKeyAgreementKeysLimitExceeded ); }); @@ -674,10 +681,7 @@ fn check_max_limit_public_keys_to_remove_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::MaxVerificationKeysToRemoveLimitExceeded ); }); @@ -707,10 +711,7 @@ fn check_url_too_long_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::MaxUrlLengthExceeded ); }); @@ -744,10 +745,7 @@ fn check_currently_active_authentication_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::CurrentlyActiveKey ); }); @@ -781,10 +779,7 @@ fn check_currently_active_delegation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::CurrentlyActiveKey ); }); @@ -818,10 +813,7 @@ fn check_currently_active_attestation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::CurrentlyActiveKey ); }); @@ -854,10 +846,7 @@ fn check_verification_key_not_present_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update( - Origin::signed(alice_did.clone()), - details.clone(), - ), + Did::update(Origin::signed(alice_did.clone()), details.clone(),), did::Error::::VerificationKeyNotPresent ); }); @@ -876,9 +865,7 @@ fn check_successful_deletion() { .build(None); ext.execute_with(|| { - assert_ok!(Did::delete( - Origin::signed(alice_did.clone()), - )); + assert_ok!(Did::delete(Origin::signed(alice_did.clone()),)); }); assert_eq!(ext.execute_with(|| Did::get_did(alice_did.clone())), None); @@ -908,9 +895,7 @@ fn check_did_not_present_deletion() { ext.execute_with(|| { assert_noop!( - Did::delete( - Origin::signed(alice_did.clone()), - ), + Did::delete(Origin::signed(alice_did.clone()),), did::Error::::DidNotPresent ); }); @@ -1321,7 +1306,11 @@ fn check_null_key_error() { ext.execute_with(|| { assert_noop!( - Did::submit_did_call(Origin::signed(caller), Box::new(call_operation.operation), did::DidSignature::from(signature)), + Did::submit_did_call( + Origin::signed(caller), + Box::new(call_operation.operation), + did::DidSignature::from(signature) + ), did::Error::::UnsupportedDidAuthorizationCall ); }); @@ -1344,16 +1333,15 @@ fn check_authentication_successful_operation_verification() { let signature = auth_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { - assert_ok!( - Did::verify_did_operation_signature_and_increase_nonce( - &call_operation, - &did::DidSignature::from(signature) - ) - ); + assert_ok!(Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &did::DidSignature::from(signature) + )); }); // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + let did_details = + ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); assert_eq!( did_details.get_tx_counter_value(), mock_did.get_tx_counter_value() + 1u64 @@ -1377,16 +1365,15 @@ fn check_attestation_successful_operation_verification() { let signature = attestation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { - assert_ok!( - Did::verify_did_operation_signature_and_increase_nonce( - &call_operation, - &did::DidSignature::from(signature) - ) - ); + assert_ok!(Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &did::DidSignature::from(signature) + )); }); // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + let did_details = + ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); assert_eq!( did_details.get_tx_counter_value(), mock_did.get_tx_counter_value() + 1u64 @@ -1410,16 +1397,15 @@ fn check_delegation_successful_operation_verification() { let signature = delegation_key.sign(call_operation.encode().as_ref()); ext.execute_with(|| { - assert_ok!( - Did::verify_did_operation_signature_and_increase_nonce( - &call_operation, - &did::DidSignature::from(signature) - ) - ); + assert_ok!(Did::verify_did_operation_signature_and_increase_nonce( + &call_operation, + &did::DidSignature::from(signature) + )); }); // Verify that the DID tx counter has increased - let did_details = ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); + let did_details = + ext.execute_with(|| Did::get_did(&call_operation.operation.did).expect("DID should be present on chain.")); assert_eq!( did_details.get_tx_counter_value(), mock_did.get_tx_counter_value() + 1u64 @@ -1482,7 +1468,8 @@ fn check_smaller_counter_operation_verification() { let mut mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); mock_did.last_tx_counter = 1; - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); + let mut call_operation = + generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = 0u64; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1508,7 +1495,8 @@ fn check_equal_counter_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); + let mut call_operation = + generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = mock_did.last_tx_counter; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1534,7 +1522,8 @@ fn check_too_large_counter_operation_verification() { let mock_did = generate_base_did_details(did::DidVerificationKey::from(auth_key.public())); - let mut call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); + let mut call_operation = + generate_test_did_call(did::DidVerificationKeyRelationship::CapabilityDelegation, did.clone()); call_operation.operation.tx_counter = mock_did.last_tx_counter + 2; let signature = auth_key.sign(call_operation.encode().as_ref()); @@ -1573,7 +1562,9 @@ fn check_verification_key_not_present_operation_verification() { &call_operation, &did::DidSignature::from(signature) ), - did::DidError::StorageError(did::StorageError::DidKeyNotPresent(did::DidVerificationKeyRelationship::AssertionMethod)) + did::DidError::StorageError(did::StorageError::DidKeyNotPresent( + did::DidVerificationKeyRelationship::AssertionMethod + )) ); }); } @@ -1751,11 +1742,7 @@ fn check_ipfs_url() { ); assert_eq!( - did::IpfsUrl::try_from( - "ipfs://¶ -QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" - .as_bytes() - ), + did::IpfsUrl::try_from("ipfs://¶QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL".as_bytes()), Err(did::UrlError::InvalidUrlEncoding) ); @@ -1765,11 +1752,7 @@ QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" ); assert_eq!( - did::IpfsUrl::try_from( - "ipfss:// -QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQkL" - .as_bytes() - ), + did::IpfsUrl::try_from("ipfss://QmdQ1rHHHTbgbGorfuMMYDQQ36q4sxvYcB4GDEHREuJQk".as_bytes()), Err(did::UrlError::InvalidUrlScheme) ); } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 24d980d5cd..37542c4cd6 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -842,18 +842,19 @@ construct_runtime! { } impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { match self { - Call::Attestation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), - Call::Ctype(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), - Call::Delegation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)), - Call::Did(did::Call::submit_did_create_operation(..)) => Some(did::DidOperationAuthorizationKey::NoKey), - Call::Did(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), + Call::Attestation(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), + Call::Ctype(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), + Call::Delegation(_) => Some(did::DidVerificationKeyRelationship::CapabilityDelegation), + // DID creation is not allowed through the DID proxy. + Call::Did(did::Call::create(..)) => None, + Call::Did(_) => Some(did::DidVerificationKeyRelationship::Authentication), #[cfg(not(feature = "runtime-benchmarks"))] _ => None, // By default, returns the authentication key #[cfg(feature = "runtime-benchmarks")] - _ => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), + _ => Some(did::DidVerificationKeyRelationship::Authentication), } } diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 7f421c7e4e..069c5a8a69 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -476,18 +476,19 @@ construct_runtime!( ); impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for Call { - fn derive_verification_key_relationship(&self) -> Option { + fn derive_verification_key_relationship(&self) -> Option { match self { - Call::Attestation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), - Call::Ctype(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::AssertionMethod)), - Call::Delegation(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::CapabilityDelegation)), - Call::Did(did::Call::submit_did_create_operation(..)) => Some(did::DidOperationAuthorizationKey::NoKey), - Call::Did(_) => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), + Call::Attestation(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), + Call::Ctype(_) => Some(did::DidVerificationKeyRelationship::AssertionMethod), + Call::Delegation(_) => Some(did::DidVerificationKeyRelationship::CapabilityDelegation), + // DID creation is not allowed through the DID proxy. + Call::Did(did::Call::create(..)) => None, + Call::Did(_) => Some(did::DidVerificationKeyRelationship::Authentication), #[cfg(not(feature = "runtime-benchmarks"))] _ => None, // By default, returns the authentication key #[cfg(feature = "runtime-benchmarks")] - _ => Some(did::DidOperationAuthorizationKey::DidKey(did::DidVerificationKeyRelationship::Authentication)), + _ => Some(did::DidVerificationKeyRelationship::Authentication), } } From 92cc17010448b1bf09135b7ae485fbd4ddcfb6d6 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 08:50:17 +0200 Subject: [PATCH 05/21] wip: comment out current weights --- pallets/did/src/benchmarking.rs | 24 +++++++++------------ pallets/did/src/lib.rs | 37 ++++---------------------------- pallets/did/src/mock.rs | 5 ++++- pallets/did/src/tests.rs | 38 ++++++++++++++++----------------- 4 files changed, 37 insertions(+), 67 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 44e8cc255d..71366dd103 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -124,7 +124,7 @@ fn generate_base_did_creation_details(did: DidIdentifierOf) -> Did } } -fn generate_base_did_update_details(did: DidIdentifierOf) -> DidUpdateDetails { +fn generate_base_did_update_details(_did: DidIdentifierOf) -> DidUpdateDetails { DidUpdateDetails { new_authentication_key: None, new_key_agreement_keys: BTreeSet::new(), @@ -302,8 +302,6 @@ benchmarks! { let m in 1 .. T::MaxVerificationKeysToRevoke::get(); let u in (DEFAULT_URL_SCHEME.len().saturated_into::()) .. T::MaxUrlLength::get(); - let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); - let did_public_auth_key = get_ed25519_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); // To cover cases in which m > n without failing, we add m + n keys to the set of keys before the update operation @@ -330,7 +328,7 @@ benchmarks! { did_update_details.new_endpoint_url = Some(new_url); let did_update_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: update(RawOrigin::Signed(submitter), did_update_details.clone()) + }: update(RawOrigin::Signed(did_subject.clone()), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -391,7 +389,7 @@ benchmarks! { did_update_details.new_endpoint_url = Some(new_url); let did_update_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: update(RawOrigin::Signed(submitter), did_update_details.clone()) + }: update(RawOrigin::Signed(did_subject.clone()), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -452,7 +450,7 @@ benchmarks! { did_update_details.new_endpoint_url = Some(new_url); let did_update_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_update_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: update(RawOrigin::Signed(submitter), did_update_details.clone()) + }: update(RawOrigin::Signed(did_subject.clone()), did_update_details.clone()) verify { let stored_did = Did::::get(&did_subject).expect("DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids(); @@ -481,14 +479,12 @@ benchmarks! { } delete { - let submitter: AccountIdentifierOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); - let did_public_auth_key = get_ed25519_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); let did_details = get_did_base_details(DidVerificationKey::from(did_public_auth_key)); Did::::insert(&did_subject, did_details); - }: _(RawOrigin::Signed(submitter)) + }: _(RawOrigin::Signed(did_subject.clone())) verify { assert_eq!( Did::::get(&did_subject), @@ -539,8 +535,8 @@ benchmarks! { }: submit_did_call(RawOrigin::Signed(submitter), Box::new(did_call_op.clone()), DidSignature::from(did_call_signature)) } -// impl_benchmark_test_suite! { -// Pallet, -// crate::mock::ExtBuilder::default().build_with_keystore(None), -// crate::mock::Test -// } +impl_benchmark_test_suite! { + Pallet, + crate::mock::ExtBuilder::default().build_with_keystore(None), + crate::mock::Test +} diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 914d9456a0..b4c96ab447 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -349,20 +349,7 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Writes: Did (with K new key agreement keys) /// # - #[pallet::weight( - ::WeightInfo::submit_did_create_operation_ed25519_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - ) - .max(::WeightInfo::submit_did_create_operation_sr25519_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - )) - .max(::WeightInfo::submit_did_create_operation_ecdsa_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - )) - )] + #[pallet::weight(1)] pub fn create(origin: OriginFor, details: DidCreationDetails, signature: DidSignature) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -376,7 +363,7 @@ pub mod pallet { .verify_and_recover_signature(&details.encode(), &signature) .map_err(>::from)?; - let did_entry = DidDetails::try_from((details.clone(), account_did_auth_key)).map_err(>::from)?; + let did_entry = DidDetails::try_from((details, account_did_auth_key)).map_err(>::from)?; log::debug!("Creating DID {:?}", &did_identifier); >::insert(&did_identifier, did_entry); @@ -449,23 +436,7 @@ pub mod pallet { /// - Writes: Did (with K new key agreement keys and removing D public /// keys) /// # - #[pallet::weight( - ::WeightInfo::submit_did_update_operation_ed25519_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.public_keys_to_remove.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - ) - .max(::WeightInfo::submit_did_update_operation_sr25519_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.public_keys_to_remove.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - )) - .max(::WeightInfo::submit_did_update_operation_ecdsa_keys( - details.new_key_agreement_keys.len().saturated_into::(), - details.public_keys_to_remove.len().saturated_into::(), - details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) - )) - )] + #[pallet::weight(1)] pub fn update(origin: OriginFor, details: DidUpdateDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; @@ -505,7 +476,7 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Kills: Did entry associated to the DID identifier /// # - #[pallet::weight(::WeightInfo::submit_did_delete_operation())] + #[pallet::weight(1)] pub fn delete(origin: OriginFor) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index dea845693c..663c077045 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -19,7 +19,7 @@ #![allow(clippy::from_over_into)] #![allow(unused_must_use)] -use codec::{Decode, Encode}; + use frame_support::{parameter_types, weights::constants::RocksDbWeight}; use frame_system::EnsureSigned; use sp_core::{ecdsa, ed25519, sr25519, Pair}; @@ -97,7 +97,10 @@ impl Config for Test { type DidIdentifier = TestDidIdentifier; type Origin = Origin; type Call = Call; + #[cfg(feature = "runtime-benchmarks")] type EnsureOrigin = EnsureSigned; + #[cfg(not(feature = "runtime-benchmarks"))] + type EnsureOrigin = did::EnsureDidOrigin; type Event = (); type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxUrlLength = MaxUrlLength; diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 9b9cbbffd6..08ebb555da 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -215,7 +215,7 @@ fn check_duplicate_did_creation() { let signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), mock_did)]) + .with_dids(vec![(alice_did, mock_did)]) .build(None); ext.execute_with(|| { @@ -237,7 +237,7 @@ fn check_invalid_signature_format_did_creation() { // Using an Ed25519 key where an Sr25519 is expected let invalid_key = get_ed25519_authentication_key(true); // DID creation contains auth_key, but signature is generated using invalid_key - let details = generate_base_did_creation_details(alice_did.clone()); + let details = generate_base_did_creation_details(alice_did); let signature = invalid_key.sign(details.encode().as_ref()); @@ -260,7 +260,7 @@ fn check_invalid_signature_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let alternative_key = get_sr25519_authentication_key(false); - let details = generate_base_did_creation_details(alice_did.clone()); + let details = generate_base_did_creation_details(alice_did); let signature = alternative_key.sign(details.encode().as_ref()); @@ -283,7 +283,7 @@ fn check_swapped_did_subject_did_creation() { let auth_key = get_sr25519_authentication_key(true); let swapped_key = get_sr25519_authentication_key(false); let swapped_did = get_did_identifier_from_sr25519_key(swapped_key.public()); - let details = generate_base_did_creation_details(swapped_did.clone()); + let details = generate_base_did_creation_details(swapped_did); let signature = auth_key.sign(details.encode().as_ref()); @@ -307,7 +307,7 @@ fn check_max_limit_key_agreement_keys_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); // Max keys allowed + 1 let enc_keys = get_key_agreement_keys(::MaxNewKeyAgreementKeys::get().saturating_add(1)); - let mut details = generate_base_did_creation_details(alice_did.clone()); + let mut details = generate_base_did_creation_details(alice_did); details.new_key_agreement_keys = enc_keys; let signature = auth_key.sign(details.encode().as_ref()); @@ -331,7 +331,7 @@ fn check_url_too_long_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let url_endpoint = get_url_endpoint(::MaxUrlLength::get().saturating_add(1)); - let mut details = generate_base_did_creation_details(alice_did.clone()); + let mut details = generate_base_did_creation_details(alice_did); // Max length allowed + 1 details.new_endpoint_url = Some(url_endpoint); @@ -473,7 +473,7 @@ fn check_successful_keys_deletion_update() { details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; details.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) @@ -579,7 +579,7 @@ fn check_successful_keys_multiuse_update() { let mut details = generate_base_did_update_details(); details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) @@ -670,7 +670,7 @@ fn check_max_limit_public_keys_to_remove_did_update() { let mut details = generate_base_did_update_details(); details.public_keys_to_remove = keys_ids_to_remove; - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -700,7 +700,7 @@ fn check_url_too_long_did_update() { let mut details = generate_base_did_update_details(); details.new_endpoint_url = Some(new_endpoint_url); - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -734,7 +734,7 @@ fn check_currently_active_authentication_key_update() { .copied() .collect::>(); - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -768,7 +768,7 @@ fn check_currently_active_delegation_key_update() { .copied() .collect::>(); - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -802,7 +802,7 @@ fn check_currently_active_attestation_key_update() { .copied() .collect::>(); - let signature = auth_key.sign(details.encode().as_ref()); + let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) @@ -1474,7 +1474,7 @@ fn check_smaller_counter_operation_verification() { let signature = auth_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { @@ -1501,7 +1501,7 @@ fn check_equal_counter_operation_verification() { let signature = auth_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { @@ -1528,7 +1528,7 @@ fn check_too_large_counter_operation_verification() { let signature = auth_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { @@ -1553,7 +1553,7 @@ fn check_verification_key_not_present_operation_verification() { let signature = auth_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { @@ -1582,7 +1582,7 @@ fn check_invalid_signature_format_operation_verification() { let signature = invalid_key.sign(call_operation.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { @@ -1609,7 +1609,7 @@ fn check_invalid_signature_operation_verification() { let signature = alternative_key.sign(&call_operation.encode()); let mut ext = ExtBuilder::default() - .with_dids(vec![(did.clone(), mock_did.clone())]) + .with_dids(vec![(did, mock_did)]) .build(None); ext.execute_with(|| { From 64dc85b5fdb785212a627d6513f7a3ad555af138 Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 3 Aug 2021 16:37:56 +0200 Subject: [PATCH 06/21] feat: delegation pallet storage refactor (#226) * feat: initial commit * feat: pallet compiling * wip: fixing test cases * wip: refactoring test cases * test: unit tests passing again * benchmark tests passing * test: attestation unit tests passing * chore: clippy fixes * chore: fmt fixes * bench: re-execute benchmarks for delegation and attestation + temporarily remove benchmarks from peregrine runtime * fix: adjust pre-condition for an attestation unit test * wip: add initial support for delegation storage migrations * delegation storage migrator compiling * wip: storage migrator working, now writing unit tests for it * test: unit tests passing * chore: clippy hints * chore: further clippy fixes * chore: fmt * wip: adjusting last things before possible to review * chore: clippy + fmt * fix: address part of PR review comments * fix: more comments from PR review * wip: change vector of versions to enum * test: unit tests refactor for the new storage migrator version * chore: refactor * test: unit tests passing again * chore: move deprecated type to deprecated.rs * chore: refactor import/export * doc: refine documentation * chore: fmt + clippy * test: add TODO to improve post_migration tests * feat: remove test feature from traits * test: add checks during migration in try-runtime * bench: add spiritnet benchmarks for delegation and attestation * feat: additional logs for count of migrated nodes * chore: fmt * chore: removed useless file * chore: bump runtime version * fix: PR comments * fix: re-introduce root_id in delegation creation hash * chore: capitalize storage version enum * chore: move migrations in their own module * chore: move migrations.rs out of the migrations folder * fix: add check for parent revocation status when creating a delegation * chore: fmt * feat: remove revoke_hierarchy extrinsic * bench: add peregrine benchmarks * bench: re-run delegation and attestation default benchmarks * chore: fmt --- Cargo.lock | 1 + pallets/attestation/src/default_weights.rs | 47 +- pallets/attestation/src/lib.rs | 13 +- pallets/attestation/src/tests.rs | 197 ++- pallets/ctype/src/lib.rs | 4 +- pallets/delegation/Cargo.toml | 1 + pallets/delegation/src/benchmarking.rs | 115 +- pallets/delegation/src/default_weights.rs | 110 +- .../delegation/src/delegation_hierarchy.rs | 129 +- pallets/delegation/src/deprecated.rs | 103 ++ pallets/delegation/src/lib.rs | 416 +++--- pallets/delegation/src/migrations.rs | 223 +++ pallets/delegation/src/migrations/v1.rs | 522 +++++++ pallets/delegation/src/mock.rs | 193 +-- pallets/delegation/src/tests.rs | 1284 ++++++++--------- runtimes/peregrine/src/lib.rs | 2 +- runtimes/peregrine/src/weights/attestation.rs | 40 +- runtimes/peregrine/src/weights/delegation.rs | 76 +- runtimes/spiritnet/src/lib.rs | 2 +- runtimes/standalone/src/lib.rs | 2 +- 20 files changed, 2108 insertions(+), 1372 deletions(-) create mode 100644 pallets/delegation/src/deprecated.rs create mode 100644 pallets/delegation/src/migrations.rs create mode 100644 pallets/delegation/src/migrations/v1.rs diff --git a/Cargo.lock b/Cargo.lock index e8e3039cd8..45d9488d42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1809,6 +1809,7 @@ version = "0.24.0" dependencies = [ "bitflags", "ctype", + "env_logger 0.8.4", "frame-benchmarking", "frame-support", "frame-system", diff --git a/pallets/attestation/src/default_weights.rs b/pallets/attestation/src/default_weights.rs index a8fd991570..271f0e2371 100644 --- a/pallets/attestation/src/default_weights.rs +++ b/pallets/attestation/src/default_weights.rs @@ -19,29 +19,24 @@ //! Autogenerated weights for attestation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-11, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-07-21, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// ./kilt-parachain +// target/release/kilt-parachain // benchmark -// --chain -// dev -// --execution -// wasm -// --wasm-execution -// compiled -// --pallet -// attestation -// --extrinsic -// * -// --steps -// 1 -// --repeat -// 10 +// --chain=dev +// --execution=wasm +// --wasm-execution=Compiled // --heap-pages=4096 -// --output=../../pallets/attestation/src/default_weights.rs -// --template=../../.maintain/weight-template.hbs +// --extrinsic=* +// --pallet=attestation +// --steps=1 +// --repeat=20 +// --template +// .maintain/weight-template.hbs +// --output +// pallets/attestation/src/default_weights.rs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -61,14 +56,14 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn add() -> Weight { - (37_981_000_u64) + (38_091_000_u64) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn revoke(d: u32, ) -> Weight { - (23_806_000_u64) - // Standard Error: 20_000 - .saturating_add((4_670_000_u64).saturating_mul(d as Weight)) + (25_042_000_u64) + // Standard Error: 28_000 + .saturating_add((4_866_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) @@ -78,14 +73,14 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { fn add() -> Weight { - (37_981_000_u64) + (38_091_000_u64) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn revoke(d: u32, ) -> Weight { - (23_806_000_u64) - // Standard Error: 20_000 - .saturating_add((4_670_000_u64).saturating_mul(d as Weight)) + (25_042_000_u64) + // Standard Error: 28_000 + .saturating_add((4_866_000_u64).saturating_mul(d as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(RocksDbWeight::get().writes(1_u64)) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 78ca160f63..5df121d82e 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -224,21 +224,22 @@ pub mod pallet { // Check for validity of the delegation node if specified. if let Some(delegation_id) = delegation_id { - let delegation = >::get(delegation_id) + let delegation = >::get(delegation_id) .ok_or(delegation::Error::::DelegationNotFound)?; - ensure!(!delegation.revoked, Error::::DelegationRevoked); + ensure!(!delegation.details.revoked, Error::::DelegationRevoked); - ensure!(delegation.owner == attester, Error::::NotDelegatedToAttester); + ensure!(delegation.details.owner == attester, Error::::NotDelegatedToAttester); ensure!( - (delegation.permissions & delegation::Permissions::ATTEST) == delegation::Permissions::ATTEST, + (delegation.details.permissions & delegation::Permissions::ATTEST) + == delegation::Permissions::ATTEST, Error::::DelegationUnauthorizedToAttest ); // Check if the CType of the delegation is matching the CType of the attestation - let root = - >::get(delegation.root_id).ok_or(delegation::Error::::RootNotFound)?; + let root = >::get(delegation.hierarchy_root_id) + .ok_or(delegation::Error::::HierarchyNotFound)?; ensure!(root.ctype_hash == ctype_hash, Error::::CTypeMismatch); // If the attestation is based on a delegation, store separately diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 4afe8ef452..c333f203ed 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -61,15 +61,16 @@ fn attest_with_delegation_successful() { let attester_keypair = get_alice_ed25519(); let attester = get_ed25519_account(attester_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(attester.clone()), + + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attester.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attester.clone(), Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; + delegation_node.details.permissions = delegation::Permissions::ATTEST; let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -79,9 +80,8 @@ fn attest_with_delegation_successful() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, attester.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -196,16 +196,16 @@ fn delegation_revoked_attest_error() { let attester_keypair = get_alice_ed25519(); let attester = get_ed25519_account(attester_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(attester.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attester.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attester.clone(), Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; - delegation_node.revoked = true; + delegation_node.details.permissions = delegation::Permissions::ATTEST; + delegation_node.details.revoked = true; let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -215,9 +215,8 @@ fn delegation_revoked_attest_error() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, attester.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -240,15 +239,15 @@ fn not_delegation_owner_attest_error() { let alternative_owner_keypair = get_bob_ed25519(); let alternative_owner = get_ed25519_account(alternative_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(alternative_owner.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, alternative_owner), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, alternative_owner, Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; + delegation_node.details.permissions = delegation::Permissions::ATTEST; let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -258,9 +257,8 @@ fn not_delegation_owner_attest_error() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, attester.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -281,13 +279,14 @@ fn unauthorised_permissions_attest_error() { let attester_keypair = get_alice_ed25519(); let attester = get_ed25519_account(attester_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(attester.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); + // Delegation node does not have permissions to attest. let (delegation_id, delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attester.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attester.clone(), Some(hierarchy_root_id)), ); let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -298,9 +297,8 @@ fn unauthorised_permissions_attest_error() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, attester.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -321,16 +319,20 @@ fn root_not_present_attest_error() { let attester_keypair = get_alice_ed25519(); let attester = get_ed25519_account(attester_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(attester.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); - let alternative_root_id = delegation_mock::get_delegation_root_id(false); + let alternative_hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id(false); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attester.clone()), + delegation_mock::generate_base_delegation_node( + hierarchy_root_id, + attester.clone(), + Some(alternative_hierarchy_root_id), + ), ); - delegation_node.permissions = delegation::Permissions::ATTEST; + delegation_node.details.permissions = delegation::Permissions::ATTEST; let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -340,9 +342,12 @@ fn root_not_present_attest_error() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(alternative_root_id, root_node)]) + .with_delegation_hierarchies(vec![( + alternative_hierarchy_root_id, + hierarchy_details, + attester.clone(), + )]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(alternative_root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -353,7 +358,7 @@ fn root_not_present_attest_error() { operation.ctype_hash, operation.delegation_id ), - delegation::Error::::RootNotFound + delegation::Error::::HierarchyNotFound ); }); } @@ -364,16 +369,16 @@ fn root_ctype_mismatch_attest_error() { let attester = get_ed25519_account(attester_keypair.public()); let claim_hash = get_claim_hash(true); let alternative_ctype_hash = ctype_mock::get_ctype_hash(false); - let (root_id, mut root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(attester.clone()), + let (hierarchy_root_id, mut hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); - root_node.ctype_hash = alternative_ctype_hash; + hierarchy_details.ctype_hash = alternative_ctype_hash; let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attester.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attester.clone(), Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; + delegation_node.details.permissions = delegation::Permissions::ATTEST; let mut attestation = generate_base_attestation(attester.clone()); attestation.delegation_id = Some(delegation_id); @@ -383,9 +388,8 @@ fn root_ctype_mismatch_attest_error() { .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) .build(None); let mut ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, attester.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -442,15 +446,15 @@ fn revoke_with_delegation_successful() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; + delegation_node.details.permissions = delegation::Permissions::ATTEST; // Attestation owned by a different user, but delegation owned by the user // submitting the operation. let mut attestation = generate_base_attestation(attestation_owner); @@ -464,9 +468,8 @@ fn revoke_with_delegation_successful() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) @@ -495,18 +498,18 @@ fn revoke_with_parent_delegation_successful() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (parent_id, mut parent_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - parent_node.permissions = delegation::Permissions::ATTEST; + parent_node.details.permissions = delegation::Permissions::ATTEST; let (delegation_id, delegation_node) = ( delegation_mock::get_delegation_id(false), - delegation_mock::generate_base_delegation_node(root_id, attestation_owner.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attestation_owner.clone(), Some(parent_id)), ); let mut attestation = generate_base_attestation(attestation_owner); attestation.delegation_id = Some(delegation_id); @@ -520,9 +523,8 @@ fn revoke_with_parent_delegation_successful() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![parent_id]), (parent_id, vec![delegation_id])]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) @@ -550,18 +552,18 @@ fn revoke_parent_delegation_no_attestation_permissions_successful() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (parent_id, mut parent_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - parent_node.permissions = delegation::Permissions::DELEGATE; + parent_node.details.permissions = delegation::Permissions::DELEGATE; let (delegation_id, delegation_node) = ( delegation_mock::get_delegation_id(false), - delegation_mock::generate_base_delegation_node(root_id, attestation_owner.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attestation_owner.clone(), Some(parent_id)), ); let mut attestation = generate_base_attestation(attestation_owner); attestation.delegation_id = Some(delegation_id); @@ -575,9 +577,8 @@ fn revoke_parent_delegation_no_attestation_permissions_successful() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![parent_id]), (parent_id, vec![delegation_id])]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) @@ -605,20 +606,20 @@ fn revoke_parent_delegation_with_direct_delegation_revoked_successful() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (parent_id, mut parent_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - parent_node.permissions = delegation::Permissions::ATTEST; + parent_node.details.permissions = delegation::Permissions::ATTEST; let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(false), - delegation_mock::generate_base_delegation_node(root_id, attestation_owner.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attestation_owner.clone(), Some(parent_id)), ); - delegation_node.revoked = true; + delegation_node.details.revoked = true; let mut attestation = generate_base_attestation(attestation_owner); attestation.delegation_id = Some(delegation_id); @@ -631,9 +632,8 @@ fn revoke_parent_delegation_with_direct_delegation_revoked_successful() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![parent_id]), (parent_id, vec![delegation_id])]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) @@ -752,20 +752,19 @@ fn max_parent_lookups_revoke_error() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); let (delegation_id, mut delegation_node) = ( - delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, attestation_owner.clone()), + delegation_mock::get_delegation_id(false), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, attestation_owner.clone(), Some(parent_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; - delegation_node.parent = Some(parent_delegation_id); + delegation_node.details.permissions = delegation::Permissions::ATTEST; let mut attestation = generate_base_attestation(attestation_owner); attestation.delegation_id = Some(delegation_id); @@ -776,15 +775,8 @@ fn max_parent_lookups_revoke_error() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) @@ -810,16 +802,16 @@ fn revoked_delegation_revoke_error() { let attestation_owner = get_ed25519_account(attestation_owner_keypair.public()); let claim_hash = get_claim_hash(true); - let (root_id, root_node) = ( - delegation_mock::get_delegation_root_id(true), - delegation_mock::generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + delegation_mock::get_delegation_hierarchy_id(true), + delegation_mock::generate_base_delegation_hierarchy_details(), ); let (delegation_id, mut delegation_node) = ( delegation_mock::get_delegation_id(true), - delegation_mock::generate_base_delegation_node(root_id, revoker.clone()), + delegation_mock::generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - delegation_node.permissions = delegation::Permissions::ATTEST; - delegation_node.revoked = true; + delegation_node.details.permissions = delegation::Permissions::ATTEST; + delegation_node.details.revoked = true; let mut attestation = generate_base_attestation(attestation_owner); attestation.delegation_id = Some(delegation_id); @@ -829,9 +821,8 @@ fn revoked_delegation_revoke_error() { .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build(None); let ext = delegation_mock::ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); let mut ext = ExtBuilder::default() .with_attestations(vec![(operation.claim_hash, attestation)]) diff --git a/pallets/ctype/src/lib.rs b/pallets/ctype/src/lib.rs index a45c79991b..285579adb0 100644 --- a/pallets/ctype/src/lib.rs +++ b/pallets/ctype/src/lib.rs @@ -115,8 +115,8 @@ pub mod pallet { #[pallet::call] impl Pallet { - /// Create a new CType from the given unique CType hashand associates it - /// with its creator. + /// Create a new CType from the given unique CType hash and associates + /// it with its creator. /// /// A CType with the same hash must not be stored on chain. /// diff --git a/pallets/delegation/Cargo.toml b/pallets/delegation/Cargo.toml index 06a551e0e1..4b2e647ac7 100644 --- a/pallets/delegation/Cargo.toml +++ b/pallets/delegation/Cargo.toml @@ -14,6 +14,7 @@ substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] ctype = {features = ["mock"], path = "../ctype", version = "0.24.0"} +env_logger = {version = "0.8.4"} kilt-primitives = {default-features = false, path = "../../primitives"} serde = {version = "1.0.101"} sp-core = {branch = "polkadot-v0.9.8", default-features = false, git = "https://github.com/paritytech/substrate", version = "3.0.0"} diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index 1836759089..62a00badd6 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -23,7 +23,7 @@ use frame_system::RawOrigin; use sp_core::{offchain::KeyTypeId, sr25519}; use sp_io::crypto::sr25519_generate; use sp_runtime::MultiSignature; -use sp_std::{num::NonZeroU32, vec::Vec}; +use sp_std::{collections::btree_set::BTreeSet, num::NonZeroU32, vec::Vec}; use crate::*; @@ -45,20 +45,10 @@ where hash.into() } -/// sets parent to `None` if it is the root -fn parent_id_check( - root_id: T::DelegationNodeId, - parent_id: T::DelegationNodeId, -) -> Option { - if parent_id == root_id { - None - } else { - Some(parent_id) - } -} - /// add ctype to storage and root delegation -fn add_root_delegation(number: u32) -> Result<(DelegationTriplet, T::Hash), DispatchErrorWithPostInfo> +fn add_delegation_hierarchy( + number: u32, +) -> Result<(DelegationTriplet, T::Hash), DispatchErrorWithPostInfo> where T::AccountId: From, T::DelegationNodeId: From, @@ -66,16 +56,20 @@ where let root_public = sr25519_generate(KeyTypeId(*b"aura"), None); let root_acc: T::AccountId = root_public.into(); let ctype_hash = ::default(); - let root_id = generate_delegation_id::(number); + let hierarchy_root_id = generate_delegation_id::(number); ctype::Pallet::::add(RawOrigin::Signed(root_acc.clone()).into(), ctype_hash)?; - Pallet::::create_root(RawOrigin::Signed(root_acc.clone()).into(), root_id, ctype_hash)?; + Pallet::::create_hierarchy( + RawOrigin::Signed(root_acc.clone()).into(), + hierarchy_root_id, + ctype_hash, + )?; Ok(( DelegationTriplet:: { public: root_public, acc: root_acc, - delegation_id: root_id, + delegation_id: hierarchy_root_id, }, ctype_hash, )) @@ -107,11 +101,10 @@ where let delegation_acc_id: T::AccountId = delegation_acc_public.into(); let delegation_id = generate_delegation_id::(level * children_per_level.get() + c); - // only set parent if not root - let parent = parent_id_check::(root_id, parent_id); - // delegate signs delegation to parent - let hash: Vec = Pallet::::calculate_hash(&delegation_id, &root_id, &parent, &permissions).encode(); + let hash: Vec = + Pallet::::calculate_delegation_creation_hash(&delegation_id, &root_id, &parent_id, &permissions) + .encode(); let sig = sp_io::crypto::sr25519_sign(KeyTypeId(*b"aura"), &delegation_acc_public, hash.as_ref()) .ok_or("Error while building signature of delegation.")?; @@ -119,8 +112,7 @@ where let _ = Pallet::::add_delegation( RawOrigin::Signed(parent_acc_id.clone()).into(), delegation_id, - root_id, - parent, + parent_id, delegation_acc_id.clone().into(), permissions, MultiSignature::from(sig).encode(), @@ -169,58 +161,40 @@ where DelegationTriplet:: { public: root_public, acc: root_acc, - delegation_id: root_id, + delegation_id: hierarchy_id, }, _, - ) = add_root_delegation::(0)?; + ) = add_delegation_hierarchy::(0)?; // iterate levels and start with parent == root let (leaf_acc_public, _, leaf_id) = add_children::( - root_id, - root_id, + hierarchy_id, + hierarchy_id, root_public, root_acc, permissions, levels, children_per_level, )?; - Ok((root_public, root_id, leaf_acc_public, leaf_id)) + Ok((root_public, hierarchy_id, leaf_acc_public, leaf_id)) } benchmarks! { where_clause { where T: core::fmt::Debug, T::AccountId: From + Into, T::DelegationNodeId: From, ::Origin: From::DelegationEntityId>> } - create_root { + create_hierarchy { let caller: T::AccountId = account("caller", 0, SEED); let ctype = ::default(); let delegation = generate_delegation_id::(0); ctype::Pallet::::add(RawOrigin::Signed(caller.clone()).into(), ctype)?; }: _(RawOrigin::Signed(caller), delegation, ctype) verify { - assert!(Roots::::contains_key(delegation)); - } - - revoke_root { - let r in 1 .. T::MaxRevocations::get(); - let (root_acc, root_id, leaf_acc, leaf_id) = setup_delegations::(r, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; - let root_acc_id: T::AccountId = root_acc.into(); - }: _(RawOrigin::Signed(root_acc_id.clone()), root_id, r) - verify { - assert!(Roots::::contains_key(root_id)); - let root_delegation = Roots::::get(root_id).ok_or("Missing root delegation")?; - assert_eq!(root_delegation.owner, root_acc_id.into()); - assert!(root_delegation.revoked); - - assert!(Delegations::::contains_key(leaf_id)); - let leaf_delegation = Delegations::::get(leaf_id).ok_or("Missing leaf delegation")?; - assert_eq!(leaf_delegation.root_id, root_id); - assert_eq!(leaf_delegation.owner, T::AccountId::from(leaf_acc).into()); - assert!(leaf_delegation.revoked); + assert!(DelegationHierarchies::::contains_key(delegation)); } add_delegation { // do setup - let (_, root_id, leaf_acc, leaf_id) = setup_delegations::(1, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let (_, hierarchy_id, leaf_acc, leaf_id) = setup_delegations::(1, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; // add one more delegation let delegate_acc_public = sr25519_generate( @@ -228,17 +202,17 @@ benchmarks! { None ); let delegation_id = generate_delegation_id::(u32::MAX); - let parent_id = parent_id_check::(root_id, leaf_id); + let parent_id = leaf_id; let perm: Permissions = Permissions::ATTEST | Permissions::DELEGATE; - let hash_root = Pallet::::calculate_hash(&delegation_id, &root_id, &parent_id, &perm); + let hash_root = Pallet::::calculate_delegation_creation_hash(&delegation_id, &hierarchy_id, &parent_id, &perm); let sig = sp_io::crypto::sr25519_sign(KeyTypeId(*b"aura"), &delegate_acc_public, hash_root.as_ref()).ok_or("Error while building signature of delegation.")?; let delegate_acc_id: T::AccountId = delegate_acc_public.into(); let leaf_acc_id: T::AccountId = leaf_acc.into(); - }: _(RawOrigin::Signed(leaf_acc_id), delegation_id, root_id, parent_id, delegate_acc_id.into(), perm, MultiSignature::from(sig).encode()) + }: _(RawOrigin::Signed(leaf_acc_id), delegation_id, parent_id, delegate_acc_id.into(), perm, MultiSignature::from(sig).encode()) verify { - assert!(Delegations::::contains_key(delegation_id)); + assert!(DelegationNodes::::contains_key(delegation_id)); } // worst case #1: revoke a child of the root delegation @@ -247,21 +221,22 @@ benchmarks! { revoke_delegation_root_child { let r in 1 .. T::MaxRevocations::get(); let c in 1 .. T::MaxParentChecks::get(); - let (_, root_id, leaf_acc, leaf_id) = setup_delegations::(r, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; - let children: Vec = Children::::get(root_id).unwrap_or_default(); - let child_id: T::DelegationNodeId = *children.get(0).ok_or("Root should have children")?; - let child_delegation = Delegations::::get(child_id).ok_or("Child of root should have delegation id")?; - }: revoke_delegation(RawOrigin::Signed(child_delegation.owner.clone()), child_id, c, r) + let (_, hierarchy_id, leaf_acc, leaf_id) = setup_delegations::(r, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let root_node = DelegationNodes::::get(hierarchy_id).expect("Root hierarchy node should be present on chain."); + let children: BTreeSet = root_node.children; + let child_id: T::DelegationNodeId = *children.iter().next().ok_or("Root should have children")?; + let child_delegation = DelegationNodes::::get(child_id).ok_or("Child of root should have delegation id")?; + }: revoke_delegation(RawOrigin::Signed(child_delegation.details.owner.clone()), child_id, c, r) verify { - assert!(Delegations::::contains_key(child_id)); - let DelegationNode:: { revoked, .. } = Delegations::::get(leaf_id).ok_or("Child of root should have delegation id")?; - assert!(revoked); - - assert!(Delegations::::contains_key(leaf_id)); - let leaf_delegation = Delegations::::get(leaf_id).ok_or("Missing leaf delegation")?; - assert_eq!(leaf_delegation.root_id, root_id); - assert_eq!(leaf_delegation.owner, T::AccountId::from(leaf_acc).into()); - assert!(leaf_delegation.revoked); + assert!(DelegationNodes::::contains_key(child_id)); + let DelegationNode:: { details, .. } = DelegationNodes::::get(leaf_id).ok_or("Child of root should have delegation id")?; + assert!(details.revoked); + + assert!(DelegationNodes::::contains_key(leaf_id)); + let leaf_delegation = DelegationNodes::::get(leaf_id).ok_or("Missing leaf delegation")?; + assert_eq!(leaf_delegation.hierarchy_root_id, hierarchy_id); + assert_eq!(leaf_delegation.details.owner, T::AccountId::from(leaf_acc).into()); + assert!(leaf_delegation.details.revoked); } // TODO: Might want to add variant iterating over children instead of depth at some later point @@ -274,9 +249,9 @@ benchmarks! { let (root_acc, _, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; }: revoke_delegation(RawOrigin::Signed(T::AccountId::from(root_acc).into()), leaf_id, c, r) verify { - assert!(Delegations::::contains_key(leaf_id)); - let DelegationNode:: { revoked, .. } = Delegations::::get(leaf_id).ok_or("Child of root should have delegation id")?; - assert!(revoked); + assert!(DelegationNodes::::contains_key(leaf_id)); + let DelegationNode:: { details, .. } = DelegationNodes::::get(leaf_id).ok_or("Child of root should have delegation id")?; + assert!(details.revoked); } // TODO: Might want to add variant iterating over children instead of depth at some later point } diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index a5f96986d2..6bee212825 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -19,29 +19,24 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-11, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-07-21, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// ./kilt-parachain +// target/release/kilt-parachain // benchmark -// --chain -// dev -// --execution -// wasm -// --wasm-execution -// compiled -// --pallet -// delegation -// --extrinsic -// * -// --steps -// 1 -// --repeat -// 10 +// --chain=dev +// --execution=wasm +// --wasm-execution=Compiled // --heap-pages=4096 -// --output=../../pallets/delegation/src/default_weights.rs -// --template=../../.maintain/weight-template.hbs +// --extrinsic=* +// --pallet=delegation +// --steps=1 +// --repeat=20 +// --template +// .maintain/weight-template.hbs +// --output +// pallets/delegation/src/default_weights.rs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -53,8 +48,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for delegation. pub trait WeightInfo { - fn create_root() -> Weight; - fn revoke_root(r: u32, ) -> Weight; + fn create_hierarchy() -> Weight; fn add_delegation() -> Weight; fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight; fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight; @@ -63,38 +57,30 @@ pub trait WeightInfo { /// Weights for delegation using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_root() -> Weight { - (22_442_000_u64) + fn create_hierarchy() -> Weight { + (24_797_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn revoke_root(r: u32, ) -> Weight { - (24_418_000_u64) - // Standard Error: 29_000 - .saturating_add((17_591_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(1_u64)) - .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn add_delegation() -> Weight { - (83_497_000_u64) - .saturating_add(T::DbWeight::get().reads(4_u64)) + (80_841_000_u64) + .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn revoke_delegation_root_child(r: u32, _c: u32, ) -> Weight { - (11_210_000_u64) - // Standard Error: 55_000 - .saturating_add((17_485_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + (17_804_000_u64) + // Standard Error: 94_000 + .saturating_add((17_445_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(r as Weight))) } fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (27_413_000_u64) - // Standard Error: 30_000 - .saturating_add((111_000_u64).saturating_mul(r as Weight)) - // Standard Error: 30_000 - .saturating_add((4_692_000_u64).saturating_mul(c as Weight)) + (31_213_000_u64) + // Standard Error: 61_000 + .saturating_add((327_000_u64).saturating_mul(r as Weight)) + // Standard Error: 61_000 + .saturating_add((5_210_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) @@ -103,38 +89,30 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn create_root() -> Weight { - (22_442_000_u64) + fn create_hierarchy() -> Weight { + (24_797_000_u64) .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - fn revoke_root(r: u32, ) -> Weight { - (24_418_000_u64) - // Standard Error: 29_000 - .saturating_add((17_591_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn add_delegation() -> Weight { - (83_497_000_u64) - .saturating_add(RocksDbWeight::get().reads(4_u64)) + (80_841_000_u64) + .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn revoke_delegation_root_child(r: u32, _c: u32, ) -> Weight { - (11_210_000_u64) - // Standard Error: 55_000 - .saturating_add((17_485_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + (17_804_000_u64) + // Standard Error: 94_000 + .saturating_add((17_445_000_u64).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(r as Weight))) } fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (27_413_000_u64) - // Standard Error: 30_000 - .saturating_add((111_000_u64).saturating_mul(r as Weight)) - // Standard Error: 30_000 - .saturating_add((4_692_000_u64).saturating_mul(c as Weight)) + (31_213_000_u64) + // Standard Error: 61_000 + .saturating_add((327_000_u64).saturating_mul(r as Weight)) + // Standard Error: 61_000 + .saturating_add((5_210_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c as Weight))) .saturating_add(RocksDbWeight::get().writes(1_u64)) diff --git a/pallets/delegation/src/delegation_hierarchy.rs b/pallets/delegation/src/delegation_hierarchy.rs index b0eee43d48..1f55ed5833 100644 --- a/pallets/delegation/src/delegation_hierarchy.rs +++ b/pallets/delegation/src/delegation_hierarchy.rs @@ -18,6 +18,8 @@ use bitflags::bitflags; use codec::{Decode, Encode}; +use ctype::CtypeHashOf; +use sp_std::collections::btree_set::BTreeSet; use crate::*; @@ -52,87 +54,88 @@ impl Default for Permissions { } } -/// A node representing a delegation hierarchy root. +/// A node in a delegation hierarchy. +/// +/// For quicker lookups of the hierarchy details, all nodes maintain a direct +/// link to the hierarchy root node. Furthermore, all nodes have a parent except +/// the root nodes, which point to themselves for the hierarchy root node link. #[derive(Clone, Debug, Encode, Decode, PartialEq)] -pub struct DelegationRoot { - /// The hash of the CType that delegated attesters within this trust - /// hierarchy can attest. - pub ctype_hash: CtypeHashOf, - /// The identifier of the root owner. - pub owner: DelegatorIdOf, - /// The flag indicating whether the root has been revoked or not. - pub revoked: bool, +pub struct DelegationNode { + /// The ID of the delegation hierarchy the node is part of. + pub hierarchy_root_id: DelegationNodeIdOf, + /// The ID of the parent. For all but root nodes this is not None. + pub parent: Option>, + /// The set of IDs of all the children nodes. + pub children: BTreeSet>, + /// The additional information attached to the delegation node. + pub details: DelegationDetails, } -impl DelegationRoot { - pub fn new(ctype_hash: CtypeHashOf, owner: DelegatorIdOf) -> Self { - DelegationRoot { - ctype_hash, - owner, - revoked: false, +impl DelegationNode { + /// Creates a new delegation root node with the given ID and delegation + /// details. + pub fn new_root_node(id: DelegationNodeIdOf, details: DelegationDetails) -> Self { + Self { + hierarchy_root_id: id, + parent: None, + children: BTreeSet::new(), + details, } } + + /// Creates a new delegation node under the given hierarchy ID, with the + /// given parent and delegation details. + pub fn new_node( + hierarchy_root_id: DelegationNodeIdOf, + parent: DelegationNodeIdOf, + details: DelegationDetails, + ) -> Self { + Self { + hierarchy_root_id, + parent: Some(parent), + children: BTreeSet::new(), + details, + } + } + + /// Adds a node by its ID to the current node's children. + pub fn add_child(&mut self, child_id: DelegationNodeIdOf) { + self.children.insert(child_id); + } } -/// A node representing a node in the delegation hierarchy. +/// Delegation information attached to delegation nodes. #[derive(Clone, Debug, Encode, Decode, PartialEq)] -pub struct DelegationNode { - /// The ID of the delegation hierarchy root. - pub root_id: DelegationNodeIdOf, - /// \[OPTIONAL\] The ID of the parent node. If None, the node is - /// considered a direct child of the root node. - pub parent: Option>, - /// The identifier of the owner of the delegation node, i.e., the delegate. +pub struct DelegationDetails { + /// The owner of the delegation (and its node). pub owner: DelegatorIdOf, - /// The permission flags for the operations the delegate is allowed to - /// perform. - pub permissions: Permissions, - /// The flag indicating whether the delegation has been revoked or not. + /// Status indicating whether the delegation has been revoked (true) or not + /// (false). pub revoked: bool, + /// The set of permissions associated with the delegation. + pub permissions: Permissions, } -impl DelegationNode { - /// Create a new delegation node that is a direct descendent of the - /// given root. +impl DelegationDetails { + /// Creates new delegation details including the given owner. /// - /// * root_id: the root node ID this node will be a child of - /// * owner: the identifier of the owner of the new delegation, i.e., the - /// new delegate - /// * permissions: the permission flags for the operations the delegate is - /// allowed to perform - pub fn new_root_child(root_id: DelegationNodeIdOf, owner: DelegatorIdOf, permissions: Permissions) -> Self { - DelegationNode { - root_id, + /// The default revocation status is false and all permissions are granted + /// by default. + pub fn default_with_owner(owner: DelegatorIdOf) -> Self { + Self { owner, - permissions, + permissions: Permissions::all(), revoked: false, - parent: None, } } +} - /// Creates a new delegation node that is a direct descendent of the - /// given node. - /// - /// * root_id: the root node ID this node will be a child of - /// * parent - the parent node ID this node will be a child of - /// * owner: the identifier of the owner of the new delegation, i.e., the - /// new delegate - /// * permissions: the permission flags for the operations the delegate is - /// allowed to perform - pub fn new_node_child( - root_id: DelegationNodeIdOf, - parent: DelegationNodeIdOf, - owner: DelegatorIdOf, - permissions: Permissions, - ) -> Self { - DelegationNode { - root_id, - parent: Some(parent), - owner, - permissions, - revoked: false, - } - } +/// The details associated with a delegation hierarchy. +#[derive(Clone, Debug, Encode, Decode, Eq, PartialEq, Ord, PartialOrd)] +pub struct DelegationHierarchyDetails { + /// The authorised CTYPE hash that attesters can attest using this + /// delegation hierarchy. + pub ctype_hash: CtypeHashOf, } /// The result that the delegation pallet expects from the implementer of the diff --git a/pallets/delegation/src/deprecated.rs b/pallets/delegation/src/deprecated.rs new file mode 100644 index 0000000000..0f6ea788b3 --- /dev/null +++ b/pallets/delegation/src/deprecated.rs @@ -0,0 +1,103 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2021 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +/// Deprecated types used in version 1. +pub(crate) mod v1 { + use codec::{Decode, Encode}; + + use crate::*; + + #[derive(Clone, Debug, Encode, Decode, PartialEq)] + pub struct DelegationRoot { + pub(crate) ctype_hash: CtypeHashOf, + pub(crate) owner: DelegatorIdOf, + pub(crate) revoked: bool, + } + + impl DelegationRoot { + #[cfg(test)] + pub(crate) fn new(ctype_hash: CtypeHashOf, owner: DelegatorIdOf) -> Self { + DelegationRoot { + ctype_hash, + owner, + revoked: false, + } + } + } + + #[derive(Clone, Debug, Encode, Decode, PartialEq)] + pub struct DelegationNode { + pub(crate) root_id: DelegationNodeIdOf, + pub(crate) parent: Option>, + pub(crate) owner: DelegatorIdOf, + pub(crate) permissions: Permissions, + pub(crate) revoked: bool, + } + + impl DelegationNode { + #[cfg(test)] + pub(crate) fn new_root_child( + root_id: DelegationNodeIdOf, + owner: DelegatorIdOf, + permissions: Permissions, + ) -> Self { + DelegationNode { + root_id, + owner, + permissions, + revoked: false, + parent: None, + } + } + + #[cfg(test)] + pub(crate) fn new_node_child( + root_id: DelegationNodeIdOf, + parent: DelegationNodeIdOf, + owner: DelegatorIdOf, + permissions: Permissions, + ) -> Self { + DelegationNode { + root_id, + parent: Some(parent), + owner, + permissions, + revoked: false, + } + } + } + + pub(crate) mod storage { + use frame_support::{decl_module, decl_storage}; + use sp_std::prelude::*; + + use super::*; + + decl_module! { + pub struct OldPallet for enum Call where origin: T::Origin {} + } + + decl_storage! { + trait Store for OldPallet as Delegation { + pub(crate) Roots get(fn roots): map hasher(blake2_128_concat) DelegationNodeIdOf => Option>; + pub(crate) Delegations get(fn delegations): map hasher(blake2_128_concat) DelegationNodeIdOf => Option>; + pub(crate) Children get(fn children): map hasher(blake2_128_concat) DelegationNodeIdOf => Option>>; + } + } + } +} diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index 54bef58ccc..ce3229ac82 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -75,6 +75,7 @@ pub mod default_weights; pub mod delegation_hierarchy; +pub mod migrations; #[cfg(any(feature = "mock", test))] pub mod mock; @@ -85,14 +86,19 @@ pub mod benchmarking; #[cfg(test)] mod tests; +mod deprecated; + pub use crate::{default_weights::WeightInfo, delegation_hierarchy::*, pallet::*}; use frame_support::{ensure, pallet_prelude::Weight, traits::Get}; use sp_runtime::{traits::Hash, DispatchError}; use sp_std::vec::Vec; +use migrations::*; + #[frame_support::pallet] pub mod pallet { + use super::*; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -121,7 +127,7 @@ pub mod pallet { Signature = Vec, >; type DelegationEntityId: Parameter; - type DelegationNodeId: Parameter + Copy + AsRef<[u8]>; + type DelegationNodeId: Parameter + Copy + AsRef<[u8]> + Eq + PartialEq + Ord + PartialOrd; type EnsureOrigin: EnsureOrigin, ::Origin>; type Event: From> + IsType<::Event>; #[pallet::constant] @@ -138,39 +144,51 @@ pub mod pallet { pub struct Pallet(_); #[pallet::hooks] - impl Hooks> for Pallet {} + impl Hooks> for Pallet { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + migrations::DelegationStorageMigrator::::pre_migrate() + } - /// Delegation root nodes stored on chain. - /// - /// It maps from a root node ID to the full root node. + fn on_runtime_upgrade() -> Weight { + migrations::DelegationStorageMigrator::::migrate() + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + migrations::DelegationStorageMigrator::::post_migrate() + } + } + + /// Contains the latest storage version deployed. #[pallet::storage] - #[pallet::getter(fn roots)] - pub type Roots = StorageMap<_, Blake2_128Concat, DelegationNodeIdOf, DelegationRoot>; + #[pallet::getter(fn last_version_migration_used)] + pub(crate) type StorageVersion = StorageValue<_, DelegationStorageVersion, ValueQuery>; /// Delegation nodes stored on chain. /// - /// It maps from a node ID to the full delegation node. + /// It maps from a node ID to the node details. #[pallet::storage] - #[pallet::getter(fn delegations)] - pub type Delegations = StorageMap<_, Blake2_128Concat, DelegationNodeIdOf, DelegationNode>; + #[pallet::getter(fn delegation_nodes)] + pub type DelegationNodes = StorageMap<_, Blake2_128Concat, DelegationNodeIdOf, DelegationNode>; - /// Children delegation nodes. + /// Delegation hierarchies stored on chain. /// - /// It maps from a delegation node ID, including the root node, to the list - /// of children nodes, sorted by time of creation. + /// It maps for a (root) node ID to the hierarchy details. #[pallet::storage] - #[pallet::getter(fn children)] - pub type Children = StorageMap<_, Blake2_128Concat, DelegationNodeIdOf, Vec>>; + #[pallet::getter(fn delegation_hierarchies)] + pub type DelegationHierarchies = + StorageMap<_, Blake2_128Concat, DelegationNodeIdOf, DelegationHierarchyDetails>; #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - /// A new root has been created. - /// \[creator ID, root node ID, CType hash\] - RootCreated(DelegatorIdOf, DelegationNodeIdOf, CtypeHashOf), - /// A root has been revoked. + /// A new hierarchy has been created. + /// \[creator ID, root node ID, CTYPE hash\] + HierarchyCreated(DelegatorIdOf, DelegationNodeIdOf, CtypeHashOf), + /// A hierarchy has been revoked. /// \[revoker ID, root node ID\] - RootRevoked(DelegatorIdOf, DelegationNodeIdOf), + HierarchyRevoked(DelegatorIdOf, DelegationNodeIdOf), /// A new delegation has been created. /// \[creator ID, root node ID, delegation node ID, parent node ID, /// delegate ID, permissions\] @@ -178,7 +196,7 @@ pub mod pallet { DelegatorIdOf, DelegationNodeIdOf, DelegationNodeIdOf, - Option>, + DelegationNodeIdOf, DelegatorIdOf, Permissions, ), @@ -198,19 +216,21 @@ pub mod pallet { DelegationNotFound, /// No delegate with the given ID stored on chain. DelegateNotFound, - /// There is already a root node with the same ID stored on chain. - RootAlreadyExists, - /// No root delegation with the given ID stored on chain. - RootNotFound, + /// There is already a hierarchy with the same ID stored on chain. + HierarchyAlreadyExists, + /// No hierarchy with the given ID stored on chain. + HierarchyNotFound, /// Max number of nodes checked without verifying the given condition. MaxSearchDepthReached, /// Max number of nodes checked without verifying the given condition. NotOwnerOfParentDelegation, /// The delegation creator is not allowed to write the delegation /// because he is not the owner of the delegation root node. - NotOwnerOfRootDelegation, + NotOwnerOfDelegationHierarchy, /// No parent delegation with the given ID stored on chain. ParentDelegationNotFound, + /// The parent delegation has previously been revoked. + ParentDelegationRevoked, /// The delegation revoker is not allowed to revoke the delegation. UnauthorizedRevocation, /// The delegation creator is not allowed to create the delegation. @@ -246,15 +266,18 @@ pub mod pallet { /// - Reads: [Origin Account], Roots, CTypes /// - Writes: Roots /// # - #[pallet::weight(::WeightInfo::create_root())] - pub fn create_root( + #[pallet::weight(::WeightInfo::create_hierarchy())] + pub fn create_hierarchy( origin: OriginFor, - root_id: DelegationNodeIdOf, + root_node_id: DelegationNodeIdOf, ctype_hash: CtypeHashOf, ) -> DispatchResult { let creator = ::EnsureOrigin::ensure_origin(origin)?; - ensure!(!>::contains_key(&root_id), Error::::RootAlreadyExists); + ensure!( + !>::contains_key(&root_node_id), + Error::::HierarchyAlreadyExists + ); ensure!( >::contains_key(&ctype_hash), @@ -262,9 +285,14 @@ pub mod pallet { ); log::debug!("insert Delegation Root"); - >::insert(&root_id, DelegationRoot::new(ctype_hash, creator.clone())); - Self::deposit_event(Event::RootCreated(creator, root_id, ctype_hash)); + Self::create_and_store_new_hierarchy( + root_node_id, + DelegationHierarchyDetails:: { ctype_hash }, + creator.clone(), + ); + + Self::deposit_event(Event::HierarchyCreated(creator, root_node_id, ctype_hash)); Ok(()) } @@ -298,16 +326,24 @@ pub mod pallet { pub fn add_delegation( origin: OriginFor, delegation_id: DelegationNodeIdOf, - root_id: DelegationNodeIdOf, - parent_id: Option>, + parent_id: DelegationNodeIdOf, delegate: DelegatorIdOf, permissions: Permissions, delegate_signature: DelegateSignatureTypeOf, ) -> DispatchResult { let delegator = ::EnsureOrigin::ensure_origin(origin)?; + ensure!( + !>::contains_key(&delegation_id), + Error::::DelegationAlreadyExists + ); + + let parent_node = >::get(&parent_id).ok_or(Error::::ParentDelegationNotFound)?; + let hierarchy_root_id = parent_node.hierarchy_root_id; + // Calculate the hash root - let hash_root = Self::calculate_hash(&delegation_id, &root_id, &parent_id, &permissions); + let hash_root = + Self::calculate_delegation_creation_hash(&delegation_id, &hierarchy_root_id, &parent_id, &permissions); // Verify that the hash root signature is correct. DelegationSignatureVerificationOf::::verify(&delegate, &hash_root.encode(), &delegate_signature) @@ -316,57 +352,38 @@ pub mod pallet { SignatureVerificationError::SignatureInvalid => Error::::InvalidDelegateSignature, })?; + // Check if the parent's delegate is the creator of this delegation node... ensure!( - !>::contains_key(&delegation_id), - Error::::DelegationAlreadyExists + parent_node.details.owner == delegator, + Error::::NotOwnerOfParentDelegation + ); + // ... and that the node has not been revoked... + ensure!(!parent_node.details.revoked, Error::::ParentDelegationRevoked); + // ... and that has permission to delegate + ensure!( + (parent_node.details.permissions & Permissions::DELEGATE) == Permissions::DELEGATE, + Error::::UnauthorizedDelegation ); - let root = >::get(&root_id).ok_or(Error::::RootNotFound)?; - - // Computes the delegation parent. Either the given parent (if allowed) or the - // root node. - let parent = if let Some(parent_id) = parent_id { - let parent_node = >::get(&parent_id).ok_or(Error::::ParentDelegationNotFound)?; - - // Check if the parent's delegate is the creator of this delegation node... - ensure!(parent_node.owner == delegator, Error::::NotOwnerOfParentDelegation); - // ... and has permission to delegate - ensure!( - (parent_node.permissions & Permissions::DELEGATE) == Permissions::DELEGATE, - Error::::UnauthorizedDelegation - ); - - log::debug!("insert Delegation with parent"); - >::insert( - &delegation_id, - DelegationNode::::new_node_child(root_id, parent_id, delegate.clone(), permissions), - ); - - // Return parent_id as the result of this if branch - parent_id - } else { - // Check if the creator of this delegation node is the creator of the root node - // (as no parent is given) - ensure!(root.owner == delegator, Error::::NotOwnerOfRootDelegation); - - log::debug!("insert Delegation without parent"); - >::insert( - &delegation_id, - DelegationNode::::new_root_child(root_id, delegate.clone(), permissions), - ); - - // Return node_id as the result of this if branch - root_id - }; - - // Regardless of the node returned as parent, add the new node as a child of - // that node - Self::add_child(delegation_id, parent); + Self::store_delegation_under_parent( + delegation_id, + DelegationNode::new_node( + hierarchy_root_id, + parent_id, + DelegationDetails { + owner: delegate.clone(), + permissions, + revoked: false, + }, + ), + parent_id, + parent_node, + ); Self::deposit_event(Event::DelegationCreated( delegator, + hierarchy_root_id, delegation_id, - root_id, parent_id, delegate, permissions, @@ -375,62 +392,8 @@ pub mod pallet { Ok(()) } - /// Revoke a delegation root and the whole delegation hierarchy below. - /// - /// Revoking a delegation root results in the whole trust hierarchy - /// being revoked. Nevertheless, revocation starts from the leave nodes - /// upwards, so if the operation ends prematurely because it runs out of - /// gas, the delegation state would be consistent as no child would - /// "survive" its parent. As a consequence, if the root node is revoked, - /// the whole trust hierarchy is to be considered revoked. - /// - /// The dispatch origin must be `DelegationEntityId`. - /// - /// Emits `RootRevoked` and C * `DelegationRevoked`. - /// - /// # - /// Weight: O(C) where C is the number of children of the root which is - /// bounded by `max_children`. - /// - Reads: [Origin Account], Roots, C * Delegations, C * Children. - /// - Writes: Roots, C * Delegations - /// # - #[pallet::weight(::WeightInfo::revoke_root(*max_children))] - pub fn revoke_root( - origin: OriginFor, - root_id: DelegationNodeIdOf, - max_children: u32, - ) -> DispatchResultWithPostInfo { - let invoker = ::EnsureOrigin::ensure_origin(origin)?; - - let mut root = >::get(&root_id).ok_or(Error::::RootNotFound)?; - - ensure!(root.owner == invoker, Error::::UnauthorizedRevocation); - - ensure!( - max_children <= T::MaxRevocations::get(), - Error::::MaxRevocationsTooLarge - ); - - let consumed_weight: Weight = if !root.revoked { - // Recursively revoke all children - let (_, post_weight) = Self::revoke_children(&root_id, &invoker, max_children)?; - - // If we didn't return an ExceededRevocationBounds error, we can revoke the root - // too. - root.revoked = true; - >::insert(&root_id, root); - - post_weight.saturating_add(T::DbWeight::get().writes(1)) - } else { - 0 - }; - - Self::deposit_event(Event::RootRevoked(invoker, root_id)); - - Ok(Some(consumed_weight.saturating_add(T::DbWeight::get().reads(1))).into()) - } - - /// Revoke a delegation node and all its children. + /// Revoke a delegation node (potentially a root node) and all its + /// children. /// /// Revoking a delegation node results in the trust hierarchy starting /// from the given node being revoked. Nevertheless, revocation starts @@ -462,7 +425,7 @@ pub mod pallet { let invoker = ::EnsureOrigin::ensure_origin(origin)?; ensure!( - >::contains_key(&delegation_id), + >::contains_key(&delegation_id), Error::::DelegationNotFound ); @@ -471,16 +434,22 @@ pub mod pallet { Error::::MaxParentChecksTooLarge ); - let (authorized, parent_checks) = Self::is_delegating(&invoker, &delegation_id, max_parent_checks)?; - ensure!(authorized, Error::::UnauthorizedRevocation); - ensure!( max_revocations <= T::MaxRevocations::get(), Error::::MaxRevocationsTooLarge ); - // Revoke the delegation and recursively all of its children - let (revocation_checks, _) = Self::revoke(&delegation_id, &invoker, max_revocations)?; + let (authorized, parent_checks) = Self::is_delegating(&invoker, &delegation_id, max_parent_checks)?; + ensure!(authorized, Error::::UnauthorizedRevocation); + + // Revoke the delegation and recursively all of its children (add 1 to + // max_revocations to account for the node itself) + let (revocation_checks, _) = Self::revoke(&delegation_id, &invoker, max_revocations.saturating_add(1))?; + + // If the revoked node is a root node, emit also a HierarchyRevoked event. + if DelegationHierarchies::::contains_key(&delegation_id) { + Self::deposit_event(Event::HierarchyRevoked(invoker, delegation_id)); + } // Add worst case reads from `is_delegating` Ok(Some( @@ -494,28 +463,55 @@ pub mod pallet { } impl Pallet { - /// Calculate the hash of all values of a delegation transaction. + /// Calculate the hash of all values of a delegation creation transaction. /// /// # /// Weight: O(1) /// # - fn calculate_hash( + fn calculate_delegation_creation_hash( delegation_id: &DelegationNodeIdOf, root_id: &DelegationNodeIdOf, - parent_id: &Option>, + parent_id: &DelegationNodeIdOf, permissions: &Permissions, ) -> T::Hash { - // Add all values to an u8 vector + // Add all values to an u8 vector. let mut hashed_values: Vec = delegation_id.as_ref().to_vec(); hashed_values.extend_from_slice(root_id.as_ref()); - if let Some(parent) = parent_id { - hashed_values.extend_from_slice(parent.as_ref()) - } + hashed_values.extend_from_slice(parent_id.as_ref()); hashed_values.extend_from_slice(permissions.as_u8().as_ref()); // Hash the resulting vector T::Hashing::hash(&hashed_values) } + // Creates a new root node with the given details and store the new hierarchy in + // the hierarchies storage and the new root node in the nodes storage. + fn create_and_store_new_hierarchy( + root_id: DelegationNodeIdOf, + hierarchy_details: DelegationHierarchyDetails, + hierarchy_owner: DelegatorIdOf, + ) { + let root_node = DelegationNode::new_root_node(root_id, DelegationDetails::default_with_owner(hierarchy_owner)); + >::insert(root_id, root_node); + >::insert(root_id, hierarchy_details); + } + + // Adds the given node to the storage and updates the parent node to include the + // given node as child. + // + // This function assumes that the parent node is already stored on the chain. If + // not, the behaviour of the system is undefined. + fn store_delegation_under_parent( + delegation_id: DelegationNodeIdOf, + delegation_node: DelegationNode, + parent_id: DelegationNodeIdOf, + mut parent_node: DelegationNode, + ) { + >::insert(delegation_id, delegation_node); + // Add the new node as a child of that node + parent_node.add_child(delegation_id); + >::insert(parent_id, parent_node); + } + /// Check if an identity is the owner of the given delegation node or any /// node up the hierarchy, and if the delegation has not been yet revoked. /// @@ -535,34 +531,60 @@ impl Pallet { delegation: &DelegationNodeIdOf, max_parent_checks: u32, ) -> Result<(bool, u32), DispatchError> { - let delegation_node = >::get(delegation).ok_or(Error::::DelegationNotFound)?; + let delegation_node = >::get(delegation).ok_or(Error::::DelegationNotFound)?; // Check if the given account is the owner of the delegation and that the // delegation has not been removed - if &delegation_node.owner == identity { - Ok((!delegation_node.revoked, 0u32)) - } else { - // Counter is decreased regardless of whether we are checking the parent node - // next of the root node, as the root node is as a matter of fact the top node's - // parent. + if &delegation_node.details.owner == identity { + Ok((!delegation_node.details.revoked, 0u32)) + } else if let Some(parent) = delegation_node.parent { + // Only decrease (and perhaps fail) remaining_lookups if there are more parents + // to visit let remaining_lookups = max_parent_checks .checked_sub(1) .ok_or(Error::::MaxSearchDepthReached)?; - if let Some(parent) = delegation_node.parent { - // Recursively check upwards in hierarchy - Self::is_delegating(identity, &parent, remaining_lookups) - } else { - // Return whether the given account is the owner of the root and the root has - // not been revoked - let root = >::get(delegation_node.root_id).ok_or(Error::::RootNotFound)?; - Ok(( - (&root.owner == identity) && !root.revoked, - // safe because remaining lookups is at most max_parent_checks - max_parent_checks - remaining_lookups, - )) + // Recursively check upwards in hierarchy + Self::is_delegating(identity, &parent, remaining_lookups) + } else { + // Return false and return max_parent_checks as no other check is performed + Ok((false, max_parent_checks)) + } + } + + /// Revokes all children of a delegation. + /// Returns the number of revoked delegations and the consumed weight. + /// + /// # + /// Weight: O(C) where C is the number of children of the delegation node + /// which is bounded by `max_children`. + /// - Reads: C * Children, C * Delegations + /// - Writes: C * Delegations + /// # + fn revoke_children( + delegation: &DelegationNodeIdOf, + sender: &DelegatorIdOf, + max_revocations: u32, + ) -> Result<(u32, Weight), DispatchError> { + let mut revocations: u32 = 0; + let mut consumed_weight: Weight = 0; + if let Some(delegation_node) = >::get(delegation) { + // Iterate children and revoke all nodes + for child in delegation_node.children.iter() { + let remaining_revocations = max_revocations + .checked_sub(revocations) + .ok_or(Error::::ExceededRevocationBounds)?; + + // Check whether we ran out of gas + ensure!(remaining_revocations > 0, Error::::ExceededRevocationBounds); + + Self::revoke(child, sender, remaining_revocations).map(|(r, w)| { + revocations = revocations.saturating_add(r); + consumed_weight = consumed_weight.saturating_add(w); + })?; } } + Ok((revocations, consumed_weight.saturating_add(T::DbWeight::get().reads(1)))) } /// Revoke a delegation and all of its children recursively. @@ -583,11 +605,11 @@ impl Pallet { let mut revocations: u32 = 0; let mut consumed_weight: Weight = 0; // Retrieve delegation node from storage - let mut delegation_node = >::get(*delegation).ok_or(Error::::DelegationNotFound)?; + let mut delegation_node = >::get(*delegation).ok_or(Error::::DelegationNotFound)?; consumed_weight = consumed_weight.saturating_add(T::DbWeight::get().reads(1)); // Check if already revoked - if !delegation_node.revoked { + if !delegation_node.details.revoked { // First revoke all children recursively let remaining_revocations = max_revocations .checked_sub(1) @@ -602,8 +624,8 @@ impl Pallet { ensure!(revocations < max_revocations, Error::::ExceededRevocationBounds); // Set revoked flag and store delegation node - delegation_node.revoked = true; - >::insert(*delegation, delegation_node); + delegation_node.details.revoked = true; + >::insert(*delegation, delegation_node); consumed_weight = consumed_weight.saturating_add(T::DbWeight::get().writes(1)); // Deposit event that the delegation has been revoked Self::deposit_event(Event::DelegationRevoked(sender.clone(), *delegation)); @@ -611,56 +633,4 @@ impl Pallet { } Ok((revocations, consumed_weight)) } - - /// Revokes all children of a delegation. - /// Returns the number of revoked delegations and the consumed weight. - /// - /// # - /// Weight: O(C) where C is the number of children of the delegation node - /// which is bounded by `max_children`. - /// - Reads: C * Children, C * Delegations - /// - Writes: C * Delegations - /// # - fn revoke_children( - delegation: &DelegationNodeIdOf, - sender: &DelegatorIdOf, - max_revocations: u32, - ) -> Result<(u32, Weight), DispatchError> { - let mut revocations: u32 = 0; - let mut consumed_weight: Weight = 0; - // Check if there's a child vector in the storage - if let Some(children) = >::get(delegation) { - consumed_weight = consumed_weight.saturating_add(T::DbWeight::get().reads(1)); - - // Iterate child vector and revoke all nodes - for child in children { - let remaining_revocations = max_revocations - .checked_sub(revocations) - .ok_or(Error::::ExceededRevocationBounds)?; - - // Check whether we ran out of gas - ensure!(remaining_revocations > 0, Error::::ExceededRevocationBounds); - - Self::revoke(&child, sender, remaining_revocations).map(|(r, w)| { - revocations = revocations.saturating_add(r); - consumed_weight = consumed_weight.saturating_add(w); - })?; - } - } - Ok((revocations, consumed_weight.saturating_add(T::DbWeight::get().reads(1)))) - } - - /// Add a child node into the delegation hierarchy. - /// - /// # - /// Weight: O(1) - /// - Reads: Children - /// - Writes: Children - /// # - fn add_child(child: DelegationNodeIdOf, parent: DelegationNodeIdOf) { - // Get the children vector or initialize an empty one if none - let mut children = >::get(parent).unwrap_or_default(); - children.push(child); - >::insert(parent, children); - } } diff --git a/pallets/delegation/src/migrations.rs b/pallets/delegation/src/migrations.rs new file mode 100644 index 0000000000..b4a6b447e9 --- /dev/null +++ b/pallets/delegation/src/migrations.rs @@ -0,0 +1,223 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2021 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +use codec::{Decode, Encode}; +use sp_std::marker::PhantomData; + +use crate::*; + +mod v1; + +/// A trait that allows version migrators to access the underlying pallet's +/// context, e.g., its Config trait. +/// +/// In this way, the migrator can access the pallet's storage and the pallet's +/// types directly. +pub trait VersionMigratorTrait { + #[cfg(feature = "try-runtime")] + fn pre_migrate(&self) -> Result<(), &str>; + fn migrate(&self) -> Weight; + #[cfg(feature = "try-runtime")] + fn post_migrate(&self) -> Result<(), &str>; +} + +/// Storage version of the delegation pallet. +#[derive(Copy, Clone, Encode, Eq, Decode, Ord, PartialEq, PartialOrd)] +pub enum DelegationStorageVersion { + V1, + V2, +} + +#[cfg(feature = "try-runtime")] +impl DelegationStorageVersion { + /// The latest storage version. + fn latest() -> Self { + Self::V2 + } +} + +// All nodes will default to this, which is not bad, as in case the "real" +// version is a later one (i.e. the node has been started with already the +// latest version), the migration will simply do nothing as there's nothing in +// the old storage entries to migrate from. +// +// It might get updated in the future when we know that no node is running this +// old version anymore. +impl Default for DelegationStorageVersion { + fn default() -> Self { + Self::V1 + } +} + +impl VersionMigratorTrait for DelegationStorageVersion { + // It runs the right pre_migrate logic depending on the current storage version. + #[cfg(feature = "try-runtime")] + fn pre_migrate(&self) -> Result<(), &str> { + match *self { + Self::V1 => v1::pre_migrate::(), + Self::V2 => Err("Already latest v2 version."), + } + } + + // It runs the righ migration logic depending on the current storage version. + fn migrate(&self) -> Weight { + match *self { + Self::V1 => v1::migrate::(), + Self::V2 => 0u64, + } + } + + // It runs the right post_migrate logic depending on the current storage + // version. + #[cfg(feature = "try-runtime")] + fn post_migrate(&self) -> Result<(), &str> { + match *self { + Self::V1 => v1::post_migrate::(), + Self::V2 => Err("Migration from v2 should have never happened in the first place."), + } + } +} + +/// The delegation pallet's storage migrator, which handles all version +/// migrations in a sequential fashion. +/// +/// If a node has missed on more than one upgrade, the migrator will apply the +/// needed migrations one after the other. Otherwise, if no migration is needed, +/// the migrator will simply not do anything. +pub struct DelegationStorageMigrator(PhantomData); + +impl DelegationStorageMigrator { + // Contains the migration sequence logic. + fn get_next_storage_version(current: DelegationStorageVersion) -> Option { + // If the version current deployed is at least v1, there is no more migrations + // to run (other than the one from v1). + match current { + DelegationStorageVersion::V1 => None, + DelegationStorageVersion::V2 => None, + } + } + + /// Checks whether the latest storage version deployed is lower than the + /// latest possible. + #[cfg(feature = "try-runtime")] + pub(crate) fn pre_migrate() -> Result<(), &'static str> { + ensure!( + StorageVersion::::get() < DelegationStorageVersion::latest(), + "Already the latest storage version." + ); + + // Don't need to check for any other pre_migrate, as in try-runtime it is also + // called in the migrate() function. Same applies for post_migrate checks for + // each version migrator. + + Ok(()) + } + + /// Applies all the needed migrations from the currently deployed version to + /// the latest possible, one after the other. + /// + /// It returns the total weight consumed by ALL the migrations applied. + pub(crate) fn migrate() -> Weight { + let mut current_version: Option = Some(StorageVersion::::get()); + // Weight for StorageVersion::get(). + let mut total_weight = T::DbWeight::get().reads(1); + + while let Some(ver) = current_version { + // If any of the needed migrations pre-checks fail, the whole chain panics + // (during tests). + #[cfg(feature = "try-runtime")] + if let Err(err) = >::pre_migrate(&ver) { + panic!("{:?}", err); + } + let consumed_weight = >::migrate(&ver); + total_weight = total_weight.saturating_add(consumed_weight); + // If any of the needed migrations post-checks fail, the whole chain panics + // (during tests). + #[cfg(feature = "try-runtime")] + if let Err(err) = >::post_migrate(&ver) { + panic!("{:?}", err); + } + // If more migrations should be applied, current_version will not be None. + current_version = Self::get_next_storage_version(ver); + } + + total_weight + } + + /// Checks whether the storage version after all the needed migrations match + /// the latest one. + #[cfg(feature = "try-runtime")] + pub(crate) fn post_migrate() -> Result<(), &'static str> { + ensure!( + StorageVersion::::get() == DelegationStorageVersion::latest(), + "Not updated to the latest version." + ); + + Ok(()) + } +} + +// Tests for the entire storage migrator. +#[cfg(test)] +mod tests { + use super::*; + + use crate::mock::Test as TestRuntime; + + #[test] + fn ok_from_v1_migration() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V1) + .build(None); + ext.execute_with(|| { + #[cfg(feature = "try-runtime")] + assert!( + DelegationStorageMigrator::::pre_migrate().is_ok(), + "Storage pre-migrate from v1 should not fail." + ); + + DelegationStorageMigrator::::migrate(); + + #[cfg(feature = "try-runtime")] + assert!( + DelegationStorageMigrator::::post_migrate().is_ok(), + "Storage post-migrate from v1 should not fail." + ); + }); + } + + #[test] + fn ok_from_default_migration() { + let mut ext = mock::ExtBuilder::default().build(None); + ext.execute_with(|| { + #[cfg(feature = "try-runtime")] + assert!( + DelegationStorageMigrator::::pre_migrate().is_ok(), + "Storage pre-migrate from default version should not fail." + ); + + DelegationStorageMigrator::::migrate(); + + #[cfg(feature = "try-runtime")] + assert!( + DelegationStorageMigrator::::post_migrate().is_ok(), + "Storage post-migrate from default version should not fail." + ); + }); + } +} diff --git a/pallets/delegation/src/migrations/v1.rs b/pallets/delegation/src/migrations/v1.rs new file mode 100644 index 0000000000..f69a115ec7 --- /dev/null +++ b/pallets/delegation/src/migrations/v1.rs @@ -0,0 +1,522 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2021 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +use crate::*; + +use frame_support::{IterableStorageMap, StorageMap, StoragePrefixedMap}; +use sp_runtime::traits::Zero; +use sp_std::collections::btree_map::BTreeMap; + +/// Checks whether the deployed storage version is v1. If not, it won't try +/// migrate any data. +/// +/// Since we have the default storage version to this one, it can happen +/// that new nodes will still try to perform runtime migrations. This is not +/// a problem as at the end of the day there will not be anything in the old +/// storage entries to migrate from. Hence, the "pseudo-"migration will +/// simply result in the update of the storage deployed version. +#[cfg(feature = "try-runtime")] +pub(crate) fn pre_migrate() -> Result<(), &'static str> { + ensure!( + StorageVersion::::get() == DelegationStorageVersion::V1, + "Current deployed version is not v1." + ); + + log::info!("Version storage migrating from v1 to v2"); + Ok(()) +} + +/// It migrates the old storage entries to the new ones. +/// +/// Specifically, for each entry in Roots, a new entry in +/// DelegationHierarchies + a new node in DelegationNodes is created. +/// Furthermore, nodes in Delegations are migrated to the new structure and +/// stored under DelegationNodes, with any children from the Children +/// storage entry added to the nodes under the children set. +pub(crate) fn migrate() -> Weight { + log::info!("v1 -> v2 delegation storage migrator started!"); + let mut total_weight = Weight::zero(); + + // Before being stored, the nodes are saved in a map so that after we go over + // all the nodes and the parent-child relationship in the storage, we can update + // the `parent` link of each node accordingly. Otherwise, we would need to save + // the node in the storage, and then retrieve it again to update the parent + // link. + let mut new_nodes: BTreeMap, DelegationNode> = BTreeMap::new(); + + // First iterate over the delegation roots and translate them to hierarchies. + total_weight = total_weight.saturating_add(migrate_roots::(&mut new_nodes)); + + // Then iterate over the regular delegation nodes. + total_weight = total_weight.saturating_add(migrate_nodes::(&mut new_nodes, total_weight)); + + // By now, all the children should have been correctly added to the nodes. + // We now need to modify all the nodes that are children by adding a reference + // to their parents. + total_weight = total_weight.saturating_add(finalize_children_nodes::(&mut new_nodes, total_weight)); + + StorageVersion::::set(DelegationStorageVersion::V2); + // Adds a write from StorageVersion::set() weight. + total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); + log::debug!("Total weight consumed: {}", total_weight); + log::info!("v1 -> v2 delegation storage migrator finished!"); + + total_weight +} + +fn migrate_roots(new_nodes: &mut BTreeMap, DelegationNode>) -> Weight { + let total_weight = deprecated::v1::storage::Roots::::iter().fold( + Weight::zero(), + |mut total_weight, (old_root_id, old_root_node)| { + let new_hierarchy_details = DelegationHierarchyDetails:: { + ctype_hash: old_root_node.ctype_hash, + }; + let new_root_details = DelegationDetails:: { + owner: old_root_node.owner, + // Old roots did not have any permissions. So now we give them all permissions. + permissions: Permissions::all(), + revoked: old_root_node.revoked, + }; + // In here, we already check for potential children of root nodes and ONLY + // update the children information. The parent information will be updated + // later, when we know we have seen all the children already. + let mut new_root_node = DelegationNode::new_root_node(old_root_id, new_root_details); + if let Some(root_children_ids) = deprecated::v1::storage::Children::::take(old_root_id) { + new_root_node.children = root_children_ids.iter().copied().collect(); + } + // Add Children::take() weight + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); + + DelegationHierarchies::insert(old_root_id, new_hierarchy_details); + // Adds a read from Roots::drain() and a write from + // DelegationHierarchies::insert() weights + total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); + // Add the node to the temporary map of nodes to be added at the end. + new_nodes.insert(old_root_id, new_root_node); + + total_weight + }, + ); + + // If runtime testing, makes sure that the old number of roots is reflected in + // the new number of nodes and hierarchies migrated. + #[cfg(feature = "try-runtime")] + { + assert_eq!( + deprecated::v1::storage::Roots::::iter().count(), + DelegationHierarchies::::iter().count(), + "The # of old roots does not match the # of new delegation hierarchies." + ); + + assert_eq!( + deprecated::v1::storage::Roots::::iter().count(), + new_nodes.iter().count(), + "The # of old roots does not match the current # of new delegation nodes." + ); + + log::info!( + "{} root(s) migrated.", + deprecated::v1::storage::Roots::::iter().count() + ); + } + + // Removes the whole Roots storage. + frame_support::migration::remove_storage_prefix( + deprecated::v1::storage::Roots::::module_prefix(), + deprecated::v1::storage::Roots::::storage_prefix(), + b"", + ); + + total_weight +} + +fn migrate_nodes( + new_nodes: &mut BTreeMap, DelegationNode>, + initial_weight: Weight, +) -> Weight { + let total_weight = deprecated::v1::storage::Delegations::::iter().fold( + initial_weight, + |mut total_weight, (old_node_id, old_node)| { + let new_node_details = DelegationDetails:: { + owner: old_node.owner, + permissions: old_node.permissions, + revoked: old_node.revoked, + }; + // In the old version, a parent None indicated the node is a child of the root. + let new_node_parent_id = old_node.parent.unwrap_or(old_node.root_id); + let mut new_node = DelegationNode::new_node(old_node.root_id, new_node_parent_id, new_node_details); + if let Some(children_ids) = deprecated::v1::storage::Children::::take(old_node_id) { + new_node.children = children_ids.iter().copied().collect(); + } + // Add Children::take() weight + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); + // Adds a read from Roots::drain() weight + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); + new_nodes.insert(old_node_id, new_node); + + total_weight + }, + ); + + // If runtime testing, makes sure that the old number of delegations is + // reflected in the new number of nodes that will be added to the storage. + #[cfg(feature = "try-runtime")] + { + assert_eq!( + deprecated::v1::storage::Delegations::::iter().count(), + new_nodes.iter().count().saturating_sub(DelegationHierarchies::::iter().count()), + "The # of old delegation nodes does not match the # of new delegation nodes (calculate as the total # of nodes - the # of delegation hierarchies)." + ); + + log::info!( + "{} regular node(s) migrated.", + deprecated::v1::storage::Delegations::::iter().count() + ); + } + + // Removes the whole Delegations and Children storages. + frame_support::migration::remove_storage_prefix( + deprecated::v1::storage::Delegations::::module_prefix(), + deprecated::v1::storage::Delegations::::storage_prefix(), + b"", + ); + frame_support::migration::remove_storage_prefix( + deprecated::v1::storage::Children::::module_prefix(), + deprecated::v1::storage::Children::::storage_prefix(), + b"", + ); + + total_weight +} + +fn finalize_children_nodes( + new_nodes: &mut BTreeMap, DelegationNode>, + initial_weight: Weight, +) -> Weight { + new_nodes + .clone() + .into_iter() + .fold(initial_weight, |mut total_weight, (new_node_id, new_node)| { + // Iterate over the children of every node and update their parent link. + new_node.children.iter().for_each(|child_id| { + new_nodes + .entry(*child_id) + .and_modify(|node| node.parent = Some(new_node_id)); + }); + // We can then finally insert the new delegation node in the storage as it won't + // be updated anymore during the migration. + DelegationNodes::::insert(new_node_id, new_node); + // Adds a write from DelegationNodes::insert() weight + total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); + + total_weight + }) +} + +/// Checks whether the deployed storage version is v2 and whether any +/// parent-child link has gone missing. +#[cfg(feature = "try-runtime")] +pub(crate) fn post_migrate() -> Result<(), &'static str> { + ensure!( + StorageVersion::::get() == DelegationStorageVersion::V2, + "The version after deployment is not 2 as expected." + ); + ensure!( + verify_parent_children_integrity::(), + "Some parent-child relationship has been broken in the migration." + ); + log::info!("Version storage migrated from v1 to v2"); + Ok(()) +} + +// Verifies that for any node that has a parent, the parent includes that node +// in its children. +#[cfg(feature = "try-runtime")] +fn verify_parent_children_integrity() -> bool { + // If all's good and false is returned, returns true. + !DelegationNodes::::iter().any(|(node_id, node)| { + if let Some(parent_id) = node.parent { + if let Some(parent_node) = DelegationNodes::::get(parent_id) { + // True if the children set does not contain the parent ID + return !parent_node.children.contains(&node_id); + } else { + // If the parent node cannot be found, it is definitely an error, so return + // true. + return true; + } + } + // If all's good we keep returning false. + false + }) +} + +// Tests for the v1 storage migrator. +#[cfg(test)] +mod tests { + use sp_core::Pair; + + use super::*; + use crate::mock::Test as TestRuntime; + + #[test] + fn fail_version_higher() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V2) + .build(None); + ext.execute_with(|| { + #[cfg(feature = "try-runtime")] + assert!( + pre_migrate::().is_err(), + "Pre-migration for v1 should fail." + ); + }); + } + + #[test] + fn ok_no_delegations() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V1) + .build(None); + ext.execute_with(|| { + #[cfg(feature = "try-runtime")] + assert!( + pre_migrate::().is_ok(), + "Pre-migration for v1 should not fail." + ); + + migrate::(); + + #[cfg(feature = "try-runtime")] + assert!( + post_migrate::().is_ok(), + "Post-migration for v1 should not fail." + ); + }); + } + + #[test] + fn ok_only_root() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V1) + .build(None); + ext.execute_with(|| { + let alice = mock::get_ed25519_account(mock::get_alice_ed25519().public()); + let old_root_id = mock::get_delegation_id(true); + let old_root_node = + crate::deprecated::v1::DelegationRoot::::new(ctype::mock::get_ctype_hash(true), alice); + deprecated::v1::storage::Roots::insert(old_root_id, old_root_node.clone()); + + #[cfg(feature = "try-runtime")] + assert!( + pre_migrate::().is_ok(), + "Pre-migration for v1 should not fail." + ); + + migrate::(); + + #[cfg(feature = "try-runtime")] + assert!( + post_migrate::().is_ok(), + "Post-migration for v1 should not fail." + ); + + assert_eq!(deprecated::v1::storage::Roots::::iter_values().count(), 0); + assert_eq!( + deprecated::v1::storage::Delegations::::iter_values().count(), + 0 + ); + assert_eq!( + deprecated::v1::storage::Children::::iter_values().count(), + 0 + ); + + let new_stored_hierarchy = DelegationHierarchies::::get(old_root_id) + .expect("New delegation hierarchy should exist in the storage."); + assert_eq!(new_stored_hierarchy.ctype_hash, old_root_node.ctype_hash); + let new_stored_root = DelegationNodes::::get(old_root_id) + .expect("New delegation root should exist in the storage."); + assert_eq!(new_stored_root.hierarchy_root_id, old_root_id); + assert!(new_stored_root.parent.is_none()); + assert!(new_stored_root.children.is_empty()); + assert_eq!(new_stored_root.details.owner, old_root_node.owner); + assert_eq!(new_stored_root.details.revoked, old_root_node.revoked); + }); + } + + #[test] + fn ok_root_two_children() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V1) + .build(None); + ext.execute_with(|| { + let alice = mock::get_ed25519_account(mock::get_alice_ed25519().public()); + let bob = mock::get_sr25519_account(mock::get_bob_sr25519().public()); + let old_root_id = mock::get_delegation_id(true); + let old_root_node = + deprecated::v1::DelegationRoot::::new(ctype::mock::get_ctype_hash(true), alice.clone()); + let old_node_id_1 = mock::get_delegation_id(false); + let old_node_1 = deprecated::v1::DelegationNode::::new_root_child( + old_root_id, + alice, + Permissions::DELEGATE, + ); + let old_node_id_2 = mock::get_delegation_id_2(true); + let old_node_2 = + deprecated::v1::DelegationNode::::new_root_child(old_root_id, bob, Permissions::ATTEST); + deprecated::v1::storage::Roots::insert(old_root_id, old_root_node.clone()); + deprecated::v1::storage::Delegations::insert(old_node_id_1, old_node_1.clone()); + deprecated::v1::storage::Delegations::insert(old_node_id_2, old_node_2.clone()); + deprecated::v1::storage::Children::::insert(old_root_id, vec![old_node_id_1, old_node_id_2]); + + #[cfg(feature = "try-runtime")] + assert!( + pre_migrate::().is_ok(), + "Pre-migration for v1 should not fail." + ); + + migrate::(); + + #[cfg(feature = "try-runtime")] + assert!( + post_migrate::().is_ok(), + "Post-migration for v1 should not fail." + ); + + assert_eq!(deprecated::v1::storage::Roots::::iter_values().count(), 0); + assert_eq!( + deprecated::v1::storage::Delegations::::iter_values().count(), + 0 + ); + assert_eq!( + deprecated::v1::storage::Children::::iter_values().count(), + 0 + ); + + let new_stored_hierarchy = DelegationHierarchies::::get(old_root_id) + .expect("New delegation hierarchy should exist in the storage."); + assert_eq!(new_stored_hierarchy.ctype_hash, old_root_node.ctype_hash); + let new_stored_root = DelegationNodes::::get(old_root_id) + .expect("New delegation root should exist in the storage."); + assert_eq!(new_stored_root.hierarchy_root_id, old_root_id); + assert!(new_stored_root.parent.is_none()); + assert_eq!(new_stored_root.children.len(), 2); + assert!(new_stored_root.children.contains(&old_node_id_1)); + assert!(new_stored_root.children.contains(&old_node_id_2)); + assert_eq!(new_stored_root.details.owner, old_root_node.owner); + assert_eq!(new_stored_root.details.revoked, old_root_node.revoked); + + let new_stored_node_1 = DelegationNodes::::get(old_node_id_1) + .expect("New delegation 1 should exist in the storage."); + assert_eq!(new_stored_node_1.hierarchy_root_id, old_root_id); + assert_eq!(new_stored_node_1.parent, Some(old_root_id)); + assert!(new_stored_node_1.children.is_empty()); + assert_eq!(new_stored_node_1.details.owner, old_node_1.owner); + assert_eq!(new_stored_node_1.details.revoked, old_node_1.revoked); + + let new_stored_node_2 = DelegationNodes::::get(old_node_id_2) + .expect("New delegation 2 should exist in the storage."); + assert_eq!(new_stored_node_2.hierarchy_root_id, old_root_id); + assert_eq!(new_stored_node_2.parent, Some(old_root_id)); + assert!(new_stored_node_2.children.is_empty()); + assert_eq!(new_stored_node_2.details.owner, old_node_2.owner); + assert_eq!(new_stored_node_2.details.revoked, old_node_2.revoked); + }); + } + + #[test] + fn ok_three_level_hierarchy() { + let mut ext = mock::ExtBuilder::default() + .with_storage_version(DelegationStorageVersion::V1) + .build(None); + ext.execute_with(|| { + let alice = mock::get_ed25519_account(mock::get_alice_ed25519().public()); + let bob = mock::get_sr25519_account(mock::get_bob_sr25519().public()); + let old_root_id = mock::get_delegation_id(true); + let old_root_node = + deprecated::v1::DelegationRoot::::new(ctype::mock::get_ctype_hash(true), alice.clone()); + let old_parent_id = mock::get_delegation_id(false); + let old_parent_node = + deprecated::v1::DelegationNode::::new_root_child(old_root_id, alice, Permissions::all()); + let old_node_id = mock::get_delegation_id_2(true); + let old_node = deprecated::v1::DelegationNode::::new_node_child( + old_root_id, + old_parent_id, + bob, + Permissions::ATTEST, + ); + deprecated::v1::storage::Roots::insert(old_root_id, old_root_node.clone()); + deprecated::v1::storage::Delegations::insert(old_parent_id, old_parent_node.clone()); + deprecated::v1::storage::Delegations::insert(old_node_id, old_node.clone()); + deprecated::v1::storage::Children::::insert(old_root_id, vec![old_parent_id]); + deprecated::v1::storage::Children::::insert(old_parent_id, vec![old_node_id]); + + #[cfg(feature = "try-runtime")] + assert!( + pre_migrate::().is_ok(), + "Pre-migration for v1 should not fail." + ); + + migrate::(); + + #[cfg(feature = "try-runtime")] + assert!( + post_migrate::().is_ok(), + "Post-migration for v1 should not fail." + ); + + assert_eq!(deprecated::v1::storage::Roots::::iter_values().count(), 0); + assert_eq!( + deprecated::v1::storage::Delegations::::iter_values().count(), + 0 + ); + assert_eq!( + deprecated::v1::storage::Children::::iter_values().count(), + 0 + ); + + let new_stored_hierarchy = DelegationHierarchies::::get(old_root_id) + .expect("New delegation hierarchy should exist in the storage."); + assert_eq!(new_stored_hierarchy.ctype_hash, old_root_node.ctype_hash); + let new_stored_root = DelegationNodes::::get(old_root_id) + .expect("New delegation root should exist in the storage."); + assert_eq!(new_stored_root.hierarchy_root_id, old_root_id); + assert!(new_stored_root.parent.is_none()); + assert_eq!(new_stored_root.children.len(), 1); + assert!(new_stored_root.children.contains(&old_parent_id)); + assert_eq!(new_stored_root.details.owner, old_root_node.owner); + assert_eq!(new_stored_root.details.revoked, old_root_node.revoked); + + let new_stored_parent = DelegationNodes::::get(old_parent_id) + .expect("New delegation parent should exist in the storage."); + assert_eq!(new_stored_parent.hierarchy_root_id, old_root_id); + assert_eq!(new_stored_parent.parent, Some(old_root_id)); + assert_eq!(new_stored_parent.children.len(), 1); + assert!(new_stored_parent.children.contains(&old_node_id)); + assert_eq!(new_stored_parent.details.owner, old_parent_node.owner); + assert_eq!(new_stored_parent.details.revoked, old_parent_node.revoked); + + let new_stored_node = DelegationNodes::::get(old_node_id) + .expect("New delegation node should exist in the storage."); + assert_eq!(new_stored_node.hierarchy_root_id, old_root_id); + assert_eq!(new_stored_node.parent, Some(old_parent_id)); + assert!(new_stored_node.children.is_empty()); + assert_eq!(new_stored_node.details.owner, old_node.owner); + assert_eq!(new_stored_node.details.revoked, old_node.revoked); + }); + } +} diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index 36410c3d56..4c8b77baf9 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -28,7 +28,7 @@ use sp_runtime::{ traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify}, MultiSignature, MultiSigner, }; -use sp_std::sync::Arc; +use sp_std::{collections::btree_set::BTreeSet, sync::Arc}; #[cfg(test)] use codec::Encode; @@ -143,12 +143,12 @@ const ALICE_SEED: [u8; 32] = [0u8; 32]; const BOB_SEED: [u8; 32] = [1u8; 32]; const CHARLIE_SEED: [u8; 32] = [2u8; 32]; -const DEFAULT_ROOT_ID_SEED: u64 = 1u64; -const ALTERNATIVE_ROOT_ID_SEED: u64 = 2u64; +const DEFAULT_HIERARCHY_ID_SEED: u64 = 1u64; +const ALTERNATIVE_HIERARCHY_ID_SEED: u64 = 2u64; const DEFAULT_DELEGATION_ID_SEED: u64 = 3u64; const ALTERNATIVE_DELEGATION_ID_SEED: u64 = 4u64; -const DEFAULT_DELEGATION_ID_2_SEED: u64 = 3u64; -const ALTERNATIVE_DELEGATION_ID_2_SEED: u64 = 4u64; +const DEFAULT_DELEGATION_ID_2_SEED: u64 = 5u64; +const ALTERNATIVE_DELEGATION_ID_2_SEED: u64 = 6u64; pub fn get_origin(account: TestDelegatorId) -> Origin { Origin::signed(account) @@ -186,27 +186,27 @@ pub fn get_charlie_sr25519() -> sr25519::Pair { sr25519::Pair::from_seed(&CHARLIE_SEED) } -pub fn get_delegation_root_id(default: bool) -> TestDelegationNodeId { +pub fn get_delegation_hierarchy_id(default: bool) -> TestDelegationNodeId { if default { - TestCtypeHash::from_low_u64_be(DEFAULT_ROOT_ID_SEED) + TestDelegationNodeId::from_low_u64_be(DEFAULT_HIERARCHY_ID_SEED) } else { - TestCtypeHash::from_low_u64_be(ALTERNATIVE_ROOT_ID_SEED) + TestDelegationNodeId::from_low_u64_be(ALTERNATIVE_HIERARCHY_ID_SEED) } } pub fn get_delegation_id(default: bool) -> TestDelegationNodeId { if default { - TestCtypeHash::from_low_u64_be(DEFAULT_DELEGATION_ID_SEED) + TestDelegationNodeId::from_low_u64_be(DEFAULT_DELEGATION_ID_SEED) } else { - TestCtypeHash::from_low_u64_be(ALTERNATIVE_DELEGATION_ID_SEED) + TestDelegationNodeId::from_low_u64_be(ALTERNATIVE_DELEGATION_ID_SEED) } } pub fn get_delegation_id_2(default: bool) -> TestDelegationNodeId { if default { - TestCtypeHash::from_low_u64_be(DEFAULT_DELEGATION_ID_2_SEED) + TestDelegationNodeId::from_low_u64_be(DEFAULT_DELEGATION_ID_2_SEED) } else { - TestCtypeHash::from_low_u64_be(ALTERNATIVE_DELEGATION_ID_2_SEED) + TestDelegationNodeId::from_low_u64_be(ALTERNATIVE_DELEGATION_ID_2_SEED) } } @@ -215,116 +215,133 @@ pub(crate) fn hash_to_u8(hash: T) -> Vec { hash.encode() } -pub struct DelegationRootCreationDetails { - pub root_id: TestDelegationNodeId, +pub fn generate_base_delegation_hierarchy_details() -> DelegationHierarchyDetails { + DelegationHierarchyDetails { + ctype_hash: ctype_mock::get_ctype_hash(true), + } +} + +pub fn generate_base_delegation_node( + hierarchy_id: TestDelegationNodeId, + owner: TestDelegatorId, + parent: Option, +) -> DelegationNode { + DelegationNode { + details: generate_base_delegation_details(owner), + children: BTreeSet::new(), + hierarchy_root_id: hierarchy_id, + parent, + } +} + +pub fn generate_base_delegation_details(owner: TestDelegatorId) -> DelegationDetails { + DelegationDetails { + owner, + permissions: Permissions::DELEGATE, + revoked: false, + } +} + +pub struct DelegationHierarchyCreationOperation { + pub id: TestDelegationNodeId, pub ctype_hash: TestCtypeHash, } -pub fn generate_base_delegation_root_creation_details( - root_id: TestDelegationNodeId, - root_node: DelegationRoot, -) -> DelegationRootCreationDetails { - DelegationRootCreationDetails { - ctype_hash: root_node.ctype_hash, - root_id, +pub fn generate_base_delegation_hierarchy_creation_operation( + id: TestDelegationNodeId, +) -> DelegationHierarchyCreationOperation { + DelegationHierarchyCreationOperation { + id, + ctype_hash: ctype::mock::get_ctype_hash(true), } } -pub struct DelegationCreationDetails { +pub struct DelegationCreationOperation { pub delegation_id: TestDelegationNodeId, - pub root_id: TestDelegationNodeId, - pub parent_id: Option, + pub hierarchy_id: TestDelegationNodeId, + pub parent_id: TestDelegationNodeId, pub delegate: TestDelegatorId, pub permissions: Permissions, pub delegate_signature: TestDelegateSignature, } -pub fn generate_base_delegation_creation_details( +pub fn generate_base_delegation_creation_operation( delegation_id: TestDelegationNodeId, delegate_signature: TestDelegateSignature, delegation_node: DelegationNode, -) -> DelegationCreationDetails { - DelegationCreationDetails { +) -> DelegationCreationOperation { + DelegationCreationOperation { delegation_id, - parent_id: delegation_node.parent, - root_id: delegation_node.root_id, - delegate: delegation_node.owner, + parent_id: delegation_node + .parent + .expect("Delegation node must specify a parent ID upon creation"), + hierarchy_id: delegation_node.hierarchy_root_id, + delegate: delegation_node.details.owner, delegate_signature, - permissions: delegation_node.permissions, + permissions: delegation_node.details.permissions, } } -pub struct DelegationRootRevocationDetails { - pub root_id: TestDelegationNodeId, +pub struct DelegationHierarchyRevocationOperation { + pub id: TestDelegationNodeId, pub max_children: u32, } -pub fn generate_base_delegation_root_revocation_details( - root_id: TestDelegationNodeId, -) -> DelegationRootRevocationDetails { - DelegationRootRevocationDetails { - root_id, - max_children: 0u32, - } +pub fn generate_base_delegation_hierarchy_revocation_operation( + id: TestDelegationNodeId, +) -> DelegationHierarchyRevocationOperation { + DelegationHierarchyRevocationOperation { id, max_children: 0u32 } } -pub struct DelegationRevocationDetails { +pub struct DelegationRevocationOperation { pub delegation_id: TestDelegationNodeId, pub max_parent_checks: u32, pub max_revocations: u32, } -pub fn generate_base_delegation_revocation_details(delegation_id: TestDelegationNodeId) -> DelegationRevocationDetails { - DelegationRevocationDetails { +pub fn generate_base_delegation_revocation_operation( + delegation_id: TestDelegationNodeId, +) -> DelegationRevocationOperation { + DelegationRevocationOperation { delegation_id, max_parent_checks: 0u32, max_revocations: 0u32, } } -pub fn generate_base_delegation_root(owner: TestDelegatorId) -> DelegationRoot { - DelegationRoot { - owner, - ctype_hash: ctype_mock::get_ctype_hash(true), - revoked: false, - } -} - -pub fn generate_base_delegation_node(root_id: TestDelegationNodeId, owner: TestDelegatorId) -> DelegationNode { - DelegationNode { - owner, - parent: None, - root_id, - permissions: Permissions::DELEGATE, - revoked: false, - } -} - #[derive(Clone)] pub struct ExtBuilder { ctype_builder: Option, - root_delegations_stored: Vec<(TestDelegationNodeId, DelegationRoot)>, + delegation_hierarchies_stored: Vec<( + TestDelegationNodeId, + DelegationHierarchyDetails, + DelegatorIdOf, + )>, delegations_stored: Vec<(TestDelegationNodeId, DelegationNode)>, - children_stored: Vec<(TestDelegationNodeId, Vec)>, + storage_version: DelegationStorageVersion, } impl Default for ExtBuilder { fn default() -> Self { Self { ctype_builder: None, - root_delegations_stored: vec![], + delegation_hierarchies_stored: vec![], delegations_stored: vec![], - children_stored: vec![], + storage_version: DelegationStorageVersion::default(), } } } impl ExtBuilder { - pub fn with_root_delegations( + pub fn with_delegation_hierarchies( mut self, - root_delegations: Vec<(TestDelegationNodeId, DelegationRoot)>, + delegation_hierarchies: Vec<( + TestDelegationNodeId, + DelegationHierarchyDetails, + DelegatorIdOf, + )>, ) -> Self { - self.root_delegations_stored = root_delegations; + self.delegation_hierarchies_stored = delegation_hierarchies; self } @@ -333,8 +350,8 @@ impl ExtBuilder { self } - pub fn with_children(mut self, children: Vec<(TestDelegationNodeId, Vec)>) -> Self { - self.children_stored = children; + pub fn with_storage_version(mut self, storage_version: DelegationStorageVersion) -> Self { + self.storage_version = storage_version; self } @@ -346,29 +363,41 @@ impl ExtBuilder { sp_io::TestExternalities::new(storage) }; - if !self.root_delegations_stored.is_empty() { + if !self.delegation_hierarchies_stored.is_empty() { ext.execute_with(|| { - self.root_delegations_stored.iter().for_each(|root_delegation| { - delegation::Roots::::insert(root_delegation.0, root_delegation.1.clone()); - }) + self.delegation_hierarchies_stored + .iter() + .for_each(|delegation_hierarchy| { + delegation::Pallet::create_and_store_new_hierarchy( + delegation_hierarchy.0, + delegation_hierarchy.1.clone(), + delegation_hierarchy.2.clone(), + ); + }) }); } if !self.delegations_stored.is_empty() { ext.execute_with(|| { self.delegations_stored.iter().for_each(|del| { - delegation::Delegations::::insert(del.0, del.1.clone()); + let parent_node_id = del + .1 + .parent + .expect("Delegation node that is not a root must have a parent ID specified."); + let parent_node = delegation::DelegationNodes::::get(&parent_node_id).unwrap(); + delegation::Pallet::store_delegation_under_parent( + del.0, + del.1.clone(), + parent_node_id, + parent_node, + ); }) }); } - if !self.children_stored.is_empty() { - ext.execute_with(|| { - self.children_stored.iter().for_each(|child| { - delegation::Children::::insert(child.0, child.1.clone()); - }) - }); - } + ext.execute_with(|| { + delegation::StorageVersion::::set(self.storage_version); + }); ext } diff --git a/pallets/delegation/src/tests.rs b/pallets/delegation/src/tests.rs index 9fd63a451e..3a618352e5 100644 --- a/pallets/delegation/src/tests.rs +++ b/pallets/delegation/src/tests.rs @@ -30,31 +30,37 @@ fn create_root_delegation_successful() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), - ); + let hierarchy_root_id = get_delegation_hierarchy_id(true); - let operation = generate_base_delegation_root_creation_details(root_id, root_node.clone()); + let operation = generate_base_delegation_hierarchy_creation_operation(hierarchy_root_id); let mut ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(operation.ctype_hash, creator.clone())]) .build(None); ext.execute_with(|| { - assert_ok!(Delegation::create_root( + assert_ok!(Delegation::create_hierarchy( get_origin(creator.clone()), - operation.root_id, + operation.id, operation.ctype_hash )); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); + let stored_hierarchy_details = ext.execute_with(|| { + Delegation::delegation_hierarchies(&hierarchy_root_id) + .expect("Delegation hierarchy should be present on chain.") + }); + assert_eq!(stored_hierarchy_details.ctype_hash, operation.ctype_hash); - assert_eq!(stored_delegation_root.owner, creator); - assert_eq!(stored_delegation_root.ctype_hash, operation.ctype_hash); - assert!(!stored_delegation_root.revoked); + let stored_delegation_root = ext.execute_with(|| { + Delegation::delegation_nodes(&hierarchy_root_id).expect("Delegation root should be present on chain.") + }); + + assert_eq!(stored_delegation_root.hierarchy_root_id, hierarchy_root_id); + assert_eq!(stored_delegation_root.parent, None); + assert_eq!(stored_delegation_root.children.len(), 0); + assert_eq!(stored_delegation_root.details.owner, creator); + assert!(!stored_delegation_root.details.revoked); } #[test] @@ -62,24 +68,24 @@ fn duplicate_create_root_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let operation = generate_base_delegation_root_creation_details(root_id, root_node.clone()); + let operation = generate_base_delegation_hierarchy_creation_operation(hierarchy_root_id); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(operation.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) .build(Some(ext)); ext.execute_with(|| { - assert_err!( - Delegation::create_root(get_origin(creator.clone()), operation.root_id, operation.ctype_hash), - delegation::Error::::RootAlreadyExists + assert_noop!( + Delegation::create_hierarchy(get_origin(creator.clone()), operation.id, operation.ctype_hash), + delegation::Error::::HierarchyAlreadyExists ); }); } @@ -89,19 +95,16 @@ fn ctype_not_found_create_root_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), - ); + let hierarchy_root_id = get_delegation_hierarchy_id(true); - let operation = generate_base_delegation_root_creation_details(root_id, root_node); + let operation = generate_base_delegation_hierarchy_creation_operation(hierarchy_root_id); // No CType stored, let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { - assert_err!( - Delegation::create_root(get_origin(creator.clone()), operation.root_id, operation.ctype_hash), + assert_noop!( + Delegation::create_hierarchy(get_origin(creator.clone()), operation.id, operation.ctype_hash), ctype::Error::::CTypeNotFound ); }); @@ -110,45 +113,44 @@ fn ctype_not_found_create_root_delegation_error() { // submit_delegation_creation_operation() #[test] -fn create_delegation_no_parent_successful() { +fn create_delegation_direct_root_successful() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); let (delegation_id, delegation_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(hierarchy_root_id)), ); - let delegation_info = Delegation::calculate_hash( + let delegation_info = Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &hierarchy_root_id, + &delegation_node.details.permissions, ); let delegate_signature = delegate_keypair.sign(&hash_to_u8(delegation_info)); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) .build(Some(ext)); ext.execute_with(|| { assert_ok!(Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, operation.delegate.clone(), operation.permissions, @@ -157,21 +159,22 @@ fn create_delegation_no_parent_successful() { }); let stored_delegation = ext.execute_with(|| { - Delegation::delegations(&operation.delegation_id).expect("Delegation should be present on chain.") + Delegation::delegation_nodes(&operation.delegation_id).expect("Delegation should be present on chain.") }); - assert_eq!(stored_delegation.root_id, operation.root_id); - assert_eq!(stored_delegation.parent, operation.parent_id); - assert_eq!(stored_delegation.owner, operation.delegate); - assert_eq!(stored_delegation.permissions, operation.permissions); - assert!(!stored_delegation.revoked); + assert_eq!(stored_delegation.hierarchy_root_id, operation.hierarchy_id); + assert_eq!(stored_delegation.parent, Some(operation.parent_id)); + assert!(stored_delegation.children.is_empty()); + assert_eq!(stored_delegation.details.owner, operation.delegate); + assert_eq!(stored_delegation.details.permissions, operation.permissions); + assert!(!stored_delegation.details.revoked); // Verify that the root has the new delegation among its children - let stored_root_children = ext.execute_with(|| { - Delegation::children(&operation.root_id).expect("Delegation root children should be present on chain.") + let stored_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.hierarchy_id).expect("Delegation root should be present on chain.") }); - assert_eq!(stored_root_children, vec![operation.delegation_id]); + assert!(stored_root.children.contains(&operation.delegation_id)); } #[test] @@ -181,418 +184,423 @@ fn create_delegation_with_parent_successful() { let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, creator.clone()), + generate_base_delegation_node(hierarchy_root_id, creator.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegation_info = Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, - ))); + &hierarchy_root_id, + &parent_id, + &delegation_node.details.permissions, + ); + + let delegate_signature = delegate_keypair.sign(&hash_to_u8(delegation_info)); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![(parent_delegation_id, parent_delegation_node)]) - .with_children(vec![(root_id, vec![(parent_delegation_id)])]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .with_delegations(vec![(parent_id, parent_node)]) .build(Some(ext)); ext.execute_with(|| { assert_ok!(Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, - delegate.clone(), + operation.delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), )); }); let stored_delegation = ext.execute_with(|| { - Delegation::delegations(&operation.delegation_id).expect("Delegation should be present on chain.") + Delegation::delegation_nodes(&operation.delegation_id).expect("Delegation should be present on chain.") }); - assert_eq!(stored_delegation.root_id, operation.root_id); - assert_eq!(stored_delegation.parent, operation.parent_id); - assert_eq!(stored_delegation.owner, operation.delegate); - assert_eq!(stored_delegation.permissions, operation.permissions); - assert!(!stored_delegation.revoked); + assert_eq!(stored_delegation.hierarchy_root_id, operation.hierarchy_id); + assert_eq!(stored_delegation.parent, Some(operation.parent_id)); + assert!(stored_delegation.children.is_empty()); + assert_eq!(stored_delegation.details.owner, operation.delegate); + assert_eq!(stored_delegation.details.permissions, operation.permissions); + assert!(!stored_delegation.details.revoked); // Verify that the parent has the new delegation among its children - let stored_parent_children = ext.execute_with(|| { - Delegation::children(&operation.parent_id.unwrap()) - .expect("Delegation parent children should be present on chain.") + let stored_parent = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.parent_id).expect("Delegation parent should be present on chain.") }); - assert_eq!(stored_parent_children, vec![delegation_id]); + assert!(stored_parent.children.contains(&operation.delegation_id)); } #[test] -fn invalid_delegate_signature_create_delegation_error() { +fn create_delegation_direct_root_revoked_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let alternative_keypair = get_alice_sr25519(); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); let (delegation_id, delegation_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(hierarchy_root_id)), ); - let delegate_signature = alternative_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegation_info = Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, - ))); + &hierarchy_root_id, + &hierarchy_root_id, + &delegation_node.details.permissions, + ); + + let delegate_signature = delegate_keypair.sign(&hash_to_u8(delegation_info)); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) .build(Some(ext)); ext.execute_with(|| { - assert_err!( + let _ = Delegation::revoke_delegation( + get_origin(creator.clone()), + operation.hierarchy_id, + 0u32, + MaxRevocations::get(), + ); + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, - delegate.clone(), + delegate, operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::InvalidDelegateSignature + delegation::Error::::ParentDelegationRevoked ); }); } #[test] -fn duplicate_delegation_create_delegation_error() { +fn create_delegation_with_parent_revoked_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (delegation_id, delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, creator.clone(), Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(parent_id)), ); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegation_info = Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, - ))); + &hierarchy_root_id, + &parent_id, + &delegation_node.details.permissions, + ); + + let delegate_signature = delegate_keypair.sign(&hash_to_u8(delegation_info)); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node.clone()); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .with_delegations(vec![(parent_id, parent_node)]) .build(Some(ext)); ext.execute_with(|| { - assert_err!( + let _ = Delegation::revoke_delegation( + get_origin(creator.clone()), + operation.parent_id, + MaxRevocations::get(), + MaxParentChecks::get(), + ); + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, - delegate.clone(), + delegate, operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::DelegationAlreadyExists + delegation::Error::::ParentDelegationRevoked ); }); } #[test] -fn root_not_existing_create_delegation_error() { +fn invalid_delegate_signature_create_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); + let alternative_keypair = get_alice_sr25519(); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); let (delegation_id, delegation_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(hierarchy_root_id)), ); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegate_signature = alternative_keypair.sign(&hash_to_u8(Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &hierarchy_root_id, + &delegation_node.details.permissions, ))); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); - // No delegations added to the pallet storage - let mut ext = ExtBuilder::default().build(Some(ext)); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .build(Some(ext)); ext.execute_with(|| { - assert_err!( + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::RootNotFound + delegation::Error::::InvalidDelegateSignature ); }); } #[test] -fn parent_not_existing_create_delegation_error() { +fn duplicate_delegation_create_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let alternative_parent_id = get_delegation_id(false); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(hierarchy_root_id)), ); - delegation_node.parent = Some(alternative_parent_id); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &hierarchy_root_id, + &delegation_node.details.permissions, ))); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node.clone()); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .with_delegations(vec![(delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { - assert_err!( + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::ParentDelegationNotFound + delegation::Error::::DelegationAlreadyExists ); }); } #[test] -fn not_owner_of_parent_create_delegation_error() { +fn parent_not_existing_create_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let alternative_owner_keypair = get_charlie_ed25519(); - let alternative_owner = get_ed25519_account(alternative_owner_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, alternative_owner), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( - get_delegation_id(false), - generate_base_delegation_node(root_id, delegate.clone()), - ); - delegation_node.parent = Some(parent_delegation_id); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &hierarchy_root_id, + &delegation_node.details.permissions, ))); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![(parent_delegation_id, parent_delegation_node)]) - .with_children(vec![(root_id, vec![parent_delegation_id])]) - .build(Some(ext)); + // No delegations added to the pallet storage + let mut ext = ExtBuilder::default().build(Some(ext)); ext.execute_with(|| { - assert_err!( + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::NotOwnerOfParentDelegation + delegation::Error::::ParentDelegationNotFound ); }); } #[test] -fn unauthorised_delegation_create_delegation_error() { +fn not_owner_of_parent_create_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); + let alternative_owner_keypair = get_charlie_ed25519(); + let alternative_owner = get_ed25519_account(alternative_owner_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(creator.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, mut parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, creator.clone()), + generate_base_delegation_node(hierarchy_root_id, alternative_owner, Some(hierarchy_root_id)), ); - parent_delegation_node.permissions = delegation::Permissions::ATTEST; - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &parent_id, + &delegation_node.details.permissions, ))); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![(parent_delegation_id, parent_delegation_node)]) - .with_children(vec![(root_id, vec![parent_delegation_id])]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .with_delegations(vec![(parent_id, parent_node)]) .build(Some(ext)); ext.execute_with(|| { - assert_err!( + assert_noop!( Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::UnauthorizedDelegation + delegation::Error::::NotOwnerOfParentDelegation ); }); } #[test] -fn not_owner_of_root_create_delegation_error() { +fn unauthorised_delegation_create_delegation_error() { let creator_keypair = get_alice_ed25519(); let creator = get_ed25519_account(creator_keypair.public()); - let alternative_owner_keypair = get_charlie_ed25519(); - let alternative_owner = get_ed25519_account(alternative_owner_keypair.public()); let delegate_keypair = get_bob_sr25519(); let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(alternative_owner), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (delegation_id, delegation_node) = ( + let (parent_id, mut parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, creator.clone(), Some(hierarchy_root_id)), + ); + parent_node.details.permissions = delegation::Permissions::ATTEST; + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(parent_id)), ); - let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_hash( + let delegate_signature = delegate_keypair.sign(&hash_to_u8(Delegation::calculate_delegation_creation_hash( &delegation_id, - &delegation_node.root_id, - &delegation_node.parent, - &delegation_node.permissions, + &hierarchy_root_id, + &parent_id, + &delegation_node.details.permissions, ))); let operation = - generate_base_delegation_creation_details(delegation_id, delegate_signature.into(), delegation_node); + generate_base_delegation_creation_operation(delegation_id, delegate_signature.into(), delegation_node); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, creator.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, creator.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, creator.clone())]) + .with_delegations(vec![(parent_id, parent_node)]) .build(Some(ext)); ext.execute_with(|| { @@ -600,218 +608,220 @@ fn not_owner_of_root_create_delegation_error() { Delegation::add_delegation( get_origin(creator.clone()), operation.delegation_id, - operation.root_id, operation.parent_id, delegate.clone(), operation.permissions, operation.delegate_signature.clone().encode(), ), - delegation::Error::::NotOwnerOfRootDelegation + delegation::Error::::UnauthorizedDelegation ); }); } // submit_delegation_root_revocation_operation() + #[test] fn empty_revoke_root_successful() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let mut operation = generate_base_delegation_root_revocation_details(root_id); - operation.max_children = 2u32; + let operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .build(Some(ext)); ext.execute_with(|| { - assert_ok!(Delegation::revoke_root( + assert_ok!(Delegation::revoke_delegation( get_origin(revoker.clone()), - operation.root_id, + operation.id, + 0u32, operation.max_children )); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); - assert!(stored_delegation_root.revoked); + let stored_delegation_hierarchy_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.id).expect("Delegation root should be present on chain.") + }); + + assert!(stored_delegation_hierarchy_root.details.revoked); } #[test] fn list_hierarchy_revoke_root_successful() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let delegate_keypair = get_bob_ed25519(); - let delegate = get_ed25519_account(delegate_keypair.public()); + let delegate_keypair = get_bob_sr25519(); + let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, revoker.clone()), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let mut operation = generate_base_delegation_root_revocation_details(root_id); + let mut operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); operation.max_children = 2u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - // Root -> Parent -> Delegation - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { - assert_ok!(Delegation::revoke_root( + assert_ok!(Delegation::revoke_delegation( get_origin(revoker.clone()), - operation.root_id, + operation.id, + 0u32, operation.max_children )); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); - assert!(stored_delegation_root.revoked); + let stored_delegation_hierarchy_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.id).expect("Delegation root should be present on chain.") + }); + assert!(stored_delegation_hierarchy_root.details.revoked); let stored_parent_delegation = ext.execute_with(|| { - Delegation::delegations(&parent_delegation_id).expect("Parent delegation should be present on chain.") + Delegation::delegation_nodes(&parent_id).expect("Parent delegation should be present on chain.") }); - assert!(stored_parent_delegation.revoked); + assert!(stored_parent_delegation.details.revoked); - let stored_delegation = - ext.execute_with(|| Delegation::delegations(&delegation_id).expect("Delegation should be present on chain.")); - assert!(stored_delegation.revoked); + let stored_delegation = ext + .execute_with(|| Delegation::delegation_nodes(&delegation_id).expect("Delegation should be present on chain.")); + assert!(stored_delegation.details.revoked); } #[test] fn tree_hierarchy_revoke_root_successful() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let delegate_keypair = get_bob_ed25519(); - let delegate = get_ed25519_account(delegate_keypair.public()); + let delegate_keypair = get_bob_sr25519(); + let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (delegation_id_1, delegation_node_1) = ( + let (delegation1_id, delegation1_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, revoker.clone()), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - let (delegation_id_2, delegation_node_2) = ( + let (delegation2_id, delegation2_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(hierarchy_root_id)), ); - let mut operation = generate_base_delegation_root_revocation_details(root_id); + let mut operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); operation.max_children = 2u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - // Root -> Delegation 1 && Delegation 2 - (root_id, vec![delegation_id_1, delegation_id_2]), + (delegation1_id, delegation1_node), + (delegation2_id, delegation2_node), ]) .build(Some(ext)); ext.execute_with(|| { - assert_ok!(Delegation::revoke_root( + assert_ok!(Delegation::revoke_delegation( get_origin(revoker.clone()), - operation.root_id, + operation.id, + 0u32, operation.max_children )); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); - assert!(stored_delegation_root.revoked); + let stored_delegation_hierarchy_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.id).expect("Delegation root should be present on chain.") + }); + assert!(stored_delegation_hierarchy_root.details.revoked); - let stored_delegation_1 = ext - .execute_with(|| Delegation::delegations(&delegation_id_1).expect("Delegation 1 should be present on chain.")); - assert!(stored_delegation_1.revoked); + let stored_delegation_1 = ext.execute_with(|| { + Delegation::delegation_nodes(&delegation1_id).expect("Delegation 1 should be present on chain.") + }); + assert!(stored_delegation_1.details.revoked); - let stored_delegation_2 = ext - .execute_with(|| Delegation::delegations(&delegation_id_2).expect("Delegation 2 should be present on chain.")); - assert!(stored_delegation_2.revoked); + let stored_delegation_2 = ext.execute_with(|| { + Delegation::delegation_nodes(&delegation2_id).expect("Delegation 2 should be present on chain.") + }); + assert!(stored_delegation_2.details.revoked); } #[test] -fn greater_max_revocations_revoke_root_successful() { +fn max_max_revocations_revoke_successful() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let delegate_keypair = get_alice_ed25519(); - let delegate = get_ed25519_account(delegate_keypair.public()); + let delegate_keypair = get_bob_sr25519(); + let delegate = get_sr25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - let mut operation = generate_base_delegation_root_revocation_details(root_id); + let mut operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); operation.max_children = MaxRevocations::get(); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![ - // Root -> Delegation - (root_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { - assert_ok!(Delegation::revoke_root( + assert_ok!(Delegation::revoke_delegation( get_origin(revoker.clone()), - operation.root_id, + operation.id, + 0u32, operation.max_children )); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); - assert!(stored_delegation_root.revoked); + let stored_delegation_hierarchy_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.id).expect("Delegation root should be present on chain.") + }); + assert!(stored_delegation_hierarchy_root.details.revoked); + + let stored_parent_delegation = ext.execute_with(|| { + Delegation::delegation_nodes(&parent_id).expect("Parent delegation should be present on chain.") + }); + assert!(stored_parent_delegation.details.revoked); - let stored_delegation = - ext.execute_with(|| Delegation::delegations(&delegation_id).expect("Delegation should be present on chain.")); - assert!(stored_delegation.revoked); + let stored_delegation = ext + .execute_with(|| Delegation::delegation_nodes(&delegation_id).expect("Delegation should be present on chain.")); + assert!(stored_delegation.details.revoked); } #[test] @@ -819,16 +829,16 @@ fn root_not_found_revoke_root_error() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let root_id = get_delegation_root_id(true); + let hierarchy_root_id = get_delegation_hierarchy_id(true); - let operation = generate_base_delegation_root_revocation_details(root_id); + let operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); let mut ext = ExtBuilder::default().build(None); ext.execute_with(|| { assert_noop!( - Delegation::revoke_root(get_origin(revoker.clone()), operation.root_id, operation.max_children), - delegation::Error::::RootNotFound + Delegation::revoke_delegation(get_origin(revoker.clone()), operation.id, 0u32, operation.max_children), + delegation::Error::::DelegationNotFound ); }); } @@ -840,23 +850,23 @@ fn different_root_creator_revoke_root_error() { let alternative_revoker_keypair = get_charlie_ed25519(); let alternative_revoker = get_ed25519_account(alternative_revoker_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(alternative_revoker), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let operation = generate_base_delegation_root_revocation_details(root_id); + let operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, alternative_revoker)]) .build(Some(ext)); ext.execute_with(|| { assert_noop!( - Delegation::revoke_root(get_origin(revoker.clone()), operation.root_id, operation.max_children), + Delegation::revoke_delegation(get_origin(revoker.clone()), operation.id, 0u32, operation.max_children), delegation::Error::::UnauthorizedRevocation ); }); @@ -869,33 +879,29 @@ fn too_small_max_revocations_revoke_root_error() { let delegate_keypair = get_alice_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(hierarchy_root_id)), ); - let mut operation = generate_base_delegation_root_revocation_details(root_id); + let mut operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); operation.max_children = 0u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![ - // Root -> Delegation - (root_id, vec![delegation_id]), - ]) .build(Some(ext)); ext.execute_with(|| { assert_noop!( - Delegation::revoke_root(get_origin(revoker.clone()), operation.root_id, operation.max_children), + Delegation::revoke_delegation(get_origin(revoker.clone()), operation.id, 0u32, operation.max_children), delegation::Error::::ExceededRevocationBounds ); }); @@ -908,42 +914,35 @@ fn exact_children_max_revocations_revoke_root_error() { let delegate_keypair = get_alice_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (delegation_id_1, delegation_node_1) = ( + let (delegation1_id, delegation1_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(hierarchy_root_id)), ); - let (delegation_id_2, mut delegation_node_2) = ( + let (delegation2_id, delegation2_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate.clone()), + generate_base_delegation_node(hierarchy_root_id, delegate.clone(), Some(delegation1_id)), ); - delegation_node_2.parent = Some(delegation_id_1); - let (delegation_id_3, mut delegation_node_3) = ( - get_delegation_root_id(false), - generate_base_delegation_node(root_id, delegate), + let (delegation3_id, delegation3_node) = ( + get_delegation_id_2(true), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(delegation1_id)), ); - delegation_node_3.parent = Some(delegation_id_1); - let mut operation = generate_base_delegation_root_revocation_details(root_id); + let mut operation = generate_base_delegation_hierarchy_revocation_operation(hierarchy_root_id); operation.max_children = 2u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - (delegation_id_3, delegation_node_3), - ]) - .with_children(vec![ - // Root -> Delegation 1 -> Delegation 2 && Delegation 3 - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2, delegation_id_3]), + (delegation1_id, delegation1_node), + (delegation2_id, delegation2_node), + (delegation3_id, delegation3_node), ]) .build(Some(ext)); @@ -951,28 +950,32 @@ fn exact_children_max_revocations_revoke_root_error() { // assert_err and not asser_noop becase the storage is indeed changed, even tho // partially assert_err!( - Delegation::revoke_root(get_origin(revoker.clone()), operation.root_id, operation.max_children), + Delegation::revoke_delegation(get_origin(revoker.clone()), operation.id, 0u32, operation.max_children), delegation::Error::::ExceededRevocationBounds ); }); - let stored_delegation_root = ext - .execute_with(|| Delegation::roots(&operation.root_id).expect("Delegation root should be present on chain.")); - assert!(!stored_delegation_root.revoked); + let stored_delegation_root = ext.execute_with(|| { + Delegation::delegation_nodes(&operation.id).expect("Delegation root should be present on chain.") + }); + assert!(!stored_delegation_root.details.revoked); - let stored_delegation_1 = ext - .execute_with(|| Delegation::delegations(&delegation_id_1).expect("Delegation 1 should be present on chain.")); - assert!(!stored_delegation_1.revoked); + let stored_delegation_1 = ext.execute_with(|| { + Delegation::delegation_nodes(&delegation1_id).expect("Delegation 1 should be present on chain.") + }); + assert!(!stored_delegation_1.details.revoked); // Only this leaf should have been revoked as it is the first child of // delegation_1 - let stored_delegation_2 = ext - .execute_with(|| Delegation::delegations(&delegation_id_2).expect("Delegation 2 should be present on chain.")); - assert!(stored_delegation_2.revoked); + let stored_delegation_2 = ext.execute_with(|| { + Delegation::delegation_nodes(&delegation2_id).expect("Delegation 2 should be present on chain.") + }); + assert!(stored_delegation_2.details.revoked); - let stored_delegation_3 = ext - .execute_with(|| Delegation::delegations(&delegation_id_3).expect("Delegation 3 should be present on chain.")); - assert!(!stored_delegation_3.revoked); + let stored_delegation_3 = ext.execute_with(|| { + Delegation::delegation_nodes(&delegation3_id).expect("Delegation 3 should be present on chain.") + }); + assert!(!stored_delegation_3.details.revoked); } // submit_delegation_revocation_operation() @@ -984,37 +987,28 @@ fn direct_owner_revoke_delegation_successful() { let delegate_keypair = get_alice_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, revoker.clone()), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let mut operation = generate_base_delegation_revocation_details(parent_delegation_id); + let mut operation = generate_base_delegation_revocation_operation(parent_id); operation.max_revocations = 2u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); - // Root -> Parent -> Child let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { @@ -1027,14 +1021,14 @@ fn direct_owner_revoke_delegation_successful() { }); let stored_parent_delegation = ext.execute_with(|| { - Delegation::delegations(&parent_delegation_id).expect("Parent delegation should be present on chain.") + Delegation::delegation_nodes(&parent_id).expect("Parent delegation should be present on chain.") }); - assert!(stored_parent_delegation.revoked); + assert!(stored_parent_delegation.details.revoked); let stored_child_delegation = ext.execute_with(|| { - Delegation::delegations(&delegation_id).expect("Child delegation should be present on chain.") + Delegation::delegation_nodes(&delegation_id).expect("Child delegation should be present on chain.") }); - assert!(stored_child_delegation.revoked); + assert!(stored_child_delegation.details.revoked); } #[test] @@ -1044,37 +1038,29 @@ fn parent_owner_revoke_delegation_successful() { let delegate_keypair = get_alice_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, revoker.clone()), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let mut operation = generate_base_delegation_revocation_details(delegation_id); + let mut operation = generate_base_delegation_revocation_operation(delegation_id); operation.max_parent_checks = 1u32; operation.max_revocations = 1u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { @@ -1087,14 +1073,14 @@ fn parent_owner_revoke_delegation_successful() { }); let stored_parent_delegation = ext.execute_with(|| { - Delegation::delegations(&parent_delegation_id).expect("Parent delegation should be present on chain.") + Delegation::delegation_nodes(&parent_id).expect("Parent delegation should be present on chain.") }); - assert!(!stored_parent_delegation.revoked); + assert!(!stored_parent_delegation.details.revoked); let stored_child_delegation = ext.execute_with(|| { - Delegation::delegations(&delegation_id).expect("Child delegation should be present on chain.") + Delegation::delegation_nodes(&delegation_id).expect("Child delegation should be present on chain.") }); - assert!(stored_child_delegation.revoked); + assert!(stored_child_delegation.details.revoked); } #[test] @@ -1102,19 +1088,19 @@ fn delegation_not_found_revoke_delegation_error() { let revoker_keypair = get_alice_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let delegation_id = get_delegation_id(true); + let delegation_id = get_delegation_id(false); - let operation = generate_base_delegation_revocation_details(delegation_id); + let operation = generate_base_delegation_revocation_operation(delegation_id); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) .build(Some(ext)); ext.execute_with(|| { @@ -1137,22 +1123,24 @@ fn not_delegating_revoke_delegation_error() { let revoker_keypair = get_bob_ed25519(); let revoker = get_ed25519_account(revoker_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(owner.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, owner.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, delegation_node) = (get_delegation_id(false), generate_base_delegation_node(root_id, owner)); - let mut operation = generate_base_delegation_revocation_details(delegation_id); + let mut operation = generate_base_delegation_revocation_operation(delegation_id); operation.max_parent_checks = MaxParentChecks::get(); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, owner)]) .with_delegations(vec![(delegation_id, delegation_node)]) - .with_children(vec![(root_id, vec![delegation_id])]) .build(Some(ext)); ext.execute_with(|| { @@ -1177,36 +1165,28 @@ fn parent_too_far_revoke_delegation_error() { let delegate_keypair = get_bob_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(owner.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, intermediate.clone()), + generate_base_delegation_node(hierarchy_root_id, intermediate.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let mut operation = generate_base_delegation_revocation_details(delegation_id); + let mut operation = generate_base_delegation_revocation_operation(delegation_id); operation.max_parent_checks = 0u32; let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, owner)]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, owner.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, owner)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { @@ -1229,36 +1209,27 @@ fn too_many_revocations_revoke_delegation_error() { let delegate_keypair = get_bob_ed25519(); let delegate = get_ed25519_account(delegate_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(revoker.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); - let (parent_delegation_id, parent_delegation_node) = ( + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, revoker.clone()), + generate_base_delegation_node(hierarchy_root_id, revoker.clone(), Some(hierarchy_root_id)), ); - let (delegation_id, mut delegation_node) = ( + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, delegate), + generate_base_delegation_node(hierarchy_root_id, delegate, Some(parent_id)), ); - delegation_node.parent = Some(parent_delegation_id); - let mut operation = generate_base_delegation_revocation_details(delegation_id); - operation.max_parent_checks = 1u32; + let operation = generate_base_delegation_revocation_operation(parent_id); let ext = ctype_mock::ExtBuilder::default() - .with_ctypes(vec![(root_node.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(hierarchy_details.ctype_hash, revoker.clone())]) .build(None); let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (parent_delegation_id, parent_delegation_node), - (delegation_id, delegation_node), - ]) - .with_children(vec![ - (root_id, vec![parent_delegation_id]), - (parent_delegation_id, vec![delegation_id]), - ]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, revoker.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) .build(Some(ext)); ext.execute_with(|| { @@ -1285,33 +1256,33 @@ fn is_delegating_direct_not_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - let (delegation_id_2, mut delegation_node_2) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, user_3.clone()), + generate_base_delegation_node(hierarchy_root_id, user_3.clone(), Some(parent_id)), ); - delegation_node_2.parent = Some(delegation_id_1); let max_parent_checks = 0u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_3, &delegation_id_2, max_parent_checks), + Delegation::is_delegating(&user_3, &delegation_id, max_parent_checks), Ok((true, max_parent_checks)) ); }); @@ -1326,33 +1297,33 @@ fn is_delegating_direct_not_revoked_max_parent_checks_value() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - let (delegation_id_2, mut delegation_node_2) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, user_3.clone()), + generate_base_delegation_node(hierarchy_root_id, user_3.clone(), Some(parent_id)), ); - delegation_node_2.parent = Some(delegation_id_1); let max_parent_checks = u32::MAX; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_3, &delegation_id_2, max_parent_checks), + Delegation::is_delegating(&user_3, &delegation_id, max_parent_checks), Ok((true, 0u32)) ); }); @@ -1367,35 +1338,35 @@ fn is_delegating_direct_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - let (delegation_id_2, mut delegation_node_2) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, mut delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, user_3.clone()), + generate_base_delegation_node(hierarchy_root_id, user_3.clone(), Some(parent_id)), ); - delegation_node_2.parent = Some(delegation_id_1); - delegation_node_2.revoked = true; + delegation_node.details.revoked = true; let max_parent_checks = 0u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_3, &delegation_id_2, max_parent_checks), - Ok((false, max_parent_checks)) + Delegation::is_delegating(&user_3, &delegation_id, max_parent_checks), + Ok((false, 0)) ); }); } @@ -1409,35 +1380,35 @@ fn is_delegating_direct_revoked_max_parent_checks_value() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - let (delegation_id_2, mut delegation_node_2) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, mut delegation_node) = ( get_delegation_id(false), - generate_base_delegation_node(root_id, user_3.clone()), + generate_base_delegation_node(hierarchy_root_id, user_3.clone(), Some(parent_id)), ); - delegation_node_2.parent = Some(delegation_id_1); - delegation_node_2.revoked = true; + delegation_node.details.revoked = true; let max_parent_checks = u32::MAX; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_3, &delegation_id_2, max_parent_checks), - Ok((false, 0u32)) + Delegation::is_delegating(&user_3, &delegation_id, max_parent_checks), + Ok((false, 0)) ); }); } @@ -1451,33 +1422,33 @@ fn is_delegating_max_parent_not_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, delegation_node_1) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, user_2.clone()), + generate_base_delegation_node(hierarchy_root_id, user_2.clone(), Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, user_3, Some(parent_id)), ); - let (delegation_id_2, mut delegation_node_2) = - (get_delegation_id(false), generate_base_delegation_node(root_id, user_3)); - delegation_node_2.parent = Some(delegation_id_1); let max_parent_checks = 1u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_2, &delegation_id_2, max_parent_checks), + Delegation::is_delegating(&user_2, &delegation_id, max_parent_checks), Ok((true, max_parent_checks - 1)) ); }); @@ -1492,34 +1463,34 @@ fn is_delegating_max_parent_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = (get_delegation_root_id(true), generate_base_delegation_root(user_1)); - let (delegation_id_1, mut delegation_node_1) = ( + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, mut parent_node) = ( get_delegation_id(true), - generate_base_delegation_node(root_id, user_2.clone()), + generate_base_delegation_node(hierarchy_root_id, user_2.clone(), Some(hierarchy_root_id)), + ); + parent_node.details.revoked = true; + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, user_3, Some(parent_id)), ); - delegation_node_1.revoked = true; - let (delegation_id_2, mut delegation_node_2) = - (get_delegation_id(false), generate_base_delegation_node(root_id, user_3)); - delegation_node_2.parent = Some(delegation_id_1); let max_parent_checks = 2u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1)]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_2, &delegation_id_2, max_parent_checks), + Delegation::is_delegating(&user_2, &delegation_id, max_parent_checks), Ok((false, max_parent_checks - 2)) ); }); @@ -1534,35 +1505,34 @@ fn is_delegating_root_owner_not_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(user_1.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, user_3, Some(parent_id)), ); - let (delegation_id_1, delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - let (delegation_id_2, mut delegation_node_2) = - (get_delegation_id(false), generate_base_delegation_node(root_id, user_3)); - delegation_node_2.parent = Some(delegation_id_1); let max_parent_checks = 2u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_eq!( - Delegation::is_delegating(&user_1, &delegation_id_2, max_parent_checks), - Ok((true, max_parent_checks - 1)) + Delegation::is_delegating(&user_1, &delegation_id, max_parent_checks), + Ok((true, max_parent_checks - 2)) ); }); } @@ -1576,37 +1546,36 @@ fn is_delegating_root_owner_revoked() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, mut root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(user_1.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, user_3, Some(parent_id)), ); - root_node.revoked = true; - let (delegation_id_1, mut delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - delegation_node_1.revoked = true; - let (delegation_id_2, mut delegation_node_2) = - (get_delegation_id(false), generate_base_delegation_node(root_id, user_3)); - delegation_node_2.parent = Some(delegation_id_1); - let max_parent_checks = u32::MAX; + let max_parent_checks = 2u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { + // First revoke the hierarchy, then test is_delegating. + let _ = Delegation::revoke_delegation(get_origin(user_1.clone()), hierarchy_root_id, 0u32, 2); assert_eq!( - Delegation::is_delegating(&user_1, &delegation_id_2, max_parent_checks), - Ok((false, 1u32)) + Delegation::is_delegating(&user_1, &delegation_id, max_parent_checks), + Ok((false, 0u32)) ); }); } @@ -1616,9 +1585,9 @@ fn is_delegating_delegation_not_found() { let user_1_keypair = get_alice_ed25519(); let user_1 = get_ed25519_account(user_1_keypair.public()); - let (root_id, root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(user_1.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), ); let delegation_id = get_delegation_id(true); @@ -1626,7 +1595,7 @@ fn is_delegating_delegation_not_found() { // Root -> Delegation 1 let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1.clone())]) .build(None); ext.execute_with(|| { @@ -1646,37 +1615,34 @@ fn is_delegating_root_after_max_limit() { let user_3_keypair = get_charlie_ed25519(); let user_3 = get_ed25519_account(user_3_keypair.public()); - let (root_id, mut root_node) = ( - get_delegation_root_id(true), - generate_base_delegation_root(user_1.clone()), + let (hierarchy_root_id, hierarchy_details) = ( + get_delegation_hierarchy_id(true), + generate_base_delegation_hierarchy_details(), + ); + let (parent_id, parent_node) = ( + get_delegation_id(true), + generate_base_delegation_node(hierarchy_root_id, user_2, Some(hierarchy_root_id)), + ); + let (delegation_id, delegation_node) = ( + get_delegation_id(false), + generate_base_delegation_node(hierarchy_root_id, user_3, Some(parent_id)), ); - root_node.revoked = true; - let (delegation_id_1, mut delegation_node_1) = - (get_delegation_id(true), generate_base_delegation_node(root_id, user_2)); - delegation_node_1.revoked = true; - let (delegation_id_2, mut delegation_node_2) = - (get_delegation_id(false), generate_base_delegation_node(root_id, user_3)); - delegation_node_2.parent = Some(delegation_id_1); // 1 less than needed let max_parent_checks = 1u32; - // Root -> Delegation 1 -> Delegation 2 - let mut ext = ExtBuilder::default() - .with_root_delegations(vec![(root_id, root_node)]) - .with_delegations(vec![ - (delegation_id_1, delegation_node_1), - (delegation_id_2, delegation_node_2), - ]) - .with_children(vec![ - (root_id, vec![delegation_id_1]), - (delegation_id_1, vec![delegation_id_2]), - ]) + // Root -> Parent -> Delegation + let ext = ctype_mock::ExtBuilder::default() + .with_ctypes(vec![(hierarchy_details.ctype_hash, user_1.clone())]) .build(None); + let mut ext = ExtBuilder::default() + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, user_1.clone())]) + .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) + .build(Some(ext)); ext.execute_with(|| { assert_noop!( - Delegation::is_delegating(&user_1, &delegation_id_2, max_parent_checks), + Delegation::is_delegating(&user_1, &delegation_id, max_parent_checks), delegation::Error::::MaxSearchDepthReached ); }); diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 37542c4cd6..26858ee8e4 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -121,7 +121,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("mashnet-node"), impl_name: create_runtime_str!("mashnet-node"), authoring_version: 4, - spec_version: 16, + spec_version: 17, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, diff --git a/runtimes/peregrine/src/weights/attestation.rs b/runtimes/peregrine/src/weights/attestation.rs index 1d409b375d..ca84370d96 100644 --- a/runtimes/peregrine/src/weights/attestation.rs +++ b/runtimes/peregrine/src/weights/attestation.rs @@ -19,30 +19,22 @@ //! Autogenerated weights for attestation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-17, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-07-21, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// ./target/release/kilt-parachain +// target/release/kilt-parachain // benchmark -// --chain -// dev -// --heap-pages -// 4096 -// --extrinsic -// * -// --pallet -// attestation -// --steps -// 1 -// --repeat -// 20 -// --execution -// wasm -// --wasm-execution -// Compiled +// --chain=dev +// --execution=wasm +// --wasm-execution=Compiled +// --heap-pages=4096 +// --extrinsic=* +// --pallet=attestation +// --steps=1 +// --repeat=20 // --output -// runtimes/parachain/src/weights/attestation.rs +// runtimes/peregrine/src/weights/attestation.rs // --template // .maintain/runtime-weight-template.hbs @@ -58,16 +50,16 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl attestation::WeightInfo for WeightInfo { fn add() -> Weight { - 67_947_000_u64 + (68_138_000_u64) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn revoke(d: u32, ) -> Weight { - 44_056_000_u64 - // Standard Error: 30_000 - .saturating_add(8_178_000_u64.saturating_mul(d as Weight)) + (46_806_000_u64) + // Standard Error: 134_000 + .saturating_add((8_122_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads(1_u64.saturating_mul(d as Weight))) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) } } \ No newline at end of file diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index 358cf251e3..f77437b42c 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -19,30 +19,22 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-17, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-07-21, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// ./target/release/kilt-parachain +// target/release/kilt-parachain // benchmark -// --chain -// dev -// --heap-pages -// 4096 -// --extrinsic -// * -// --pallet -// delegation -// --steps -// 1 -// --repeat -// 20 -// --execution -// wasm -// --wasm-execution -// Compiled +// --chain=dev +// --execution=wasm +// --wasm-execution=Compiled +// --heap-pages=4096 +// --extrinsic=* +// --pallet=delegation +// --steps=1 +// --repeat=20 // --output -// runtimes/parachain/src/weights/delegation.rs +// runtimes/peregrine/src/weights/delegation.rs // --template // .maintain/runtime-weight-template.hbs @@ -57,40 +49,34 @@ use sp_std::marker::PhantomData; /// Weights for delegation using the recommended hardware. pub struct WeightInfo(PhantomData); impl delegation::WeightInfo for WeightInfo { - fn create_root() -> Weight { - 45_635_000_u64 + fn create_hierarchy() -> Weight { + (48_641_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn revoke_root(r: u32, ) -> Weight { - 48_761_000_u64 - // Standard Error: 311_000 - .saturating_add(31_784_000_u64.saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads(2_u64.saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64.saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn add_delegation() -> Weight { - 142_316_000_u64 - .saturating_add(T::DbWeight::get().reads(4_u64)) + (130_555_000_u64) + .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn revoke_delegation_root_child(r: u32, _c: u32, ) -> Weight { - 21_746_000_u64 - // Standard Error: 60_000 - .saturating_add(32_601_000_u64.saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64.saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(1_u64.saturating_mul(r as Weight))) + fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { + (24_607_000_u64) + // Standard Error: 425_000 + .saturating_add((30_915_000_u64).saturating_mul(r as Weight)) + // Standard Error: 425_000 + .saturating_add((860_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(r as Weight))) } fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - 52_521_000_u64 - // Standard Error: 45_000 - .saturating_add(93_000_u64.saturating_mul(r as Weight)) - // Standard Error: 45_000 - .saturating_add(8_110_000_u64.saturating_mul(c as Weight)) + (58_402_000_u64) + // Standard Error: 307_000 + .saturating_add((57_000_u64).saturating_mul(r as Weight)) + // Standard Error: 307_000 + .saturating_add((8_464_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads(1_u64.saturating_mul(c as Weight))) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) } } \ No newline at end of file diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index a36be7b449..871bc46ad4 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -102,7 +102,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("kilt-spiritnet"), impl_name: create_runtime_str!("kilt-spiritnet"), authoring_version: 1, - spec_version: 16, + spec_version: 17, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 069c5a8a69..88097a61d7 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("mashnet-node"), impl_name: create_runtime_str!("mashnet-node"), authoring_version: 4, - spec_version: 16, + spec_version: 17, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, From 92bda811af08e6f709fef63aa290ff2cbb5106bf Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 09:06:48 +0200 Subject: [PATCH 07/21] fix: refine code after checking the changes --- pallets/did/src/did_details.rs | 2 +- pallets/did/src/lib.rs | 6 +++--- pallets/did/src/mock.rs | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index c7a613b39f..129ce2cfe3 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -726,7 +726,7 @@ pub struct DidAuthorizedCallOperationWithVerificationRelationship { /// The wrapped [DidAuthorizedCallOperation]. pub operation: DidAuthorizedCallOperation, /// The type of DID key to use for authorization. - pub operation_authorization_key_type: DidVerificationKeyRelationship, + pub verification_key_relationship: DidVerificationKeyRelationship, } impl core::ops::Deref for DidAuthorizedCallOperationWithVerificationRelationship { diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index b4c96ab447..4b39f07626 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -555,7 +555,7 @@ pub mod pallet { // Wrap the operation in the expected structure, specifying the key retrieved let wrapped_operation = DidAuthorizedCallOperationWithVerificationRelationship { operation: *did_call, - operation_authorization_key_type: verification_key_relationship, + verification_key_relationship, }; Self::verify_did_operation_signature_and_increase_nonce(&wrapped_operation, &signature) @@ -602,10 +602,10 @@ impl Pallet { // signature is valid or not. did_details.increase_tx_counter().map_err(DidError::StorageError)?; Self::verify_payload_signature_with_did_key_type( - operation.encode().as_ref(), + &operation.encode(), signature, &did_details, - operation.operation_authorization_key_type, + operation.verification_key_relationship, )?; >::insert(&operation.did, did_details); diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 663c077045..112a0e6032 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -19,9 +19,10 @@ #![allow(clippy::from_over_into)] #![allow(unused_must_use)] +#[cfg(feature = "runtime-benchmarks")] +use frame_system::EnsureSigned; use frame_support::{parameter_types, weights::constants::RocksDbWeight}; -use frame_system::EnsureSigned; use sp_core::{ecdsa, ed25519, sr25519, Pair}; use sp_keystore::{testing::KeyStore, KeystoreExt}; use sp_runtime::{ @@ -310,7 +311,7 @@ pub(crate) fn get_delegation_key_call() -> Call { Call::Ctype(ctype::Call::add(get_delegation_key_test_input())) } pub(crate) fn get_none_key_test_input() -> TestCtypeHash { - TestCtypeHash::from_slice(&[4u8; 32]) + TestCtypeHash::from_slice(&[3u8; 32]) } pub(crate) fn get_none_key_call() -> Call { Call::Ctype(ctype::Call::add(get_none_key_test_input())) @@ -357,7 +358,7 @@ pub fn generate_test_did_call( call, tx_counter: 1u64, }, - operation_authorization_key_type: verification_key_required, + verification_key_relationship: verification_key_required, } } From cc52a6cf8a303fcdb04afc50eded1787d2833b65 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 09:18:33 +0200 Subject: [PATCH 08/21] chore: update comments to reflect latest changes --- pallets/did/src/did_details.rs | 20 +++----------------- pallets/did/src/lib.rs | 34 +++++++++++++--------------------- pallets/did/src/tests.rs | 28 +++++++--------------------- 3 files changed, 23 insertions(+), 59 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 129ce2cfe3..3b658a51d2 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -542,14 +542,10 @@ impl TryFrom<(DidCreationDetails, DidVerificationKey)> for DidDeta } // Generates a new DID entry starting from the current one stored in the -// storage and by applying the changes in the [DidUpdateOperation]. +// storage and by applying the changes in the [DidUpdateDetails]. // // The operation fails with a [DidError] if the update operation instructs to // delete a verification key that is not associated with the DID. -// -// Please note that this method does not perform any checks regarding -// the validity of the [DidUpdateOperation] signature nor whether the nonce -// provided is valid. impl TryFrom<(DidDetails, DidUpdateDetails)> for DidDetails { type Error = DidError; @@ -623,12 +619,7 @@ impl TryFrom<(DidDetails, DidUpdateDetails)> for DidDetails } } -/// An operation to create a new DID. -/// -/// The struct implements the [DidOperation] trait, and as such it must -/// contain information about the caller's DID, the type of DID key -/// required to verify the operation signature, and the tx counter to -/// protect against replay attacks. +/// The details of a new DID to create. #[derive(Clone, Debug, Decode, Encode, PartialEq)] pub struct DidCreationDetails { /// The DID identifier. It has to be unique. @@ -643,12 +634,7 @@ pub struct DidCreationDetails { pub new_endpoint_url: Option, } -/// An operation to update a DID. -/// -/// The struct implements the [DidOperation] trait, and as such it must -/// contain information about the caller's DID, the type of DID key -/// required to verify the operation signature, and the tx counter to -/// protect against replay attacks. +/// The details to update a DID. #[derive(Clone, Debug, Decode, Encode, PartialEq)] pub struct DidUpdateDetails { /// \[OPTIONAL\] The new authentication key. diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 4b39f07626..3b4a2ded7b 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -72,12 +72,11 @@ //! //! ### Dispatchable Functions //! -//! - `submit_did_create_operation` - Register a new DID on the KILT blockchain -//! under the given DID identifier. -//! - `submit_did_update_operation` - Update any keys or the endpoint URL of an -//! existing DID. -//! - `submit_did_delete_operation` - Delete the specified DID and all related -//! keys from the KILT blockchain. +//! - `create` - Register a new DID on the KILT blockchain under the given DID +//! identifier. +//! - `update` - Update any keys or the endpoint URL of an existing DID. +//! - `dekete` - Delete the specified DID and all related keys from the KILT +//! blockchain. //! - `submit_did_call` - Proxy a dispatchable function for an extrinsic that //! expects a DID origin. The DID pallet verifies the signature and the nonce //! of the wrapping operation and then dispatches the underlying extrinsic @@ -373,9 +372,7 @@ pub mod pallet { Ok(()) } - /// Update an existing DID on chain, after verifying that the update - /// operation has been signed by the DID subject using the - /// authentication key currently stored on chain. + /// Update an existing DID on chain. /// /// The referenced DID identifier must be present on chain before the /// update operation is evaluated. @@ -413,12 +410,8 @@ pub mod pallet { /// contain information about the block number when the operation is /// evaluated and executed. /// - /// A successful update operation results in the tx counter associated - /// with the given DID to be incremented, to mitigate replay attacks. - /// - /// The dispatch origin can be any KILT account with enough funds to - /// execute the extrinsic and it does not have to be tied in any way to - /// the KILT account identifying the DID subject. + /// The dispatch origin must be a DID origin proxied via the + /// `submit_did_call` extrinsic. /// /// Emits `DidUpdated`. /// @@ -465,9 +458,8 @@ pub mod pallet { /// from the storage, which results in the invalidation of all /// attestations issued by the DID subject. /// - /// The dispatch origin can be any KILT account with enough funds to - /// execute the extrinsic and it does not have to be tied in any way to - /// the KILT account identifying the DID subject. + /// The dispatch origin must be a DID origin proxied via the + /// `submit_did_call` extrinsic. /// /// Emits `DidDeleted`. /// @@ -581,8 +573,8 @@ pub mod pallet { } impl Pallet { - /// Verify the validity (i.e., nonce and signature) of a generic - /// [DidOperation] and, if valid, update the DID state with the latest + /// Verify the validity (i.e., nonce and signature) of a DID-authorized + /// operation and, if valid, update the DID state with the latest /// nonce. /// /// # @@ -613,7 +605,7 @@ impl Pallet { Ok(()) } - // Verify the validity of a DID operation nonce. + // Verify the validity of a DID-authorized operation nonce. // To be valid, the nonce must be equal to the one currently stored + 1. // This is to avoid quickly "consuming" all the possible values for the counter, // as that would result in the DID being unusable, since we do not have yet any diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 08ebb555da..d4d9bca835 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -214,9 +214,7 @@ fn check_duplicate_did_creation() { let signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(alice_did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1473,9 +1471,7 @@ fn check_smaller_counter_operation_verification() { call_operation.operation.tx_counter = 0u64; let signature = auth_key.sign(call_operation.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1500,9 +1496,7 @@ fn check_equal_counter_operation_verification() { call_operation.operation.tx_counter = mock_did.last_tx_counter; let signature = auth_key.sign(call_operation.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1527,9 +1521,7 @@ fn check_too_large_counter_operation_verification() { call_operation.operation.tx_counter = mock_did.last_tx_counter + 2; let signature = auth_key.sign(call_operation.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1552,9 +1544,7 @@ fn check_verification_key_not_present_operation_verification() { let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::AssertionMethod, did.clone()); let signature = auth_key.sign(call_operation.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1581,9 +1571,7 @@ fn check_invalid_signature_format_operation_verification() { let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did.clone()); let signature = invalid_key.sign(call_operation.encode().as_ref()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( @@ -1608,9 +1596,7 @@ fn check_invalid_signature_operation_verification() { let call_operation = generate_test_did_call(did::DidVerificationKeyRelationship::Authentication, did.clone()); let signature = alternative_key.sign(&call_operation.encode()); - let mut ext = ExtBuilder::default() - .with_dids(vec![(did, mock_did)]) - .build(None); + let mut ext = ExtBuilder::default().with_dids(vec![(did, mock_did)]).build(None); ext.execute_with(|| { assert_noop!( From fd9de5552cadbee075dcf2d24af88200cf0601d8 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 10:51:20 +0200 Subject: [PATCH 09/21] bench: update benchmarks --- pallets/did/src/benchmarking.rs | 1 + pallets/did/src/default_weights.rs | 196 +++++++++++---------- pallets/did/src/lib.rs | 35 +++- runtimes/peregrine/src/lib.rs | 3 + runtimes/peregrine/src/weights/did.rs | 236 +++++++++++++------------- runtimes/peregrine/src/weights/mod.rs | 2 +- runtimes/standalone/src/lib.rs | 3 + 7 files changed, 260 insertions(+), 216 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 71366dd103..ccda05506c 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -146,6 +146,7 @@ fn generate_base_did_call_operation(did: DidIdentifierOf) -> DidAu } } +//TODO: We might want to extract the logic about which key is the longest encoded and which key takes the longest to verify and always use that. Furthermore, update operations now only depend on the key according to its size and not the time it takes to verify a signature with it, as that happens in the `did_dispatch_call` extrinsic. benchmarks! { where_clause { where T::DidIdentifier: From, ::Origin: From>} diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index 0a518bfdcc..7a1c3d8f3e 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-22, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-08-04, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -33,10 +33,10 @@ // --pallet=did // --steps=1 // --repeat=20 -// --output -// pallets/did/src/default_weights.rs // --template // .maintain/weight-template.hbs +// --output +// pallets/did/src/default_weights.rs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -48,13 +48,13 @@ use sp_std::marker::PhantomData; /// Weight functions needed for did. pub trait WeightInfo { - fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight; - fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight; - fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight; - fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, u: u32, ) -> Weight; - fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, u: u32, ) -> Weight; - fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight; - fn submit_did_delete_operation() -> Weight; + fn create_ed25519_keys(n: u32, u: u32, ) -> Weight; + fn create_sr25519_keys(n: u32, u: u32, ) -> Weight; + fn create_ecdsa_keys(n: u32, u: u32, ) -> Weight; + fn update_ed25519_keys(n: u32, m: u32, u: u32, ) -> Weight; + fn update_sr25519_keys(n: u32, m: u32, u: u32, ) -> Weight; + fn update_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight; + fn delete() -> Weight; fn submit_did_call_ed25519_key() -> Weight; fn submit_did_call_sr25519_key() -> Weight; fn submit_did_call_ecdsa_key() -> Weight; @@ -63,79 +63,83 @@ pub trait WeightInfo { /// Weights for did using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight { - (77_831_000_u64) - // Standard Error: 42_000 - .saturating_add((1_626_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, u: u32, ) -> Weight { + (77_652_000_u64) + // Standard Error: 35_000 + .saturating_add((1_627_000_u64).saturating_mul(n as Weight)) // Standard Error: 1_000 - .saturating_add((6_000_u64).saturating_mul(u as Weight)) + .saturating_add((7_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight { - (75_106_000_u64) - // Standard Error: 41_000 - .saturating_add((1_959_000_u64).saturating_mul(n as Weight)) - // Standard Error: 1_000 - .saturating_add((23_000_u64).saturating_mul(u as Weight)) + fn create_sr25519_keys(n: u32, u: u32, ) -> Weight { + (77_617_000_u64) + // Standard Error: 43_000 + .saturating_add((1_950_000_u64).saturating_mul(n as Weight)) + // Standard Error: 2_000 + .saturating_add((2_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight { - (181_586_000_u64) - // Standard Error: 51_000 - .saturating_add((1_768_000_u64).saturating_mul(n as Weight)) - // Standard Error: 2_000 - .saturating_add((3_000_u64).saturating_mul(u as Weight)) + fn create_ecdsa_keys(n: u32, u: u32, ) -> Weight { + (182_132_000_u64) + // Standard Error: 68_000 + .saturating_add((1_606_000_u64).saturating_mul(n as Weight)) + // Standard Error: 3_000 + .saturating_add((10_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (71_793_000_u64) - // Standard Error: 39_000 - .saturating_add((3_444_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((2_574_000_u64).saturating_mul(m as Weight)) + fn update_ed25519_keys(n: u32, m: u32, u: u32, ) -> Weight { + (19_490_000_u64) + // Standard Error: 46_000 + .saturating_add((3_089_000_u64).saturating_mul(n as Weight)) + // Standard Error: 46_000 + .saturating_add((2_376_000_u64).saturating_mul(m as Weight)) + // Standard Error: 2_000 + .saturating_add((7_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (73_079_000_u64) - // Standard Error: 59_000 - .saturating_add((3_770_000_u64).saturating_mul(n as Weight)) - // Standard Error: 59_000 - .saturating_add((2_991_000_u64).saturating_mul(m as Weight)) + fn update_sr25519_keys(n: u32, m: u32, u: u32, ) -> Weight { + (24_388_000_u64) + // Standard Error: 26_000 + .saturating_add((2_472_000_u64).saturating_mul(n as Weight)) + // Standard Error: 26_000 + .saturating_add((1_873_000_u64).saturating_mul(m as Weight)) + // Standard Error: 1_000 + .saturating_add((1_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { - (170_458_000_u64) - // Standard Error: 67_000 - .saturating_add((3_849_000_u64).saturating_mul(n as Weight)) - // Standard Error: 67_000 - .saturating_add((3_102_000_u64).saturating_mul(m as Weight)) - // Standard Error: 3_000 - .saturating_add((1_000_u64).saturating_mul(u as Weight)) + fn update_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { + (20_557_000_u64) + // Standard Error: 11_000 + .saturating_add((2_816_000_u64).saturating_mul(n as Weight)) + // Standard Error: 11_000 + .saturating_add((1_874_000_u64).saturating_mul(m as Weight)) + // Standard Error: 0 + .saturating_add((3_000_u64).saturating_mul(u as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn submit_did_delete_operation() -> Weight { - (69_009_000_u64) + fn delete() -> Weight { + (17_563_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (71_504_000_u64) + (66_766_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (75_271_000_u64) + (68_679_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_543_000_u64) + (170_040_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -143,79 +147,83 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight { - (77_831_000_u64) - // Standard Error: 42_000 - .saturating_add((1_626_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, u: u32, ) -> Weight { + (77_652_000_u64) + // Standard Error: 35_000 + .saturating_add((1_627_000_u64).saturating_mul(n as Weight)) // Standard Error: 1_000 - .saturating_add((6_000_u64).saturating_mul(u as Weight)) + .saturating_add((7_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight { - (75_106_000_u64) - // Standard Error: 41_000 - .saturating_add((1_959_000_u64).saturating_mul(n as Weight)) - // Standard Error: 1_000 - .saturating_add((23_000_u64).saturating_mul(u as Weight)) + fn create_sr25519_keys(n: u32, u: u32, ) -> Weight { + (77_617_000_u64) + // Standard Error: 43_000 + .saturating_add((1_950_000_u64).saturating_mul(n as Weight)) + // Standard Error: 2_000 + .saturating_add((2_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight { - (181_586_000_u64) - // Standard Error: 51_000 - .saturating_add((1_768_000_u64).saturating_mul(n as Weight)) - // Standard Error: 2_000 - .saturating_add((3_000_u64).saturating_mul(u as Weight)) + fn create_ecdsa_keys(n: u32, u: u32, ) -> Weight { + (182_132_000_u64) + // Standard Error: 68_000 + .saturating_add((1_606_000_u64).saturating_mul(n as Weight)) + // Standard Error: 3_000 + .saturating_add((10_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (71_793_000_u64) - // Standard Error: 39_000 - .saturating_add((3_444_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((2_574_000_u64).saturating_mul(m as Weight)) + fn update_ed25519_keys(n: u32, m: u32, u: u32, ) -> Weight { + (19_490_000_u64) + // Standard Error: 46_000 + .saturating_add((3_089_000_u64).saturating_mul(n as Weight)) + // Standard Error: 46_000 + .saturating_add((2_376_000_u64).saturating_mul(m as Weight)) + // Standard Error: 2_000 + .saturating_add((7_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (73_079_000_u64) - // Standard Error: 59_000 - .saturating_add((3_770_000_u64).saturating_mul(n as Weight)) - // Standard Error: 59_000 - .saturating_add((2_991_000_u64).saturating_mul(m as Weight)) + fn update_sr25519_keys(n: u32, m: u32, u: u32, ) -> Weight { + (24_388_000_u64) + // Standard Error: 26_000 + .saturating_add((2_472_000_u64).saturating_mul(n as Weight)) + // Standard Error: 26_000 + .saturating_add((1_873_000_u64).saturating_mul(m as Weight)) + // Standard Error: 1_000 + .saturating_add((1_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { - (170_458_000_u64) - // Standard Error: 67_000 - .saturating_add((3_849_000_u64).saturating_mul(n as Weight)) - // Standard Error: 67_000 - .saturating_add((3_102_000_u64).saturating_mul(m as Weight)) - // Standard Error: 3_000 - .saturating_add((1_000_u64).saturating_mul(u as Weight)) + fn update_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { + (20_557_000_u64) + // Standard Error: 11_000 + .saturating_add((2_816_000_u64).saturating_mul(n as Weight)) + // Standard Error: 11_000 + .saturating_add((1_874_000_u64).saturating_mul(m as Weight)) + // Standard Error: 0 + .saturating_add((3_000_u64).saturating_mul(u as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn submit_did_delete_operation() -> Weight { - (69_009_000_u64) + fn delete() -> Weight { + (17_563_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (71_504_000_u64) + (66_766_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (75_271_000_u64) + (68_679_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_543_000_u64) + (170_040_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 3b4a2ded7b..ae56479bd3 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -348,7 +348,20 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Writes: Did (with K new key agreement keys) /// # - #[pallet::weight(1)] + #[pallet::weight( + ::WeightInfo::create_ed25519_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + ) + .max(::WeightInfo::create_sr25519_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + )) + .max(::WeightInfo::create_ecdsa_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + )) + )] pub fn create(origin: OriginFor, details: DidCreationDetails, signature: DidSignature) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -429,7 +442,23 @@ pub mod pallet { /// - Writes: Did (with K new key agreement keys and removing D public /// keys) /// # - #[pallet::weight(1)] + #[pallet::weight( + ::WeightInfo::update_ed25519_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + ) + .max(::WeightInfo::update_sr25519_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + )) + .max(::WeightInfo::update_ecdsa_keys( + details.new_key_agreement_keys.len().saturated_into::(), + details.public_keys_to_remove.len().saturated_into::(), + details.new_endpoint_url.as_ref().map_or(0u32, |url| url.len().saturated_into::()) + )) + )] pub fn update(origin: OriginFor, details: DidUpdateDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; @@ -468,7 +497,7 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Kills: Did entry associated to the DID identifier /// # - #[pallet::weight(1)] + #[pallet::weight(::WeightInfo::delete())] pub fn delete(origin: OriginFor) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 26858ee8e4..d945e5402a 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -693,7 +693,10 @@ impl did::Config for Runtime { type Event = Event; type Call = Call; type Origin = Origin; + #[cfg(not(feature = "runtime-benchmarks"))] type EnsureOrigin = did::EnsureDidOrigin; + #[cfg(feature = "runtime-benchmarks")] + type EnsureOrigin = EnsureSigned; type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index bd99d21f90..47f4d44b89 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -1,128 +1,128 @@ -// KILT Blockchain – https://botlabs.org -// Copyright (C) 2019-2021 BOTLabs GmbH +// // KILT Blockchain – https://botlabs.org +// // Copyright (C) 2019-2021 BOTLabs GmbH -// The KILT Blockchain is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// // The KILT Blockchain is free software: you can redistribute it and/or modify +// // it under the terms of the GNU General Public License as published by +// // the Free Software Foundation, either version 3 of the License, or +// // (at your option) any later version. -// The KILT Blockchain is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. +// // The KILT Blockchain is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU General Public License for more details. -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . +// // You should have received a copy of the GNU General Public License +// // along with this program. If not, see . -// If you feel like getting in touch with us, you can do so at info@botlabs.org +// // If you feel like getting in touch with us, you can do so at info@botlabs.org -//! Autogenerated weights for did -//! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-23, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +// //! Autogenerated weights for did +// //! +// //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +// //! DATE: 2021-06-23, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +// //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 -// Executed Command: -// target/release/kilt-parachain -// benchmark -// --chain=dev -// --execution=wasm -// --wasm-execution=Compiled -// --heap-pages=4096 -// --extrinsic=* -// --pallet=did -// --steps=1 -// --repeat=20 -// --output -// runtimes/peregrine/src/weights/did.rs -// --template -// .maintain/runtime-weight-template.hbs +// // Executed Command: +// // target/release/kilt-parachain +// // benchmark +// // --chain=dev +// // --execution=wasm +// // --wasm-execution=Compiled +// // --heap-pages=4096 +// // --extrinsic=* +// // --pallet=did +// // --steps=1 +// // --repeat=20 +// // --output +// // runtimes/peregrine/src/weights/did.rs +// // --template +// // .maintain/runtime-weight-template.hbs -#![cfg_attr(rustfmt, rustfmt_skip)] -#![allow(unused_parens)] -#![allow(unused_imports)] +// #![cfg_attr(rustfmt, rustfmt_skip)] +// #![allow(unused_parens)] +// #![allow(unused_imports)] -use frame_support::{traits::Get, weights::Weight}; -use sp_std::marker::PhantomData; +// use frame_support::{traits::Get, weights::Weight}; +// use sp_std::marker::PhantomData; -/// Weights for did using the recommended hardware. -pub struct WeightInfo(PhantomData); -impl did::WeightInfo for WeightInfo { - fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight { - (125_019_000_u64) - // Standard Error: 238_000 - .saturating_add((3_633_000_u64).saturating_mul(n as Weight)) - // Standard Error: 11_000 - .saturating_add((22_000_u64).saturating_mul(u as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight { - (135_504_000_u64) - // Standard Error: 229_000 - .saturating_add((3_162_000_u64).saturating_mul(n as Weight)) - // Standard Error: 10_000 - .saturating_add((23_000_u64).saturating_mul(u as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight { - (271_370_000_u64) - // Standard Error: 293_000 - .saturating_add((3_211_000_u64).saturating_mul(n as Weight)) - // Standard Error: 13_000 - .saturating_add((43_000_u64).saturating_mul(u as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (131_919_000_u64) - // Standard Error: 316_000 - .saturating_add((4_751_000_u64).saturating_mul(n as Weight)) - // Standard Error: 316_000 - .saturating_add((4_037_000_u64).saturating_mul(m as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { - (161_713_000_u64) - // Standard Error: 318_000 - .saturating_add((4_679_000_u64).saturating_mul(n as Weight)) - // Standard Error: 318_000 - .saturating_add((2_747_000_u64).saturating_mul(m as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { - (234_806_000_u64) - // Standard Error: 517_000 - .saturating_add((5_536_000_u64).saturating_mul(n as Weight)) - // Standard Error: 517_000 - .saturating_add((4_204_000_u64).saturating_mul(m as Weight)) - // Standard Error: 24_000 - .saturating_add((72_000_u64).saturating_mul(u as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_delete_operation() -> Weight { - (111_799_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_call_ed25519_key() -> Weight { - (117_500_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_call_sr25519_key() -> Weight { - (128_541_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - fn submit_did_call_ecdsa_key() -> Weight { - (255_939_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } -} \ No newline at end of file +// /// Weights for did using the recommended hardware. +// pub struct WeightInfo(PhantomData); +// impl did::WeightInfo for WeightInfo { +// fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight { +// (125_019_000_u64) +// // Standard Error: 238_000 +// .saturating_add((3_633_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 11_000 +// .saturating_add((22_000_u64).saturating_mul(u as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight { +// (135_504_000_u64) +// // Standard Error: 229_000 +// .saturating_add((3_162_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 10_000 +// .saturating_add((23_000_u64).saturating_mul(u as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight { +// (271_370_000_u64) +// // Standard Error: 293_000 +// .saturating_add((3_211_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 13_000 +// .saturating_add((43_000_u64).saturating_mul(u as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { +// (131_919_000_u64) +// // Standard Error: 316_000 +// .saturating_add((4_751_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 316_000 +// .saturating_add((4_037_000_u64).saturating_mul(m as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { +// (161_713_000_u64) +// // Standard Error: 318_000 +// .saturating_add((4_679_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 318_000 +// .saturating_add((2_747_000_u64).saturating_mul(m as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { +// (234_806_000_u64) +// // Standard Error: 517_000 +// .saturating_add((5_536_000_u64).saturating_mul(n as Weight)) +// // Standard Error: 517_000 +// .saturating_add((4_204_000_u64).saturating_mul(m as Weight)) +// // Standard Error: 24_000 +// .saturating_add((72_000_u64).saturating_mul(u as Weight)) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_delete_operation() -> Weight { +// (111_799_000_u64) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_call_ed25519_key() -> Weight { +// (117_500_000_u64) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_call_sr25519_key() -> Weight { +// (128_541_000_u64) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// fn submit_did_call_ecdsa_key() -> Weight { +// (255_939_000_u64) +// .saturating_add(T::DbWeight::get().reads(1_u64)) +// .saturating_add(T::DbWeight::get().writes(1_u64)) +// } +// } diff --git a/runtimes/peregrine/src/weights/mod.rs b/runtimes/peregrine/src/weights/mod.rs index 66fa4afcd9..72819c9c77 100644 --- a/runtimes/peregrine/src/weights/mod.rs +++ b/runtimes/peregrine/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -pub mod did; +// pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 88097a61d7..93b60c5e8d 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -371,7 +371,10 @@ impl did::Config for Runtime { type Event = Event; type Call = Call; type Origin = Origin; + #[cfg(not(feature = "runtime-benchmarks"))] type EnsureOrigin = did::EnsureDidOrigin; + #[cfg(feature = "runtime-benchmarks")] + type EnsureOrigin = EnsureSigned; type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; From 939c9dbfdbfe8151b10c1a312a6c29410f31b47f Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 10:55:41 +0200 Subject: [PATCH 10/21] chore: fmt --- pallets/did/src/benchmarking.rs | 6 +++++- runtimes/peregrine/src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index ccda05506c..c9d32f99d3 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -146,7 +146,11 @@ fn generate_base_did_call_operation(did: DidIdentifierOf) -> DidAu } } -//TODO: We might want to extract the logic about which key is the longest encoded and which key takes the longest to verify and always use that. Furthermore, update operations now only depend on the key according to its size and not the time it takes to verify a signature with it, as that happens in the `did_dispatch_call` extrinsic. +//TODO: We might want to extract the logic about which key is the longest +// encoded and which key takes the longest to verify and always use that. +// Furthermore, update operations now only depend on the key according to its +// size and not the time it takes to verify a signature with it, as that happens +// in the `did_dispatch_call` extrinsic. benchmarks! { where_clause { where T::DidIdentifier: From, ::Origin: From>} diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index d945e5402a..490082225e 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -700,7 +700,7 @@ impl did::Config for Runtime { type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; - type WeightInfo = weights::did::WeightInfo; + type WeightInfo = (); } /// Minimum round length is 1 hour (600 * 6 second block times) From 9360ec3848e6afb288c0c59dd4b198aaf3b21aff Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 11:35:32 +0200 Subject: [PATCH 11/21] bench: add peregrine benchmarks --- runtimes/peregrine/src/weights/did.rs | 236 +++++++++++++------------- runtimes/peregrine/src/weights/mod.rs | 2 +- 2 files changed, 119 insertions(+), 119 deletions(-) diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index 47f4d44b89..08ec0b725d 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -1,128 +1,128 @@ -// // KILT Blockchain – https://botlabs.org -// // Copyright (C) 2019-2021 BOTLabs GmbH +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2021 BOTLabs GmbH -// // The KILT Blockchain is free software: you can redistribute it and/or modify -// // it under the terms of the GNU General Public License as published by -// // the Free Software Foundation, either version 3 of the License, or -// // (at your option) any later version. +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. -// // The KILT Blockchain is distributed in the hope that it will be useful, -// // but WITHOUT ANY WARRANTY; without even the implied warranty of -// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// // GNU General Public License for more details. +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. -// // You should have received a copy of the GNU General Public License -// // along with this program. If not, see . +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . -// // If you feel like getting in touch with us, you can do so at info@botlabs.org +// If you feel like getting in touch with us, you can do so at info@botlabs.org -// //! Autogenerated weights for did -// //! -// //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -// //! DATE: 2021-06-23, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -// //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +//! Autogenerated weights for did +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +//! DATE: 2021-08-04, STEPS: `[1, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 -// // Executed Command: -// // target/release/kilt-parachain -// // benchmark -// // --chain=dev -// // --execution=wasm -// // --wasm-execution=Compiled -// // --heap-pages=4096 -// // --extrinsic=* -// // --pallet=did -// // --steps=1 -// // --repeat=20 -// // --output -// // runtimes/peregrine/src/weights/did.rs -// // --template -// // .maintain/runtime-weight-template.hbs +// Executed Command: +// target/release/kilt-parachain +// benchmark +// --chain=dev +// --execution=wasm +// --wasm-execution=Compiled +// --heap-pages=4096 +// --extrinsic=* +// --pallet=did +// --steps=1 +// --repeat=20 +// --output +// runtimes/peregrine/src/weights/did.rs +// --template +// .maintain/runtime-weight-template.hbs -// #![cfg_attr(rustfmt, rustfmt_skip)] -// #![allow(unused_parens)] -// #![allow(unused_imports)] +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] -// use frame_support::{traits::Get, weights::Weight}; -// use sp_std::marker::PhantomData; +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; -// /// Weights for did using the recommended hardware. -// pub struct WeightInfo(PhantomData); -// impl did::WeightInfo for WeightInfo { -// fn submit_did_create_operation_ed25519_keys(n: u32, u: u32, ) -> Weight { -// (125_019_000_u64) -// // Standard Error: 238_000 -// .saturating_add((3_633_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 11_000 -// .saturating_add((22_000_u64).saturating_mul(u as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_create_operation_sr25519_keys(n: u32, u: u32, ) -> Weight { -// (135_504_000_u64) -// // Standard Error: 229_000 -// .saturating_add((3_162_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 10_000 -// .saturating_add((23_000_u64).saturating_mul(u as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_create_operation_ecdsa_keys(n: u32, u: u32, ) -> Weight { -// (271_370_000_u64) -// // Standard Error: 293_000 -// .saturating_add((3_211_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 13_000 -// .saturating_add((43_000_u64).saturating_mul(u as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_update_operation_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { -// (131_919_000_u64) -// // Standard Error: 316_000 -// .saturating_add((4_751_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 316_000 -// .saturating_add((4_037_000_u64).saturating_mul(m as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_update_operation_sr25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { -// (161_713_000_u64) -// // Standard Error: 318_000 -// .saturating_add((4_679_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 318_000 -// .saturating_add((2_747_000_u64).saturating_mul(m as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_update_operation_ecdsa_keys(n: u32, m: u32, u: u32, ) -> Weight { -// (234_806_000_u64) -// // Standard Error: 517_000 -// .saturating_add((5_536_000_u64).saturating_mul(n as Weight)) -// // Standard Error: 517_000 -// .saturating_add((4_204_000_u64).saturating_mul(m as Weight)) -// // Standard Error: 24_000 -// .saturating_add((72_000_u64).saturating_mul(u as Weight)) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_delete_operation() -> Weight { -// (111_799_000_u64) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_call_ed25519_key() -> Weight { -// (117_500_000_u64) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_call_sr25519_key() -> Weight { -// (128_541_000_u64) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// fn submit_did_call_ecdsa_key() -> Weight { -// (255_939_000_u64) -// .saturating_add(T::DbWeight::get().reads(1_u64)) -// .saturating_add(T::DbWeight::get().writes(1_u64)) -// } -// } +/// Weights for did using the recommended hardware. +pub struct WeightInfo(PhantomData); +impl did::WeightInfo for WeightInfo { + fn create_ed25519_keys(n: u32, u: u32, ) -> Weight { + (125_979_000_u64) + // Standard Error: 84_000 + .saturating_add((2_372_000_u64).saturating_mul(n as Weight)) + // Standard Error: 3_000 + .saturating_add((8_000_u64).saturating_mul(u as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn create_sr25519_keys(n: u32, u: u32, ) -> Weight { + (124_731_000_u64) + // Standard Error: 57_000 + .saturating_add((2_682_000_u64).saturating_mul(n as Weight)) + // Standard Error: 2_000 + .saturating_add((13_000_u64).saturating_mul(u as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn create_ecdsa_keys(n: u32, u: u32, ) -> Weight { + (262_279_000_u64) + // Standard Error: 209_000 + .saturating_add((1_404_000_u64).saturating_mul(n as Weight)) + // Standard Error: 9_000 + .saturating_add((2_000_u64).saturating_mul(u as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn update_ed25519_keys(n: u32, m: u32, _u: u32, ) -> Weight { + (39_215_000_u64) + // Standard Error: 46_000 + .saturating_add((4_467_000_u64).saturating_mul(n as Weight)) + // Standard Error: 46_000 + .saturating_add((2_842_000_u64).saturating_mul(m as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn update_sr25519_keys(n: u32, m: u32, u: u32, ) -> Weight { + (32_203_000_u64) + // Standard Error: 145_000 + .saturating_add((4_900_000_u64).saturating_mul(n as Weight)) + // Standard Error: 145_000 + .saturating_add((3_101_000_u64).saturating_mul(m as Weight)) + // Standard Error: 6_000 + .saturating_add((18_000_u64).saturating_mul(u as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn update_ecdsa_keys(n: u32, m: u32, _u: u32, ) -> Weight { + (48_996_000_u64) + // Standard Error: 128_000 + .saturating_add((4_504_000_u64).saturating_mul(n as Weight)) + // Standard Error: 128_000 + .saturating_add((2_235_000_u64).saturating_mul(m as Weight)) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn delete() -> Weight { + (32_491_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn submit_did_call_ed25519_key() -> Weight { + (112_430_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn submit_did_call_sr25519_key() -> Weight { + (114_584_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn submit_did_call_ecdsa_key() -> Weight { + (234_789_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } +} \ No newline at end of file diff --git a/runtimes/peregrine/src/weights/mod.rs b/runtimes/peregrine/src/weights/mod.rs index 72819c9c77..66fa4afcd9 100644 --- a/runtimes/peregrine/src/weights/mod.rs +++ b/runtimes/peregrine/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -// pub mod did; +pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; From c084dd7b63bf2c5ab5bf4fe44c08ae61c07572fc Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 11:36:41 +0200 Subject: [PATCH 12/21] fix: add new DID weights to peregrine runtime --- runtimes/peregrine/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 490082225e..d945e5402a 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -700,7 +700,7 @@ impl did::Config for Runtime { type MaxNewKeyAgreementKeys = MaxNewKeyAgreementKeys; type MaxVerificationKeysToRevoke = MaxVerificationKeysToRevoke; type MaxUrlLength = MaxUrlLength; - type WeightInfo = (); + type WeightInfo = weights::did::WeightInfo; } /// Minimum round length is 1 hour (600 * 6 second block times) From 0930911d6c6be59af931828d1a2abaf4b1b98c38 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 11:51:40 +0200 Subject: [PATCH 13/21] chore: bump runtime version --- runtimes/peregrine/src/lib.rs | 2 +- runtimes/spiritnet/src/lib.rs | 2 +- runtimes/standalone/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index d945e5402a..d709cb892c 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -121,7 +121,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("mashnet-node"), impl_name: create_runtime_str!("mashnet-node"), authoring_version: 4, - spec_version: 17, + spec_version: 18, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 871bc46ad4..1a0d5b0665 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -102,7 +102,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("kilt-spiritnet"), impl_name: create_runtime_str!("kilt-spiritnet"), authoring_version: 1, - spec_version: 17, + spec_version: 18, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 93b60c5e8d..56aa463ac2 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("mashnet-node"), impl_name: create_runtime_str!("mashnet-node"), authoring_version: 4, - spec_version: 17, + spec_version: 18, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, From a6d09467307cf9ca1b4458ec0811efb3f9da0fe0 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 12:25:35 +0200 Subject: [PATCH 14/21] chore: fmt + clippy --- pallets/did/src/tests.rs | 46 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index d4d9bca835..5d2a9f30bf 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -39,7 +39,7 @@ fn check_successful_simple_ed25519_creation() { ext.execute_with(|| { assert_ok!(Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), )); }); @@ -74,7 +74,7 @@ fn check_successful_simple_sr25519_creation() { ext.execute_with(|| { assert_ok!(Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), )); }); @@ -109,7 +109,7 @@ fn check_successful_simple_ecdsa_creation() { ext.execute_with(|| { assert_ok!(Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), )); }); @@ -220,7 +220,7 @@ fn check_duplicate_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::DidAlreadyPresent @@ -245,7 +245,7 @@ fn check_invalid_signature_format_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::InvalidSignature @@ -268,7 +268,7 @@ fn check_invalid_signature_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::InvalidSignature @@ -291,7 +291,7 @@ fn check_swapped_did_subject_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::InvalidSignature @@ -316,7 +316,7 @@ fn check_max_limit_key_agreement_keys_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::MaxKeyAgreementKeysLimitExceeded @@ -341,7 +341,7 @@ fn check_url_too_long_did_creation() { assert_noop!( Did::create( Origin::signed(DEFAULT_ACCOUNT), - details.clone(), + details, did::DidSignature::from(signature), ), did::Error::::MaxUrlLengthExceeded @@ -402,7 +402,7 @@ fn check_successful_complete_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details)); }); let new_did_details = ext.execute_with(|| Did::get_did(&alice_did).expect("ALICE_DID should be present on chain.")); @@ -481,7 +481,7 @@ fn check_successful_keys_deletion_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details)); }); // Auth key and key agreement key unchanged @@ -531,7 +531,7 @@ fn check_successful_keys_overwrite_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details)); }); // Auth key unchanged @@ -587,7 +587,7 @@ fn check_successful_keys_multiuse_update() { ext.execute_with(|| { System::set_block_number(new_block_number); - assert_ok!(Did::update(Origin::signed(alice_did.clone()), details.clone(),)); + assert_ok!(Did::update(Origin::signed(alice_did.clone()), details)); }); // Auth key unchanged @@ -620,7 +620,7 @@ fn check_did_not_present_update() { ext.execute_with(|| { assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::DidNotPresent ); }); @@ -648,7 +648,7 @@ fn check_max_limit_key_agreement_keys_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::MaxKeyAgreementKeysLimitExceeded ); }); @@ -679,7 +679,7 @@ fn check_max_limit_public_keys_to_remove_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::MaxVerificationKeysToRemoveLimitExceeded ); }); @@ -709,7 +709,7 @@ fn check_url_too_long_did_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::MaxUrlLengthExceeded ); }); @@ -743,7 +743,7 @@ fn check_currently_active_authentication_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::CurrentlyActiveKey ); }); @@ -777,7 +777,7 @@ fn check_currently_active_delegation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::CurrentlyActiveKey ); }); @@ -811,7 +811,7 @@ fn check_currently_active_attestation_key_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::CurrentlyActiveKey ); }); @@ -844,7 +844,7 @@ fn check_verification_key_not_present_update() { ext.execute_with(|| { System::set_block_number(new_block_number); assert_noop!( - Did::update(Origin::signed(alice_did.clone()), details.clone(),), + Did::update(Origin::signed(alice_did.clone()), details), did::Error::::VerificationKeyNotPresent ); }); @@ -878,7 +878,7 @@ fn check_successful_deletion() { ext.execute_with(|| { assert_ok!(Did::create( Origin::signed(alice_did.clone()), - details.clone(), + details, did::DidSignature::from(signature), )); }); @@ -893,7 +893,7 @@ fn check_did_not_present_deletion() { ext.execute_with(|| { assert_noop!( - Did::delete(Origin::signed(alice_did.clone()),), + Did::delete(Origin::signed(alice_did)), did::Error::::DidNotPresent ); }); From ddab01b1b1ccde12f53d339c31c4e6cfe31bc55a Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 13:05:13 +0200 Subject: [PATCH 15/21] chore: clippy --- pallets/did/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 5d2a9f30bf..c8c52dc5d4 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -395,7 +395,7 @@ fn check_successful_complete_update() { details.new_endpoint_url = Some(new_url); let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); let new_block_number: TestBlockNumber = 1; @@ -580,7 +580,7 @@ fn check_successful_keys_multiuse_update() { let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) + .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); let new_block_number: TestBlockNumber = 1; From d4f9462145393d3718e1abfeb026033b68ad6062 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 13:29:30 +0200 Subject: [PATCH 16/21] fix: test errors --- pallets/did/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index c8c52dc5d4..803e106e4c 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -580,7 +580,7 @@ fn check_successful_keys_multiuse_update() { let _signature = auth_key.sign(details.encode().as_ref()); let mut ext = ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) .build(None); let new_block_number: TestBlockNumber = 1; From 25ba666268942490f25562b95e8778f67583b267 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 13:33:35 +0200 Subject: [PATCH 17/21] fix: typo in docs --- pallets/did/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index ae56479bd3..24eab4be70 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -75,7 +75,7 @@ //! - `create` - Register a new DID on the KILT blockchain under the given DID //! identifier. //! - `update` - Update any keys or the endpoint URL of an existing DID. -//! - `dekete` - Delete the specified DID and all related keys from the KILT +//! - `delete` - Delete the specified DID and all related keys from the KILT //! blockchain. //! - `submit_did_call` - Proxy a dispatchable function for an extrinsic that //! expects a DID origin. The DID pallet verifies the signature and the nonce From 677a827c2531efc1e80df819b201df8fca8d0334 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 14:24:13 +0200 Subject: [PATCH 18/21] chore: remove useless elements in unit tests --- pallets/did/src/tests.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 803e106e4c..d432f12e91 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -471,8 +471,6 @@ fn check_successful_keys_deletion_update() { details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; details.delegation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) .build(None); @@ -577,8 +575,6 @@ fn check_successful_keys_multiuse_update() { let mut details = generate_base_did_update_details(); details.attestation_key_update = did::DidVerificationKeyUpdateAction::Delete; - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details.clone())]) .build(None); @@ -668,8 +664,6 @@ fn check_max_limit_public_keys_to_remove_did_update() { let mut details = generate_base_did_update_details(); details.public_keys_to_remove = keys_ids_to_remove; - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); @@ -698,8 +692,6 @@ fn check_url_too_long_did_update() { let mut details = generate_base_did_update_details(); details.new_endpoint_url = Some(new_endpoint_url); - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); @@ -732,8 +724,6 @@ fn check_currently_active_authentication_key_update() { .copied() .collect::>(); - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); @@ -766,8 +756,6 @@ fn check_currently_active_delegation_key_update() { .copied() .collect::>(); - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); @@ -800,8 +788,6 @@ fn check_currently_active_attestation_key_update() { .copied() .collect::>(); - let _signature = auth_key.sign(details.encode().as_ref()); - let mut ext = ExtBuilder::default() .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None); From d8bddbf9ca78a7d0f3084ee0eac1f781755e782e Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 14:26:49 +0200 Subject: [PATCH 19/21] feat: remove TryFrom for DidDetails --- pallets/did/src/did_details.rs | 222 ++++++++++++++++----------------- pallets/did/src/lib.rs | 10 +- 2 files changed, 109 insertions(+), 123 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 3b658a51d2..8bcbf0fad7 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -281,6 +281,111 @@ impl DidDetails { } } + // Creates a new DID entry from [DidUpdateDetails] and a given authentication key. + pub fn from_creation_details((details, new_auth_key): (DidCreationDetails, DidVerificationKey)) -> Result { + ensure!( + details.new_key_agreement_keys.len() <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), + InputError::MaxKeyAgreementKeysLimitExceeded + ); + + if let Some(ref endpoint_url) = details.new_endpoint_url { + ensure!( + endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::(), + InputError::MaxUrlLengthExceeded + ); + } + + let current_block_number = >::block_number(); + + // Creates a new DID with the given authentication key. + let mut new_did_details = DidDetails::new(new_auth_key, current_block_number); + + new_did_details.add_key_agreement_keys(details.new_key_agreement_keys, current_block_number); + + if let Some(attesation_key) = details.new_attestation_key { + new_did_details.update_attestation_key(attesation_key, current_block_number); + } + + if let Some(delegation_key) = details.new_delegation_key { + new_did_details.update_delegation_key(delegation_key, current_block_number); + } + + new_did_details.endpoint_url = details.new_endpoint_url; + + Ok(new_did_details) + } + + // Updates a DID entry by applying the changes in the [DidUpdateDetails]. + // + // The operation fails with a [DidError] if the update details instructs to + // delete a verification key that is not associated with the DID. + pub fn apply_update_details(&mut self, update_details: DidUpdateDetails) -> Result<(), DidError> { + ensure!( + update_details.new_key_agreement_keys.len() + <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), + DidError::InputError(InputError::MaxKeyAgreementKeysLimitExceeded) + ); + + ensure!( + update_details.public_keys_to_remove.len() + <= <::MaxVerificationKeysToRevoke>::get().saturated_into::(), + DidError::InputError(InputError::MaxVerificationKeysToRemoveLimitExceeded) + ); + + if let Some(ref endpoint_url) = update_details.new_endpoint_url { + ensure!( + endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::(), + DidError::InputError(InputError::MaxUrlLengthExceeded) + ); + } + + let current_block_number = >::block_number(); + + // Remove specified public keys. + self + .remove_public_keys(&update_details.public_keys_to_remove) + .map_err(DidError::StorageError)?; + + // Update the authentication key, if needed. + if let Some(new_authentication_key) = update_details.new_authentication_key { + self.update_authentication_key(new_authentication_key, current_block_number); + } + + // Add any new key agreement keys. + self.add_key_agreement_keys(update_details.new_key_agreement_keys, current_block_number); + + // Update/remove the attestation key, if needed. + match update_details.attestation_key_update { + DidVerificationKeyUpdateAction::Delete => { + self.delete_attestation_key(); + } + DidVerificationKeyUpdateAction::Change(new_attestation_key) => { + self.update_attestation_key(new_attestation_key, current_block_number); + } + // Nothing happens. + DidVerificationKeyUpdateAction::Ignore => {} + } + + // Update/remove the delegation key, if needed. + match update_details.delegation_key_update { + DidVerificationKeyUpdateAction::Delete => { + self.delete_delegation_key(); + } + DidVerificationKeyUpdateAction::Change(new_delegation_key) => { + self.update_delegation_key(new_delegation_key, current_block_number); + } + // Nothing happens. + DidVerificationKeyUpdateAction::Ignore => {} + } + + // Update URL, if needed. + if let Some(new_endpoint_url) = update_details.new_endpoint_url { + self.endpoint_url = Some(new_endpoint_url); + } + + Ok(()) + } + /// Update the DID authentication key. /// /// The old key is deleted from the set of verification keys if it is @@ -502,123 +607,6 @@ impl DidDetails { } } -impl TryFrom<(DidCreationDetails, DidVerificationKey)> for DidDetails { - type Error = InputError; - - fn try_from(new_details: (DidCreationDetails, DidVerificationKey)) -> Result { - let (op, new_auth_key) = new_details; - - ensure!( - op.new_key_agreement_keys.len() <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), - InputError::MaxKeyAgreementKeysLimitExceeded - ); - - if let Some(ref endpoint_url) = op.new_endpoint_url { - ensure!( - endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::(), - InputError::MaxUrlLengthExceeded - ); - } - - let current_block_number = >::block_number(); - - // Creates a new DID with the given authentication key. - let mut new_did_details = DidDetails::new(new_auth_key, current_block_number); - - new_did_details.add_key_agreement_keys(op.new_key_agreement_keys, current_block_number); - - if let Some(attesation_key) = op.new_attestation_key { - new_did_details.update_attestation_key(attesation_key, current_block_number); - } - - if let Some(delegation_key) = op.new_delegation_key { - new_did_details.update_delegation_key(delegation_key, current_block_number); - } - - new_did_details.endpoint_url = op.new_endpoint_url; - - Ok(new_did_details) - } -} - -// Generates a new DID entry starting from the current one stored in the -// storage and by applying the changes in the [DidUpdateDetails]. -// -// The operation fails with a [DidError] if the update operation instructs to -// delete a verification key that is not associated with the DID. -impl TryFrom<(DidDetails, DidUpdateDetails)> for DidDetails { - type Error = DidError; - - fn try_from((old_details, update_operation): (DidDetails, DidUpdateDetails)) -> Result { - ensure!( - update_operation.new_key_agreement_keys.len() - <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), - DidError::InputError(InputError::MaxKeyAgreementKeysLimitExceeded) - ); - - ensure!( - update_operation.public_keys_to_remove.len() - <= <::MaxVerificationKeysToRevoke>::get().saturated_into::(), - DidError::InputError(InputError::MaxVerificationKeysToRemoveLimitExceeded) - ); - - if let Some(ref endpoint_url) = update_operation.new_endpoint_url { - ensure!( - endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::(), - DidError::InputError(InputError::MaxUrlLengthExceeded) - ); - } - - let current_block_number = >::block_number(); - - let mut new_details = old_details; - - // Remove specified public keys. - new_details - .remove_public_keys(&update_operation.public_keys_to_remove) - .map_err(DidError::StorageError)?; - - // Update the authentication key, if needed. - if let Some(new_authentication_key) = update_operation.new_authentication_key { - new_details.update_authentication_key(new_authentication_key, current_block_number); - } - - // Add any new key agreement keys. - new_details.add_key_agreement_keys(update_operation.new_key_agreement_keys, current_block_number); - - // Update/remove the attestation key, if needed. - match update_operation.attestation_key_update { - DidVerificationKeyUpdateAction::Delete => { - new_details.delete_attestation_key(); - } - DidVerificationKeyUpdateAction::Change(new_attestation_key) => { - new_details.update_attestation_key(new_attestation_key, current_block_number); - } - // Nothing happens. - DidVerificationKeyUpdateAction::Ignore => {} - } - - // Update/remove the delegation key, if needed. - match update_operation.delegation_key_update { - DidVerificationKeyUpdateAction::Delete => { - new_details.delete_delegation_key(); - } - DidVerificationKeyUpdateAction::Change(new_delegation_key) => { - new_details.update_delegation_key(new_delegation_key, current_block_number); - } - // Nothing happens. - DidVerificationKeyUpdateAction::Ignore => {} - } - - // Update URL, if needed. - if let Some(new_endpoint_url) = update_operation.new_endpoint_url { - new_details.endpoint_url = Some(new_endpoint_url); - } - - Ok(new_details) - } -} - /// The details of a new DID to create. #[derive(Clone, Debug, Decode, Encode, PartialEq)] pub struct DidCreationDetails { diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 24eab4be70..555b221c04 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -375,7 +375,7 @@ pub mod pallet { .verify_and_recover_signature(&details.encode(), &signature) .map_err(>::from)?; - let did_entry = DidDetails::try_from((details, account_did_auth_key)).map_err(>::from)?; + let did_entry = DidDetails::from_creation_details((details, account_did_auth_key)).map_err(>::from)?; log::debug!("Creating DID {:?}", &did_identifier); >::insert(&did_identifier, did_entry); @@ -462,14 +462,12 @@ pub mod pallet { pub fn update(origin: OriginFor, details: DidUpdateDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?; - let did_details = >::get(&did_subject).ok_or(>::DidNotPresent)?; + let mut did_details = >::get(&did_subject).ok_or(>::DidNotPresent)?; - // Generate a new DidDetails object by applying the changes in the update - // operation to the old object (and consuming both). - let new_did_details = DidDetails::try_from((did_details, details)).map_err(>::from)?; + did_details.apply_update_details(details).map_err(>::from)?; log::debug!("Updating DID {:?}", did_subject); - >::insert(&did_subject, new_did_details); + >::insert(&did_subject, did_details); Self::deposit_event(Event::DidUpdated(did_subject)); From 87f68d6a52a4ee07033c6183713ce0cc857cca7f Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 15:18:25 +0200 Subject: [PATCH 20/21] chore: fmt --- pallets/did/src/did_details.rs | 13 ++++++++----- pallets/did/src/lib.rs | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 8bcbf0fad7..a0748ccf15 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -281,10 +281,14 @@ impl DidDetails { } } - // Creates a new DID entry from [DidUpdateDetails] and a given authentication key. - pub fn from_creation_details((details, new_auth_key): (DidCreationDetails, DidVerificationKey)) -> Result { + // Creates a new DID entry from [DidUpdateDetails] and a given authentication + // key. + pub fn from_creation_details( + (details, new_auth_key): (DidCreationDetails, DidVerificationKey), + ) -> Result { ensure!( - details.new_key_agreement_keys.len() <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), + details.new_key_agreement_keys.len() + <= <::MaxNewKeyAgreementKeys>::get().saturated_into::(), InputError::MaxKeyAgreementKeysLimitExceeded ); @@ -342,8 +346,7 @@ impl DidDetails { let current_block_number = >::block_number(); // Remove specified public keys. - self - .remove_public_keys(&update_details.public_keys_to_remove) + self.remove_public_keys(&update_details.public_keys_to_remove) .map_err(DidError::StorageError)?; // Update the authentication key, if needed. diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 555b221c04..f1d5d8e2f1 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -375,7 +375,8 @@ pub mod pallet { .verify_and_recover_signature(&details.encode(), &signature) .map_err(>::from)?; - let did_entry = DidDetails::from_creation_details((details, account_did_auth_key)).map_err(>::from)?; + let did_entry = + DidDetails::from_creation_details((details, account_did_auth_key)).map_err(>::from)?; log::debug!("Creating DID {:?}", &did_identifier); >::insert(&did_identifier, did_entry); From 391260f57754d6072b91848e420b4487e229e5b2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 4 Aug 2021 15:37:37 +0200 Subject: [PATCH 21/21] fix: replace tuple with two parameters in from_creation_details --- pallets/did/src/did_details.rs | 3 ++- pallets/did/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index a0748ccf15..7f42b6efac 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -284,7 +284,8 @@ impl DidDetails { // Creates a new DID entry from [DidUpdateDetails] and a given authentication // key. pub fn from_creation_details( - (details, new_auth_key): (DidCreationDetails, DidVerificationKey), + details: DidCreationDetails, + new_auth_key: DidVerificationKey, ) -> Result { ensure!( details.new_key_agreement_keys.len() diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index f1d5d8e2f1..32c54f9e99 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -376,7 +376,7 @@ pub mod pallet { .map_err(>::from)?; let did_entry = - DidDetails::from_creation_details((details, account_did_auth_key)).map_err(>::from)?; + DidDetails::from_creation_details(details, account_did_auth_key).map_err(>::from)?; log::debug!("Creating DID {:?}", &did_identifier); >::insert(&did_identifier, did_entry);