Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
crypto: Require `Signer: signature::Signer`
Lorenz Leutgeb committed 1 month ago
commit 74fa4425a130c38d06602f46d530c8c079350682
parent 06fae85
4 files changed +16 -72
modified crates/radicle-crypto/src/lib.rs
@@ -42,30 +42,19 @@ impl SignerError {
    }
}

-
pub trait Signer: Send {
+
pub trait Signer: Send + signature::Signer<Signature> {
    /// 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<Signature, SignerError>;
}

-
impl<T> Signer for Box<T>
+
impl<S> Signer for S
where
-
    T: Signer + ?Sized,
+
    S: Send,
+
    S: signature::Signer<Signature>,
+
    S: signature::KeypairRef<VerifyingKey = PublicKey>,
{
    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<Signature, SignerError> {
-
        self.deref().try_sign(msg)
+
        self.as_ref()
    }
}

modified 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<Signature> for AgentSigner {
    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)
    }
}
-

-
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))
-
    }
-
}
modified 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<Signature> for MemorySigner {
    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;
}

-
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")]
impl EcSk for MemorySigner {
    type Pk = PublicKey;
modified 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<Signature, SignerError> {
-
        Ok(self.sign(msg))
-
    }
-
}
-

#[cfg(feature = "cyphernet")]
impl cyphernet::EcSk for MockSigner {
    type Pk = PublicKey;