rust_eigenda_signers/
lib.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5pub mod ethereum_types {
6    pub use ethereum_types::H160;
7}
8
9mod message;
10mod public_key;
11mod recovery_id;
12mod secret_key;
13mod signature;
14pub mod signers;
15
16use std::error::Error;
17
18pub use message::Message;
19pub use public_key::{Error as PublicKeyError, PublicKey};
20pub use recovery_id::{InvalidRecoveryId, RecoveryId};
21pub use secret_key::{Error as SecretKeyError, SecretKey};
22pub use signature::RecoverableSignature;
23
24/// A trait for signing messages using different key management strategies.
25#[async_trait]
26pub trait Sign: Send + Sync + std::fmt::Debug {
27    /// The specific error type returned by the signer.
28    type Error: Error + Send + Sync + 'static;
29
30    /// Signs a 32-byte digest using the signer's key.
31    ///
32    /// # Arguments
33    ///
34    /// * `message`: The message digest to sign.
35    ///
36    /// # Returns
37    ///
38    /// A `Result` containing the `RecoverableSignature` on success, or the signer's specific `Error` on failure.
39    async fn sign_digest(&self, message: &Message) -> Result<RecoverableSignature, Self::Error>;
40
41    /// Returns the public key associated with this signer.
42    fn public_key(&self) -> PublicKey;
43}
44
45/// Blanket implementation for references to `Sign` implementors.
46#[async_trait]
47impl<T> Sign for &T
48where
49    T: Sign + Sync,
50{
51    type Error = T::Error;
52
53    /// Delegates the signing operation to the underlying signer.
54    async fn sign_digest(&self, message: &Message) -> Result<RecoverableSignature, Self::Error> {
55        (*self).sign_digest(message).await
56    }
57
58    /// Delegates the public key retrieval to the underlying signer.
59    fn public_key(&self) -> PublicKey {
60        (*self).public_key()
61    }
62}
63
64/// Blanket implementation for `Arc<T>` where `T` implements `Sign`.
65#[async_trait]
66impl<T> Sign for Arc<T>
67where
68    T: Sign + Sync,
69{
70    type Error = T::Error;
71
72    /// Delegates the signing operation to the underlying signer.
73    async fn sign_digest(&self, message: &Message) -> Result<RecoverableSignature, Self::Error> {
74        (**self).sign_digest(message).await
75    }
76
77    /// Delegates the public key retrieval to the underlying signer.
78    fn public_key(&self) -> PublicKey {
79        (**self).public_key()
80    }
81}
82
83/// Blanket implementation for `Box<T>` where `T` implements `Sign`.
84#[async_trait]
85impl<T> Sign for Box<T>
86where
87    T: Sign + Sync,
88{
89    type Error = T::Error;
90
91    /// Use our Message and RecoverableSignature types
92    /// Delegates the signing operation to the underlying signer.
93    async fn sign_digest(&self, message: &Message) -> Result<RecoverableSignature, Self::Error> {
94        (**self).sign_digest(message).await
95    }
96
97    /// Delegates the public key retrieval to the underlying signer.
98    fn public_key(&self) -> PublicKey {
99        (**self).public_key()
100    }
101}