use ed25519_dalek::ed25519::signature::Signer as _;
use crate::{crypto::CryptoError, record::KeyRecord};
use relay_core::signed::Signed;
pub fn sign(data: &[u8], key: &ed25519_dalek::SigningKey) -> Signed {
let signature = key.sign(data);
Signed {
payload: data.to_vec(),
sig: Some(signature.to_bytes().to_vec()),
}
}
pub fn verify(signed: &Signed, record: &KeyRecord) -> Result<(), CryptoError> {
if let Some(sig_bytes) = &signed.sig {
if record.is_expired() {
return Err(CryptoError::ExpiredKeyRecord);
}
let key = record
.signing_key()
.map_err(|_| CryptoError::BadPublicSignatureKey)?;
let signature = ed25519_dalek::Signature::from_slice(sig_bytes)
.map_err(|_| CryptoError::BadSignature)?;
key.verify_strict(&signed.payload, &signature)
.map_err(|_| CryptoError::BadSignature)
} else {
Err(CryptoError::MissingSignature)
}
}