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
1 change: 1 addition & 0 deletions packages/ciphernode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use config::AppConfig;
use enclave_core::EventBus;
use evm::{
helpers::{
create_provider_with_signer, create_readonly_provider, ensure_http_rpc, ensure_ws_rpc,
get_signer_from_repository,
create_provider_with_signer, create_readonly_provider, get_signer_from_repository, RPC,
},
CiphernodeRegistrySol, EnclaveSol, RegistryFilterSol,
};
Expand Down Expand Up @@ -45,10 +44,10 @@ pub async fn setup_aggregator(
.iter()
.filter(|chain| chain.enabled.unwrap_or(true))
{
let rpc_url = &chain.rpc_url;
let read_provider = create_readonly_provider(&ensure_ws_rpc(rpc_url)).await?;
let rpc_url = RPC::from_url(&chain.rpc_url).unwrap();
let read_provider = create_readonly_provider(&rpc_url.as_ws_url()).await?;
let write_provider =
create_provider_with_signer(&ensure_http_rpc(rpc_url), &signer).await?;
create_provider_with_signer(&rpc_url.as_http_url(), &signer).await?;

hmzakhalid marked this conversation as resolved.
Show resolved Hide resolved
EnclaveSol::attach(
&bus,
Expand Down
6 changes: 3 additions & 3 deletions 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::{create_readonly_provider, ensure_ws_rpc},
helpers::{create_readonly_provider, RPC},
CiphernodeRegistrySol, EnclaveSolReader,
};
use logger::SimpleLogger;
Expand Down Expand Up @@ -44,9 +44,9 @@ pub async fn setup_ciphernode(
.iter()
.filter(|chain| chain.enabled.unwrap_or(true))
{
let rpc_url = &chain.rpc_url;
let rpc_url = RPC::from_url(&chain.rpc_url).unwrap();
hmzakhalid marked this conversation as resolved.
Show resolved Hide resolved

let read_provider = create_readonly_provider(&ensure_ws_rpc(rpc_url)).await?;
let read_provider = create_readonly_provider(&rpc_url.as_ws_url()).await?;
EnclaveSolReader::attach(
&bus,
&read_provider,
Expand Down
1 change: 1 addition & 0 deletions packages/ciphernode/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ alloy = { workspace = true }
alloy-primitives = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
base64 = { workspace = true }
enclave-core = { path = "../core" }
data = { path = "../data" }
futures-util = { workspace = true }
Expand Down
123 changes: 95 additions & 28 deletions packages/ciphernode/evm/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,61 @@ use alloy::{
signers::local::PrivateKeySigner,
transports::{BoxTransport, Transport},
};
use anyhow::{bail, Context, Result};
use anyhow::{bail,Context, Result};
use cipher::Cipher;
use data::Repository;
use std::{env, marker::PhantomData, sync::Arc};
use zeroize::Zeroizing;


#[derive(Clone)]
pub enum RPC {
Http(String),
Https(String),
Ws(String),
Wss(String),
}

impl RPC {
pub fn from_url(url: &str) -> Result<Self> {
if url.starts_with("http://") {
Ok(RPC::Http(url.to_string()))
} else if url.starts_with("https://") {
Ok(RPC::Https(url.to_string()))
} else if url.starts_with("ws://") {
Ok(RPC::Ws(url.to_string()))
} else if url.starts_with("wss://") {
Ok(RPC::Wss(url.to_string()))
} else {
bail!("Invalid RPC URL, Please specify a protocol. (http://, https://, ws://, wss://)");
}
}
hmzakhalid marked this conversation as resolved.
Show resolved Hide resolved

pub fn as_http_url(&self) -> String {
match self {
RPC::Http(url) | RPC::Https(url) => url.clone(),
RPC::Ws(url) => url.replacen("ws://", "http://", 1),
RPC::Wss(url) => url.replacen("wss://", "https://", 1),
}
}

pub fn as_ws_url(&self) -> String {
match self {
RPC::Http(url) => url.replacen("http://", "ws://", 1),
RPC::Https(url) => url.replacen("https://", "wss://", 1),
RPC::Ws(url) | RPC::Wss(url) => url.clone(),
}
}
hmzakhalid marked this conversation as resolved.
Show resolved Hide resolved

pub fn is_websocket(&self) -> bool {
matches!(self, RPC::Ws(_) | RPC::Wss(_))
}

pub fn is_secure(&self) -> bool {
matches!(self, RPC::Https(_) | RPC::Wss(_))
}
}

/// 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 @@ -117,40 +166,58 @@ pub async fn get_signer_from_repository(
Ok(Arc::new(signer))
}

pub fn ensure_http_rpc(rpc_url: &str) -> String {
if rpc_url.starts_with("ws://") {
return rpc_url.replacen("ws://", "http://", 1);
} else if rpc_url.starts_with("wss://") {
return rpc_url.replacen("wss://", "https://", 1);
}
rpc_url.to_string()
}

pub fn ensure_ws_rpc(rpc_url: &str) -> String {
if rpc_url.starts_with("http://") {
return rpc_url.replacen("http://", "ws://", 1);
} else if rpc_url.starts_with("https://") {
return rpc_url.replacen("https://", "wss://", 1);
}
rpc_url.to_string()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_ensure_http_rpc() {
assert_eq!(ensure_http_rpc("http://foo.com"), "http://foo.com");
assert_eq!(ensure_http_rpc("https://foo.com"), "https://foo.com");
assert_eq!(ensure_http_rpc("ws://foo.com"), "http://foo.com");
assert_eq!(ensure_http_rpc("wss://foo.com"), "https://foo.com");
fn test_rpc_type_conversion() {
// Test HTTP URLs
let http = RPC::from_url("http://localhost:8545").unwrap();
assert!(matches!(http, RPC::Http(_)));
assert_eq!(http.as_http_url(), "http://localhost:8545");
assert_eq!(http.as_ws_url(), "ws://localhost:8545");

// Test HTTPS URLs
let https = RPC::from_url("https://example.com").unwrap();
assert!(matches!(https, RPC::Https(_)));
assert_eq!(https.as_http_url(), "https://example.com");
assert_eq!(https.as_ws_url(), "wss://example.com");

// Test WS URLs
let ws = RPC::from_url("ws://localhost:8545").unwrap();
assert!(matches!(ws, RPC::Ws(_)));
assert_eq!(ws.as_http_url(), "http://localhost:8545");
assert_eq!(ws.as_ws_url(), "ws://localhost:8545");

// Test WSS URLs
let wss = RPC::from_url("wss://example.com").unwrap();
assert!(matches!(wss, RPC::Wss(_)));
assert_eq!(wss.as_http_url(), "https://example.com");
assert_eq!(wss.as_ws_url(), "wss://example.com");
}

#[test]
fn test_ensure_ws_rpc() {
assert_eq!(ensure_ws_rpc("http://foo.com"), "ws://foo.com");
assert_eq!(ensure_ws_rpc("https://foo.com"), "wss://foo.com");
assert_eq!(ensure_ws_rpc("wss://foo.com"), "wss://foo.com");
assert_eq!(ensure_ws_rpc("ws://foo.com"), "ws://foo.com");
fn test_rpc_type_properties() {
assert!(!RPC::from_url("http://example.com").unwrap().is_secure());
assert!(RPC::from_url("https://example.com")
.unwrap()
.is_secure());
assert!(!RPC::from_url("ws://example.com").unwrap().is_secure());
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("ws://example.com")
.unwrap()
.is_websocket());
assert!(RPC::from_url("wss://example.com")
.unwrap()
.is_websocket());
}
}
Loading