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

cleaning up the codebase #285

Merged
merged 8 commits into from
Dec 20, 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
74 changes: 39 additions & 35 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,52 @@ mediator-coordination = { path = "./crates/web-plugins/didcomm-messaging/protoco
# Other common dependencies
serde = "1.0"
sha2 = "0.10"
cfg-if = "0.1"
cfg-if = "1.0"
getrandom = "0.2"
hyper-tls = "0.5.0"
json-patch = "1.0.0"
x25519-dalek = "2.0.0-rc.3"
multibase = "0.8.0"
hyper-tls = "0.6.0"
json-patch = "3.0.1"
x25519-dalek = "2.0.1"
multibase = "0.9.1"
json-canon = "0.1.3"
qrcode = "0.12.0"
image = "0.23"
reqwest = "0.11"
qrcode = "0.14.1"
image = "0.25"
reqwest = "0.12"
tempdir = "0.3.7"
headers = "0.3"
thiserror = "1.0.48"
url = "2.4.1"
num-bigint = "0.4.4"
base64 = "0.13.0"
headers = "0.4"
thiserror = "2.0.7"
url = "2.5.4"
num-bigint = "0.4.6"
base64 = "0.22.1"
hex = "0.4.3"
eyre = "0.6"
anyhow = "1"
subtle = "2.5.0"
regex = "1.10.2"
mongodb = "2.7.1"
once_cell = "1.20.0"
tower = "0.4"
nix = "0.22.0"
uuid = "1.4.1"
axum = "0.6.20"
tokio = "1.30.0"
tracing = "0.1.37"
chrono = "0.4.26"
subtle = "2.6.1"
regex = "1.11.1"
mongodb = "3.1.1"
once_cell = "1.20.2"
tower = "0.5"
nix = "0.29.0"
uuid = "1.11.0"
axum = "0.7.9"
tokio = "1.42.0"
tracing = "0.1.41"
chrono = "0.4.39"
paste = "1.0"
didcomm = "0.4.1"
hyper = "0.14.27"
lazy_static = "1.4.0"
async-trait = "0.1.73"
dotenv-flow = "0.15.0"
hyper = "1.5.2"
hyper-util = "0.1"
http-body-util = "0.1"
lazy_static = "1.5.0"
async-trait = "0.1.83"
dotenv-flow = "0.16.2"
serde_json = "1.0"
parking_lot = "0.12.0"
curve25519-dalek = "4.0.0-rc.3"
ed25519-dalek = "2.0.0-rc.3"
tracing-subscriber = "0.3.17"
tower-http = "0.4.3"
parking_lot = "0.12.3"
curve25519-dalek = "4.1.3"
ed25519-dalek = "2.1.1"
tracing-subscriber = "0.3.19"
tower-http = "0.6.2"
base64ct = { version = "1.6.0", default-features = false }
zeroize = { version = "1.6.0", default-features = false }
zeroize = { version = "1.8.1", default-features = false }


[dependencies]
Expand All @@ -104,6 +107,7 @@ tracing.workspace = true
lazy_static.workspace = true
serde_json.workspace = true
hyper.workspace = true
http-body-util.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing-subscriber = { workspace = true, features = ["json"] }
tower-http = { workspace = true, features = ["catch-panic", "trace", "cors"] }
Expand Down Expand Up @@ -135,4 +139,4 @@ mediator-coordination = ["plugin-didcomm_messaging", "didcomm-messaging/mediator


[dev-dependencies]
tower = { version = "0.4.13", features = ["util"] }
tower = { version = "0.5.2", features = ["util"] }
15 changes: 7 additions & 8 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
let collection = self.get_collection();

// Lock the Mutex and get the Collection
let mut cursor = collection.read().await.find(None, None).await?;
let mut cursor = collection.read().await.find(doc! {}).await?;
while cursor.advance().await? {
entities.push(cursor.deserialize_current()?);
}
Expand All @@ -91,7 +91,7 @@ where
// Lock the Mutex and get the Collection
let collection = collection.read().await;
Ok(collection
.count_documents(filter, None)
.count_documents(filter)
.await?
.try_into()
.map_err(|_| RepositoryError::Generic("count overflow".to_owned()))?)
Expand All @@ -108,7 +108,7 @@ where

// Lock the Mutex and get the Collection
let collection = collection.read().await;
Ok(collection.find_one(filter, None).await?)
Ok(collection.find_one(filter).await?)
}

/// Stores a new entity.
Expand All @@ -119,7 +119,7 @@ where
let collection = collection.read().await;

// Insert the new entity into the database
let metadata = collection.insert_one(entity.clone(), None).await?;
let metadata = collection.insert_one(entity.clone()).await?;

// Set the ID if it was inserted and return the updated entity
if let Bson::ObjectId(oid) = metadata.inserted_id {
Expand All @@ -144,7 +144,7 @@ where
let collection = collection.read().await;

// Retrieve all entities from the database
let mut cursor = collection.find(filter, find_options).await?;
let mut cursor = collection.find(filter).with_options(find_options).await?;
while cursor.advance().await? {
entities.push(cursor.deserialize_current()?);
}
Expand All @@ -160,7 +160,7 @@ where
let collection = collection.read().await;

// Delete the entity from the database
collection.delete_one(doc! {"_id": id}, None).await?;
collection.delete_one(doc! {"_id": id}).await?;

Ok(())
}
Expand All @@ -179,8 +179,7 @@ where
let metadata = collection
.update_one(
doc! {"_id": entity.id().unwrap()},
doc! {"$set": bson::to_document(&entity).map_err(|_| RepositoryError::BsonConversionError)?},
None,
doc! {"$set": bson::to_document(&entity).map_err(|_| RepositoryError::BsonConversionError)?}
)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion crates/filesystem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
nix.workspace = true
nix ={ workspace = true, features = ["fs"] }

[features]
test-utils = []
14 changes: 7 additions & 7 deletions crates/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use nix::fcntl::{flock, FlockArg};
use nix::fcntl::{Flock, FlockArg};
use std::{
fs::OpenOptions,
io::{Error as IoError, ErrorKind, Result as IoResult},
os::unix::io::AsRawFd,
path::Path,
};

#[doc(hidden)]
// Define a trait for file system operations
pub trait FileSystem: Send + Sync + 'static {
pub trait FileSystem: Send + 'static {
fn read_to_string(&self, path: &Path) -> IoResult<String>;
fn write(&mut self, path: &Path, content: &str) -> IoResult<()>;
fn read_dir_files(&self, path: &Path) -> IoResult<Vec<String>>;
Expand Down Expand Up @@ -54,22 +53,23 @@ impl FileSystem for StdFileSystem {
let file = OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.create(true)
.open(&path)?;
.open(path)?;

// Acquire an exclusive lock before writing to the file
flock(file.as_raw_fd(), FlockArg::LockExclusive)
let file = Flock::lock(file, FlockArg::LockExclusive)
.map_err(|_| IoError::new(ErrorKind::Other, "Error acquiring file lock"))?;

std::fs::write(path, &content).map_err(|_| {
std::fs::write(path, content).map_err(|_| {
IoError::new(
ErrorKind::Other,
"Error saving base64-encoded image to file",
)
})?;

// Release the lock after writing to the file
flock(file.as_raw_fd(), FlockArg::Unlock)
file.unlock()
.map_err(|_| IoError::new(ErrorKind::Other, "Error releasing file lock"))?;

Ok(())
Expand Down
11 changes: 7 additions & 4 deletions crates/keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ where
collection: Collection<T>,
}

impl Default for KeyStore<Secrets> {
fn default() -> Self {
Self::new()
}
}

impl KeyStore<Secrets> {
/// Create a new keystore with default Secrets type.
///
Expand All @@ -54,10 +60,7 @@ impl KeyStore<Secrets> {
let db_lock = db.write().await;
db_lock.collection::<Secrets>("secrets").clone()
};
let collection = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(task)
});
collection
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(task))
})
.clone();

Expand Down
5 changes: 3 additions & 2 deletions crates/web-plugins/did-endpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ serde_json.workspace = true
dotenv-flow.workspace = true
multibase.workspace = true
tracing.workspace = true
http-body-util.workspace = true
uuid = { workspace = true, features = ["v4"] }
hyper = { workspace = true, features = ["full"] }
tokio = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }

[dev-dependencies]
async-trait.workspace = true
mockall = "0.13.0"
mockall = "0.13.1"
json-canon = "0.1.3"
filesystem = { workspace = true, features = ["test-utils"] }
keystore = { workspace = true, features = ["test-utils"] }
tower = { version = "0.4.13", features = ["util"] }
tower = { version = "0.5.2", features = ["util"] }
2 changes: 1 addition & 1 deletion crates/web-plugins/did-endpoint/src/didgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where

// Create directory and write the DID document
filesystem
.create_dir_all(&storage_dirpath)
.create_dir_all(storage_dirpath)
.map_err(|_| Error::PersistenceError)?;
filesystem
.write(&storage_dirpath.join("did.json"), &pretty_diddoc)
Expand Down
15 changes: 8 additions & 7 deletions crates/web-plugins/did-endpoint/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn diddoc(State(state): State<Arc<DidEndPointState>>) -> Result<Json<Value
let did_path = Path::new(&storage_dirpath).join("did.json");

match filesystem.read_to_string(&did_path).as_ref() {
Ok(content) => Ok(Json(serde_json::from_str(&content).map_err(|_| {
Ok(content) => Ok(Json(serde_json::from_str(content).map_err(|_| {
tracing::error!("Unparseable did.json");
StatusCode::NOT_FOUND
})?)),
Expand All @@ -56,7 +56,7 @@ async fn didpop(
let diddoc: Document = serde_json::from_value(diddoc_value.clone()).unwrap();

let did_address = diddoc.id.clone();
let methods = diddoc.verification_method.clone().unwrap_or(vec![]);
let methods = diddoc.verification_method.clone().unwrap_or_default();

// Build verifiable credential (VC)
let vc: VerifiableCredential = serde_json::from_value(json!({
Expand Down Expand Up @@ -157,15 +157,15 @@ async fn didpop(
fn inspect_vm_relationship(diddoc: &Document, vm_id: &str) -> Option<String> {
let vrel = [
(
json!(diddoc.authentication.clone().unwrap_or(vec![])),
json!(diddoc.authentication.clone().unwrap_or_default()),
String::from("authentication"),
),
(
json!(diddoc.assertion_method.clone().unwrap_or(vec![])),
json!(diddoc.assertion_method.clone().unwrap_or_default()),
String::from("assertionMethod"),
),
(
json!(diddoc.key_agreement.clone().unwrap_or(vec![])),
json!(diddoc.key_agreement.clone().unwrap_or_default()),
String::from("keyAgreement"),
),
];
Expand Down Expand Up @@ -199,6 +199,7 @@ mod tests {
proof::{CryptoProof, EdDsaJcs2022},
vc::VerifiablePresentation,
};
use http_body_util::BodyExt;
use serde_json::json;
use tower::util::ServiceExt;

Expand Down Expand Up @@ -284,8 +285,8 @@ mod tests {

assert_eq!(response.status(), StatusCode::OK);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let vp: VerifiablePresentation = serde_json::from_slice(&body).unwrap();
let body = BodyExt::collect(response.into_body()).await.unwrap();
let vp: VerifiablePresentation = serde_json::from_slice(&body.to_bytes()).unwrap();

let vc = vp.verifiable_credential.get(0).unwrap();
let diddoc = serde_json::from_value(json!(vc.credential_subject)).unwrap();
Expand Down
7 changes: 3 additions & 4 deletions crates/web-plugins/didcomm-messaging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ tracing.workspace = true
once_cell.workspace = true
serde_json.workspace = true
thiserror.workspace = true
http-body-util.workspace = true
tokio = { workspace = true, features = ["full"] }
hyper = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }
serde = { version = "1.0", features = ["derive"] }


[features]
Expand All @@ -50,12 +50,11 @@ mediator-coordination = ["dep:mediator-coordination"]

[dev-dependencies]
async-trait.workspace = true
mockall = "0.13.0"
uuid = { workspace = true, features = ["v4"] }
json-canon = "0.1.3"
shared = { workspace = true, features = ["test-utils"] }
tokio = { version = "1.27.0", default-features = false, features = [
tokio = { version = "1.42.0", default-features = false, features = [
"macros",
"rt",
] }
tower = { version = "0.4.13", features = ["util"] }
tower = { version = "0.5.2", features = ["util"] }
13 changes: 8 additions & 5 deletions crates/web-plugins/didcomm-messaging/did-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ edition = "2021"

[dependencies]
serde_json.workspace = true
chrono = { workspace = true, features = ["serde"] }
hyper = { workspace = true, features = ["client", "http2"] }
tokio.workspace = true
serde.workspace = true
axum.workspace = true
hyper-tls.workspace = true
http-body-util.workspace = true
hyper-util ={ workspace = true, features = ["full"] }
hyper = { workspace = true, features = ["full"] }
chrono = { workspace = true, features = ["serde"] }

# Cross-platform random number generator from os
getrandom = { workspace = true, features = ["js"] }
Expand Down Expand Up @@ -38,10 +41,10 @@ subtle.workspace = true
regex.workspace = true

[dev-dependencies]
hyper = { version = "0.14.26", features = ["server"] }
async-std = { version = "1.12.0", features = ["attributes"] }
hyper = { version = "1.5.2", features = ["server"] }
async-std = { version = "1.13.0", features = ["attributes"] }
hex = "0.4.3"
tokio = { version = "1.27.0", default-features = false, features = [
tokio = { version = "1.42.0", default-features = false, features = [
"macros",
"rt",
] }
Loading
Loading