Skip to content

Commit

Permalink
Merge pull request #65 from helius-labs/feat/add-tls-support
Browse files Browse the repository at this point in the history
feat(deps): Add TLS Support
  • Loading branch information
0xIchigo authored Sep 3, 2024
2 parents 2e4f524 + fdbab02 commit ec899cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
19 changes: 12 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ futures = "0.3.30"
futures-util = "0.3.30"
phf = { version = "0.11.2", features = ["macros"] }
rand = "0.8.5"
reqwest = { version = "0.12.3", features = ["json"] }
reqwest = { version = "0.11", features = ["json"], default-features = false }
semver = "1.0.23"
serde = "1.0.198"
serde-enum-str = "0.4.0"
serde_json = "1.0.116"
solana-account-decoder = "=2.0.7"
solana-client = "=2.0.7"
solana-program = "=2.0.7"
solana-rpc-client-api = "=2.0.7"
solana-sdk = "=2.0.7"
solana-transaction-status = "=2.0.7"
solana-account-decoder = "=1.17.0"
solana-client = "=1.17.0"
solana-program = "=1.17.0"
solana-rpc-client-api = "=1.17.0"
solana-sdk = "=1.17.0"
solana-transaction-status = "=1.17.0"
thiserror = "1.0.58"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "net"] }
tokio-stream = "0.1.15"
Expand All @@ -38,3 +38,8 @@ url = "2.5.0"

[dev-dependencies]
mockito = "1.4.0"

[features]
default = ["native-tls"]
native-tls = ["reqwest/native-tls"]
rustls = ["reqwest/rustls-tls"]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ where `x.y.z` is your desired version. Alternatively, use `cargo add helius` to

Remember to run `cargo update` regularly to fetch the latest version of the SDK.

### TLS Options
The Helius Rust SDK uses the native TLS implementation by default via:
```toml
[dependencies]
helius = "x.y.z"
```

However, the SDK also supports `rustls`. Add the following to your `Cargo.toml` to use `rustls` instead of the native TLS implementation:
```toml
[dependencies]
helius = { version = "x.y.z", default-features = false, features = ["rustls"] }
```

Using `rustls` may be preferred in environments where OpenSSL is not available or when a pure Rust TLS implementation is desired. However, it may not support all the same features as the native TLS implementation

## Usage
### `Helius`
The SDK provides a [`Helius`](https://github.com/helius-labs/helius-rust-sdk/blob/dev/src/client.rs) instance that can be configured with an API key and a given Solana cluster. Developers can generate a new API key on the [Helius Developer Dashboard](https://dev.helius.xyz/dashboard/app). This instance acts as the main entry point for interacting with the SDK by providing methods to access different Solana and RPC client functionalities. The following code is an example of how to use the SDK to fetch info on [Mad Lad #8420](https://explorer.solana.com/address/F9Lw3ki3hJ7PF9HQXsBzoY8GyE6sPoEZZdXJBsTTD2rk?network=mainnet):
Expand Down
8 changes: 5 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ impl Helius {
/// ```
pub fn new(api_key: &str, cluster: Cluster) -> Result<Self> {
let config: Arc<Config> = Arc::new(Config::new(api_key, cluster)?);
let client: Client = Client::new();
let client: Client = Client::builder().build().map_err(HeliusError::ReqwestError)?;
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), config.clone())?);

Ok(Helius {
config,
client,
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Helius {
/// ```
pub fn new_with_async_solana(api_key: &str, cluster: Cluster) -> Result<Self> {
let config: Arc<Config> = Arc::new(Config::new(api_key, cluster)?);
let client: Client = Client::new();
let client: Client = Client::builder().build().map_err(HeliusError::ReqwestError)?;
let url: String = format!("{}/?api-key={}", config.endpoints.rpc, config.api_key);
let async_solana_client: Arc<AsyncSolanaRpcClient> = Arc::new(AsyncSolanaRpcClient::new(url));

Expand All @@ -98,10 +99,11 @@ impl Helius {
/// An instance of `Helius` if successful. A `HeliusError` is returned if an error occurs during configuration or initialization of the HTTP, RPC, or WS client
pub async fn new_with_ws(api_key: &str, cluster: Cluster) -> Result<Self> {
let config: Arc<Config> = Arc::new(Config::new(api_key, cluster)?);
let client: Client = Client::new();
let client: Client = Client::builder().build().map_err(HeliusError::ReqwestError)?;
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), config.clone())?);
let wss: String = format!("{}{}", ENHANCED_WEBSOCKET_URL, api_key);
let ws_client: Arc<EnhancedWebsocket> = Arc::new(EnhancedWebsocket::new(&wss).await?);

Ok(Helius {
config,
client,
Expand Down
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ pub enum HeliusError {

#[error("Url parse error")]
UrlParseError(#[from] url::ParseError),

#[error("TLS error: {0}")]
TlsError(String),
}

impl HeliusError {
Expand Down Expand Up @@ -156,5 +159,15 @@ impl From<SanitizeError> for HeliusError {
}
}

impl From<ReqwestError> for HeliusError {
fn from(err: reqwest::Error) -> Self {
if err.is_builder() {
HeliusError::TlsError(err.to_string())
} else {
HeliusError::ReqwestError(err)
}
}
}

/// A handy type alias for handling results across the Helius SDK
pub type Result<T> = std::result::Result<T, HeliusError>;

0 comments on commit ec899cc

Please sign in to comment.