use crate::{ed25519::PublicKey, object, Client};
use signature::Error;
pub struct Signer {
client: Client,
signing_key_id: object::Id,
public_key: PublicKey,
}
impl Signer {
pub fn create(client: Client, signing_key_id: object::Id) -> Result<Self, Error> {
let public_key = client
.get_public_key(signing_key_id)?
.ed25519()
.ok_or_else(Error::new)?;
Ok(Self {
client,
signing_key_id,
public_key,
})
}
pub fn public_key(&self) -> &PublicKey {
&self.public_key
}
}
impl From<&Signer> for PublicKey {
fn from(signer: &Signer) -> PublicKey {
signer.public_key
}
}
impl signature::Signer<ed25519::Signature> for Signer {
fn try_sign(&self, msg: &[u8]) -> Result<ed25519::Signature, Error> {
Ok(self.client.sign_ed25519(self.signing_key_id, msg)?)
}
}