Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cd2efcb
test: first unit tests passing after refactor
ntn-x2 Aug 2, 2021
99ba362
feat: refactor complete
ntn-x2 Aug 3, 2021
ce9ade2
feat: revert DID creation to old version and update test cases
ntn-x2 Aug 3, 2021
7752137
fix: update test cases and benchmarks
ntn-x2 Aug 4, 2021
92cc170
wip: comment out current weights
ntn-x2 Aug 4, 2021
64dc85b
feat: delegation pallet storage refactor (#226)
ntn-x2 Aug 3, 2021
92bda81
fix: refine code after checking the changes
ntn-x2 Aug 4, 2021
cc52a6c
chore: update comments to reflect latest changes
ntn-x2 Aug 4, 2021
fd9de55
bench: update benchmarks
ntn-x2 Aug 4, 2021
939c9db
chore: fmt
ntn-x2 Aug 4, 2021
9360ec3
bench: add peregrine benchmarks
ntn-x2 Aug 4, 2021
c084dd7
fix: add new DID weights to peregrine runtime
ntn-x2 Aug 4, 2021
2dc0e97
Merge branch 'develop' into aa-did-pallet-origin
ntn-x2 Aug 4, 2021
0930911
chore: bump runtime version
ntn-x2 Aug 4, 2021
d415ee1
merge develop branch
ntn-x2 Aug 4, 2021
a6d0946
chore: fmt + clippy
ntn-x2 Aug 4, 2021
ddab01b
chore: clippy
ntn-x2 Aug 4, 2021
d4f9462
fix: test errors
ntn-x2 Aug 4, 2021
64f36ec
Merge remote-tracking branch 'origin/develop' into aa-did-pallet-origin
ntn-x2 Aug 4, 2021
25ba666
fix: typo in docs
ntn-x2 Aug 4, 2021
677a827
chore: remove useless elements in unit tests
ntn-x2 Aug 4, 2021
d8bddbf
feat: remove TryFrom for DidDetails
ntn-x2 Aug 4, 2021
87f68d6
chore: fmt
ntn-x2 Aug 4, 2021
9b1df9f
Merge remote-tracking branch 'origin/develop' into aa-did-pallet-origin
ntn-x2 Aug 4, 2021
391260f
fix: replace tuple with two parameters in from_creation_details
ntn-x2 Aug 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: remove TryFrom for DidDetails
  • Loading branch information
ntn-x2 committed Aug 4, 2021
commit d8bddbf9ca78a7d0f3084ee0eac1f781755e782e
222 changes: 105 additions & 117 deletions pallets/did/src/did_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,111 @@ impl<T: Config> DidDetails<T> {
}
}

// Creates a new DID entry from [DidUpdateDetails] and a given authentication key.
pub fn from_creation_details((details, new_auth_key): (DidCreationDetails<T>, DidVerificationKey)) -> Result<Self, InputError> {
ensure!(
details.new_key_agreement_keys.len() <= <<T as Config>::MaxNewKeyAgreementKeys>::get().saturated_into::<usize>(),
InputError::MaxKeyAgreementKeysLimitExceeded
);

if let Some(ref endpoint_url) = details.new_endpoint_url {
ensure!(
endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::<usize>(),
InputError::MaxUrlLengthExceeded
);
}

let current_block_number = <frame_system::Pallet<T>>::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<T>) -> Result<(), DidError> {
ensure!(
update_details.new_key_agreement_keys.len()
<= <<T as Config>::MaxNewKeyAgreementKeys>::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxKeyAgreementKeysLimitExceeded)
);

ensure!(
update_details.public_keys_to_remove.len()
<= <<T as Config>::MaxVerificationKeysToRevoke>::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxVerificationKeysToRemoveLimitExceeded)
);

if let Some(ref endpoint_url) = update_details.new_endpoint_url {
ensure!(
endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxUrlLengthExceeded)
);
}

let current_block_number = <frame_system::Pallet<T>>::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
Expand Down Expand Up @@ -502,123 +607,6 @@ impl<T: Config> DidDetails<T> {
}
}

impl<T: Config> TryFrom<(DidCreationDetails<T>, DidVerificationKey)> for DidDetails<T> {
type Error = InputError;

fn try_from(new_details: (DidCreationDetails<T>, DidVerificationKey)) -> Result<Self, Self::Error> {
let (op, new_auth_key) = new_details;

ensure!(
op.new_key_agreement_keys.len() <= <<T as Config>::MaxNewKeyAgreementKeys>::get().saturated_into::<usize>(),
InputError::MaxKeyAgreementKeysLimitExceeded
);

if let Some(ref endpoint_url) = op.new_endpoint_url {
ensure!(
endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::<usize>(),
InputError::MaxUrlLengthExceeded
);
}

let current_block_number = <frame_system::Pallet<T>>::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<T: Config> TryFrom<(DidDetails<T>, DidUpdateDetails<T>)> for DidDetails<T> {
type Error = DidError;

fn try_from((old_details, update_operation): (DidDetails<T>, DidUpdateDetails<T>)) -> Result<Self, Self::Error> {
ensure!(
update_operation.new_key_agreement_keys.len()
<= <<T as Config>::MaxNewKeyAgreementKeys>::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxKeyAgreementKeysLimitExceeded)
);

ensure!(
update_operation.public_keys_to_remove.len()
<= <<T as Config>::MaxVerificationKeysToRevoke>::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxVerificationKeysToRemoveLimitExceeded)
);

if let Some(ref endpoint_url) = update_operation.new_endpoint_url {
ensure!(
endpoint_url.len() <= T::MaxUrlLength::get().saturated_into::<usize>(),
DidError::InputError(InputError::MaxUrlLengthExceeded)
);
}

let current_block_number = <frame_system::Pallet<T>>::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<T: Config> {
Expand Down
10 changes: 4 additions & 6 deletions pallets/did/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ pub mod pallet {
.verify_and_recover_signature(&details.encode(), &signature)
.map_err(<Error<T>>::from)?;

let did_entry = DidDetails::try_from((details, account_did_auth_key)).map_err(<Error<T>>::from)?;
let did_entry = DidDetails::from_creation_details((details, account_did_auth_key)).map_err(<Error<T>>::from)?;

log::debug!("Creating DID {:?}", &did_identifier);
<Did<T>>::insert(&did_identifier, did_entry);
Expand Down Expand Up @@ -462,14 +462,12 @@ pub mod pallet {
pub fn update(origin: OriginFor<T>, details: DidUpdateDetails<T>) -> DispatchResult {
let did_subject = T::EnsureOrigin::ensure_origin(origin)?;

let did_details = <Did<T>>::get(&did_subject).ok_or(<Error<T>>::DidNotPresent)?;
let mut did_details = <Did<T>>::get(&did_subject).ok_or(<Error<T>>::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(<Error<T>>::from)?;
did_details.apply_update_details(details).map_err(<Error<T>>::from)?;

log::debug!("Updating DID {:?}", did_subject);
<Did<T>>::insert(&did_subject, new_did_details);
<Did<T>>::insert(&did_subject, did_details);

Self::deposit_event(Event::DidUpdated(did_subject));

Expand Down