use std::sync::Arc;
use celestia_fibre::{
Blob, BlobID, DownloadOptions, EncodedBlob, FibreClient, FibreError, SignedPaymentPromise,
};
use celestia_grpc::{SubmittedTx, TxConfig};
use celestia_types::nmt::Namespace;
use k256::ecdsa::SigningKey;
use crate::Error;
use crate::client::ClientInner;
pub struct FibreApi {
inner: Arc<ClientInner>,
fibre_client: Arc<FibreClient>,
}
impl FibreApi {
pub(crate) fn new(inner: Arc<ClientInner>, fibre_client: FibreClient) -> Self {
Self {
inner,
fibre_client: Arc::new(fibre_client),
}
}
pub async fn upload(
&self,
signing_key: &SigningKey,
namespace: Namespace,
blob: EncodedBlob,
) -> Result<SignedPaymentPromise, FibreError> {
self.fibre_client.upload(signing_key, namespace, blob).await
}
pub async fn put(
&self,
signing_key: &SigningKey,
namespace: Namespace,
data: &[u8],
) -> Result<SubmittedTx, Error> {
let grpc = self.inner.grpc()?;
let signer_address = grpc
.get_account_address()
.ok_or(Error::NoAssociatedAddress)?;
let msg = self
.fibre_client
.upload_and_prepare(signing_key, namespace, data, &signer_address)
.await
.map_err(fibre_err)?;
let submitted = grpc.broadcast_message(msg, TxConfig::default()).await?;
Ok(submitted)
}
pub async fn download(&self, id: &BlobID, opts: DownloadOptions) -> Result<Blob, FibreError> {
self.fibre_client.download(id, opts).await
}
pub fn is_closed(&self) -> bool {
self.fibre_client.is_closed()
}
pub fn close(&self) {
self.fibre_client.close();
}
}
fn fibre_err(e: FibreError) -> Error {
match e {
FibreError::GrpcClient(grpc_err) => Error::Grpc(grpc_err),
other => Error::Fibre(other),
}
}