use super::*;
pub use forge::crypto::signature::{Error, SignatureEncoding, Signer, Verifier};
#[derive(Clone, PartialEq)]
pub struct Signature {
bytes: Vec<u8>, }
impl<'a> TryFrom<&'a [u8]> for Signature {
type Error = OurError;
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
if value.len() != SIGNATURE_LEN {
log::error!(
"Signature is expected to be exactly {SIGNATURE_LEN} bytes, got {}",
value.len()
);
return Err(anyhow!(
"signature length mismatch, got {}, expected {SIGNATURE_LEN}",
value.len()
));
}
let bytes = value.to_vec();
Ok(Signature { bytes })
}
}
impl<'a, const N: usize> TryFrom<&'a [u8; N]> for Signature {
type Error = <Signature as TryFrom<&'a [u8]>>::Error;
fn try_from(value: &'a [u8; N]) -> Result<Self, Self::Error> {
TryFrom::<&'a [u8]>::try_from(value)
}
}
#[derive(Clone)]
pub struct SignatureBytes(Vec<u8>);
impl AsRef<[u8]> for SignatureBytes {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl TryInto<SignatureBytes> for Signature {
type Error = OurError;
fn try_into(self) -> Result<SignatureBytes, Self::Error> {
Ok(SignatureBytes(self.bytes))
}
}
impl SignatureEncoding for Signature {
type Repr = SignatureBytes;
}
pub(crate) trait VerifierWithCtx<S> {
fn verify_with_ctx(&self, msg: &[u8], signature: &S, ctx: &[u8]) -> Result<(), Error>;
}
pub(crate) trait SignerWithCtx<S> {
fn sign_with_ctx(&self, msg: &[u8], ctx: &[u8]) -> S {
self.try_sign_with_ctx(msg, ctx)
.expect("signature operation failed")
}
fn try_sign_with_ctx(&self, msg: &[u8], ctx: &[u8]) -> Result<S, Error>;
}