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

162 move the repository module of the mediator coordination crate to a separate crate #170

Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"crates/plugins/did-endpoint",
"crates/plugins/mediator-coordination",
"crates/plugins/oob-messages",
"crates/database",
]


Expand All @@ -24,6 +25,7 @@ plugin-api = { path = "./crates/plugin-api", version = "0.1.0" }
did-endpoint = { path = "./crates/plugins/did-endpoint", version = "0.1.0" }
oob-messages = { path = "./crates/plugins/oob-messages", version = "0.1.0" }
mediator-coordination = { path = "./crates/plugins/mediator-coordination", version = "0.1.0" }
database = { path = "./crates/database", version = "0.1.0" }

# Other common dependencies
serde = "1.0"
Expand Down
37 changes: 37 additions & 0 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "database"
version = "0.1.0"
edition = "2021"

[dependencies]
did-utils.workspace = true
did-endpoint.workspace = true
plugin-api.workspace = true
keystore.workspace = true

chrono.workspace = true
mongodb.workspace = true
async-trait.workspace = true
multibase.workspace = true
serde.workspace = true
serde_json.workspace = true
tracing.workspace = true
thiserror.workspace = true
json-canon.workspace = true
didcomm = { workspace = true, features = ["uniffi"] }
tokio = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }
uuid = { workspace = true, features = ["v4"] }
hyper = { workspace = true, features = ["full"] }
ndefokou marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
dotenv-flow = "0.15.0"
hyper = "0.14.27"
json-canon = "0.1.3"
tokio = { version = "1.27.0", default-features = false, features = ["macros", "rt"] }
tokio-test = "0.4.2"
tower = { version = "0.4.13", features = ["util"] }

[features]
default = ["stateful"]
stateful = []
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use async_trait::async_trait;
use mongodb::bson::{oid::ObjectId, Document as BsonDocument};
use mongodb::error::Error as MongoError;
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// A trait representing an abstract resource.
/// Any type implementing this trait should also implement `Serialize`.
pub trait Entity: Sized + Serialize {}

/// Definition of custom errors for repository operations
#[derive(Debug, Serialize, Deserialize, Error)]
Expand All @@ -22,7 +21,10 @@ pub enum RepositoryError {

/// Definition of a trait for repository operations
#[async_trait]
pub trait Repository<Entity>: Sync + Send {
pub trait Repository<Entity>: Sync + Send
where
Entity: Sized + Serialize,
{
/// Retrieves all entities.
async fn find_all(&self) -> Result<Vec<Entity>, RepositoryError>;

Expand All @@ -41,3 +43,9 @@ pub trait Repository<Entity>: Sync + Send {
/// Deletes a single entity by its identifier.
async fn delete_one(&self, entity_id: ObjectId) -> Result<(), RepositoryError>;
}

impl From<MongoError> for RepositoryError {
fn from(error: MongoError) -> Self {
RepositoryError::Generic(error.to_string())
}
}
4 changes: 2 additions & 2 deletions crates/did-utils/examples/dsa-verify-with-jwk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use did_utils::{
crypto::{Ed25519KeyPair, CoreSign},
jwk::Jwk
crypto::{CoreSign, Ed25519KeyPair},
jwk::Jwk,
};
use multibase::Base::Base64Url;

Expand Down
7 changes: 4 additions & 3 deletions crates/did-utils/examples/ed25519-sign.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate did_utils;
use did_utils::crypto::{Ed25519KeyPair, {Generate, CoreSign}};

use did_utils::crypto::{
Ed25519KeyPair, {CoreSign, Generate},
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
};

fn main() {
let keypair = Ed25519KeyPair::new().unwrap();
Expand All @@ -11,6 +12,6 @@ fn main() {
let signature = keypair.sign(json_data.as_bytes()).unwrap();

// Verify the signature
let verified = keypair.verify(json_data.as_bytes(), &signature);
let verified = keypair.verify(json_data.as_bytes(), &signature);
assert!(verified.is_ok());
}
4 changes: 3 additions & 1 deletion crates/did-utils/examples/multibase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ extern crate did_utils;
use multibase::Base;

fn main() {
let public_key = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32];
let public_key = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
];

let base58_public_key = Base::Base58Btc.encode(public_key);

Expand Down
4 changes: 2 additions & 2 deletions crates/did-utils/src/crypto/alg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{
jwk::{Bytes, Ec, EcCurves, Jwk, Key, Okp, OkpCurves, Parameters},
};

use multibase::Base::Base58Btc;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use multibase::Base::Base58Btc;

/// Supported cryptographic algorithms.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -249,8 +249,8 @@ pub(crate) fn decode_multikey(multikey: &str) -> Result<(Algorithm, Vec<u8>), De
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
use multibase::Base::Base64Url;
use serde_json::Value;

#[test]
fn test_can_build_secp256k1_jwk() {
Expand Down
2 changes: 1 addition & 1 deletion crates/did-utils/src/crypto/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pub enum Error {
InvalidCall(String),
Unsupported,
Unknown(String),
}
}
2 changes: 1 addition & 1 deletion crates/did-utils/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use ed25519::Ed25519KeyPair;
pub use errors::Error;
pub use format::PublicKeyFormat;
pub use sha256_hash::{sha256_hash, sha256_multihash};
pub use traits::{CoreSign, Generate, KeyMaterial, ToPublic, ToMultikey, BYTES_LENGTH_32, ECDH};
pub use traits::{CoreSign, Generate, KeyMaterial, ToMultikey, ToPublic, BYTES_LENGTH_32, ECDH};
pub use x25519::X25519KeyPair;

/// A wrapper struct for an asymmetric key pair.
Expand Down
22 changes: 11 additions & 11 deletions crates/did-utils/src/crypto/sha256_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ pub fn sha256_hash(bytes: &[u8]) -> [u8; 32] {
hash.as_slice()[..32].try_into().unwrap()
}

/// Compute SHA256 hash of a given input and return it as a multibase-encoded string.
///
/// # Example
///
/// ```rust
/// use did_utils::crypto::sha256_multihash;
///
/// let bytes = b"Hello, world!";
/// let hash = sha256_multihash(bytes);
/// ```
/// Compute SHA256 hash of a given input and return it as a multibase-encoded string.
///
/// # Example
///
/// ```rust
/// use did_utils::crypto::sha256_multihash;
///
/// let bytes = b"Hello, world!";
/// let hash = sha256_multihash(bytes);
/// ```
pub fn sha256_multihash(bytes: &[u8]) -> String {
const MULTIHASH_CODE: u8 = 0x12;
const MULTIHASH_SIZE: u8 = 0x20;
Expand Down Expand Up @@ -59,7 +59,7 @@ mod tests {
0x31, 0x5f, 0x5b, 0xdb, 0x76, 0xd0, 0x78, 0xc4, 0x3b, 0x8a, 0xc0, 0x06, 0x4e, 0x4a, 0x01, 0x64, //
0x61, 0x2b, 0x1f, 0xce, 0x77, 0xc8, 0x69, 0x34, 0x5b, 0xfc, 0x94, 0xc7, 0x58, 0x94, 0xed, 0xd3, //
];

// Compute the hash of the input bytes
let hash = sha256_hash(bytes);

Expand Down
2 changes: 1 addition & 1 deletion crates/did-utils/src/crypto/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub trait ECDH {
/// A trait for converting a key to its public counterpart.
pub trait ToPublic {
/// Converts the key to its public counterpart.
///
///
/// This method returns a new instance of the key with any private information removed.
/// The returned key contains only the public key components.
fn to_public(&self) -> Self;
Expand Down
3 changes: 1 addition & 2 deletions crates/did-utils/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ pub(super) fn generate_seed(initial_seed: &[u8]) -> Result<[u8; BYTES_LENGTH_32]
///
/// Panics if the length of the slice is not equal to `BYTES_LENGTH_32`.
pub(super) fn clone_slice_to_array(slice: &[u8; BYTES_LENGTH_32]) -> [u8; BYTES_LENGTH_32] {

let mut array = [0u8; BYTES_LENGTH_32];

array.clone_from_slice(slice);
array
}
}
25 changes: 12 additions & 13 deletions crates/did-utils/src/didcore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Implements the DID Core specification
//!
//!
//! As specified by [Decentralized Identifiers (DIDs) v1.0 - Core architecture,
//! data model, and representations][did-core].
//!
Expand All @@ -16,7 +16,7 @@ use crate::{jwk::Jwk, ldmodel::Context, proof::Proof};
// === Structure of a did document ===

/// Represents a DID Document according to the [DID Core specification][did-core].
///
///
/// [did-core]: https://www.w3.org/TR/did-core/
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -78,13 +78,13 @@ pub struct Document {
pub proof: Option<Proofs>,
}

impl Default for Document {
fn default() -> Self {
let id = String::new();
let context = Context::SingleString(String::from("https://www.w3.org/ns/did/v1"));
Self::new(context, id)
}
impl Default for Document {
fn default() -> Self {
let id = String::new();
let context = Context::SingleString(String::from("https://www.w3.org/ns/did/v1"));

Self::new(context, id)
}
}

/// Represents a DID Document controller(s).
Expand All @@ -96,9 +96,9 @@ pub enum Controller {
}

/// Represents a [service] in a DID Document.
///
///
/// A service defines how to interact with the DID subject.
///
///
/// [service]: https://www.w3.org/TR/did-core/#services
#[derive(Serialize, Debug, Clone, PartialEq, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
Expand All @@ -118,7 +118,7 @@ pub struct Service {
}

/// Represents a [verification method] in a DID Document.
///
///
/// [verification method]: https://www.w3.org/TR/did-core/#verification-methods
#[derive(Serialize, Debug, Clone, PartialEq, Default, Deserialize)]
pub struct VerificationMethod {
Expand Down Expand Up @@ -200,7 +200,6 @@ pub enum KeyAgreement {
}

impl VerificationMethod {

/// Serializes the private key format into a JSON map with the appropriate key format field.
fn serialize_private_key_format<S>(value: &Option<KeyFormat>, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
Loading
Loading