diff --git a/radicle-crypto/src/lib.rs b/radicle-crypto/src/lib.rs index bc2a0b51..7b8e2544 100644 --- a/radicle-crypto/src/lib.rs +++ b/radicle-crypto/src/lib.rs @@ -39,7 +39,7 @@ impl SignerError { } } -pub trait Signer: Send + Sync { +pub trait Signer: Send { /// Return this signer's public/verification key. fn public_key(&self) -> &PublicKey; /// Sign a message and return the signature. diff --git a/radicle-crypto/src/ssh/agent.rs b/radicle-crypto/src/ssh/agent.rs index 6f3d9854..a4967a67 100644 --- a/radicle-crypto/src/ssh/agent.rs +++ b/radicle-crypto/src/ssh/agent.rs @@ -1,5 +1,4 @@ -use std::ops::{Deref, DerefMut}; -use std::sync::Mutex; +use std::cell::RefCell; pub use radicle_ssh::agent::client::AgentClient; pub use radicle_ssh::agent::client::Error; @@ -27,41 +26,47 @@ impl Agent { self.client.add_identity(key, &[]) } + pub fn unregister(&mut self, key: &PublicKey) -> Result<(), ssh::Error> { + self.client.remove_identity(key) + } + + pub fn unregister_all(&mut self) -> Result<(), ssh::Error> { + self.client.remove_all_identities() + } + + pub fn sign(&mut self, key: &PublicKey, data: &[u8]) -> Result<[u8; 64], ssh::Error> { + self.client.sign(key, data) + } + /// Get a signer from this agent, given the public key. pub fn signer(self, key: PublicKey) -> AgentSigner { AgentSigner::new(self, key) } -} -impl Deref for Agent { - type Target = AgentClient; - - fn deref(&self) -> &Self::Target { - &self.client + pub fn pid(&self) -> Option { + self.client.pid() } -} -impl DerefMut for Agent { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.client + pub fn request_identities(&mut self) -> Result, ssh::agent::client::Error> { + self.client.request_identities() } } /// A [`Signer`] that uses `ssh-agent`. pub struct AgentSigner { - agent: Mutex, + agent: RefCell, public: PublicKey, } impl AgentSigner { pub fn new(agent: Agent, public: PublicKey) -> Self { - let agent = Mutex::new(agent); + let agent = RefCell::new(agent); Self { agent, public } } pub fn is_ready(&self) -> Result { - let ids = self.agent.lock().unwrap().request_identities()?; + let ids = self.agent.borrow_mut().request_identities()?; Ok(ids.contains(&self.public)) } @@ -84,9 +89,7 @@ impl Signer for AgentSigner { fn try_sign(&self, msg: &[u8]) -> Result { let sig = self .agent - .lock() - // We'll take our chances here; the worse that can happen is the agent returns an error. - .unwrap_or_else(|e| e.into_inner()) + .borrow_mut() .sign(&self.public, msg) .map_err(SignerError::new)?; diff --git a/radicle-tools/src/rad-agent.rs b/radicle-tools/src/rad-agent.rs index 82d7005f..317098f6 100644 --- a/radicle-tools/src/rad-agent.rs +++ b/radicle-tools/src/rad-agent.rs @@ -28,11 +28,11 @@ fn main() -> anyhow::Result<()> { println!("ok"); } Some("remove") => { - agent.remove_identity(profile.id())?; + agent.unregister(profile.id())?; println!("ok"); } Some("remove-all") => { - agent.remove_all_identities()?; + agent.unregister_all()?; println!("ok"); } Some("sign") => {