use crate::{Error, Keystore, Message, PublicKey, SecretKey, Signature};
use borrown::Borrown;
pub trait Signer {
type Error: From<Error> + From<<Self::Keystore as Keystore>::Error>;
type Keystore: Keystore;
fn keystore(&self) -> Result<&Self::Keystore, Self::Error>;
fn id_secret(&self, id: &<Self::Keystore as Keystore>::KeyId) -> Result<Borrown<'_, SecretKey>, Self::Error> {
let keystore = self.keystore()?;
let secret = keystore.secret(id)?.ok_or(Error::KeyNotFound)?;
Ok(secret)
}
fn id_public(&self, id: &<Self::Keystore as Keystore>::KeyId) -> Result<Borrown<'_, PublicKey>, Self::Error> {
let keystore = self.keystore()?;
let public = keystore.public(id)?.ok_or(Error::KeyNotFound)?;
Ok(public)
}
fn sign(&self, id: &<Self::Keystore as Keystore>::KeyId, message: &Message) -> Result<Signature, Self::Error> {
let secret = self.id_secret(id)?;
self.sign_with_key(secret.as_ref(), message)
}
#[cfg(not(feature = "std"))]
fn sign_with_key(&self, secret: &SecretKey, message: &Message) -> Result<Signature, Self::Error>;
#[cfg(feature = "std")]
fn sign_with_key(&self, secret: &SecretKey, message: &Message) -> Result<Signature, Self::Error> {
Ok(Signature::sign(secret, message))
}
}