use frame_support::storage::bounded_btree_set::BoundedBTreeSet;
use frame_system::pallet_prelude::BlockNumberFor;
use sp_runtime::{AccountId32, SaturatedConversion};
use sp_std::{
collections::btree_set::BTreeSet,
convert::{TryFrom, TryInto},
vec,
vec::Vec,
};
use crate::{
did_details::{DidCreationDetails, DidDetails, DidEncryptionKey, DidNewKeyAgreementKeySet, DidVerificationKey},
service_endpoints::DidEndpoint,
AccountIdOf, Config, DidCreationDetailsOf, DidIdentifierOf,
};
pub(crate) type DidNewKeyAgreementKeySetOf<T> = DidNewKeyAgreementKeySet<<T as Config>::MaxNewKeyAgreementKeys>;
pub fn get_key_agreement_keys<T: Config>(n_keys: u32) -> DidNewKeyAgreementKeySetOf<T> {
BoundedBTreeSet::try_from(
(1..=n_keys)
.map(|i| {
let mut seed_vec = i.to_be_bytes().to_vec();
seed_vec.resize(32, 0u8);
let seed: [u8; 32] = seed_vec
.try_into()
.expect("Failed to create encryption key from raw seed.");
DidEncryptionKey::X25519(seed)
})
.collect::<BTreeSet<DidEncryptionKey>>(),
)
.expect("Failed to convert key_agreement_keys to BoundedBTreeSet")
}
pub fn get_service_endpoints<T: Config>(
count: u32,
endpoint_id_length: u32,
endpoint_type_count: u32,
endpoint_type_length: u32,
endpoint_url_count: u32,
endpoint_url_length: u32,
) -> Vec<DidEndpoint<T>> {
(0..count)
.map(|i| {
let endpoint_id = vec![b'a' + i as u8; endpoint_id_length.saturated_into()];
let endpoint_types = (0..endpoint_type_count)
.map(|t| {
let mut endpoint_type = t.to_be_bytes().to_vec();
endpoint_type.resize(endpoint_type_length.saturated_into(), 0u8);
endpoint_type
})
.collect();
let endpoint_urls = (0..endpoint_url_count)
.map(|u| {
vec![b'a' + u as u8; endpoint_url_length.saturated_into()]
})
.collect();
DidEndpoint::new(endpoint_id, endpoint_types, endpoint_urls)
})
.collect()
}
pub fn generate_base_did_creation_details<T: Config>(
did: DidIdentifierOf<T>,
submitter: AccountIdOf<T>,
) -> DidCreationDetailsOf<T> {
DidCreationDetails {
did,
submitter,
new_key_agreement_keys: BoundedBTreeSet::new(),
new_attestation_key: None,
new_delegation_key: None,
new_service_details: Vec::new(),
}
}
pub fn generate_base_did_details<T>(
authentication_key: DidVerificationKey<AccountIdOf<T>>,
deposit_owner: Option<AccountIdOf<T>>,
) -> DidDetails<T>
where
T: Config,
<T as frame_system::Config>::AccountId: From<AccountId32>,
{
DidDetails::new(
authentication_key,
BlockNumberFor::<T>::default(),
deposit_owner.unwrap_or(AccountId32::new([0u8; 32]).into()),
)
.expect("Failed to generate new DidDetails from auth_key due to BoundedBTreeSet bound")
}