Skip to content

Commit

Permalink
Feature/msg builders (#972)
Browse files Browse the repository at this point in the history
* Added builders for messages crate

---------

Signed-off-by: Bogdan Mircea <[email protected]>
  • Loading branch information
bobozaur authored Sep 13, 2023
1 parent 7558c3d commit f4c8642
Show file tree
Hide file tree
Showing 82 changed files with 2,026 additions and 1,499 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 25 additions & 15 deletions aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bs58;
use diddoc_legacy::aries::diddoc::AriesDidDoc;
use diddoc_legacy::aries::service::AriesService;
use messages::msg_fields::protocols::connection::invitation::Invitation;
use messages::msg_fields::protocols::connection::invitation::{Invitation, InvitationContent};
use messages::msg_fields::protocols::out_of_band::invitation::OobService;
use std::{collections::HashMap, sync::Arc};

Expand Down Expand Up @@ -93,25 +93,35 @@ pub async fn add_new_did(
pub async fn into_did_doc(indy_ledger: &Arc<dyn IndyLedgerRead>, invitation: &AnyInvitation) -> VcxResult<AriesDidDoc> {
let mut did_doc: AriesDidDoc = AriesDidDoc::default();
let (service_endpoint, recipient_keys, routing_keys) = match invitation {
AnyInvitation::Con(Invitation::Public(invitation)) => {
did_doc.set_id(invitation.content.did.to_string());
let service = get_service(indy_ledger, &invitation.content.did)
.await
.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
AriesService::default()
});
AnyInvitation::Con(Invitation {
id,
content: InvitationContent::Public(content),
decorators,
}) => {
did_doc.set_id(content.did.to_string());
let service = get_service(indy_ledger, &content.did).await.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
AriesService::default()
});
(service.service_endpoint, service.recipient_keys, service.routing_keys)
}
AnyInvitation::Con(Invitation::Pairwise(invitation)) => {
did_doc.set_id(invitation.id.clone());
AnyInvitation::Con(Invitation {
id,
content: InvitationContent::Pairwise(content),
decorators,
}) => {
did_doc.set_id(id.clone());
(
invitation.content.service_endpoint.clone(),
invitation.content.recipient_keys.clone(),
invitation.content.routing_keys.clone(),
content.service_endpoint.clone(),
content.recipient_keys.clone(),
content.routing_keys.clone(),
)
}
AnyInvitation::Con(Invitation::PairwiseDID(_)) => {
AnyInvitation::Con(Invitation {
id,
content: InvitationContent::PairwiseDID(content),
decorators,
}) => {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidDid,
format!("PairwiseDID invitation not supported yet!"),
Expand Down
39 changes: 23 additions & 16 deletions aries_vcx/src/handlers/connection/mediated_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::future::BoxFuture;
use futures::stream::StreamExt;
use messages::decorators::timing::Timing;
use messages::msg_fields::protocols::basic_message::{BasicMessage, BasicMessageContent, BasicMessageDecorators};
use messages::msg_fields::protocols::connection::invitation::Invitation;
use messages::msg_fields::protocols::connection::invitation::InvitationContent;
use messages::msg_fields::protocols::connection::request::Request;
use messages::msg_fields::protocols::connection::Connection;
use messages::msg_fields::protocols::discover_features::disclose::Disclose;
Expand Down Expand Up @@ -614,14 +614,16 @@ impl MediatedConnection {
SmConnection::Invitee(sm_invitee) => {
let (sm_invitee, can_autohop) = match message {
Some(message) => match message {
AriesMessage::Connection(Connection::Invitation(Invitation::Public(invitation))) => (
sm_invitee.handle_invitation(AnyInvitation::Con(Invitation::Public(invitation)))?,
false,
),
AriesMessage::Connection(Connection::Invitation(Invitation::Pairwise(invitation))) => (
sm_invitee.handle_invitation(AnyInvitation::Con(Invitation::Pairwise(invitation)))?,
false,
),
AriesMessage::Connection(Connection::Invitation(invitation))
if matches!(invitation.content, InvitationContent::Public(_)) =>
{
(sm_invitee.handle_invitation(AnyInvitation::Con(invitation))?, false)
}
AriesMessage::Connection(Connection::Invitation(invitation))
if matches!(invitation.content, InvitationContent::Pairwise(_)) =>
{
(sm_invitee.handle_invitation(AnyInvitation::Con(invitation))?, false)
}
AriesMessage::Connection(Connection::Response(response)) => {
let send_message = self.send_message_closure_connection(Arc::clone(wallet));
(
Expand Down Expand Up @@ -807,15 +809,20 @@ impl MediatedConnection {
Err(_) => {
let now = Utc::now();

let content = BasicMessageContent::new(message.to_owned(), now);

let mut decorators = BasicMessageDecorators::default();
let mut timing = Timing::default();
timing.out_time = Some(now);
let content = BasicMessageContent::builder()
.content(message.to_owned())
.sent_time(now)
.build();

decorators.timing = Some(timing);
let decorators = BasicMessageDecorators::builder()
.timing(Timing::builder().out_time(now).build())
.build();

BasicMessage::with_decorators(Uuid::new_v4().to_string(), content, decorators).into()
BasicMessage::builder()
.id(Uuid::new_v4().to_string())
.content(content)
.decorators(decorators)
.build()
}
}
}
Expand Down
40 changes: 26 additions & 14 deletions aries_vcx/src/handlers/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ pub async fn send_discovery_query(
pw_vk: &str,
) -> VcxResult<()> {
let query = query.unwrap_or("*".to_owned());
let mut content = QueryContent::new(query);
content.comment = comment;
let content = QueryContent::builder().query(query);

let mut decorators = QueryDecorators::default();
let mut timing = Timing::default();
timing.out_time = Some(Utc::now());
decorators.timing = Some(timing);
let content = if let Some(comment) = comment {
content.comment(comment).build()
} else {
content.build()
};

let query = Query::with_decorators(Uuid::new_v4().to_string(), content, decorators);
let decorators = QueryDecorators::builder()
.timing(Timing::builder().out_time(Utc::now()).build())
.build();

send_message(Arc::clone(wallet), pw_vk.to_string(), did_doc.clone(), query.into()).await
let query = Query::builder()
.id(Uuid::new_v4().to_string())
.content(content)
.decorators(decorators)
.build();

send_message(Arc::clone(wallet), pw_vk.to_string(), did_doc.clone(), query).await
}

pub async fn respond_discovery_query(
Expand All @@ -43,12 +51,16 @@ pub async fn respond_discovery_query(
) -> VcxResult<()> {
let content = DiscloseContent::default();

let mut decorators = DiscloseDecorators::new(Thread::new(query.id));
let mut timing = Timing::default();
timing.out_time = Some(Utc::now());
decorators.timing = Some(timing);
let decorators = DiscloseDecorators::builder()
.thread(Thread::builder().thid(query.id).build())
.timing(Timing::builder().out_time(Utc::now()).build())
.build();

let disclose = Disclose::with_decorators(Uuid::new_v4().to_string(), content, decorators);
let disclose = Disclose::builder()
.id(Uuid::new_v4().to_string())
.content(content)
.decorators(decorators)
.build();

send_message(Arc::clone(wallet), pw_vk.to_string(), did_doc.clone(), disclose.into()).await
send_message(Arc::clone(wallet), pw_vk.to_string(), did_doc.clone(), disclose).await
}
22 changes: 14 additions & 8 deletions aries_vcx/src/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use messages::msg_fields::protocols::cred_issuance::offer_credential::OfferCrede
use messages::msg_fields::protocols::cred_issuance::propose_credential::ProposeCredential;
use messages::msg_fields::protocols::cred_issuance::request_credential::RequestCredential;
use messages::msg_fields::protocols::cred_issuance::CredentialIssuance;
use messages::msg_fields::protocols::notification::ack::{AckDecorators, AckStatus};
use messages::msg_fields::protocols::notification::ack::{AckContent, AckDecorators, AckStatus};
use messages::msg_fields::protocols::report_problem::ProblemReport;
use messages::msg_fields::protocols::revocation::revoke::Revoke;
use messages::AriesMessage;
Expand All @@ -25,13 +25,19 @@ use crate::handlers::revocation_notification::receiver::RevocationNotificationRe
use crate::protocols::issuance::holder::state_machine::{HolderFullState, HolderSM, HolderState};

fn build_credential_ack(thread_id: &str) -> AckCredential {
let content = AckCredentialContent::new(AckStatus::Ok);
let mut decorators = AckDecorators::new(Thread::new(thread_id.to_owned()));
let mut timing = Timing::default();
timing.out_time = Some(Utc::now());
decorators.timing = Some(timing);

AckCredential::with_decorators(Uuid::new_v4().to_string(), content, decorators)
let content = AckCredentialContent::builder()
.inner(AckContent::builder().status(AckStatus::Ok).build())
.build();
let decorators = AckDecorators::builder()
.thread(Thread::builder().thid(thread_id.to_owned()).build())
.timing(Timing::builder().out_time(Utc::now()).build())
.build();

AckCredential::builder()
.id(Uuid::new_v4().to_string())
.content(content)
.decorators(decorators)
.build()
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
91 changes: 42 additions & 49 deletions aries_vcx/src/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;
use messages::msg_fields::protocols::cred_issuance::issue_credential::IssueCredential;
use messages::msg_fields::protocols::notification::Notification;
use messages::msg_fields::protocols::report_problem::ProblemReport;
use messages::msg_parts::MsgParts;

use crate::errors::error::prelude::*;
use crate::handlers::util::OfferInfo;
Expand Down Expand Up @@ -46,7 +45,7 @@ fn _build_credential_preview(credential_json: &str) -> VcxResult<CredentialPrevi
})?;

// todo: should throw err if cred_values is not serde_json::Value::Array or serde_json::Value::Object
let mut credential_preview = CredentialPreview::new(Vec::new());
let mut attributes = Vec::new();

match cred_values {
serde_json::Value::Array(cred_values) => {
Expand All @@ -60,48 +59,54 @@ fn _build_credential_preview(credential_json: &str) -> VcxResult<CredentialPrevi
format!("No 'value' field in cred_value: {:?}", cred_value),
))?;

let mut attr = CredentialAttr::new(
key.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential value names are currently only allowed to be strings",
))?
.to_owned(),
value
.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential values are currently only allowed to be strings",
))?
.to_owned(),
);

attr.mime_type = Some(MimeType::Plain);
credential_preview.attributes.push(attr);
let name = key
.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential value names are currently only allowed to be strings",
))?
.to_owned();

let value = value
.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential values are currently only allowed to be strings",
))?
.to_owned();

let attr = CredentialAttr::builder()
.name(name)
.value(value)
.mime_type(MimeType::Plain)
.build();

attributes.push(attr);
}
}
serde_json::Value::Object(values_map) => {
for item in values_map.iter() {
let (key, value) = item;

let mut attr = CredentialAttr::new(
key.to_owned(),
value
.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential values are currently only allowed to be strings",
))?
.to_owned(),
);

attr.mime_type = Some(MimeType::Plain);
credential_preview.attributes.push(attr);
let value = value
.as_str()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidOption,
"Credential values are currently only allowed to be strings",
))?
.to_owned();
let attr = CredentialAttr::builder()
.name(key.to_owned())
.value(value)
.mime_type(MimeType::Plain)
.build();

attributes.push(attr);
}
}
_ => {}
};
Ok(credential_preview)

Ok(CredentialPreview::new(attributes))
}

impl Issuer {
Expand Down Expand Up @@ -279,22 +284,10 @@ impl Issuer {
}
AriesMessage::ReportProblem(report) => self.issuer_sm.clone().receive_problem_report(report)?,
AriesMessage::Notification(Notification::ProblemReport(report)) => {
let MsgParts {
id,
content,
decorators,
} = report;
let report = ProblemReport::with_decorators(id, content.0, decorators);
self.issuer_sm.clone().receive_problem_report(report)?
self.issuer_sm.clone().receive_problem_report(report.into())?
}
AriesMessage::CredentialIssuance(CredentialIssuance::ProblemReport(report)) => {
let MsgParts {
id,
content,
decorators,
} = report;
let report = ProblemReport::with_decorators(id, content.0, decorators);
self.issuer_sm.clone().receive_problem_report(report)?
self.issuer_sm.clone().receive_problem_report(report.into())?
}
_ => self.issuer_sm.clone(),
};
Expand Down
Loading

0 comments on commit f4c8642

Please sign in to comment.