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
Add Auth Methods for http and ws
  • Loading branch information
hmzakhalid committed Nov 29, 2024
commit d86468f11aa3c5bdb7c7e6e122744a3f095a09e0
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.

7 changes: 4 additions & 3 deletions packages/ciphernode/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ alloy-primitives = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
base64 = { workspace = true }
enclave-core = { path = "../core" }
cipher = { path = "../cipher" }
config = { path = "../config" }
data = { path = "../data" }
enclave-core = { path = "../core" }
futures-util = { workspace = true }
sortition = { path = "../sortition" }
cipher = { path = "../cipher" }
serde = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }
url = { workspace = true }
zeroize = { workspace = true }

Expand Down
73 changes: 72 additions & 1 deletion packages/ciphernode/evm/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@ use alloy::{
},
Identity, Provider, ProviderBuilder, RootProvider,
},
pubsub::PubSubFrontend,
rpc::client::RpcClient,
signers::local::PrivateKeySigner,
transports::{BoxTransport, Transport},
transports::{
http::{
reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION},
Client,
},
Http,
},
ws::WsConnect,
Authorization, BoxTransport, Transport,
},
};
use anyhow::{bail, Context, Result};
use base64::{engine::general_purpose::STANDARD, Engine};
use cipher::Cipher;
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 @@ -110,6 +124,63 @@ where
}
}

#[derive(Clone)]
pub enum RpcAuth {
None,
Basic {
username: String,
password: String,
},
Bearer(String)
}

impl RpcAuth {
fn to_header_value(&self) -> Option<HeaderValue> {
match self {
RpcAuth::None => None,
RpcAuth::Basic { username, password } => {
let auth = format!(
"Basic {}",
STANDARD.encode(format!("{}:{}", username, password))
);
HeaderValue::from_str(&auth).ok()
hmzakhalid marked this conversation as resolved.
Show resolved Hide resolved
}
RpcAuth::Bearer(token) => HeaderValue::from_str(&format!("Bearer {}", token)).ok(),
}
}

fn to_ws_auth(&self) -> Option<Authorization> {
match self {
RpcAuth::None => None,
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 {
ConfigRpcAuth::None => RpcAuth::None,
ConfigRpcAuth::Basic { username, password } => RpcAuth::Basic { username, password },
ConfigRpcAuth::Bearer(token) => RpcAuth::Bearer(token),
}
}
}

impl From<RpcAuth> for ConfigRpcAuth {
fn from(value: RpcAuth) -> Self {
match value {
RpcAuth::None => ConfigRpcAuth::None,
RpcAuth::Basic { username, password } => ConfigRpcAuth::Basic { username, password },
RpcAuth::Bearer(token) => ConfigRpcAuth::Bearer(token),
}
}
}

pub type ReadonlyProvider = RootProvider<BoxTransport>;

pub async fn create_readonly_provider(
Expand Down
Loading