crypto: Require `Signer: signature::Signer`
All implementations of `radicle_crypto::Signer` are also `signature::Signer`. Also, the implementations of `Signer::sign` and `Signer::try_sign` call each other. To simplify, make `signature::Signer` a requirement for `radicle_crypto::Signer`.
This commit is contained in:
parent
06fae85e67
commit
74fa4425a1
|
|
@ -42,30 +42,19 @@ impl SignerError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Signer: Send {
|
pub trait Signer: Send + signature::Signer<Signature> {
|
||||||
/// Return this signer's public/verification key.
|
/// Return this signer's public/verification key.
|
||||||
fn public_key(&self) -> &PublicKey;
|
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<Signature, SignerError>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Signer for Box<T>
|
impl<S> Signer for S
|
||||||
where
|
where
|
||||||
T: Signer + ?Sized,
|
S: Send,
|
||||||
|
S: signature::Signer<Signature>,
|
||||||
|
S: signature::KeypairRef<VerifyingKey = PublicKey>,
|
||||||
{
|
{
|
||||||
fn public_key(&self) -> &PublicKey {
|
fn public_key(&self) -> &PublicKey {
|
||||||
self.deref().public_key()
|
self.as_ref()
|
||||||
}
|
|
||||||
|
|
||||||
fn sign(&self, msg: &[u8]) -> Signature {
|
|
||||||
self.deref().sign(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn try_sign(&self, msg: &[u8]) -> Result<Signature, SignerError> {
|
|
||||||
self.deref().try_sign(msg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use std::path::Path;
|
||||||
pub use radicle_ssh as ssh;
|
pub use radicle_ssh as ssh;
|
||||||
pub use ssh::agent::client::{AgentClient, Error};
|
pub use ssh::agent::client::{AgentClient, Error};
|
||||||
|
|
||||||
use crate::{PublicKey, SecretKey, Signature, Signer, SignerError};
|
use crate::{PublicKey, SecretKey, Signature, Signer};
|
||||||
|
|
||||||
use super::ExtendedSignature;
|
use super::ExtendedSignature;
|
||||||
|
|
||||||
|
|
@ -59,7 +59,12 @@ pub struct AgentSigner {
|
||||||
|
|
||||||
impl signature::Signer<Signature> for AgentSigner {
|
impl signature::Signer<Signature> for AgentSigner {
|
||||||
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
|
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
|
||||||
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)
|
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<Signature, SignerError> {
|
|
||||||
let sig = self
|
|
||||||
.agent
|
|
||||||
.borrow_mut()
|
|
||||||
.sign(&self.public, msg)
|
|
||||||
.map_err(SignerError::new)?;
|
|
||||||
|
|
||||||
Ok(Signature::from(sig))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use cyphernet::{EcSk, EcSkInvalid, Ecdh};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer, SignerError};
|
use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer};
|
||||||
|
|
||||||
use super::ExtendedSignature;
|
use super::ExtendedSignature;
|
||||||
|
|
||||||
|
|
@ -252,7 +252,7 @@ pub struct MemorySigner {
|
||||||
|
|
||||||
impl signature::Signer<Signature> for MemorySigner {
|
impl signature::Signer<Signature> for MemorySigner {
|
||||||
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
|
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
|
||||||
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;
|
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<Signature, SignerError> {
|
|
||||||
Ok(Signer::sign(self, msg))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "cyphernet")]
|
#[cfg(feature = "cyphernet")]
|
||||||
impl EcSk for MemorySigner {
|
impl EcSk for MemorySigner {
|
||||||
type Pk = PublicKey;
|
type Pk = PublicKey;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::{
|
use crate::{ssh::ExtendedSignature, KeyPair, PublicKey, SecretKey, Seed, Signature};
|
||||||
ssh::ExtendedSignature, KeyPair, PublicKey, SecretKey, Seed, Signature, Signer, SignerError,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MockSigner {
|
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<Signature, SignerError> {
|
|
||||||
Ok(self.sign(msg))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "cyphernet")]
|
#[cfg(feature = "cyphernet")]
|
||||||
impl cyphernet::EcSk for MockSigner {
|
impl cyphernet::EcSk for MockSigner {
|
||||||
type Pk = PublicKey;
|
type Pk = PublicKey;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue