Skip to main content

celestia_client/
lib.rs

1#![doc = include_str!("../README.md")]
2
3#[cfg(all(target_arch = "wasm32", test))]
4wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
5
6mod blob;
7mod blobstream;
8mod client;
9mod fibre;
10mod fraud;
11mod header;
12mod share;
13mod state;
14#[cfg(test)]
15mod test_utils;
16mod utils;
17
18/// API related types.
19pub mod api {
20    pub use crate::blob::BlobApi;
21    pub use crate::blobstream::BlobstreamApi;
22    pub use crate::fibre::FibreApi;
23    pub use crate::fraud::FraudApi;
24    pub use crate::header::HeaderApi;
25    pub use crate::share::ShareApi;
26    pub use crate::state::StateApi;
27
28    /// Blob API related types.
29    pub mod blob {
30        #[doc(inline)]
31        pub use celestia_rpc::blob::BlobsAtHeight;
32    }
33
34    /// Fibre API related types.
35    pub mod fibre {
36        #[cfg(target_arch = "wasm32")]
37        pub use celestia_fibre::BrowserWebSocketConnector;
38        #[cfg(not(target_arch = "wasm32"))]
39        pub use celestia_fibre::NativeTcpConnector;
40        #[doc(inline)]
41        pub use celestia_fibre::{
42            Blob as FibreBlob, BlobConfig, BlobID, BoxedFibreIo, DownloadOptions,
43            EncodedBlob as EncodedFibreBlob, FibreClient, FibreClientConfig, FibreError, FibreIo,
44            FibreIoConnector, PaymentPromise, SignedPaymentPromise,
45        };
46    }
47
48    /// Share API related types.
49    pub mod share {
50        #[doc(inline)]
51        pub use celestia_rpc::share::{
52            GetRangeResponse, GetRowResponse, RowSide, SampleCoordinates,
53        };
54    }
55
56    /// Fraud API related types.
57    pub mod fraud {
58        #[doc(inline)]
59        pub use celestia_rpc::fraud::{Proof, ProofType};
60    }
61
62    /// State API related types.
63    pub mod state {
64        #[doc(inline)]
65        pub use crate::state::AsyncGrpcCall;
66    }
67}
68
69/// TX related types.
70pub mod tx {
71    #[doc(inline)]
72    pub use celestia_grpc::grpc::{GasEstimate, GetTxResponse, TxPriority};
73    #[doc(inline)]
74    pub use celestia_grpc::{
75        BroadcastedTx, DocSigner, IntoProtobufAny, SignDoc, SubmittedTx, TxConfig, TxInfo,
76    };
77    #[doc(inline)]
78    pub use k256::ecdsa::signature::{Error as SignatureError, Keypair};
79    #[doc(inline)]
80    pub use k256::ecdsa::{Signature, SigningKey, VerifyingKey};
81}
82
83use celestia_grpc::GrpcClientBuilderError;
84
85/// Re-export of [`celestia-proto`].
86///
87/// [`celestia-proto`]: celestia_proto
88#[doc(inline)]
89pub use celestia_proto as proto;
90
91/// Re-export of [`celestia-types`].
92///
93/// [`celestia-types`]: celestia_types
94#[doc(inline)]
95pub use celestia_types as types;
96
97pub use crate::client::{Client, ClientBuilder, Endpoint, RpcEndpoint};
98
99/// Alias for a `Result` with the error type [`celestia_client::Error`].
100///
101/// [`celestia_client::Error`]: crate::Error
102pub type Result<T, E = Error> = std::result::Result<T, E>;
103
104/// Representation of all the errors that can occur when interacting with [`celestia_client`].
105///
106/// [`celestia_client`]: crate
107#[derive(Debug, thiserror::Error)]
108pub enum Error {
109    /// Celestia RPC error.
110    #[error("RPC error: {0}")]
111    Rpc(#[from] celestia_rpc::Error),
112
113    /// Celestia gRPC error.
114    #[error("gRPC error: {0}")]
115    Grpc(celestia_grpc::Error),
116
117    /// gRPC client builder error.
118    #[error("gRPC client builder error: {0}")]
119    GrpcBuilder(GrpcClientBuilderError),
120
121    /// Celestia types error.
122    #[error("Celestia types error: {0}")]
123    Types(#[from] celestia_types::Error),
124
125    /// Client is in read-only mode.
126    #[error("Client is constructed for read-only mode, operation not supported")]
127    ReadOnlyMode,
128
129    /// RPC chain-id and gRPC chain-id missmatch.
130    #[error("Chain id of RPC endpoint missmatch with chain id of gRPC endpoint")]
131    ChainIdMissmatch,
132
133    /// Invalid height.
134    #[error("Invalid height: {0}")]
135    InvalidHeight(u64),
136
137    /// Invalid private key.
138    #[error("Invalid private key")]
139    InvalidPrivateKey,
140
141    /// RPC endpoint is not set.
142    #[error("RPC endpoint not set")]
143    RpcEndpointNotSet,
144
145    /// Client does not have associated account address
146    #[error("Client has no associated account address")]
147    NoAssociatedAddress,
148
149    /// gRPC endpoint is not set.
150    #[error("Signer is set but gRPC endpoint is not")]
151    GrpcEndpointNotSet,
152
153    /// An error from the Fibre client library.
154    #[error("Fibre error: {0}")]
155    Fibre(celestia_fibre::FibreError),
156}
157
158impl From<jsonrpsee_core::ClientError> for Error {
159    fn from(value: jsonrpsee_core::ClientError) -> Self {
160        Error::Rpc(celestia_rpc::Error::JsonRpc(value))
161    }
162}
163
164impl From<serde_json::Error> for Error {
165    fn from(value: serde_json::Error) -> Self {
166        jsonrpsee_core::ClientError::ParseError(value).into()
167    }
168}
169
170impl From<GrpcClientBuilderError> for Error {
171    fn from(value: GrpcClientBuilderError) -> Self {
172        match value {
173            GrpcClientBuilderError::TransportNotSet => Error::GrpcEndpointNotSet,
174            GrpcClientBuilderError::InvalidPrivateKey => Error::InvalidPrivateKey,
175            e => Error::GrpcBuilder(e),
176        }
177    }
178}
179
180impl From<celestia_grpc::Error> for Error {
181    fn from(value: celestia_grpc::Error) -> Self {
182        match value {
183            celestia_grpc::Error::MissingSigner => Error::ReadOnlyMode,
184            e => Error::Grpc(e),
185        }
186    }
187}
188
189impl Error {
190    /// Helper that returns the logical error of a gRPC call.
191    pub fn as_grpc_status(&self) -> Option<&tonic::Status> {
192        match self {
193            Error::Grpc(celestia_grpc::Error::TonicError(status)) => Some(&**status),
194            _ => None,
195        }
196    }
197
198    /// Helper that returns the logical error of a RPC call.
199    pub fn as_rpc_call_error(&self) -> Option<&jsonrpsee_types::error::ErrorObjectOwned> {
200        match self {
201            Error::Rpc(celestia_rpc::Error::JsonRpc(jsonrpsee_core::ClientError::Call(e))) => {
202                Some(e)
203            }
204            _ => None,
205        }
206    }
207}