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
1 change: 1 addition & 0 deletions crates/web-plugins/didcomm-messaging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ thiserror.workspace = true
tokio = { workspace = true, features = ["full"] }
hyper = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }
chrono.workspace = true
ndefokou marked this conversation as resolved.
Show resolved Hide resolved


[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[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]
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,
#[error("Encryption or transmission error")]
TransmissionError,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::error::ProtocolError;
use crate::model::{BasicMessage, MessageBody};
use chrono::Utc;
use uuid::Uuid;

///creating a new basic message
pub fn basic_message(content: &str, lang: Option<String>) -> Result<BasicMessage, ProtocolError> {
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
Ok(BasicMessage {
id: Uuid::new_v4().to_string(),
message_type: "https://didcomm.org/basicmessage/2.0/message".to_string(),
locale: lang,
created_time: Utc::now(),
body: MessageBody {
content: content.to_string(),
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

/// Function to handle the received message
pub fn handle_received_message(message: &BasicMessage) -> Result<(), ProtocolError> {
println!("Received message: {}", message.body.content);
Ok(())
}

#[cfg(test)]
mod tests {
use crate::handler::{basic_message, handle_received_message};

#[test]
fn test_create_and_handle_basic_message() {
let message_content = "Hello, this is a basic message.";
let message = basic_message(message_content, Some("en".to_string())).unwrap();
assert_eq!(message.body.content, message_content);

handle_received_message(&message).expect("Failed to handle received message");
}
}
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,24 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct BasicMessage {
/// Message ID (unique identifier)
pub id: String,
/// Message type URI for basic messages
#[serde(rename = "type")]
pub message_type: String,
/// Localization information for message language
#[serde(rename = "lang")]
pub locale: Option<String>,
ndefokou marked this conversation as resolved.
Show resolved Hide resolved
/// Timestamp for when the message was created (DIDComm V2 standard)
#[serde(rename = "created_time")]
pub created_time: DateTime<Utc>,
/// Main message bodsssssssssy
pub body: MessageBody,
}

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