From ac883f29cea60e27e99aa27f23858635a0ba40d2 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Tue, 2 Apr 2024 21:47:09 +0100 Subject: [PATCH] Lint and format --- Cargo.lock | 2 + checked_cli/src/fetch.rs | 9 +- checked_cli/src/hc_client.rs | 150 +++++++++++------- checked_cli/src/lib.rs | 6 +- .../zomes/coordinator/fetch/src/lib.rs | 2 +- .../zomes/coordinator/signing_keys/Cargo.toml | 1 + .../signing_keys/src/verification_key_dist.rs | 1 + .../zomes/integrity/signing_keys/Cargo.toml | 1 + .../signing_keys/src/verification_key_dist.rs | 5 +- package.json | 4 +- types/checked_types/src/lib.rs | 3 +- types/signing_keys/src/lib.rs | 1 + 12 files changed, 119 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c5d81a..45dd7dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2218,6 +2218,7 @@ dependencies = [ name = "signing_keys" version = "0.0.1" dependencies = [ + "checked_types", "chrono", "hdk", "minisign-verify", @@ -2231,6 +2232,7 @@ name = "signing_keys_integrity" version = "0.0.1" dependencies = [ "anyhow", + "checked_types", "chrono", "hdi", "hex", diff --git a/checked_cli/src/fetch.rs b/checked_cli/src/fetch.rs index de9b7a5..d112625 100644 --- a/checked_cli/src/fetch.rs +++ b/checked_cli/src/fetch.rs @@ -112,7 +112,11 @@ pub async fn fetch(fetch_args: FetchArgs) -> anyhow::Result<()> { println!("Downloaded to {:?}", path); - let file = fetch_url.path_segments().ok_or_else(|| anyhow::anyhow!("Invalid URL"))?.last().ok_or_else(|| anyhow::anyhow!("Invalid URL"))?; + let file = fetch_url + .path_segments() + .ok_or_else(|| anyhow::anyhow!("Invalid URL"))? + .last() + .ok_or_else(|| anyhow::anyhow!("Invalid URL"))?; std::fs::rename(path.clone(), file)?; let decision = dialoguer::Confirm::new() @@ -189,12 +193,13 @@ where } async fn report_progress(state: Arc) -> anyhow::Result<()> { - if let Err(_) = tokio::time::timeout(std::time::Duration::from_secs(5), async { + if tokio::time::timeout(std::time::Duration::from_secs(5), async { while state.asset_size.load(std::sync::atomic::Ordering::Relaxed) == 0 { tokio::time::sleep(std::time::Duration::from_millis(100)).await; } }) .await + .is_err() { return Ok(()); } diff --git a/checked_cli/src/hc_client.rs b/checked_cli/src/hc_client.rs index fc14189..b5dc49e 100644 --- a/checked_cli/src/hc_client.rs +++ b/checked_cli/src/hc_client.rs @@ -1,13 +1,16 @@ -use std::fs::{File, Permissions}; -use std::io::Write; -use holochain_client::{AdminWebsocket, AppAgentWebsocket, AppStatusFilter, AuthorizeSigningCredentialsPayload, ClientAgentSigner, SigningCredentials}; +use crate::common::get_store_dir; +use holochain_client::{ + AdminWebsocket, AppAgentWebsocket, AppStatusFilter, AuthorizeSigningCredentialsPayload, + ClientAgentSigner, SigningCredentials, +}; use holochain_conductor_api::CellInfo; use holochain_types::prelude::{AgentPubKey, CapSecret, CellId}; use holochain_types::websocket::AllowedOrigins; use serde::{Deserialize, Serialize}; -use crate::common::get_store_dir; +use std::fs::{File, Permissions}; +use std::io::Write; -const DEFAULT_INSTALLED_APP_ID: &'static str = "checked"; +const DEFAULT_INSTALLED_APP_ID: &str = "checked"; pub async fn get_authenticated_app_agent_client() -> anyhow::Result { // TODO connect timeout not configurable! Really slow if Holochain is not running. @@ -18,31 +21,41 @@ pub async fn get_authenticated_app_agent_client() -> anyhow::Result anyhow::Result { - let app_interfaces = admin_client.list_app_interfaces().await.map_err(|e| anyhow::anyhow!("Error listing app interfaces: {:?}", e))?; + let app_interfaces = admin_client + .list_app_interfaces() + .await + .map_err(|e| anyhow::anyhow!("Error listing app interfaces: {:?}", e))?; // The client doesn't tell us what origins are set for each app interface so we have to pick one. let app_port = match app_interfaces.first() { - Some(app_port) => { - *app_port - } - None => { - admin_client.attach_app_interface(0, AllowedOrigins::Any).await.map_err(|e| anyhow::anyhow!("Error attaching app interface: {:?}", e))? - } + Some(app_port) => *app_port, + None => admin_client + .attach_app_interface(0, AllowedOrigins::Any) + .await + .map_err(|e| anyhow::anyhow!("Error attaching app interface: {:?}", e))?, }; Ok(app_port) } -async fn load_or_create_signing_credentials(mut admin_client: &mut AdminWebsocket, signer: &mut ClientAgentSigner) -> anyhow::Result<()> { +async fn load_or_create_signing_credentials( + admin_client: &mut AdminWebsocket, + signer: &mut ClientAgentSigner, +) -> anyhow::Result<()> { match try_load_credentials()? { Some((cell_id, credentials)) => { signer.add_credentials(cell_id, credentials); } None => { - let (cell_id, credentials) = create_new_credentials(&mut admin_client).await?; + let (cell_id, credentials) = create_new_credentials(admin_client).await?; dump_credentials(cell_id.clone(), &credentials)?; signer.add_credentials(cell_id, credentials); } @@ -50,29 +63,42 @@ async fn load_or_create_signing_credentials(mut admin_client: &mut AdminWebsocke Ok(()) } -async fn create_new_credentials(client: &mut AdminWebsocket) -> anyhow::Result<(CellId, SigningCredentials)> { - let apps = client.list_apps(Some(AppStatusFilter::Running)).await.map_err(|e| anyhow::anyhow!("Error listing apps: {:?}", e))?; - - let app = apps.iter().find(|app| { - // TODO allow this to be overridden on the CLI. - app.installed_app_id == DEFAULT_INSTALLED_APP_ID - }).ok_or_else(|| anyhow::anyhow!("App `checked` not found"))?; - - let cells = app.cell_info.get("checked").ok_or_else(|| anyhow::anyhow!("Role `checked` not found"))?; - - let cell = cells.iter().find_map(|cell| { - match cell { - CellInfo::Provisioned(cell) if cell.name == "checked" => { - Some(cell) - } - _ => None - } - }).ok_or_else(|| anyhow::anyhow!("Cell `checked` not found"))?; - - let credentials = client.authorize_signing_credentials(AuthorizeSigningCredentialsPayload { - cell_id: cell.cell_id.clone(), - functions: None, // For all, not documented! - }).await.map_err(|e| anyhow::anyhow!("Error authorizing signing credentials: {:?}", e))?; +async fn create_new_credentials( + client: &mut AdminWebsocket, +) -> anyhow::Result<(CellId, SigningCredentials)> { + let apps = client + .list_apps(Some(AppStatusFilter::Running)) + .await + .map_err(|e| anyhow::anyhow!("Error listing apps: {:?}", e))?; + + let app = apps + .iter() + .find(|app| { + // TODO allow this to be overridden on the CLI. + app.installed_app_id == DEFAULT_INSTALLED_APP_ID + }) + .ok_or_else(|| anyhow::anyhow!("App `checked` not found"))?; + + let cells = app + .cell_info + .get("checked") + .ok_or_else(|| anyhow::anyhow!("Role `checked` not found"))?; + + let cell = cells + .iter() + .find_map(|cell| match cell { + CellInfo::Provisioned(cell) if cell.name == "checked" => Some(cell), + _ => None, + }) + .ok_or_else(|| anyhow::anyhow!("Cell `checked` not found"))?; + + let credentials = client + .authorize_signing_credentials(AuthorizeSigningCredentialsPayload { + cell_id: cell.cell_id.clone(), + functions: None, // For all, not documented! + }) + .await + .map_err(|e| anyhow::anyhow!("Error authorizing signing credentials: {:?}", e))?; Ok((cell.cell_id.clone(), credentials)) } @@ -85,27 +111,40 @@ struct SavedCredentials { cap_secret: CapSecret, } -fn dump_credentials(cell_id: CellId, signing_credentials: &SigningCredentials) -> anyhow::Result<()> { +fn dump_credentials( + cell_id: CellId, + signing_credentials: &SigningCredentials, +) -> anyhow::Result<()> { let saved = SavedCredentials { cell_id: cell_id.clone(), signing_agent_key: signing_credentials.signing_agent_key.clone(), keypair: signing_credentials.keypair.to_keypair_bytes().to_vec(), - cap_secret: signing_credentials.cap_secret.clone(), + cap_secret: signing_credentials.cap_secret, }; - let serialized = serde_json::to_string(&saved).map_err(|e| anyhow::anyhow!("Error serializing credentials: {:?}", e))?; + let serialized = serde_json::to_string(&saved) + .map_err(|e| anyhow::anyhow!("Error serializing credentials: {:?}", e))?; // generate_args.path let credentials_path = get_credentials_path()?; - let mut f= File::options().create(true).write(true).truncate(true).open(&credentials_path).map_err(|e| anyhow::anyhow!("Error opening credentials file: {:?}", e))?; + let mut f = File::options() + .create(true) + .write(true) + .truncate(true) + .open(credentials_path) + .map_err(|e| anyhow::anyhow!("Error opening credentials file: {:?}", e))?; if cfg!(unix) { use std::os::unix::fs::PermissionsExt; - f.set_permissions(Permissions::from_mode(0o660)).map_err(|e| anyhow::anyhow!("Error setting permissions on credentials file: {:?}", e))?; + f.set_permissions(Permissions::from_mode(0o660)) + .map_err(|e| { + anyhow::anyhow!("Error setting permissions on credentials file: {:?}", e) + })?; } - f.write_all(serialized.as_bytes()).map_err(|e| anyhow::anyhow!("Error writing credentials file: {:?}", e))?; + f.write_all(serialized.as_bytes()) + .map_err(|e| anyhow::anyhow!("Error writing credentials file: {:?}", e))?; Ok(()) } @@ -113,7 +152,7 @@ fn dump_credentials(cell_id: CellId, signing_credentials: &SigningCredentials) - fn try_load_credentials() -> anyhow::Result> { let credentials_path = get_credentials_path()?; - let f = match File::open(&credentials_path) { + let f = match File::open(credentials_path) { Ok(f) => f, Err(e) if e.kind() == std::io::ErrorKind::NotFound => { return Ok(None); @@ -127,23 +166,28 @@ fn try_load_credentials() -> anyhow::Result Ok(saved) => saved, Err(e) => { eprintln!("Saved credentials file is corrupt: {:?}", e); - return Ok(None) + return Ok(None); } }; - let keypair = match ed25519_dalek::SigningKey::from_keypair_bytes(saved.keypair.as_slice().try_into().unwrap()) { + let keypair = match ed25519_dalek::SigningKey::from_keypair_bytes( + saved.keypair.as_slice().try_into().unwrap(), + ) { Ok(keypair) => keypair, Err(e) => { eprintln!("Saved credentials file is corrupt: {:?}", e); - return Ok(None) + return Ok(None); } }; - Ok(Some((saved.cell_id, SigningCredentials { - signing_agent_key: saved.signing_agent_key, - keypair, - cap_secret: saved.cap_secret, - }))) + Ok(Some(( + saved.cell_id, + SigningCredentials { + signing_agent_key: saved.signing_agent_key, + keypair, + cap_secret: saved.cap_secret, + }, + ))) } fn get_credentials_path() -> anyhow::Result { diff --git a/checked_cli/src/lib.rs b/checked_cli/src/lib.rs index 41c7d11..1fcf0ad 100644 --- a/checked_cli/src/lib.rs +++ b/checked_cli/src/lib.rs @@ -1,17 +1,17 @@ mod common; pub mod cli; +mod fetch; pub mod generate; +pub(crate) mod hc_client; mod password; pub mod sign; pub mod verify; -mod fetch; -pub(crate) mod hc_client; pub mod prelude { pub use crate::cli::{Cli, Commands, GenerateArgs, SignArgs}; + pub use crate::fetch::fetch; pub use crate::generate::generate; pub use crate::sign::sign; pub use crate::verify::verify; - pub use crate::fetch::fetch; } diff --git a/dnas/checked/zomes/coordinator/fetch/src/lib.rs b/dnas/checked/zomes/coordinator/fetch/src/lib.rs index 3a61e37..5931e4e 100644 --- a/dnas/checked/zomes/coordinator/fetch/src/lib.rs +++ b/dnas/checked/zomes/coordinator/fetch/src/lib.rs @@ -1,5 +1,5 @@ -use fetch_integrity::prelude::*; use checked_types::*; +use fetch_integrity::prelude::*; use hdk::prelude::hash_type::AnyLinkable; use hdk::prelude::*; use rand::prelude::IteratorRandom; diff --git a/dnas/checked/zomes/coordinator/signing_keys/Cargo.toml b/dnas/checked/zomes/coordinator/signing_keys/Cargo.toml index b6f6832..2372016 100644 --- a/dnas/checked/zomes/coordinator/signing_keys/Cargo.toml +++ b/dnas/checked/zomes/coordinator/signing_keys/Cargo.toml @@ -15,4 +15,5 @@ minisign-verify = { workspace = true } chrono = { workspace = true } nanoid = { workspace = true } +checked_types = { workspace = true } signing_keys_integrity = { workspace = true } diff --git a/dnas/checked/zomes/coordinator/signing_keys/src/verification_key_dist.rs b/dnas/checked/zomes/coordinator/signing_keys/src/verification_key_dist.rs index 55eac77..4548334 100644 --- a/dnas/checked/zomes/coordinator/signing_keys/src/verification_key_dist.rs +++ b/dnas/checked/zomes/coordinator/signing_keys/src/verification_key_dist.rs @@ -1,4 +1,5 @@ use crate::{convert_to_app_entry_type, key_collection::get_key_collections_reference_count}; +use checked_types::*; use hdk::prelude::*; use signing_keys_integrity::prelude::*; diff --git a/dnas/checked/zomes/integrity/signing_keys/Cargo.toml b/dnas/checked/zomes/integrity/signing_keys/Cargo.toml index ed34ce9..9fc03c9 100644 --- a/dnas/checked/zomes/integrity/signing_keys/Cargo.toml +++ b/dnas/checked/zomes/integrity/signing_keys/Cargo.toml @@ -17,4 +17,5 @@ regex = { workspace = true } chrono = { workspace = true } anyhow = { workspace = true } +checked_types = { workspace = true } signing_keys_types = { workspace = true } diff --git a/dnas/checked/zomes/integrity/signing_keys/src/verification_key_dist.rs b/dnas/checked/zomes/integrity/signing_keys/src/verification_key_dist.rs index 8b180af..e902682 100644 --- a/dnas/checked/zomes/integrity/signing_keys/src/verification_key_dist.rs +++ b/dnas/checked/zomes/integrity/signing_keys/src/verification_key_dist.rs @@ -2,10 +2,9 @@ use crate::convert::try_extract_entry_to_app_type; use crate::prelude::*; +use checked_types::VerificationKeyType; use hdi::prelude::*; -use signing_keys_types::{ - MarkVfKeyDistOpt, VerificationKeyDist, VerificationKeyDistMark, VerificationKeyType, -}; +use signing_keys_types::{MarkVfKeyDistOpt, VerificationKeyDist, VerificationKeyDistMark}; pub const VERIFICATION_KEY_NAME_MIN_LENGTH: usize = 3; pub const MAX_VF_KEY_DIST_COMPROMISED_NOTE_LENGTH: usize = 120; diff --git a/package.json b/package.json index 62921d1..d2f8af0 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "package": "npm run build:happ && npm run package -w ui && hc web-app pack workdir --recursive", "build:happ": "npm run build:zomes && hc app pack workdir --recursive", "build:zomes": "RUSTFLAGS='' CARGO_TARGET_DIR=target cargo build --release --target wasm32-unknown-unknown", - "lint": "npm run -w ui lint && npm run -w tests lint && cargo clippy", - "format": "npm run -w ui format && npm run -w tests format && cargo fmt" + "lint": "npm run -w ui lint && npm run -w tests lint && cargo clippy && cargo clippy --manifest-path ./checked_cli/Cargo.toml", + "format": "npm run -w ui format && npm run -w tests format && cargo fmt && cargo fmt --manifest-path ./checked_cli/Cargo.toml" }, "devDependencies": { "@holochain-playground/cli": "^0.1.1", diff --git a/types/checked_types/src/lib.rs b/types/checked_types/src/lib.rs index 5cf48bb..7c0fd86 100644 --- a/types/checked_types/src/lib.rs +++ b/types/checked_types/src/lib.rs @@ -1,5 +1,5 @@ -use serde::{Serialize, Deserialize}; use holochain_zome_types::prelude::AgentPubKey; +use serde::{Deserialize, Serialize}; /// Supported key types for verification keys. #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] @@ -8,7 +8,6 @@ pub enum VerificationKeyType { MiniSignEd25519, } - #[derive(Serialize, Deserialize, Debug)] pub struct PrepareFetchRequest { pub fetch_url: String, diff --git a/types/signing_keys/src/lib.rs b/types/signing_keys/src/lib.rs index abf4f1f..3cbea2d 100644 --- a/types/signing_keys/src/lib.rs +++ b/types/signing_keys/src/lib.rs @@ -1,3 +1,4 @@ +use checked_types::VerificationKeyType; use chrono::{DateTime, Utc}; use hdk::prelude::*;