Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for hash, content type, and multiple URLs as service endpoints #235

Merged
merged 20 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
292 changes: 146 additions & 146 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pallets/delegation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sp-keystore = {branch = "polkadot-v0.9.8", default-features = false, git = "http
[dependencies]
# Internal dependencies
ctype = {default-features = false, path = "../ctype", version = "0.24.0"}
kilt-primitives = {default-features = false, optional = true, path = "../../primitives"}
kilt-primitives = {default-features = false, path = "../../primitives"}

#External dependencies
bitflags = {default-features = false, version = "1.2.1"}
Expand All @@ -42,7 +42,6 @@ sp-std = {branch = "polkadot-v0.9.8", default-features = false, git = "https://g
[features]
default = ["std"]
mock = [
"kilt-primitives",
"serde",
"sp-core",
"sp-io",
Expand All @@ -68,5 +67,6 @@ std = [
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime"
"frame-support/try-runtime",
"kilt-primitives/try-runtime",
]
16 changes: 2 additions & 14 deletions pallets/delegation/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,13 @@
// If you feel like getting in touch with us, you can do so at [email protected]

use codec::{Decode, Encode};
use kilt_primitives::VersionMigratorTrait;
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<T> {
#[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 {
Expand Down Expand Up @@ -74,7 +62,7 @@ impl<T: Config> VersionMigratorTrait<T> for DelegationStorageVersion {
}
}

// It runs the righ migration logic depending on the current storage version.
// It runs the right migration logic depending on the current storage version.
fn migrate(&self) -> Weight {
match *self {
Self::V1 => v1::migrate::<T>(),
Expand Down
3 changes: 2 additions & 1 deletion pallets/did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ std = [
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime"
"frame-support/try-runtime",
"kilt-primitives/try-runtime",
]
79 changes: 46 additions & 33 deletions pallets/did/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use codec::Encode;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use kilt_primitives::AccountId;
use kilt_primitives::{AccountId, Hash};
use sp_core::{crypto::KeyTypeId, ecdsa, ed25519, sr25519};
use sp_io::crypto::{ecdsa_generate, ecdsa_sign, ed25519_generate, ed25519_sign, sr25519_generate, sr25519_sign};
use sp_runtime::{traits::IdentifyAccount, MultiSigner, SaturatedConversion};
use sp_std::{collections::btree_set::BTreeSet, convert::TryInto};
use sp_std::{collections::btree_set::BTreeSet, convert::TryInto, vec};

use crate::*;
use did_details::*;
Expand Down Expand Up @@ -101,13 +101,20 @@ fn get_ecdsa_public_delegation_key() -> ecdsa::Public {
}

// Assumes that the length of the URL is larger than 8 (length of the prefix https://)
fn get_url_endpoint(length: u32) -> Url {
fn get_service_endpoints(count: u32, length: u32) -> ServiceEndpoints {
let total_length = usize::try_from(length).expect("Failed to convert URL max length value to usize value.");
let total_count = usize::try_from(count).expect("Failed to convert max number of URLs value to usize value.");
let mut url_encoded_string = DEFAULT_URL_SCHEME.to_vec();
url_encoded_string.resize(total_length, b'0');
Url::Http(
let url = Url::Http(
HttpUrl::try_from(url_encoded_string.as_ref()).expect("Failed to create default URL with provided length."),
)
);

ServiceEndpoints {
content_hash: Hash::default(),
urls: vec![url; total_count],
content_type: ContentType::ApplicationJson,
}
}

fn get_did_base_details<T: Config>(auth_key: DidVerificationKey) -> DidDetails<T> {
Expand All @@ -120,17 +127,17 @@ fn generate_base_did_creation_details<T: Config>(did: DidIdentifierOf<T>) -> Did
new_key_agreement_keys: BTreeSet::new(),
new_attestation_key: None,
new_delegation_key: None,
new_endpoint_url: None,
new_service_endpoints: None,
}
}

fn generate_base_did_update_details<T: Config>(_did: DidIdentifierOf<T>) -> DidUpdateDetails<T> {
DidUpdateDetails {
new_authentication_key: None,
new_key_agreement_keys: BTreeSet::new(),
attestation_key_update: DidVerificationKeyUpdateAction::default(),
delegation_key_update: DidVerificationKeyUpdateAction::default(),
new_endpoint_url: None,
attestation_key_update: DidFragmentUpdateAction::default(),
delegation_key_update: DidFragmentUpdateAction::default(),
service_endpoints_update: DidFragmentUpdateAction::default(),
public_keys_to_remove: BTreeSet::new(),
}
}
Expand Down Expand Up @@ -158,6 +165,7 @@ benchmarks! {
create_ed25519_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
let u in (DEFAULT_URL_SCHEME.len().saturated_into::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let submitter: AccountIdentifierOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -166,13 +174,13 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys(n);
let did_public_att_key = get_ed25519_public_attestation_key();
let did_public_del_key = get_ed25519_public_delegation_key();
let did_endpoint = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_creation_details = generate_base_did_creation_details::<T>(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);
did_creation_details.new_service_endpoints = Some(service_endpoints);

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))
Expand Down Expand Up @@ -200,13 +208,14 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, did_creation_details.new_service_endpoints);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

create_sr25519_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
let u in (DEFAULT_URL_SCHEME.len().saturated_into::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let submitter: AccountIdentifierOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -215,13 +224,13 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys(n);
let did_public_att_key = get_sr25519_public_attestation_key();
let did_public_del_key = get_sr25519_public_delegation_key();
let did_endpoint = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_creation_details = generate_base_did_creation_details::<T>(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);
did_creation_details.new_service_endpoints = Some(service_endpoints);

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))
Expand Down Expand Up @@ -249,13 +258,14 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, did_creation_details.new_service_endpoints);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

create_ecdsa_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
let u in (DEFAULT_URL_SCHEME.len().saturated_into::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let submitter: AccountIdentifierOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -264,13 +274,13 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys(n);
let did_public_att_key = get_ecdsa_public_attestation_key();
let did_public_del_key = get_ecdsa_public_delegation_key();
let did_endpoint = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_creation_details = generate_base_did_creation_details::<T>(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);
did_creation_details.new_service_endpoints = Some(service_endpoints);

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))
Expand Down Expand Up @@ -298,14 +308,15 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_creation_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, did_creation_details.new_service_endpoints);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

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::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let did_public_auth_key = get_ed25519_public_authentication_key();
let did_subject: DidIdentifierOf<T> = MultiSigner::from(did_public_auth_key).into_account().into();
Expand All @@ -322,15 +333,15 @@ benchmarks! {
let new_did_public_del_key = get_ed25519_public_delegation_key();
// Public keys obtained are generated using the same logic as the key agreement keys, so that we are sure they do not generate KeyNotPresent errors
let public_keys_to_remove = get_public_keys::<T>(m);
let new_url = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_update_details = generate_base_did_update_details::<T>(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.attestation_key_update = DidFragmentUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key));
did_update_details.delegation_key_update = DidFragmentUpdateAction::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);
did_update_details.service_endpoints_update = DidFragmentUpdateAction::Change(service_endpoints.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(did_subject.clone()), did_update_details.clone())
Expand Down Expand Up @@ -358,13 +369,14 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, Some(service_endpoints));
}

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::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let submitter: AccountIdentifierOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -383,15 +395,15 @@ benchmarks! {
let new_did_public_del_key = get_sr25519_public_delegation_key();
// Public keys obtained are generated using the same logic as the key agreement keys, so that we are sure they do not generate KeyNotPresent errors
let public_keys_to_remove = get_public_keys::<T>(m);
let new_url = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_update_details = generate_base_did_update_details::<T>(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.attestation_key_update = DidFragmentUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key));
did_update_details.delegation_key_update = DidFragmentUpdateAction::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);
did_update_details.service_endpoints_update = DidFragmentUpdateAction::Change(service_endpoints.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(did_subject.clone()), did_update_details.clone())
Expand Down Expand Up @@ -419,13 +431,14 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, Some(service_endpoints));
}

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::<u32>()) .. T::MaxUrlLength::get();
let c in 1 .. T::MaxEndpointUrlsCount::get();

let submitter: AccountIdentifierOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -444,15 +457,15 @@ benchmarks! {
let new_did_public_del_key = get_ecdsa_public_delegation_key();
// Public keys obtained are generated using the same logic as the key agreement keys, so that we are sure they do not generate KeyNotPresent errors
let public_keys_to_remove = get_public_keys::<T>(m);
let new_url = get_url_endpoint(u);
let service_endpoints = get_service_endpoints(c, u);

let mut did_update_details = generate_base_did_update_details::<T>(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.attestation_key_update = DidFragmentUpdateAction::Change(DidVerificationKey::from(new_did_public_att_key.clone()));
did_update_details.delegation_key_update = DidFragmentUpdateAction::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);
did_update_details.service_endpoints_update = DidFragmentUpdateAction::Change(service_endpoints.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(did_subject.clone()), did_update_details.clone())
Expand Down Expand Up @@ -480,7 +493,7 @@ benchmarks! {
stored_did.get_attestation_key_id(),
&Some(expected_attestation_key_id)
);
assert_eq!(stored_did.endpoint_url, did_update_details.new_endpoint_url);
assert_eq!(stored_did.service_endpoints, Some(service_endpoints));
}

delete {
Expand Down
Loading