Skip to content

Commit

Permalink
Add support for pickup protocol messages (hyperledger#1018)
Browse files Browse the repository at this point in the history
Add support for mediation related protocol messages (hyperledger#1018)

Signed-off-by: Naian <126972030+nain-F49FF806@users.noreply.github.com>
  • Loading branch information
nain-F49FF806 authored Oct 19, 2023
1 parent 3f1045c commit 787b51c
Show file tree
Hide file tree
Showing 15 changed files with 604 additions and 5 deletions.
14 changes: 14 additions & 0 deletions aries_vcx/src/handlers/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use messages::{
discover_features::DiscoverFeatures,
notification::Notification,
out_of_band::{invitation::Invitation as OobInvitation, OutOfBand},
pickup::Pickup,
present_proof::{
propose::{Predicate, PresentationAttr},
PresentProof,
Expand Down Expand Up @@ -180,6 +181,19 @@ pub fn verify_thread_id(thread_id: &str, message: &AriesMessage) -> VcxResult<()
AriesMessage::Routing(msg) => msg.id == thread_id,
AriesMessage::TrustPing(TrustPing::Ping(msg)) => matches_opt_thread_id!(msg, thread_id),
AriesMessage::TrustPing(TrustPing::PingResponse(msg)) => matches_thread_id!(msg, thread_id),
AriesMessage::Pickup(Pickup::Status(msg)) => matches_opt_thread_id!(msg, thread_id),
AriesMessage::Pickup(Pickup::StatusRequest(msg)) => matches_opt_thread_id!(msg, thread_id),
AriesMessage::Pickup(Pickup::Delivery(msg)) => matches_opt_thread_id!(msg, thread_id),
AriesMessage::Pickup(Pickup::DeliveryRequest(msg)) => {
matches_opt_thread_id!(msg, thread_id)
}

AriesMessage::Pickup(Pickup::MessagesReceived(msg)) => {
matches_opt_thread_id!(msg, thread_id)
}
AriesMessage::Pickup(Pickup::LiveDeliveryChange(msg)) => {
matches_opt_thread_id!(msg, thread_id)
}
};

if !is_match {
Expand Down
1 change: 1 addition & 0 deletions messages/src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod localization;
pub mod please_ack;
pub mod thread;
pub mod timing;
pub mod transport;
63 changes: 63 additions & 0 deletions messages/src/decorators/transport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::decorators::thread::Thread;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct Transport {
pub return_route: ReturnRoute,
#[builder(default, setter(strip_option))]
#[serde(skip_serializing_if = "Option::is_none")]
pub return_route_thread: Option<Thread>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq)]
pub enum ReturnRoute {
#[default]
#[serde(rename = "none")]
None,
#[serde(rename = "all")]
All,
#[serde(rename = "thread")]
Thread,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::misc::test_utils;

#[test]
fn test_transport_minimals() {
// all variant
let transport = Transport::builder().return_route(ReturnRoute::All).build();
let expected = json!({
"return_route": "all"
});
test_utils::test_serde(transport, expected);
// none variant
let transport = Transport::builder().return_route(ReturnRoute::None).build();
let expected = json!({
"return_route": "none"
});
test_utils::test_serde(transport, expected);
}
#[test]
fn test_transport_extended() {
// thread variant
let thread = Thread::builder().thid("<thread id>".to_string()).build();
let transport = Transport::builder()
.return_route(ReturnRoute::Thread)
.return_route_thread(thread)
.build();
let expected = json!({
"return_route": "thread",
"return_route_thread": { "thid": "<thread id>" }
});
test_utils::test_serde(transport, expected);
}
}
10 changes: 8 additions & 2 deletions messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub mod msg_types;

use derive_more::From;
use misc::utils;
use msg_fields::protocols::cred_issuance::{
v1::CredentialIssuanceV1, v2::CredentialIssuanceV2, CredentialIssuance,
use msg_fields::protocols::{
cred_issuance::{v1::CredentialIssuanceV1, v2::CredentialIssuanceV2, CredentialIssuance},
pickup::Pickup,
};
use msg_types::{
cred_issuance::CredentialIssuanceType, report_problem::ReportProblemTypeV1_0,
Expand Down Expand Up @@ -65,6 +66,7 @@ pub enum AriesMessage {
BasicMessage(BasicMessage),
OutOfBand(OutOfBand),
Notification(Notification),
Pickup(Pickup),
}

impl DelayedSerde for AriesMessage {
Expand Down Expand Up @@ -164,6 +166,9 @@ impl DelayedSerde for AriesMessage {
Notification::delayed_deserialize((msg_type, kind_str), deserializer)
.map(From::from)
}
Protocol::PickupType(msg_type) => {
Pickup::delayed_deserialize((msg_type, kind_str), deserializer).map(From::from)
}
}
}

Expand All @@ -184,6 +189,7 @@ impl DelayedSerde for AriesMessage {
Self::BasicMessage(v) => MsgWithType::from(v).serialize(serializer),
Self::OutOfBand(v) => v.delayed_serialize(serializer),
Self::Notification(v) => v.delayed_serialize(serializer),
Self::Pickup(v) => v.delayed_serialize(serializer),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions messages/src/msg_fields/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod cred_issuance;
pub mod discover_features;
pub mod notification;
pub mod out_of_band;
pub mod pickup;
pub mod present_proof;
pub mod report_problem;
pub mod revocation;
Expand Down
87 changes: 87 additions & 0 deletions messages/src/msg_fields/protocols/pickup/delivery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::{
decorators::{attachment::Attachment, thread::Thread, transport::Transport},
msg_parts::MsgParts,
};

pub type Delivery = MsgParts<DeliveryContent, DeliveryDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryContent {
#[builder(default, setter(strip_option))]
#[serde(skip_serializing_if = "Option::is_none")]
pub recipient_key: Option<String>,
#[serde(rename = "~attach")]
pub attach: Vec<Attachment>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{
decorators::{
attachment::{AttachmentData, AttachmentType},
thread::Thread,
},
misc::test_utils,
msg_types::protocols::pickup::PickupTypeV2_0,
};
#[test]
fn test_delivery() {
let expected = json!(
{
"@id": "123456781",
"~thread": {
"thid": "<message id of delivery-request message>"
},
"@type": "https://didcomm.org/messagepickup/2.0/delivery",
"recipient_key": "<key for messages>",
"~attach": [{
"@id": "<messageid>",
"data": {
"base64": ""
}
}]
}
);
let attach = Attachment::builder()
.id("<messageid>".to_owned())
.data(
AttachmentData::builder()
.content(AttachmentType::Base64("".into()))
.build(),
)
.build();
let content = DeliveryContent::builder()
.recipient_key("<key for messages>".to_owned())
.attach(vec![attach])
.build();
let decorators = DeliveryDecorators::builder()
.thread(
Thread::builder()
.thid("<message id of delivery-request message>".to_owned())
.build(),
)
.build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::Delivery, expected);
}
}
62 changes: 62 additions & 0 deletions messages/src/msg_fields/protocols/pickup/delivery_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::{
decorators::{thread::Thread, transport::Transport},
msg_parts::MsgParts,
};

pub type DeliveryRequest = MsgParts<DeliveryRequestContent, DeliveryRequestDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryRequestContent {
pub limit: u32,
#[builder(default, setter(strip_option))]
#[serde(skip_serializing_if = "Option::is_none")]
pub recipient_key: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryRequestDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};
#[test]
fn test_delivery_request() {
let expected = json!(
{
"@id": "123456781",
"@type": "https://didcomm.org/messagepickup/2.0/delivery-request",
"limit": 10,
"recipient_key": "<key for messages>"
}
);
let content = DeliveryRequestContent::builder()
.recipient_key("<key for messages>".to_owned())
.limit(10)
.build();
let decorators = DeliveryRequestDecorators::builder().build();

test_utils::test_msg(
content,
decorators,
PickupTypeV2_0::DeliveryRequest,
expected,
);
}
}
56 changes: 56 additions & 0 deletions messages/src/msg_fields/protocols/pickup/live_delivery_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::{
decorators::{thread::Thread, transport::Transport},
msg_parts::MsgParts,
};

pub type LiveDeliveryChange = MsgParts<LiveDeliveryChangeContent, LiveDeliveryChangeDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct LiveDeliveryChangeContent {
pub live_delivery: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct LiveDeliveryChangeDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};
#[test]
fn test_live_delivery_change() {
let expected = json!(
{
"@type": "https://didcomm.org/messagepickup/2.0/live-delivery-change",
"live_delivery": true
}
);
let content = LiveDeliveryChangeContent::builder()
.live_delivery(true)
.build();
let decorators = LiveDeliveryChangeDecorators::builder().build();

test_utils::test_msg(
content,
decorators,
PickupTypeV2_0::LiveDeliveryChange,
expected,
);
}
}
Loading

0 comments on commit 787b51c

Please sign in to comment.