Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authentication Options for RPC Providers #191

Merged
merged 8 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix formating
  • Loading branch information
hmzakhalid committed Nov 29, 2024
commit 89d410bffcc2a40306a757b15afbdeaba1b955ef
18 changes: 9 additions & 9 deletions packages/ciphernode/config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ pub struct ContractAddresses {
#[serde(tag = "type", content = "credentials")]
pub enum RpcAuth {
None,
Basic {
username: String,
password: String,
},
Bearer(String)
Basic { username: String, password: String },
Bearer(String),
}

impl Default for RpcAuth {
Expand Down Expand Up @@ -377,10 +374,13 @@ chains:
chain.contracts.ciphernode_registry.address(),
"0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
);
assert_eq!(chain.rpc_auth, RpcAuth::Basic {
username: "testUser".to_string(),
password: "testPassword".to_string(),
});
assert_eq!(
chain.rpc_auth,
RpcAuth::Basic {
username: "testUser".to_string(),
password: "testPassword".to_string(),
}
);
assert_eq!(chain.contracts.enclave.deploy_block(), None);
assert_eq!(
chain.contracts.ciphernode_registry.deploy_block(),
Expand Down
6 changes: 2 additions & 4 deletions packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use cipher::Cipher;
use config::AppConfig;
use enclave_core::EventBus;
use evm::{
helpers::{
get_signer_from_repository, RPC, ProviderConfig
},
helpers::{get_signer_from_repository, ProviderConfig, RPC},
CiphernodeRegistrySol, EnclaveSol, RegistryFilterSol,
};
use logger::SimpleLogger;
Expand Down Expand Up @@ -46,7 +44,7 @@ pub async fn setup_aggregator(
{
let rpc_url = RPC::from_url(&chain.rpc_url).map_err(|e| {
anyhow::anyhow!("Failed to parse RPC URL for chain {}: {}", chain.name, e)
})?;
})?;
let provider_config = ProviderConfig::new(rpc_url, chain.rpc_auth.clone().into());
let read_provider = provider_config.create_readonly_provider().await?;
let write_provider = provider_config.create_ws_signer_provider(&signer).await?;
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/enclave_node/src/ciphernode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cipher::Cipher;
use config::AppConfig;
use enclave_core::{get_tag, EventBus};
use evm::{
helpers::{RPC, ProviderConfig},
helpers::{ProviderConfig, RPC},
CiphernodeRegistrySol, EnclaveSolReader,
};
use logger::SimpleLogger;
Expand Down
3 changes: 2 additions & 1 deletion packages/ciphernode/evm/src/ciphernode_registry_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use actix::{Actor, Addr};
use alloy::{
primitives::{LogData, B256},
sol,
sol_types::SolEvent, transports::BoxTransport,
sol_types::SolEvent,
transports::BoxTransport,
};
use anyhow::Result;
use data::Repository;
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/evm/src/enclave_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
enclave_sol_reader::EnclaveSolReader,
enclave_sol_writer::EnclaveSolWriter,
event_reader::EvmEventReaderState,
helpers::{ReadonlyProvider, SignerProvider, WithChainId, RpcWSClient},
helpers::{ReadonlyProvider, RpcWSClient, SignerProvider, WithChainId},
};
use actix::Addr;
use alloy::transports::BoxTransport;
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/evm/src/enclave_sol_writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::helpers::{SignerProvider, WithChainId, RpcWSClient};
use crate::helpers::{RpcWSClient, SignerProvider, WithChainId};
use actix::prelude::*;
use actix::Addr;
use alloy::{primitives::Address, sol};
Expand Down
34 changes: 14 additions & 20 deletions packages/ciphernode/evm/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use alloy::{
use anyhow::{bail, Context, Result};
use base64::{engine::general_purpose::STANDARD, Engine};
use cipher::Cipher;
use config::RpcAuth as ConfigRpcAuth;
use data::Repository;
use std::{env, marker::PhantomData, sync::Arc};
use url::Url;
use zeroize::Zeroizing;
use config::RpcAuth as ConfigRpcAuth;

#[derive(Clone)]
pub enum RPC {
Expand Down Expand Up @@ -89,11 +89,8 @@ impl RPC {
#[derive(Clone)]
pub enum RpcAuth {
None,
Basic {
username: String,
password: String,
},
Bearer(String)
Basic { username: String, password: String },
Bearer(String),
}

impl RpcAuth {
Expand All @@ -114,15 +111,12 @@ impl RpcAuth {
fn to_ws_auth(&self) -> Option<Authorization> {
match self {
RpcAuth::None => None,
RpcAuth::Basic { username, password } => {
Some(Authorization::basic(username, password))
}
RpcAuth::Basic { username, password } => Some(Authorization::basic(username, password)),
RpcAuth::Bearer(token) => Some(Authorization::bearer(token)),
}
}
}


impl From<ConfigRpcAuth> for RpcAuth {
fn from(value: ConfigRpcAuth) -> Self {
match value {
Expand All @@ -143,7 +137,6 @@ impl From<RpcAuth> for ConfigRpcAuth {
}
}


/// We need to cache the chainId so we can easily use it in a non-async situation
/// This wrapper just stores the chain_id with the Provider
#[derive(Clone)]
Expand Down Expand Up @@ -199,7 +192,6 @@ pub type SignerProvider<T> = FillProvider<

pub type ReadonlyProvider = RootProvider<BoxTransport>;


#[derive(Clone)]
pub struct ProviderConfig {
rpc: RPC,
Expand All @@ -223,9 +215,10 @@ impl ProviderConfig {
.on_client(self.create_http_client()?)
.boxed())
}


pub async fn create_readonly_provider(&self) -> Result<WithChainId<ReadonlyProvider, BoxTransport>> {
pub async fn create_readonly_provider(
&self,
) -> Result<WithChainId<ReadonlyProvider, BoxTransport>> {
let provider = if self.rpc.is_websocket() {
self.create_ws_provider().await?
} else {
Expand All @@ -243,8 +236,9 @@ impl ProviderConfig {
.with_recommended_fillers()
.wallet(wallet)
.on_ws(self.create_ws_connect())
.await.context("Failed to create WS signer provider")?;

.await
.context("Failed to create WS signer provider")?;

WithChainId::new(provider).await
}

Expand Down Expand Up @@ -273,9 +267,7 @@ impl ProviderConfig {
if let Some(auth_header) = self.auth.to_header_value() {
headers.insert(AUTHORIZATION, auth_header);
}
let client = Client::builder()
.default_headers(headers)
.build()?;
let client = Client::builder().default_headers(headers).build()?;
let http = Http::with_client(client, self.rpc.as_http_url().parse()?);
Ok(RpcClient::new(http, false))
}
Expand Down Expand Up @@ -343,7 +335,9 @@ mod test {
assert!(RPC::from_url("wss://example.com/").unwrap().is_secure());

assert!(!RPC::from_url("http://example.com/").unwrap().is_websocket());
assert!(!RPC::from_url("https://example.com/").unwrap().is_websocket());
assert!(!RPC::from_url("https://example.com/")
.unwrap()
.is_websocket());
assert!(RPC::from_url("ws://example.com/").unwrap().is_websocket());
assert!(RPC::from_url("wss://example.com/").unwrap().is_websocket());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/evm/src/registry_filter_sol.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::helpers::{SignerProvider, WithChainId, RpcWSClient};
use crate::helpers::{RpcWSClient, SignerProvider, WithChainId};
use actix::prelude::*;
use alloy::{
primitives::{Address, Bytes, U256},
Expand Down
Loading