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

Implementation of the basic message protocol #243

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ shared = { path = "./crates/web-plugins/didcomm-messaging/shared", version = "0.
pickup = { path = "./crates/web-plugins/didcomm-messaging/protocols/pickup", version = "0.1.0" }
forward = { path = "./crates/web-plugins/didcomm-messaging/protocols/forward", version = "0.1.0" }
trust-ping = { path = "./crates/web-plugins/didcomm-messaging/protocols/trust-ping", version = "0.1.0" }
basic-message = { path = "./crates/web-plugins/didcomm-messaging/protocols/basic-message", version = "0.1.0" }
mediator-coordination = { path = "./crates/web-plugins/didcomm-messaging/protocols/mediator-coordination", version = "0.1.0" }

# Other common dependencies
Expand Down Expand Up @@ -92,8 +93,6 @@ hyper.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing-subscriber = { workspace = true, features = ["json"] }
tower-http = { workspace = true, features = ["catch-panic", "trace"] }

# optional dependencies
chrono = { workspace = true, optional = true }
did-endpoint = { workspace = true, optional = true }
oob-messages = { workspace = true, optional = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "basic-message"
version = "0.1.0"
edition = "2021"

[dependencies]
shared.workspace = true

serde.workspace = true
didcomm.workspace = true
mongodb.workspace = true
serde_json.workspace = true
thiserror.workspace = true
uuid = { workspace = true, features = ["v4"] }
axum = { workspace = true, features = ["macros"] }
chrono.workspace = true

[dev-dependencies]
did-utils.workspace = true
keystore.workspace = true
hyper = "0.14.27"
shared = { workspace = true, features = ["test-utils"] }
tokio = { version = "1.27.0", default-features = false, features = [
"macros",
"rt",
] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ProtocolError {
#[error("Invalid message format")]
InvalidMessageFormat,

Check warning on line 6 in crates/web-plugins/didcomm-messaging/protocols/basic-message/src/error.rs

View workflow job for this annotation

GitHub Actions / Build and Test

variants `InvalidMessageFormat` and `TransmissionError` are never constructed

Check warning on line 6 in crates/web-plugins/didcomm-messaging/protocols/basic-message/src/error.rs

View workflow job for this annotation

GitHub Actions / Build and Test

variants `InvalidMessageFormat` and `TransmissionError` are never constructed
#[error("transmission error")]
TransmissionError,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::model::BasicMessage;
use axum::response::Response;
use chrono::Utc;
use serde_json::json;
use shared::state::AppState;
use std::sync::Arc;
use uuid::Uuid;

pub async fn handle_basic_message(
state: Arc<AppState>,

Check warning on line 10 in crates/web-plugins/didcomm-messaging/protocols/basic-message/src/handler.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `state`

Check warning on line 10 in crates/web-plugins/didcomm-messaging/protocols/basic-message/src/handler.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `state`
message: BasicMessage,
) -> Result<Option<BasicMessage>, Response> {
// Create the response message based on the input message
let response_message = BasicMessage {
id: Uuid::new_v4().to_string(),
message_type: "https://didcomm.org/basicmessage/2.0/message".to_string(),
lang: message.lang.clone(),
created_time: Utc::now(),
body: json!({ "content": format!("Received: {}", message.body["content"].as_str().unwrap_or("")) }),
};

Ok(Some(response_message))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::BasicMessage;
use chrono::Utc;
use did_utils::didcore::Document;
use keystore::tests::MockKeyStore;
use serde_json::json;
use shared::{
repository::tests::{MockConnectionRepository, MockMessagesRepository},
state::{AppState, AppStateRepository},
};
use std::sync::Arc;

#[tokio::test]
async fn test_handle_basic_message() {
let repository = AppStateRepository {
connection_repository: Arc::new(MockConnectionRepository::from(vec![])),
message_repository: Arc::new(MockMessagesRepository::from(vec![])),
keystore: Arc::new(MockKeyStore::new(vec![])),
};

let app_state = Arc::new(AppState::from(
"".to_string(),
Document::default(),
Some((repository)),
));

let input_message = BasicMessage {
id: Uuid::new_v4().to_string(),
message_type: "https://didcomm.org/basicmessage/2.0/message".to_string(),
lang: Some("en".to_string()),
created_time: Utc::now(),
body: json!({ "content": "Hello, this is a test message." }),
};

let result = handle_basic_message(app_state, input_message.clone()).await;

assert!(result.is_ok());
let response_message = result.unwrap().unwrap();

assert_eq!(
response_message.message_type,
"https://didcomm.org/basicmessage/2.0/message"
);
assert_eq!(response_message.lang, input_message.lang);
assert_eq!(
response_message.body["content"],
json!(format!(
"Received: {}",
input_message.body["content"].as_str().unwrap()
))
);

assert_ne!(response_message.id, input_message.id);
assert!(response_message.created_time >= input_message.created_time);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod error;
mod model;

pub mod handler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use chrono::{DateTime, Utc};
use serde::{ Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BasicMessage {
/// Message ID (unique identifier)
pub id: String,
/// Message type URI for basic messages
pub message_type: String,
/// Localization information for message language
pub lang: Option<String>,
/// Timestamp for when the message was created (DIDComm V2 standard)
pub created_time: DateTime<Utc>,
/// Main message body
pub body: Value,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MessageBody {
pub content: String,
}
Loading