diff --git a/crates/radicle-crypto/src/lib.rs b/crates/radicle-crypto/src/lib.rs index 16ffc801..d72b3628 100644 --- a/crates/radicle-crypto/src/lib.rs +++ b/crates/radicle-crypto/src/lib.rs @@ -42,30 +42,19 @@ impl SignerError { } } -pub trait Signer: Send { +pub trait Signer: Send + signature::Signer { /// Return this signer's public/verification key. fn public_key(&self) -> &PublicKey; - /// Sign a message and return the signature. - fn sign(&self, msg: &[u8]) -> Signature; - /// Sign a message and return the signature, or fail if the signer was unable - /// to produce a signature. - fn try_sign(&self, msg: &[u8]) -> Result; } -impl Signer for Box +impl Signer for S where - T: Signer + ?Sized, + S: Send, + S: signature::Signer, + S: signature::KeypairRef, { fn public_key(&self) -> &PublicKey { - self.deref().public_key() - } - - fn sign(&self, msg: &[u8]) -> Signature { - self.deref().sign(msg) - } - - fn try_sign(&self, msg: &[u8]) -> Result { - self.deref().try_sign(msg) + self.as_ref() } } diff --git a/crates/radicle-crypto/src/ssh/agent.rs b/crates/radicle-crypto/src/ssh/agent.rs index 901b0e62..864d53b9 100644 --- a/crates/radicle-crypto/src/ssh/agent.rs +++ b/crates/radicle-crypto/src/ssh/agent.rs @@ -4,7 +4,7 @@ use std::path::Path; pub use radicle_ssh as ssh; pub use ssh::agent::client::{AgentClient, Error}; -use crate::{PublicKey, SecretKey, Signature, Signer, SignerError}; +use crate::{PublicKey, SecretKey, Signature, Signer}; use super::ExtendedSignature; @@ -59,7 +59,12 @@ pub struct AgentSigner { impl signature::Signer for AgentSigner { fn try_sign(&self, msg: &[u8]) -> Result { - Signer::try_sign(self, msg).map_err(signature::Error::from_source) + let sig = self + .agent + .borrow_mut() + .sign(&self.public, msg) + .map_err(signature::Error::from_source)?; + Ok(Signature::from(sig)) } } @@ -101,23 +106,3 @@ impl AgentSigner { Box::new(self) } } - -impl Signer for AgentSigner { - fn public_key(&self) -> &PublicKey { - &self.public - } - - fn sign(&self, msg: &[u8]) -> Signature { - self.try_sign(msg).unwrap() - } - - fn try_sign(&self, msg: &[u8]) -> Result { - let sig = self - .agent - .borrow_mut() - .sign(&self.public, msg) - .map_err(SignerError::new)?; - - Ok(Signature::from(sig)) - } -} diff --git a/crates/radicle-crypto/src/ssh/keystore.rs b/crates/radicle-crypto/src/ssh/keystore.rs index 5f768647..bbece9fe 100644 --- a/crates/radicle-crypto/src/ssh/keystore.rs +++ b/crates/radicle-crypto/src/ssh/keystore.rs @@ -7,7 +7,7 @@ use cyphernet::{EcSk, EcSkInvalid, Ecdh}; use thiserror::Error; use zeroize::Zeroizing; -use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer, SignerError}; +use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer}; use super::ExtendedSignature; @@ -252,7 +252,7 @@ pub struct MemorySigner { impl signature::Signer for MemorySigner { fn try_sign(&self, msg: &[u8]) -> Result { - Ok(Signer::sign(self, msg)) + Ok(Signature::from(self.secret.deref().deref().sign(msg, None))) } } @@ -276,20 +276,6 @@ impl signature::KeypairRef for MemorySigner { type VerifyingKey = PublicKey; } -impl Signer for MemorySigner { - fn public_key(&self) -> &PublicKey { - &self.public - } - - fn sign(&self, msg: &[u8]) -> Signature { - Signature(self.secret.deref().deref().sign(msg, None)) - } - - fn try_sign(&self, msg: &[u8]) -> Result { - Ok(Signer::sign(self, msg)) - } -} - #[cfg(feature = "cyphernet")] impl EcSk for MemorySigner { type Pk = PublicKey; diff --git a/crates/radicle-crypto/src/test/signer.rs b/crates/radicle-crypto/src/test/signer.rs index dd036340..a0cd3196 100644 --- a/crates/radicle-crypto/src/test/signer.rs +++ b/crates/radicle-crypto/src/test/signer.rs @@ -1,6 +1,4 @@ -use crate::{ - ssh::ExtendedSignature, KeyPair, PublicKey, SecretKey, Seed, Signature, Signer, SignerError, -}; +use crate::{ssh::ExtendedSignature, KeyPair, PublicKey, SecretKey, Seed, Signature}; #[derive(Debug, Clone)] pub struct MockSigner { @@ -86,20 +84,6 @@ impl std::hash::Hash for MockSigner { } } -impl Signer for MockSigner { - fn public_key(&self) -> &PublicKey { - &self.pk - } - - fn sign(&self, msg: &[u8]) -> Signature { - self.sk.sign(msg, None).into() - } - - fn try_sign(&self, msg: &[u8]) -> Result { - Ok(self.sign(msg)) - } -} - #[cfg(feature = "cyphernet")] impl cyphernet::EcSk for MockSigner { type Pk = PublicKey;