use crypto_core::Algorithm;
use secrecy::{ExposeSecret, SecretBox};
use zeroize::Zeroizing;
use crate::{Signer, SignerError, SignerFailureKind};
#[derive(Debug)]
pub struct DispatchSigner {
alg: Algorithm,
private_key: SecretBox<Zeroizing<Vec<u8>>>,
}
impl DispatchSigner {
pub fn new(alg: Algorithm, private_key: impl Into<Zeroizing<Vec<u8>>>) -> Self {
Self {
alg,
private_key: SecretBox::new(Box::new(private_key.into())),
}
}
}
impl Signer for DispatchSigner {
fn alg(&self) -> Algorithm {
self.alg
}
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignerError> {
crypto_dispatch::sign(
self.alg,
self.private_key.expose_secret().as_slice(),
message,
)
.map_err(|source| SignerError::SignFailed {
algorithm: self.alg,
kind: SignerFailureKind::DispatchRejected,
source,
})
}
}