From f4495e92bcfb921b93872e253623ad4373431d0b Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Sun, 8 Mar 2026 09:49:32 +0100 Subject: [PATCH] radicle/device: `impl Keypair for BoxedSigner` Most consumers of `Device` are interested in the public key of the device, and `Keypair` is the trait from `signature` which captures this, so implement it. Further, for boxing a signer, introduce a new trait `BoxableSigner` (to remain dyn-compatible) which additionally requires `Keypair`. --- crates/radicle/src/node/device.rs | 38 ++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/radicle/src/node/device.rs b/crates/radicle/src/node/device.rs index 1c49b473..7a0f9869 100644 --- a/crates/radicle/src/node/device.rs +++ b/crates/radicle/src/node/device.rs @@ -2,7 +2,7 @@ use std::fmt; use std::ops::Deref; use crypto::{ - signature::{Signer, Verifier}, + signature::{Keypair, KeypairRef, Signer, Verifier}, ssh::ExtendedSignature, Signature, }; @@ -81,7 +81,7 @@ impl Device { } } -impl + 'static> Device { +impl Device { /// Construct a [`BoxedDevice`] from a given `Device`. pub fn boxed(self) -> BoxedDevice { BoxedDevice(Device { @@ -91,6 +91,16 @@ impl + 'static> Device { } } +impl AsRef for Device { + fn as_ref(&self) -> &NodeId { + &self.node + } +} + +impl KeypairRef for Device { + type VerifyingKey = NodeId; +} + impl Verifier for Device { fn verify(&self, msg: &[u8], signature: &Signature) -> Result<(), crypto::signature::Error> { self.node @@ -129,8 +139,12 @@ impl> Signer for Device { } } +pub trait BoxableSigner: Signer + Keypair {} + +impl + Keypair> BoxableSigner for S {} + /// A `Signer` that is packed in a [`Box`] for dynamic dispatch. -pub struct BoxedSigner(Box + 'static>); +pub struct BoxedSigner(Box); impl Signer for BoxedSigner { fn try_sign(&self, msg: &[u8]) -> Result { @@ -138,6 +152,14 @@ impl Signer for BoxedSigner { } } +impl Keypair for BoxedSigner { + type VerifyingKey = crypto::PublicKey; + + fn verifying_key(&self) -> Self::VerifyingKey { + self.0.verifying_key() + } +} + /// A `Device` where the signer is a dynamic `Signer`, in the form of /// a [`BoxedSigner`]. /// @@ -172,3 +194,13 @@ impl Signer for BoxedDevice { }) } } + +impl AsRef for BoxedDevice { + fn as_ref(&self) -> &crypto::PublicKey { + &self.0.node + } +} + +impl KeypairRef for BoxedDevice { + type VerifyingKey = crypto::PublicKey; +}