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