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: refactor complete
  • Loading branch information
ntn-x2 committed Aug 3, 2021
commit 99ba36229d567a0ec83bbd06a22e690c1eafe5fb
330 changes: 165 additions & 165 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 7 additions & 20 deletions pallets/did/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,15 @@ fn generate_base_did_creation_operation<T: Config>() -> DidCreationOperation {

fn generate_base_did_update_operation<T: Config>(did: DidIdentifierOf<T>) -> DidUpdateOperation<T> {
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<T: Config>(did: DidIdentifierOf<T>) -> DidDeletionOperation<T> {
DidDeletionOperation { did, tx_counter: 1u64 }
}

// Must always be dispatched with the DID authentication key
fn generate_base_did_call_operation<T: Config>(did: DidIdentifierOf<T>) -> DidAuthorizedCallOperation<T> {
let test_call = <T as Config>::Call::get_call_for_did_call_benchmark();
Expand Down Expand Up @@ -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::<T>::get(&did_subject).expect("DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids();
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<T>::get(&did_subject).expect("DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids();
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<T>::get(&did_subject).expect("DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.get_key_agreement_keys_ids();
Expand All @@ -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 {
Expand All @@ -496,11 +487,7 @@ benchmarks! {

let did_details = get_did_base_details(DidVerificationKey::from(did_public_auth_key));
Did::<T>::insert(&did_subject, did_details);

let did_deletion_op = generate_base_did_deletion_operation::<T>(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::<T>::get(&did_subject),
Expand All @@ -520,7 +507,7 @@ benchmarks! {
let did_call_op = generate_base_did_call_operation::<T>(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<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);
Expand All @@ -534,7 +521,7 @@ benchmarks! {
let did_call_op = generate_base_did_call_operation::<T>(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<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);
Expand All @@ -548,7 +535,7 @@ benchmarks! {
let did_call_op = generate_base_did_call_operation::<T>(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! {
Expand Down
2 changes: 2 additions & 0 deletions pallets/did/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions pallets/did/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -296,6 +298,7 @@ pub mod pallet {
SignatureError::InvalidSignature => Self::InvalidSignature,
SignatureError::InvalidSignatureFormat => Self::InvalidSignatureFormat,
SignatureError::InvalidNonce => Self::InvalidNonce,
SignatureError::SignatureRequired => Self::SignatureRequired,
}
}
}
Expand Down Expand Up @@ -579,7 +582,7 @@ pub mod pallet {
pub fn submit_did_call(
origin: OriginFor<T>,
did_call: Box<DidAuthorizedCallOperation<T>>,
signature: DidSignature,
signature: Option<DidSignature>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;

Expand Down Expand Up @@ -632,7 +635,7 @@ impl<T: Config> Pallet<T> {
/// # </weight>
pub fn verify_did_operation_signature_and_increase_nonce(
operation: &DidAuthorizedCallOperationWithVerificationRelationship<T>,
signature: &DidSignature,
signature: &Option<DidSignature>,
) -> Result<(), DidError> {
let mut did_details =
<Did<T>>::get(&operation.did).ok_or(DidError::StorageError(StorageError::DidNotPresent))?;
Expand All @@ -641,6 +644,7 @@ impl<T: Config> Pallet<T> {
// 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)?;
Expand Down
37 changes: 15 additions & 22 deletions pallets/did/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
}
Expand All @@ -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<Test> {
) -> did::DidAuthorizedCallOperationWithVerificationRelationship<Test> {
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();
Expand Down
Loading