Skip to content

Commit

Permalink
Merge branch 'main' into 181-automated-deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
hmzakhalid committed Dec 10, 2024
2 parents d155714 + 3a89686 commit 70d02dd
Show file tree
Hide file tree
Showing 21 changed files with 665 additions and 132 deletions.
79 changes: 55 additions & 24 deletions packages/ciphernode/Cargo.lock

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

1 change: 1 addition & 0 deletions packages/ciphernode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ num = "0.4.3"
rand_chacha = "0.3.1"
rand = "0.8.5"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = { version = "1.0.133" }
sled = "0.34.7"
sha2 = "0.10.8"
tempfile = "3.14.0"
Expand Down
14 changes: 14 additions & 0 deletions packages/ciphernode/cipher/src/password_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait PasswordManager {
async fn get_key(&self) -> Result<Zeroizing<Vec<u8>>>;
async fn delete_key(&mut self) -> Result<()>;
async fn set_key(&mut self, contents: Zeroizing<Vec<u8>>) -> Result<()>;
fn is_set(&self) -> bool;
}

pub struct InMemPasswordManager(pub Option<Zeroizing<Vec<u8>>>);
Expand Down Expand Up @@ -54,6 +55,10 @@ impl PasswordManager for EnvPasswordManager {
self.0 = None;
Ok(())
}

fn is_set(&self) -> bool {
self.0.is_some()
}
}

#[async_trait]
Expand All @@ -73,6 +78,10 @@ impl PasswordManager for InMemPasswordManager {
self.0 = None;
Ok(())
}

fn is_set(&self) -> bool {
self.0.is_some()
}
}

pub struct FilePasswordManager {
Expand Down Expand Up @@ -149,6 +158,11 @@ impl PasswordManager for FilePasswordManager {

Ok(())
}

fn is_set(&self) -> bool {
let path = &self.path;
path.exists()
}
}

fn ensure_file_permissions(path: &PathBuf, perms: u32) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions packages/ciphernode/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ shellexpand = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
url = { workspace = true }

72 changes: 71 additions & 1 deletion packages/ciphernode/config/src/app_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloy::primitives::Address;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use figment::{
providers::{Format, Serialized, Yaml},
Expand All @@ -9,6 +12,7 @@ use std::{
env,
path::{Path, PathBuf},
};
use url::Url;

use crate::yaml::load_yaml_with_env;

Expand All @@ -22,6 +26,63 @@ pub enum Contract {
AddressOnly(String),
}

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

impl RPC {
pub fn from_url(url: &str) -> Result<Self> {
let parsed = Url::parse(url).context("Invalid URL format")?;
match parsed.scheme() {
"http" => Ok(RPC::Http(url.to_string())),
"https" => Ok(RPC::Https(url.to_string())),
"ws" => Ok(RPC::Ws(url.to_string())),
"wss" => Ok(RPC::Wss(url.to_string())),
_ => bail!("Invalid protocol. Expected: http://, https://, ws://, wss://"),
}
}

pub fn as_http_url(&self) -> Result<String> {
match self {
RPC::Http(url) | RPC::Https(url) => Ok(url.clone()),
RPC::Ws(url) | RPC::Wss(url) => {
let mut parsed =
Url::parse(url).context(format!("Failed to parse URL: {}", url))?;
parsed
.set_scheme(if self.is_secure() { "https" } else { "http" })
.map_err(|_| anyhow!("http(s) are valid schemes"))?;
Ok(parsed.to_string())
}
}
}

pub fn as_ws_url(&self) -> Result<String> {
match self {
RPC::Ws(url) | RPC::Wss(url) => Ok(url.clone()),
RPC::Http(url) | RPC::Https(url) => {
let mut parsed =
Url::parse(url).context(format!("Failed to parse URL: {}", url))?;
parsed
.set_scheme(if self.is_secure() { "wss" } else { "ws" })
.map_err(|_| anyhow!("ws(s) are valid schemes"))?;
Ok(parsed.to_string())
}
}
}

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

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

impl Contract {
pub fn address(&self) -> &String {
use Contract::*;
Expand Down Expand Up @@ -65,12 +126,19 @@ impl Default for RpcAuth {
pub struct ChainConfig {
pub enabled: Option<bool>,
pub name: String,
pub rpc_url: String, // We may need multiple per chain for redundancy at a later point
rpc_url: String, // We may need multiple per chain for redundancy at a later point
#[serde(default)]
pub rpc_auth: RpcAuth,
pub contracts: ContractAddresses,
}

impl ChainConfig {
pub fn rpc_url(&self) -> Result<RPC> {
Ok(RPC::from_url(&self.rpc_url)
.map_err(|e| anyhow!("Failed to parse RPC URL for chain {}: {}", self.name, e))?)
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AppConfig {
/// The chains config
Expand Down Expand Up @@ -272,10 +340,12 @@ fn expand_tilde(path: &Path) -> PathBuf {
struct OsDirs;
impl OsDirs {
pub fn config_dir() -> PathBuf {
// TODO: handle unwrap error case
dirs::config_dir().unwrap().join("enclave")
}

pub fn data_dir() -> PathBuf {
// TODO: handle unwrap error case
dirs::data_local_dir().unwrap().join("enclave")
}
}
Expand Down
Loading

0 comments on commit 70d02dd

Please sign in to comment.