Skip to content

Commit

Permalink
Refactor sending Aries messages
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Dec 25, 2020
1 parent 605e3d4 commit 6523946
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 26 deletions.
11 changes: 0 additions & 11 deletions libvcx/src/aries/handlers/connection/agent_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::connection::create_agent_keys;
use crate::error::prelude::*;
use crate::libindy::utils::signus::create_and_store_my_did;
use crate::settings;
use crate::agency_client::httpclient;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentInfo {
Expand Down Expand Up @@ -149,16 +148,6 @@ impl AgentInfo {
EncryptionEnvelope::anon_unpack(message.payload()?)
}

/**
Sends authenticated message to connection counterparty
*/
pub fn send_message(&self, message: &A2AMessage, did_dod: &DidDoc) -> VcxResult<()> {
trace!("Agent::send_message >>> message: {:?}, did_doc: {:?}", message, did_dod);
let envelope = EncryptionEnvelope::create(&message, Some(&self.pw_vk), &did_dod)?;
httpclient::post_message(&envelope.0, &did_dod.get_endpoint())?;
Ok(())
}

/**
Sends message to one's agency signalling resources related to this connection agent can be deleted.
*/
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/aries/handlers/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ Get messages received from connection counterparty.
.ok_or(VcxError::from_msg(VcxErrorKind::NotReady, "Cannot send message: Remote Connection information is not set"))?;

warn!("Connection resolved did_doc = {:?}", did_doc);
self.agent_info().send_message(message, &did_doc)
did_doc.send_message(message, &self.agent_info().pw_vk)
}

fn parse_generic_message(message: &str) -> A2AMessage {
Expand Down
5 changes: 3 additions & 2 deletions libvcx/src/aries/handlers/connection/invitee/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ impl SmConnectionInvitee {
.set_keys(agent_info.recipient_keys(), agent_info.routing_keys()?);

trace!("invitation {:?}", state.invitation);
agent_info.send_message(&request.to_a2a_message(), &DidDoc::from(state.invitation.clone()))?;
let ddo = DidDoc::from(state.invitation.clone());
ddo.send_message(&request.to_a2a_message(), &agent_info.pw_vk)?;
InviteeState::Requested((state, request).into())
}
DidExchangeMessages::ProblemReportReceived(problem_report) => {
Expand All @@ -239,7 +240,7 @@ impl SmConnectionInvitee {
.set_problem_code(ProblemCode::ResponseProcessingError)
.set_explain(err.to_string())
.set_thread_id(&state.request.id.0);
agent_info.send_message(&problem_report.to_a2a_message(), &state.did_doc).ok();
state.did_doc.send_message(&problem_report.to_a2a_message(), &agent_info.pw_vk).ok();
InviteeState::Null((state, problem_report).into())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CompleteState {
.request_response()
.set_comment(comment);

agent_info.send_message(&ping.to_a2a_message(), &self.did_doc).ok();
self.did_doc.send_message(&ping.to_a2a_message(), &agent_info.pw_vk).ok();
Ok(())
}

Expand All @@ -72,8 +72,7 @@ impl CompleteState {
Query::create()
.set_query(query)
.set_comment(comment);

agent_info.send_message(&query_.to_a2a_message(), &self.did_doc)
self.did_doc.send_message(&query_.to_a2a_message(), &agent_info.pw_vk)
}

fn handle_discovery_query(&self, query: Query, agent_info: &AgentInfo) -> VcxResult<()> {
Expand All @@ -83,6 +82,6 @@ impl CompleteState {
.set_protocols(protocols)
.set_thread_id(query.id.0.clone());

agent_info.send_message(&disclose.to_a2a_message(), &self.did_doc)
self.did_doc.send_message(&disclose.to_a2a_message(), &agent_info.pw_vk)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl RequestedState {
.to_a2a_message()
};

agent_info.send_message(&message, &response.connection.did_doc)?;
response.connection.did_doc.send_message(&message, &agent_info.pw_vk)?;

Ok(response)
}
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/aries/handlers/connection/inviter/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl SmConnectionInviter {
.set_explain(err.to_string())
.set_thread_id(&request.id.0);

agent_info.send_message(&problem_report.to_a2a_message(), &request.connection.did_doc).ok(); // IS is possible?
request.connection.did_doc.send_message(&problem_report.to_a2a_message(), &agent_info.pw_vk).ok();
InviterState::Null((state, problem_report).into())
}
}
Expand Down Expand Up @@ -292,7 +292,7 @@ impl SmConnectionInviter {
.request_response()
.set_comment(comment);

agent_info.send_message(&ping.to_a2a_message(), &state.did_doc).ok();
state.did_doc.send_message(&ping.to_a2a_message(), &agent_info.pw_vk).ok();
InviterState::Responded(state)
}
DidExchangeMessages::PingResponseReceived(ping_response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CompleteState {
.request_response()
.set_comment(comment);

agent_info.send_message(&ping.to_a2a_message(), &self.did_doc).ok();
self.did_doc.send_message(&ping.to_a2a_message(), &agent_info.pw_vk).ok();
Ok(())
}

Expand All @@ -73,7 +73,7 @@ impl CompleteState {
.set_query(query)
.set_comment(comment);

agent_info.send_message(&query_.to_a2a_message(), &self.did_doc)
self.did_doc.send_message(&query_.to_a2a_message(), &agent_info.pw_vk)
}

fn handle_discovery_query(&self, query: Query, agent_info: &AgentInfo) -> VcxResult<()> {
Expand All @@ -83,6 +83,6 @@ impl CompleteState {
.set_protocols(protocols)
.set_thread_id(query.id.0.clone());

agent_info.send_message(&disclose.to_a2a_message(), &self.did_doc)
self.did_doc.send_message(&disclose.to_a2a_message(), &agent_info.pw_vk)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl InvitedState {
.set_thread_id(&request.id.0)
.encode(&prev_agent_info.pw_vk)?;

new_agent_info.send_message(&signed_response.to_a2a_message(), &request.connection.did_doc)?;

request.connection.did_doc.send_message(&signed_response.to_a2a_message(), &new_agent_info.pw_vk)?;

Ok((signed_response, new_agent_info))
}
Expand Down
3 changes: 2 additions & 1 deletion libvcx/src/aries/handlers/connection/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub fn handle_ping(ping: &Ping, agent_info: &AgentInfo, did_doc: &DidDoc) -> Vcx
if ping.response_requested {
let ping_response = PingResponse::create().set_thread_id(
&ping.thread.as_ref().and_then(|thread| thread.thid.clone()).unwrap_or(ping.id.0.clone()));
agent_info.send_message(&ping_response.to_a2a_message(), did_doc)?;

did_doc.send_message(&ping_response.to_a2a_message(), &agent_info.pw_vk)?;
}
Ok(())
}
22 changes: 22 additions & 0 deletions libvcx/src/aries/messages/connection/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::aries::messages::connection::invite::Invitation;
use crate::error::prelude::*;
use url::Url;
use crate::utils::validation::validate_verkey;
use crate::aries::messages::a2a::A2AMessage;
use crate::aries::utils::encryption_envelope::EncryptionEnvelope;

pub const CONTEXT: &str = "https://w3id.org/did/v1";
pub const KEY_TYPE: &str = "Ed25519VerificationKey2018";
Expand Down Expand Up @@ -273,6 +275,26 @@ impl DidDoc {
let pars: Vec<&str> = DidDoc::_key_parts(key_reference);
pars.get(1).or(pars.get(0)).map(|s| s.to_string()).unwrap_or_default()
}

/**
Sends authenticated message to connection counterparty
*/
pub fn send_message(&self, message: &A2AMessage, sender_verkey: &str) -> VcxResult<()> {
trace!("DidDoc::send_message >>> message: {:?}, did_doc: {:?}", message, &self);
let envelope = EncryptionEnvelope::create(&message, Some(sender_verkey), &self)?;
agency_client::httpclient::post_message(&envelope.0, &self.get_endpoint())?;
Ok(())
}

/**
Sends anonymous message to connection counterparty
*/
pub fn send_message_anonymously(&self, message: &A2AMessage) -> VcxResult<()> {
trace!("DidDoc::send_message_anonymously >>> message: {:?}, did_doc: {:?}", message, &self);
let envelope = EncryptionEnvelope::create(&message, None, &self)?;
agency_client::httpclient::post_message(&envelope.0, &self.get_endpoint())?;
Ok(())
}
}

impl Default for Service {
Expand Down

0 comments on commit 6523946

Please sign in to comment.