use serde::{Deserialize, Serialize};
use p256::ecdsa::{signature::Verifier, VerifyingKey};
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub enum NotaryPublicKey {
P256(p256::PublicKey),
}
impl From<p256::PublicKey> for NotaryPublicKey {
fn from(key: p256::PublicKey) -> Self {
Self::P256(key)
}
}
#[derive(Debug, thiserror::Error)]
#[error("signature verification failed: {0}")]
pub struct SignatureVerifyError(String);
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub enum Signature {
P256(p256::ecdsa::Signature),
}
impl From<p256::ecdsa::Signature> for Signature {
fn from(sig: p256::ecdsa::Signature) -> Self {
Self::P256(sig)
}
}
impl Signature {
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Self::P256(sig) => sig.to_vec(),
}
}
pub fn verify(
&self,
msg: &[u8],
notary_public_key: impl Into<NotaryPublicKey>,
) -> Result<(), SignatureVerifyError> {
match (self, notary_public_key.into()) {
(Self::P256(sig), NotaryPublicKey::P256(key)) => VerifyingKey::from(key)
.verify(msg, sig)
.map_err(|e| SignatureVerifyError(e.to_string())),
}
}
}