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

Docs & Enclave initialization #197

Merged
merged 22 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -51,6 +51,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"
tokio = { version = "1.38", features = ["full"] }
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
2 changes: 2 additions & 0 deletions packages/ciphernode/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ anyhow = { workspace = true }
serde = { workspace = true }
figment = { workspace = true }
alloy = { 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;

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
Expand All @@ -20,6 +24,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 @@ -63,12 +124,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 @@ -254,10 +322,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")
ryardley marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
Loading
Loading