rust_eigenda_client/
client.rs

1use crate::blob_info::BlobInfo;
2use crate::errors::EigenClientError;
3
4use super::{config::EigenConfig, sdk::RawEigenClient};
5use crate::rust_eigenda_signers::{signers::private_key::Signer as PrivateKeySigner, Sign};
6use async_trait::async_trait;
7use std::error::Error;
8use std::sync::Arc;
9
10/// Provides a way of retrieving blobs.
11/// Some implementations may not need it. In that case, they can return `None` in the `get_blob` method.
12/// It can be used as extra verification if you also store the blob yourself.
13#[async_trait]
14pub trait BlobProvider: std::fmt::Debug + Send + Sync {
15    /// Returns the blob for the given blob_id.
16    /// If the blob is not found, it should return None.
17    async fn get_blob(
18        &self,
19        blob_id: &str,
20    ) -> Result<Option<Vec<u8>>, Box<dyn Error + Send + Sync>>;
21}
22
23/// EigenClient is a client for the Eigen DA service.
24#[derive(Debug, Clone)]
25pub struct EigenClient<S = PrivateKeySigner> {
26    pub(crate) client: Arc<RawEigenClient<S>>,
27}
28
29impl<S> EigenClient<S> {
30    /// Creates a new EigenClient
31    pub async fn new(
32        config: EigenConfig,
33        signer: S,
34        blob_provider: Arc<dyn BlobProvider>,
35    ) -> Result<Self, EigenClientError> {
36        let client = RawEigenClient::new(signer, config, blob_provider).await?;
37        Ok(Self {
38            client: Arc::new(client),
39        })
40    }
41
42    /// Dispatches a blob to the Eigen DA service
43    pub async fn dispatch_blob(&self, data: Vec<u8>) -> Result<String, EigenClientError>
44    where
45        S: Sign,
46    {
47        let blob_id = self.client.dispatch_blob(data).await?;
48
49        Ok(blob_id)
50    }
51
52    /// Gets the inclusion data for a blob
53    pub async fn get_inclusion_data(
54        &self,
55        blob_id: &str,
56    ) -> Result<Option<Vec<u8>>, EigenClientError> {
57        let inclusion_data = self.client.get_inclusion_data(blob_id).await?;
58        Ok(inclusion_data)
59    }
60
61    /// Gets the blob info for a dispersed blob
62    pub async fn get_blob_info(&self, blob_id: &str) -> Result<Option<BlobInfo>, EigenClientError> {
63        self.client.get_blob_info(blob_id).await
64    }
65
66    /// Checks if the blob is included in EigenDA
67    pub async fn check_finality(&self, blob_id: &str) -> Result<bool, EigenClientError> {
68        let blob_info = self
69            .client
70            .try_get_inclusion_data(blob_id.to_string())
71            .await?;
72        Ok(blob_info.is_some())
73    }
74
75    /// Returns the blob size limit
76    pub fn blob_size_limit(&self) -> Option<usize> {
77        Some(RawEigenClient::<S>::blob_size_limit())
78    }
79
80    /// Returns the blob
81    pub async fn get_blob(
82        &self,
83        blob_index: u32,
84        batch_header_hash: Vec<u8>,
85    ) -> Result<Option<Vec<u8>>, EigenClientError> {
86        self.client.get_blob(blob_index, batch_header_hash).await
87    }
88}