rust_eigenda_client/
client.rs

1use crate::errors::{ConfigError, EigenClientError};
2
3use super::{
4    config::{EigenConfig, EigenSecrets},
5    sdk::RawEigenClient,
6};
7use async_trait::async_trait;
8use secp256k1::SecretKey;
9use secrecy::ExposeSecret;
10use std::error::Error;
11use std::{str::FromStr, sync::Arc};
12
13/// Provides a way of retrieving blobs.
14/// Some implementations may not need it. In that case, they can return None in the get_blob method.
15/// It can be used as extra verification if you also store the blob yourself.
16#[async_trait]
17pub trait BlobProvider: std::fmt::Debug + Send + Sync {
18    /// Returns the blob for the given blob_id.
19    /// If the blob is not found, it should return None.
20    async fn get_blob(
21        &self,
22        blob_id: &str,
23    ) -> Result<Option<Vec<u8>>, Box<dyn Error + Send + Sync>>;
24}
25
26/// EigenClient is a client for the Eigen DA service.
27#[derive(Debug, Clone)]
28pub struct EigenClient {
29    pub(crate) client: Arc<RawEigenClient>,
30}
31
32impl EigenClient {
33    /// Creates a new EigenClient
34    pub async fn new(
35        config: EigenConfig,
36        secrets: EigenSecrets,
37        blob_provider: Arc<dyn BlobProvider>,
38    ) -> Result<Self, EigenClientError> {
39        let private_key = SecretKey::from_str(secrets.private_key.0.expose_secret().as_str())
40            .map_err(ConfigError::Secp)?;
41
42        let client = RawEigenClient::new(private_key, config, blob_provider).await?;
43        Ok(Self {
44            client: Arc::new(client),
45        })
46    }
47
48    /// Dispatches a blob to the Eigen DA service
49    pub async fn dispatch_blob(&self, data: Vec<u8>) -> Result<String, EigenClientError> {
50        let blob_id = self.client.dispatch_blob(data).await?;
51
52        Ok(blob_id)
53    }
54
55    /// Gets the inclusion data for a blob
56    pub async fn get_inclusion_data(
57        &self,
58        blob_id: &str,
59    ) -> Result<Option<Vec<u8>>, EigenClientError> {
60        let inclusion_data = self.client.get_inclusion_data(blob_id).await?;
61        Ok(inclusion_data)
62    }
63
64    /// Returns the blob size limit
65    pub fn blob_size_limit(&self) -> Option<usize> {
66        Some(RawEigenClient::blob_size_limit())
67    }
68}