Skip to content

Commit

Permalink
Fix failing integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Aug 21, 2023
1 parent d8f6e2a commit 7dd93cc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 38 deletions.
68 changes: 66 additions & 2 deletions aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
use bs58;
use did_doc_sov::service::ServiceSov;
use did_doc_sov::DidDocumentSov;
use did_parser::Did;
use did_peer::peer_did::peer_did::PeerDid;
use did_peer::peer_did_resolver::resolver::PeerDidResolver;
use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput;
use did_resolver::traits::resolvable::DidResolvable;
use did_resolver_registry::ResolverRegistry;
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::out_of_band::invitation::OobService;
use messages::msg_fields::protocols::out_of_band::invitation::{Invitation as OobInvitation, OobService};
use std::{collections::HashMap, sync::Arc};

use crate::common::ledger::service_didsov::EndpointDidSov;
use crate::handlers::util::AnyInvitation;
use crate::utils::from_service_sov_to_legacy;
use crate::utils::{from_legacy_service_to_service_sov, from_service_sov_to_legacy};
use aries_vcx_core::ledger::base_ledger::{IndyLedgerRead, IndyLedgerWrite};
use aries_vcx_core::ledger::indy_vdr_ledger::{LedgerRole, UpdateRole};
use aries_vcx_core::wallet::base_wallet::BaseWallet;
Expand Down Expand Up @@ -92,6 +100,62 @@ pub async fn add_new_did(
Ok((did, verkey))
}

pub async fn resolve_oob_invitation(
resolver_registry: &Arc<ResolverRegistry>,
invitation: OobInvitation,
) -> VcxResult<DidDocumentSov> {
let mut builder = DidDocumentSov::builder(Default::default());

let mut resolved_services = vec![];
let mut resolved_vms = vec![];
let mut resolved_kas = vec![];
let mut resolved_dids = vec![];

for service in invitation.content.services {
match service {
OobService::SovService(service) => {
builder = builder.add_service(service.clone());
}
OobService::Did(did) => {
let parsed_did = Did::parse(did)?;
let DidResolutionOutput { did_document, .. } =
resolver_registry.resolve(&parsed_did, &Default::default()).await?;
resolved_services.extend(
did_document
.service()
.iter()
.map(|s| ServiceSov::try_from(s.clone()))
.collect::<Result<Vec<_>, _>>()?,
);
resolved_vms.extend_from_slice(did_document.verification_method());
resolved_kas.extend_from_slice(did_document.key_agreement());
resolved_dids.push(parsed_did);
}
OobService::AriesService(service) => {
resolved_services.push(from_legacy_service_to_service_sov(service.clone())?)
}
}
}

for service in resolved_services {
builder = builder.add_service(service);
}

for vm in resolved_vms {
builder = builder.add_verification_method(vm.clone());
}

for ka in resolved_kas {
builder = builder.add_key_agreement(ka.clone());
}

for did in resolved_dids {
builder = builder.add_controller(did);
}

Ok(builder.build())
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ pub enum ResponderState {
}

impl GenericDidExchange {
pub fn our_verkey(&self) -> &Key {
pub fn our_did_document(&self) -> &DidDocumentSov {
match self {
GenericDidExchange::Requester(requester_state) => match requester_state {
RequesterState::RequestSent(request_sent_state) => request_sent_state.our_verkey(),
RequesterState::Completed(completed_state) => completed_state.our_verkey(),
RequesterState::RequestSent(request_sent_state) => request_sent_state.our_did_doc(),
RequesterState::Completed(completed_state) => completed_state.our_did_doc(),
},
GenericDidExchange::Responder(responder_state) => match responder_state {
ResponderState::ResponseSent(response_sent_state) => response_sent_state.our_verkey(),
ResponderState::Completed(completed_state) => completed_state.our_verkey(),
ResponderState::ResponseSent(response_sent_state) => response_sent_state.our_did_doc(),
ResponderState::Completed(completed_state) => completed_state.our_did_doc(),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub fn did_doc_from_keys(did: Did, key_ver: Key, key_enc: Key, service: ServiceS

pub fn ddo_sov_to_attach(ddo: DidDocumentSov) -> Result<Attachment, AriesVcxError> {
// Interop note: acapy accepts unsigned when using peer dids?
println!("Going to attach did document: {}", serde_json::to_string(&ddo)?);
Ok(Attachment::new(AttachmentData::new(AttachmentType::Base64(
base64::encode_config(&serde_json::to_string(&ddo)?, base64::URL_SAFE_NO_PAD),
))))
Expand Down
13 changes: 6 additions & 7 deletions aries_vcx/src/protocols/did_exchange/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ use super::{
pub struct DidExchange<I, S> {
state: S,
initiation_type: PhantomData<I>,
// TODO: Do we NEED to store our DDO or does it suffice to store the verkey?
our_verkey: Key,
our_did_document: DidDocumentSov,
their_did_document: DidDocumentSov,
}

impl<I, S> DidExchange<I, S> {
pub fn our_verkey(&self) -> &Key {
&self.our_verkey
pub fn our_did_doc(&self) -> &DidDocumentSov {
&self.our_did_document
}

pub fn their_did_doc(&self) -> &DidDocumentSov {
Expand Down Expand Up @@ -76,7 +75,7 @@ impl<I, S: ThreadId> DidExchange<I, S> {
request_id: self.state.thread_id().to_string(),
},
initiation_type: PhantomData,
our_verkey: self.our_verkey,
our_did_document: self.our_did_document,
their_did_document: self.their_did_document,
},
output: problem_report,
Expand All @@ -85,11 +84,11 @@ impl<I, S: ThreadId> DidExchange<I, S> {
}

impl<I, S> DidExchange<I, S> {
pub fn from_parts(state: S, their_did_document: DidDocumentSov, our_verkey: Key) -> Self {
pub fn from_parts(state: S, their_did_document: DidDocumentSov, our_did_document: DidDocumentSov) -> Self {
Self {
state,
initiation_type: PhantomData,
our_verkey,
our_did_document,
their_did_document,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::sync::Arc;

use aries_vcx_core::{ledger::base_ledger::IndyLedgerRead, wallet::base_wallet::BaseWallet};
use did_parser::Did;
use did_resolver_registry::ResolverRegistry;
use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OobInvitation;
use url::Url;

pub struct PairwiseConstructRequestConfig {
pub ledger: Arc<dyn IndyLedgerRead>,
pub wallet: Arc<dyn BaseWallet>,
pub invitation: OobInvitation,
pub resolver_registry: Arc<ResolverRegistry>,
pub service_endpoint: Url,
pub routing_keys: Vec<String>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use messages::msg_fields::protocols::did_exchange::{
use public_key::{Key, KeyType};

use crate::{
common::{keys::get_verkey_from_ledger, ledger::transactions::into_did_doc},
common::{
keys::get_verkey_from_ledger,
ledger::transactions::{into_did_doc, resolve_oob_invitation},
},
errors::error::{AriesVcxError, AriesVcxErrorKind},
handlers::util::AnyInvitation,
protocols::did_exchange::{
Expand All @@ -35,12 +38,12 @@ impl DidExchangeRequester<RequestSent> {
service_endpoint,
routing_keys,
invitation,
resolver_registry,
}: PairwiseConstructRequestConfig,
) -> Result<TransitionResult<Self, Request>, AriesVcxError> {
verify_handshake_protocol(invitation.clone())?;
let (our_did_document, our_verkey) = create_our_did_document(&wallet, service_endpoint, routing_keys).await?;
let their_did_document =
from_legacy_did_doc_to_sov(into_did_doc(&ledger, &AnyInvitation::Oob(invitation.clone())).await?)?;
let their_did_document = resolve_oob_invitation(&resolver_registry, invitation.clone()).await?;

let signed_attach = jws_sign_attach(
ddo_sov_to_attach(our_did_document.clone())?,
Expand All @@ -61,7 +64,7 @@ impl DidExchangeRequester<RequestSent> {
request_id: request.id.clone(),
},
their_did_document,
our_verkey,
our_did_document,
),
output: request,
})
Expand Down Expand Up @@ -98,11 +101,11 @@ impl DidExchangeRequester<RequestSent> {
invitation_id,
},
their_did_document,
Key::from_base58(
&wallet.key_for_local_did(&our_did.id().to_string()).await?,
KeyType::X25519,
)?
.clone(),
our_did_document, // Key::from_base58(
// &wallet.key_for_local_did(&our_did.id().to_string()).await?,
// KeyType::X25519,
// )?
// .clone(),
),
output: request,
})
Expand Down Expand Up @@ -166,7 +169,7 @@ impl DidExchangeRequester<RequestSent> {
request_id: self.state.request_id,
},
did_document,
self.our_verkey,
self.our_did_document,
),
output: complete_message,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DidExchangeResponder<ResponseSent> {
}: ReceiveRequestConfig,
) -> Result<TransitionResult<DidExchangeResponder<ResponseSent>, Response>, AriesVcxError> {
let their_ddo = resolve_their_ddo(&resolver_registry, &request).await?;
let (our_ddo, enc_key) = create_our_did_document(&wallet, service_endpoint, routing_keys).await?;
let (our_did_document, enc_key) = create_our_did_document(&wallet, service_endpoint, routing_keys).await?;

if request.decorators.thread.and_then(|t| t.pthid) != Some(invitation_id.clone()) {
return Err(AriesVcxError::from_msg(
Expand All @@ -40,7 +40,7 @@ impl DidExchangeResponder<ResponseSent> {
));
}

let response = construct_response(our_ddo, invitation_id.clone(), request.id.clone())?;
let response = construct_response(our_did_document.clone(), invitation_id.clone(), request.id.clone())?;

Ok(TransitionResult {
state: DidExchangeResponder::from_parts(
Expand All @@ -49,7 +49,7 @@ impl DidExchangeResponder<ResponseSent> {
invitation_id,
},
their_ddo,
enc_key,
our_did_document,
),
output: response,
})
Expand Down Expand Up @@ -83,7 +83,7 @@ impl DidExchangeResponder<ResponseSent> {
request_id: self.state.request_id,
},
self.their_did_document,
self.our_verkey,
self.our_did_document,
))
}
}
37 changes: 31 additions & 6 deletions aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ extern crate log;
mod fixtures;
mod utils;

use std::sync::Arc;

use aries_vcx::protocols::did_exchange::state_machine::requester::{
ConstructRequestConfig, DidExchangeRequester, PairwiseConstructRequestConfig,
};
Expand All @@ -11,10 +13,12 @@ use aries_vcx::protocols::did_exchange::states::requester::request_sent::Request
use aries_vcx::protocols::did_exchange::states::responder::response_sent::ResponseSent;
use aries_vcx::protocols::did_exchange::transition::transition_result::TransitionResult;
use aries_vcx::utils::devsetup::SetupPoolDirectory;
use did_doc::schema::verification_method::PublicKeyField;
use did_doc::schema::verification_method::{PublicKeyField, VerificationMethodType};
use did_doc_sov::extra_fields::didcommv2::ExtraFieldsDidCommV2;
use did_doc_sov::service::didcommv2::ServiceDidCommV2;
use did_doc_sov::service::ServiceSov;
use did_peer::peer_did_resolver::resolver::PeerDidResolver;
use did_resolver_registry::ResolverRegistry;
use messages::msg_fields::protocols::out_of_band::invitation::Invitation;
use url::Url;

Expand All @@ -31,6 +35,11 @@ async fn did_exchange_test() {
let invitation: Invitation = serde_json::from_str(fixtures::OOB_INVITE).unwrap();
let invitation_id = invitation.id.clone();

let did_peer_resolver = PeerDidResolver::new();
let resolver_registry = Arc::new(
ResolverRegistry::new().register_resolver::<PeerDidResolver>("peer".into(), did_peer_resolver.into()),
);

let TransitionResult {
state: requester,
output: request,
Expand All @@ -41,19 +50,21 @@ async fn did_exchange_test() {
invitation,
service_endpoint: url.clone(),
routing_keys: vec![],
resolver_registry: resolver_registry.clone(),
},
))
.await
.unwrap();

let extra = ExtraFieldsDidCommV2::builder().build();
let service = ServiceSov::DIDCommV2(ServiceDidCommV2::new(Default::default(), url.into(), extra).unwrap());
let service =
ServiceSov::DIDCommV2(ServiceDidCommV2::new(Default::default(), url.clone().into(), extra).unwrap());
let TransitionResult {
state: responder,
output: response,
} = DidExchangeResponder::<ResponseSent>::receive_request(ReceiveRequestConfig {
wallet: institution.profile.inject_wallet(),
resolver_registry: todo!(),
resolver_registry,
request,
service_endpoint: url.clone(),
routing_keys: vec![],
Expand All @@ -69,7 +80,14 @@ async fn did_exchange_test() {

let responder = responder.receive_complete(complete).unwrap();

let responder_key = responder.our_verkey().base58();
let responder_key = responder
.our_did_doc()
.verification_method()
.first()
.unwrap()
.public_key()
.base58()
.unwrap();
assert_eq!(
requester
.their_did_doc()
Expand All @@ -82,9 +100,16 @@ async fn did_exchange_test() {
responder_key
);

let requester_key = requester.our_verkey().base58();
let requester_key = requester
.our_did_doc()
.verification_method()
.first()
.unwrap()
.public_key()
.base58()
.unwrap();
assert_eq!(
requester
responder
.their_did_doc()
.verification_method()
.first()
Expand Down
6 changes: 3 additions & 3 deletions did_resolver/src/traits/resolvable/resolution_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::shared_types::did_document_metadata::DidDocumentMetadata;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DidResolutionOutput<E: Default> {
did_document: DidDocument<E>,
did_resolution_metadata: DidResolutionMetadata,
did_document_metadata: DidDocumentMetadata,
pub did_document: DidDocument<E>,
pub did_resolution_metadata: DidResolutionMetadata,
pub did_document_metadata: DidDocumentMetadata,
}

impl<E: Default> DidResolutionOutput<E> {
Expand Down

0 comments on commit 7dd93cc

Please sign in to comment.