#![allow(missing_docs)]
use std::sync::Arc;
use signature::{Keypair, SignatureEncoding, Signer, Verifier};
#[derive(Clone)]
pub struct NoSignature;
impl SignatureEncoding for NoSignature {
type Repr = [u8; 0];
}
impl<'a> TryFrom<&'a [u8]> for NoSignature {
type Error = ();
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
if !value.is_empty() {
return Err(());
}
Ok(NoSignature)
}
}
impl TryInto<[u8; 0]> for NoSignature {
type Error = ();
fn try_into(self) -> Result<[u8; 0], Self::Error> {
Ok([0; 0])
}
}
pub struct NoSigningKey;
impl Signer<NoSignature> for NoSigningKey {
fn try_sign(&self, _msg: &[u8]) -> Result<NoSignature, signature::Error> {
Ok(NoSignature)
}
}
pub struct ArcSigner<T>(pub Arc<T>);
impl<S, T: Signer<S>> Signer<S> for ArcSigner<T> {
fn sign(&self, msg: &[u8]) -> S {
self.0.sign(msg)
}
fn try_sign(&self, msg: &[u8]) -> Result<S, signature::Error> {
self.0.try_sign(msg)
}
}
impl<T: Keypair> Keypair for ArcSigner<T> {
type VerifyingKey = T::VerifyingKey;
fn verifying_key(&self) -> Self::VerifyingKey {
self.0.verifying_key()
}
}
#[derive(Clone)]
pub struct NoVerifyingKey(Vec<u8>);
impl NoVerifyingKey {
pub fn new(id: usize) -> Self {
NoVerifyingKey((id as u64).to_be_bytes().into())
}
}
impl<T: Into<Vec<u8>>> From<T> for NoVerifyingKey {
fn from(value: T) -> Self {
NoVerifyingKey(value.into())
}
}
impl AsRef<[u8]> for NoVerifyingKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Verifier<NoSignature> for NoVerifyingKey {
fn verify(
&self,
_: &[u8],
_: &NoSignature,
) -> Result<(), signature::Error> {
Ok(())
}
}