use serde::{de::DeserializeOwned, Serialize};
use std::{fmt::Debug, hash::Hash};
pub trait PublicId: Clone + Eq + Ord + Hash + Serialize + DeserializeOwned + Debug {
type Signature: Clone + Eq + Ord + Hash + Serialize + DeserializeOwned + Debug;
fn verify_signature(&self, signature: &Self::Signature, data: &[u8]) -> bool;
}
pub trait SecretId {
type PublicId: PublicId;
fn public_id(&self) -> &Self::PublicId;
fn sign_detached(&self, data: &[u8]) -> <Self::PublicId as PublicId>::Signature;
fn create_proof(&self, data: &[u8]) -> Proof<Self::PublicId> {
Proof {
public_id: self.public_id().clone(),
signature: self.sign_detached(data),
}
}
fn encrypt<M: AsRef<[u8]>>(&self, to: &Self::PublicId, msg: M) -> Option<Vec<u8>>;
fn decrypt(&self, from: &Self::PublicId, ct: &[u8]) -> Option<Vec<u8>>;
}
#[serde(bound = "")]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Debug)]
pub struct Proof<P: PublicId> {
pub(super) public_id: P,
pub(super) signature: P::Signature,
}
impl<P: PublicId> Proof<P> {
pub fn public_id(&self) -> &P {
&self.public_id
}
pub fn signature(&self) -> &P::Signature {
&self.signature
}
pub fn is_valid(&self, data: &[u8]) -> bool {
self.public_id.verify_signature(&self.signature, data)
}
}