use serde::{Deserialize, Serialize};
use ockam_vault::PublicKey;
use crate::change_history::ProfileChangeHistory;
use crate::{EventIdentifier, ProfileChangeEvent, ProfileIdentifier, ProfileVault};
use ockam_core::compat::vec::Vec;
use ockam_core::{allow, deny, Result};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Contact {
identifier: ProfileIdentifier,
change_history: ProfileChangeHistory,
}
impl Contact {
pub fn identifier(&self) -> &ProfileIdentifier {
&self.identifier
}
pub fn change_events(&self) -> &[ProfileChangeEvent] {
self.change_history.as_ref()
}
}
impl Contact {
pub fn new(identifier: ProfileIdentifier, change_events: Vec<ProfileChangeEvent>) -> Self {
Contact {
identifier,
change_history: ProfileChangeHistory::new(change_events),
}
}
}
impl Contact {
pub async fn verify(&self, vault: &mut impl ProfileVault) -> Result<bool> {
if !ProfileChangeHistory::check_consistency(&[], self.change_events()) {
return deny();
}
if !self
.change_history
.verify_all_existing_events(vault)
.await?
{
return deny();
}
let root_public_key = self.change_history.get_first_root_public_key()?;
let root_key_id = vault
.compute_key_id_for_public_key(&root_public_key)
.await?;
let profile_id = ProfileIdentifier::from_key_id(root_key_id);
if &profile_id != self.identifier() {
return deny(); }
allow()
}
pub async fn verify_and_update<C: AsRef<[ProfileChangeEvent]>>(
&mut self,
change_events: C,
vault: &mut impl ProfileVault,
) -> Result<bool> {
if !ProfileChangeHistory::check_consistency(self.change_events(), change_events.as_ref()) {
return deny();
}
for event in change_events.as_ref().iter() {
if !ProfileChangeHistory::verify_event(self.change_events(), event, vault).await? {
return deny();
}
self.change_history.push_event(event.clone());
}
allow()
}
}
impl Contact {
pub fn get_profile_update_public_key(&self) -> Result<PublicKey> {
ProfileChangeHistory::get_current_root_public_key(self.change_events())
}
pub fn get_public_key(&self, label: &str) -> Result<PublicKey> {
self.change_history.get_public_key(label)
}
pub fn get_last_event_id(&self) -> Result<EventIdentifier> {
self.change_history.get_last_event_id()
}
#[cfg(feature = "credentials")]
pub fn get_signing_public_key(&self) -> Result<PublicKey> {
use crate::Profile;
self.get_public_key(Profile::CREDENTIALS_ISSUE)
}
}