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

Make into_did_doc async #569

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions aries_vcx/src/handlers/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ impl Connection {
self.cloud_agent_info.clone()
}

pub fn remote_did(&self, pool_handle: PoolHandle) -> VcxResult<String> {
pub async fn remote_did(&self, pool_handle: PoolHandle) -> VcxResult<String> {
match &self.connection_sm {
SmConnection::Inviter(sm_inviter) => sm_inviter.remote_did(),
SmConnection::Invitee(sm_invitee) => sm_invitee.remote_did(pool_handle),
SmConnection::Invitee(sm_invitee) => sm_invitee.remote_did(pool_handle).await,
}
}

pub fn remote_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
pub async fn remote_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
match &self.connection_sm {
SmConnection::Inviter(sm_inviter) => sm_inviter.remote_vk(),
SmConnection::Invitee(sm_invitee) => sm_invitee.remote_vk(pool_handle),
SmConnection::Invitee(sm_invitee) => sm_invitee.remote_vk(pool_handle).await,
}
}

Expand Down Expand Up @@ -251,17 +251,17 @@ impl Connection {
}
}

pub fn their_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
pub async fn their_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
match &self.connection_sm {
SmConnection::Inviter(sm_inviter) => sm_inviter.their_did_doc(),
SmConnection::Invitee(sm_invitee) => sm_invitee.their_did_doc(pool_handle),
SmConnection::Invitee(sm_invitee) => sm_invitee.their_did_doc(pool_handle).await,
}
}

pub fn bootstrap_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
pub async fn bootstrap_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
match &self.connection_sm {
SmConnection::Inviter(_sm_inviter) => None, // TODO: Inviter can remember bootstrap agent too, but we don't need it
SmConnection::Invitee(sm_invitee) => sm_invitee.bootstrap_did_doc(pool_handle),
SmConnection::Invitee(sm_invitee) => sm_invitee.bootstrap_did_doc(pool_handle).await,
}
}

Expand Down Expand Up @@ -406,7 +406,7 @@ impl Connection {
}

pub async fn handle_message(&mut self, message: A2AMessage, wallet_handle: WalletHandle, pool_handle: PoolHandle) -> VcxResult<()> {
let did_doc = self.their_did_doc(pool_handle).ok_or(VcxError::from_msg(
let did_doc = self.their_did_doc(pool_handle).await.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
format!(
"Can't answer message {:?} because counterparty did doc is not available",
Expand Down Expand Up @@ -658,7 +658,7 @@ impl Connection {
}

pub async fn get_messages(&self, pool_handle: PoolHandle, agency_client: &AgencyClient) -> VcxResult<HashMap<String, A2AMessage>> {
let expected_sender_vk = self.get_expected_sender_vk(pool_handle)?;
let expected_sender_vk = self.get_expected_sender_vk(pool_handle).await?;
match &self.connection_sm {
SmConnection::Inviter(sm_inviter) => Ok(self
.cloud_agent_info()
Expand All @@ -671,8 +671,8 @@ impl Connection {
}
}

fn get_expected_sender_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
self.remote_vk(pool_handle).map_err(|_err| {
async fn get_expected_sender_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
self.remote_vk(pool_handle).await.map_err(|_err| {
VcxError::from_msg(
VcxErrorKind::NotReady,
"Verkey of connection counterparty \
Expand All @@ -683,15 +683,15 @@ impl Connection {

pub async fn get_message_by_id(&self, pool_handle: PoolHandle, msg_id: &str, agency_client: &AgencyClient) -> VcxResult<A2AMessage> {
trace!("Connection: get_message_by_id >>> msg_id: {}", msg_id);
let expected_sender_vk = self.get_expected_sender_vk(pool_handle)?;
let expected_sender_vk = self.get_expected_sender_vk(pool_handle).await?;
self.cloud_agent_info()
.get_message_by_id(agency_client, msg_id, &expected_sender_vk, self.pairwise_info())
.await
}

pub fn send_message_closure(&self, wallet_handle: WalletHandle, pool_handle: PoolHandle) -> VcxResult<SendClosure> {
pub async fn send_message_closure(&self, wallet_handle: WalletHandle, pool_handle: PoolHandle) -> VcxResult<SendClosure> {
trace!("send_message_closure >>>");
let did_doc = self.their_did_doc(pool_handle).ok_or(VcxError::from_msg(
let did_doc = self.their_did_doc(pool_handle).await.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
"Cannot send message: Remote Connection information is not set",
))?;
Expand All @@ -715,13 +715,13 @@ impl Connection {
pub async fn send_generic_message(&self, wallet_handle: WalletHandle, pool_handle: PoolHandle, message: &str) -> VcxResult<String> {
trace!("Connection::send_generic_message >>> message: {:?}", message);
let message = Self::build_basic_message(message);
let send_message = self.send_message_closure(wallet_handle, pool_handle)?;
let send_message = self.send_message_closure(wallet_handle, pool_handle).await?;
send_message(message).await.map(|_| String::new())
}

pub async fn send_a2a_message(&self, wallet_handle: WalletHandle, pool_handle: PoolHandle, message: &A2AMessage) -> VcxResult<String> {
trace!("Connection::send_a2a_message >>> message: {:?}", message);
let send_message = self.send_message_closure(wallet_handle, pool_handle)?;
let send_message = self.send_message_closure(wallet_handle, pool_handle).await?;
send_message(message.clone()).await.map(|_| String::new())
}

Expand All @@ -732,7 +732,7 @@ impl Connection {
comment: Option<String>,
) -> VcxResult<TrustPingSender> {
let mut trust_ping = TrustPingSender::build(true, comment);
trust_ping.send_ping(self.send_message_closure(wallet_handle, pool_handle)?).await?;
trust_ping.send_ping(self.send_message_closure(wallet_handle, pool_handle).await?).await?;
Ok(trust_ping)
}

Expand All @@ -756,7 +756,7 @@ impl Connection {
));
}
};
let did_doc = self.their_did_doc(pool_handle).ok_or(VcxError::from_msg(
let did_doc = self.their_did_doc(pool_handle).await.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
format!("Can't send handshake-reuse to the counterparty, because their did doc is not available"),
))?;
Expand Down Expand Up @@ -788,15 +788,15 @@ impl Connection {
query,
comment
);
let did_doc = self.their_did_doc(pool_handle).ok_or(VcxError::from_msg(
let did_doc = self.their_did_doc(pool_handle).await.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
format!("Can't send handshake-reuse to the counterparty, because their did doc is not available"),
))?;
send_discovery_query(wallet_handle, query, comment, &did_doc, &self.pairwise_info().pw_vk).await?;
Ok(())
}

pub fn get_connection_info(&self, pool_handle: PoolHandle, agency_client: &AgencyClient) -> VcxResult<String> {
pub async fn get_connection_info(&self, pool_handle: PoolHandle, agency_client: &AgencyClient) -> VcxResult<String> {
trace!("Connection::get_connection_info >>>");

let agent_info = self.cloud_agent_info();
Expand All @@ -811,7 +811,7 @@ impl Connection {
protocols: Some(self.get_protocols()),
};

let remote = match self.their_did_doc(pool_handle) {
let remote = match self.their_did_doc(pool_handle).await {
Some(did_doc) => Some(SideConnectionInfo {
did: did_doc.id.clone(),
recipient_keys: did_doc.recipient_keys(),
Expand Down Expand Up @@ -860,7 +860,7 @@ impl Connection {
Ok(msgs)
}
_ => {
let expected_sender_vk = self.remote_vk(pool_handle)?;
let expected_sender_vk = self.remote_vk(pool_handle).await?;
let msgs = futures::stream::iter(
self.cloud_agent_info()
.download_encrypted_messages(agency_client, uids, status_codes, self.pairwise_info())
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Holder {
if self.is_terminal_state() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(wallet_handle, pool_handle)?;
let send_message = connection.send_message_closure(wallet_handle, pool_handle).await?;

let messages = connection.get_messages(pool_handle, agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Issuer {
if self.is_terminal_state() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(wallet_handle, pool_handle)?;
let send_message = connection.send_message_closure(wallet_handle, pool_handle).await?;
let messages = connection.get_messages(pool_handle, agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
self.step(wallet_handle, msg.into(), Some(send_message)).await?;
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/out_of_band/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl OutOfBandReceiver {
trace!("OutOfBandReceiver::connection_exists >>>");
for service in &self.oob.services {
for connection in connections {
match connection.bootstrap_did_doc(pool_handle) {
match connection.bootstrap_did_doc(pool_handle).await {
Some(did_doc) => {
if let ServiceResolvable::Did(did) = service {
if did.to_string() == did_doc.id {
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/proof_presentation/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Prover {
if !self.progressable_by_message() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(wallet_handle, pool_handle)?;
let send_message = connection.send_message_closure(wallet_handle, pool_handle).await?;

let messages = connection.get_messages(pool_handle, agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/proof_presentation/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Verifier {
if !self.progressable_by_message() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(wallet_handle, pool_handle)?;
let send_message = connection.send_message_closure(wallet_handle, pool_handle).await?;

let messages = connection.get_messages(pool_handle, agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
Expand Down
14 changes: 6 additions & 8 deletions aries_vcx/src/messages/connection/invite.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use vdrtools_sys::PoolHandle;

use futures::executor::block_on;

use crate::did_doc::service_aries::AriesService;
use crate::did_doc::DidDoc;
use crate::error::prelude::*;
Expand All @@ -21,12 +19,12 @@ pub enum Invitation {
}

impl Invitation {
pub fn into_did_doc(&self, pool_handle: PoolHandle) -> VcxResult<DidDoc> {
pub async fn into_did_doc(&self, pool_handle: PoolHandle) -> VcxResult<DidDoc> {
let mut did_doc: DidDoc = DidDoc::default();
let (service_endpoint, recipient_keys, routing_keys) = match self {
Invitation::Public(invitation) => {
did_doc.set_id(invitation.did.to_string());
let service = block_on(ledger::get_service(pool_handle, &invitation.did)).unwrap_or_else(|err| {
let service = ledger::get_service(pool_handle, &invitation.did).await.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
AriesService::default()
});
Expand All @@ -42,7 +40,7 @@ impl Invitation {
}
Invitation::OutOfBand(invitation) => {
did_doc.set_id(invitation.id.0.clone());
let service = block_on(invitation.services[0].resolve(pool_handle)).unwrap_or_else(|err| {
let service = invitation.services[0].resolve(pool_handle).await.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
AriesService::default()
});
Expand Down Expand Up @@ -239,14 +237,14 @@ pub mod unit_tests {
assert_eq!(_public_invitation(), invitation);
}

#[test]
fn test_did_doc_from_invitation_works() {
#[tokio::test]
async fn test_did_doc_from_invitation_works() {
let mut did_doc = DidDoc::default();
did_doc.set_id(MessageId::id().0);
did_doc.set_service_endpoint(_service_endpoint());
did_doc.set_recipient_keys(_recipient_keys());
did_doc.set_routing_keys(_routing_keys());

assert_eq!(did_doc, Invitation::Pairwise(_pairwise_invitation()).into_did_doc(0).unwrap());
assert_eq!(did_doc, Invitation::Pairwise(_pairwise_invitation()).into_did_doc(0).await.unwrap());
}
}
20 changes: 11 additions & 9 deletions aries_vcx/src/protocols/connection/invitee/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,20 @@ impl SmConnectionInvitee {
}
}

pub fn their_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
pub async fn their_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
match self.state {
InviteeFullState::Initial(_) => None,
InviteeFullState::Invited(ref state) => state.invitation.into_did_doc(pool_handle).ok(),
InviteeFullState::Invited(ref state) => state.invitation.into_did_doc(pool_handle).await.ok(),
InviteeFullState::Requested(ref state) => Some(state.did_doc.clone()),
InviteeFullState::Responded(ref state) => Some(state.did_doc.clone()),
InviteeFullState::Completed(ref state) => Some(state.did_doc.clone()),
}
}

pub fn bootstrap_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
pub async fn bootstrap_did_doc(&self, pool_handle: PoolHandle) -> Option<DidDoc> {
match self.state {
InviteeFullState::Initial(_) => None,
InviteeFullState::Invited(ref state) => state.invitation.into_did_doc(pool_handle).ok(),
InviteeFullState::Invited(ref state) => state.invitation.into_did_doc(pool_handle).await.ok(),
InviteeFullState::Requested(ref state) => Some(state.did_doc.clone()),
InviteeFullState::Responded(ref state) => Some(state.did_doc.clone()),
InviteeFullState::Completed(ref state) => Some(state.bootstrap_did_doc.clone()),
Expand Down Expand Up @@ -162,17 +162,19 @@ impl SmConnectionInvitee {
}
}

pub fn remote_did(&self, pool_handle: PoolHandle) -> VcxResult<String> {
pub async fn remote_did(&self, pool_handle: PoolHandle) -> VcxResult<String> {
self.their_did_doc(pool_handle)
.await
.map(|did_doc: DidDoc| did_doc.id)
.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
"Remote Connection DID is not set",
))
}

pub fn remote_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
pub async fn remote_vk(&self, pool_handle: PoolHandle) -> VcxResult<String> {
self.their_did_doc(pool_handle)
.await
.and_then(|did_doc| did_doc.recipient_keys().get(0).cloned())
.ok_or(VcxError::from_msg(
VcxErrorKind::NotReady,
Expand Down Expand Up @@ -316,16 +318,16 @@ impl SmConnectionInvitee {
{
let (state, thread_id) = match self.state {
InviteeFullState::Invited(ref state) => {
let ddo = state.invitation.into_did_doc(pool_handle)?;
let ddo = state.invitation.into_did_doc(pool_handle).await?;
let (request, thread_id) = self.build_connection_request_msg(routing_keys, service_endpoint)?;
send_message(
wallet_handle,
self.pairwise_info.pw_vk.clone(),
ddo,
ddo.clone(),
request.to_a2a_message(),
)
.await?;
(InviteeFullState::Requested((state.clone(), request, pool_handle).into()), thread_id)
(InviteeFullState::Requested((state.clone(), request, ddo).into()), thread_id)
}
_ => (self.state.clone(), self.get_thread_id()),
};
Expand Down
9 changes: 4 additions & 5 deletions aries_vcx/src/protocols/connection/invitee/states/invited.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use vdrtools_sys::PoolHandle;

use crate::messages::connection::invite::Invitation;
use crate::messages::connection::request::Request;
use crate::protocols::connection::invitee::states::requested::RequestedState;
use crate::did_doc::DidDoc;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct InvitedState {
pub invitation: Invitation,
}

impl From<(InvitedState, Request, PoolHandle)> for RequestedState {
fn from((state, request, pool_handle): (InvitedState, Request, PoolHandle)) -> RequestedState {
impl From<(InvitedState, Request, DidDoc)> for RequestedState {
fn from((_state, request, did_doc): (InvitedState, Request, DidDoc)) -> RequestedState {
trace!("ConnectionInvitee: transit state from InvitedState to RequestedState");
RequestedState {
request,
did_doc: state.invitation.into_did_doc(pool_handle).unwrap()
did_doc
}
}
}
4 changes: 2 additions & 2 deletions aries_vcx/tests/test_agency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mod integration_tests {

info!("test_connection_send_works:: Test if Send Message works");
{
faber.connection.send_message_closure(faber.wallet_handle, faber.pool_handle).unwrap()(message.to_a2a_message())
faber.connection.send_message_closure(faber.wallet_handle, faber.pool_handle).await.unwrap()(message.to_a2a_message())
.await
.unwrap();
}
Expand Down Expand Up @@ -213,7 +213,7 @@ mod integration_tests {
{
let credential_offer = aries_vcx::messages::issuance::credential_offer::test_utils::_credential_offer();

faber.connection.send_message_closure(faber.wallet_handle, faber.pool_handle).unwrap()(credential_offer.to_a2a_message())
faber.connection.send_message_closure(faber.wallet_handle, faber.pool_handle).await.unwrap()(credential_offer.to_a2a_message())
.await
.unwrap();

Expand Down
Loading