pub(crate) fn verify_event(
        &self,
        change_event: &ProfileChangeEvent,
        vault: &mut dyn ProfileVault,
    ) -> ockam_core::Result<()> {
        let changes = change_event.changes();
        let changes_binary = serde_bare::to_vec(&changes).map_err(|_| OckamError::BareError)?;

        let event_id = vault.sha256(&changes_binary)?;
        let event_id = EventIdentifier::from_hash(event_id);

        if &event_id != change_event.identifier() {
            return Err(OckamError::EventIdDoesntMatch.into());
        }

        match change_event.proof() {
            ProfileChangeProof::Signature(s) => match s.stype() {
                SignatureType::RootSign => {
                    let root_public_key = self.get_root_public_key()?;
                    vault.verify(s.data(), root_public_key.as_ref(), event_id.as_ref())?;
                }
            },
        }

        for change in change_event.changes().data() {
            if !match change.change_type() {
                CreateKey(c) => {
                    // Should have 1 self signature
                    let data_binary =
                        serde_bare::to_vec(c.data()).map_err(|_| OckamError::BareError)?;
                    let data_hash = vault.sha256(data_binary.as_slice())?;

                    vault
                        .verify(c.self_signature(), c.data().public_key(), &data_hash)
                        .is_ok()
                }
                RotateKey(c) => {
                    // Should have 1 self signature and 1 prev signature
                    let data_binary =
                        serde_bare::to_vec(c.data()).map_err(|_| OckamError::BareError)?;
                    let data_hash = vault.sha256(data_binary.as_slice())?;

                    if !vault
                        .verify(c.self_signature(), c.data().public_key(), &data_hash)
                        .is_ok()
                    {
                        false;
                    }

                    let prev_key_event =
                        self.find_key_event_before(&event_id, c.data().key_attributes())?;
                    let prev_key_change = ProfileChangeHistory::find_key_change_in_event(
                        prev_key_event,
                        c.data().key_attributes(),
                    )
                    .ok_or_else(|| OckamError::InvalidInternalState)?;
                    let public_key = ProfileChangeHistory::get_change_public_key(prev_key_change)?;

                    vault
                        .verify(c.prev_signature(), public_key.as_ref(), &data_hash)
                        .is_ok()
                }
            } {
                return Err(OckamError::VerifyFailed.into());
            }
        }

        Ok(())
    }
/// WARNING: This function assumes all existing events in chain are verified.
/// WARNING: Correctness of events sequence is not verified here.
This function assumes all existing events in chain are verified.
WARNING: Correctness of events sequence is not verified here.
This function assumes all existing events in chain are verified . WARNING : Correctness of events sequence is not verified here .
pub(crate) fn verify_event(
        &self,
        change_event: &ProfileChangeEvent,
        vault: &mut dyn ProfileVault,
    ) -> ockam_core::Result<()> {
        let changes = change_event.changes();
        let changes_binary = serde_bare::to_vec(&changes).map_err(|_| OckamError::BareError)?;
        let event_id = vault.sha256(&changes_binary)?;
        let event_id = EventIdentifier::from_hash(event_id);
        if &event_id != change_event.identifier() {
            return Err(OckamError::EventIdDoesntMatch.into());
        }
        match change_event.proof() {
            ProfileChangeProof::Signature(s) => match s.stype() {
                SignatureType::RootSign => {
                    let root_public_key = self.get_root_public_key()?;
                    vault.verify(s.data(), root_public_key.as_ref(), event_id.as_ref())?;
                }
            },
        }
        for change in change_event.changes().data() {
            if !match change.change_type() {
                CreateKey(c) => {
                    // Should have 1 self signature
                    let data_binary =
                        serde_bare::to_vec(c.data()).map_err(|_| OckamError::BareError)?;
                    let data_hash = vault.sha256(data_binary.as_slice())?;
                    vault
                        .verify(c.self_signature(), c.data().public_key(), &data_hash)
                        .is_ok()
                }
                RotateKey(c) => {
                    // Should have 1 self signature and 1 prev signature
                    let data_binary =
                        serde_bare::to_vec(c.data()).map_err(|_| OckamError::BareError)?;
                    let data_hash = vault.sha256(data_binary.as_slice())?;
                    if !vault
                        .verify(c.self_signature(), c.data().public_key(), &data_hash)
                        .is_ok()
                    {
                        false;
                    }
                    let prev_key_event =
                        self.find_key_event_before(&event_id, c.data().key_attributes())?;
                    let prev_key_change = ProfileChangeHistory::find_key_change_in_event(
                        prev_key_event,
                        c.data().key_attributes(),
                    )
                    .ok_or_else(|| OckamError::InvalidInternalState)?;
                    let public_key = ProfileChangeHistory::get_change_public_key(prev_key_change)?;
                    vault
                        .verify(c.prev_signature(), public_key.as_ref(), &data_hash)
                        .is_ok()
                }
            } {
                return Err(OckamError::VerifyFailed.into());
            }
        }
        Ok(())
    }
pub ( crate ) fn verify_event ( & self , change_event : & ProfileChangeEvent , vault : & mut dyn ProfileVault , ) -> ockam_core :: Result < ( ) > { let changes = change_event . changes ( ) ; let changes_binary = serde_bare :: to_vec ( & changes ) . map_err ( | _ | OckamError :: BareError ) ? ; let event_id = vault . sha256 ( & changes_binary ) ? ; let event_id = EventIdentifier :: from_hash ( event_id ) ; if & event_id != change_event . identifier ( ) { return Err ( OckamError :: EventIdDoesntMatch . into ( ) ) ; } match change_event . proof ( ) { ProfileChangeProof :: Signature ( s ) => match s . stype ( ) { SignatureType :: RootSign => { let root_public_key = self . get_root_public_key ( ) ? ; vault . verify ( s . data ( ) , root_public_key . as_ref ( ) , event_id . as_ref ( ) ) ? ; } } , } for change in change_event . changes ( ) . data ( ) { if ! match change . change_type ( ) { CreateKey ( c ) => { // Should have 1 self signature let data_binary = serde_bare :: to_vec ( c . data ( ) ) . map_err ( | _ | OckamError :: BareError ) ? ; let data_hash = vault . sha256 ( data_binary . as_slice ( ) ) ? ; vault . verify ( c . self_signature ( ) , c . data ( ) . public_key ( ) , & data_hash ) . is_ok ( ) } RotateKey ( c ) => { // Should have 1 self signature and 1 prev signature let data_binary = serde_bare :: to_vec ( c . data ( ) ) . map_err ( | _ | OckamError :: BareError ) ? ; let data_hash = vault . sha256 ( data_binary . as_slice ( ) ) ? ; if ! vault . verify ( c . self_signature ( ) , c . data ( ) . public_key ( ) , & data_hash ) . is_ok ( ) { false ; } let prev_key_event = self . find_key_event_before ( & event_id , c . data ( ) . key_attributes ( ) ) ? ; let prev_key_change = ProfileChangeHistory :: find_key_change_in_event ( prev_key_event , c . data ( ) . key_attributes ( ) , ) . ok_or_else ( | | OckamError :: InvalidInternalState ) ? ; let public_key = ProfileChangeHistory :: get_change_public_key ( prev_key_change ) ? ; vault . verify ( c . prev_signature ( ) , public_key . as_ref ( ) , & data_hash ) . is_ok ( ) } } { return Err ( OckamError :: VerifyFailed . into ( ) ) ; } } Ok ( ( ) ) }
WARNING: This function assumes all existing events in chain are verified.
WARNING : This function assumes all existing events in chain are verified .
&self
change_event
&ProfileChangeEvent
vault
&mut dyn ProfileVault
&self
change_event
&ProfileChangeEvent
vault
&mut dyn ProfileVault