use bytes::Bytes;
use core::fmt::Debug;
use thiserror::Error;
pub type InitializationVector = [u8; 12];
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct EncryptedPayload {
pub payload: Bytes,
pub iv: InitializationVector,
pub key_index: u8,
}
#[derive(Debug, Error)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
pub enum EncryptionError {
#[error("Encryption failed")]
Failed,
}
#[derive(Debug, Error)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
pub enum DecryptionError {
#[error("Decryption failed")]
Failed,
}
#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
pub trait EncryptionProvider: Send + Sync + Debug {
fn encrypt(&self, payload: Bytes) -> Result<EncryptedPayload, EncryptionError>;
}
#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
pub trait DecryptionProvider: Send + Sync + Debug {
fn decrypt(
&self,
payload: EncryptedPayload,
sender_identity: String,
) -> Result<Bytes, DecryptionError>;
}
#[cfg(feature = "uniffi")]
uniffi::custom_type!(Bytes, Vec<u8>, { remote });
#[cfg(feature = "uniffi")]
uniffi::custom_type!(InitializationVector, Vec<u8>, {
remote,
lower: |iv| iv.to_vec(),
try_lift: |v| v.try_into()
.map_err(|_| uniffi::deps::anyhow::anyhow!("IV must be exactly 12 bytes"))
});