From ca830532a44da6c4d84b36ba2f1c769dc366b73e Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Wed, 23 Aug 2023 12:57:23 +0200 Subject: [PATCH] Sign response attachment and other updates Signed-off-by: Miroslav Kovar --- agents/rust/aries-vcx-agent/src/agent/init.rs | 1 - .../src/services/did_exchange.rs | 27 +++++-------- aries_vcx/src/common/ledger/transactions.rs | 3 -- aries_vcx/src/protocols/did_exchange/mod.rs | 39 +++++++++++++++++++ .../did_exchange/state_machine/generic/mod.rs | 1 - .../did_exchange/state_machine/helpers.rs | 3 +- .../did_exchange/state_machine/mod.rs | 1 - .../requester/request_sent/config.rs | 1 - .../requester/request_sent/mod.rs | 9 +---- .../responder/response_sent/helpers.rs | 5 +-- .../responder/response_sent/mod.rs | 2 +- aries_vcx/src/utils/mod.rs | 19 +-------- aries_vcx/tests/test_did_exchange.rs | 31 ++++++++------- did_doc_sov/src/extra_fields/didcommv1.rs | 4 -- 14 files changed, 71 insertions(+), 75 deletions(-) diff --git a/agents/rust/aries-vcx-agent/src/agent/init.rs b/agents/rust/aries-vcx-agent/src/agent/init.rs index 2d7ea85329..4c96785b60 100644 --- a/agents/rust/aries-vcx-agent/src/agent/init.rs +++ b/agents/rust/aries-vcx-agent/src/agent/init.rs @@ -1,5 +1,4 @@ use std::sync::Arc; -use std::time::Duration; use aries_vcx::core::profile::ledger::{build_ledger_components, VcxPoolConfig}; use aries_vcx::global::settings::DEFAULT_LINK_SECRET_ALIAS; diff --git a/agents/rust/aries-vcx-agent/src/services/did_exchange.rs b/agents/rust/aries-vcx-agent/src/services/did_exchange.rs index 5527c4b304..1f90191744 100644 --- a/agents/rust/aries-vcx-agent/src/services/did_exchange.rs +++ b/agents/rust/aries-vcx-agent/src/services/did_exchange.rs @@ -2,17 +2,19 @@ use std::sync::Arc; use aries_vcx::{ core::profile::profile::Profile, - did_doc_sov::extra_fields::KeyKind, messages::msg_fields::protocols::{ did_exchange::{complete::Complete, request::Request, response::Response}, - out_of_band::invitation::{Invitation as OobInvitation, OobService}, + out_of_band::invitation::Invitation as OobInvitation, }, protocols::{ connection::wrap_and_send_msg, - did_exchange::state_machine::{ - generic::{GenericDidExchange, ThinState}, - requester::{ConstructRequestConfig, PairwiseConstructRequestConfig, PublicConstructRequestConfig}, - responder::ReceiveRequestConfig, + did_exchange::{ + resolve_key_from_invitation, + state_machine::{ + generic::{GenericDidExchange, ThinState}, + requester::{ConstructRequestConfig, PairwiseConstructRequestConfig, PublicConstructRequestConfig}, + responder::ReceiveRequestConfig, + }, }, }, utils::from_did_doc_sov_to_legacy, @@ -79,7 +81,6 @@ impl ServiceDidExchange { pub async fn send_request_pairwise(&self, invitation: OobInvitation) -> AgentResult { let config = ConstructRequestConfig::Pairwise(PairwiseConstructRequestConfig { - ledger: self.profile.inject_indy_ledger_read(), wallet: self.profile.inject_wallet(), invitation: invitation.clone(), resolver_registry: self.resolver_registry.clone(), @@ -110,17 +111,7 @@ impl ServiceDidExchange { // We don't want to be sending response if we don't know if there is any invitation // associated with the request. let request_id = request.clone().decorators.thread.unwrap().thid; - let invitation_key = match invitation.content.services.get(0).unwrap() { - OobService::SovService(service) => match service.extra().first_recipient_key()? { - KeyKind::DidKey(did_key) => did_key.key().to_owned(), - KeyKind::Value(key_value) => todo!("Legacy - parse key value {key_value} as base58 encoded key"), - KeyKind::Reference(reference) => unimplemented!("Can't resolve reference without a DDO: {reference}"), - }, - OobService::Did(did) => { - todo!("Resolve the thing and extract key from DDO"); - } - OobService::AriesService(_) => todo!(), - }; + let invitation_key = resolve_key_from_invitation(&invitation, &self.resolver_registry).await?; let (responder, response) = GenericDidExchange::handle_request(ReceiveRequestConfig { wallet: self.profile.inject_wallet(), resolver_registry: self.resolver_registry.clone(), diff --git a/aries_vcx/src/common/ledger/transactions.rs b/aries_vcx/src/common/ledger/transactions.rs index 6af623152c..d28c9a2117 100644 --- a/aries_vcx/src/common/ledger/transactions.rs +++ b/aries_vcx/src/common/ledger/transactions.rs @@ -2,10 +2,7 @@ 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; diff --git a/aries_vcx/src/protocols/did_exchange/mod.rs b/aries_vcx/src/protocols/did_exchange/mod.rs index 12c1fbbbc5..d38c21dd19 100644 --- a/aries_vcx/src/protocols/did_exchange/mod.rs +++ b/aries_vcx/src/protocols/did_exchange/mod.rs @@ -1,3 +1,42 @@ +use std::sync::Arc; + +use did_doc_sov::extra_fields::KeyKind; +use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput; +use did_resolver_registry::ResolverRegistry; +use messages::msg_fields::protocols::out_of_band::invitation::{Invitation as OobInvitation, OobService}; +use public_key::{Key, KeyType}; + +use crate::errors::error::{AriesVcxError, AriesVcxErrorKind}; + pub mod state_machine; pub mod states; pub mod transition; + +pub async fn resolve_key_from_invitation( + invitation: &OobInvitation, + resolver_registry: &Arc, +) -> Result { + match invitation.content.services.get(0).unwrap() { + OobService::SovService(service) => match service.extra().first_recipient_key()? { + KeyKind::DidKey(did_key) => Ok(did_key.key().to_owned()), + KeyKind::Value(value) => Ok(Key::from_base58(value, KeyType::Ed25519)?), + KeyKind::Reference(reference) => Err(AriesVcxError::from_msg( + AriesVcxErrorKind::InvalidInput, + format!("Cannot resolve the reference {reference} without a did document"), + )), + }, + OobService::Did(did) => { + let DidResolutionOutput { did_document, .. } = resolver_registry + .resolve(&did.clone().try_into()?, &Default::default()) + .await + .map_err(|err| { + AriesVcxError::from_msg(AriesVcxErrorKind::InvalidDid, format!("DID resolution failed: {err}")) + })?; + Ok(did_document.verification_method().first().unwrap().public_key()?) + } + OobService::AriesService(service) => Ok(Key::from_base58( + service.recipient_keys.first().unwrap(), + KeyType::Ed25519, + )?), + } +} diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs b/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs index 4a059f983e..1b6de2d529 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs @@ -3,7 +3,6 @@ mod thin_state; use did_doc_sov::DidDocumentSov; use messages::msg_fields::protocols::did_exchange::{complete::Complete, request::Request, response::Response}; -use public_key::Key; use crate::{ errors::error::{AriesVcxError, AriesVcxErrorKind}, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/helpers.rs b/aries_vcx/src/protocols/did_exchange/state_machine/helpers.rs index a18c119041..0b4dedb848 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/helpers.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/helpers.rs @@ -59,7 +59,6 @@ pub async fn create_our_did_document( // TODO: Make it easier to generate peer did from keys and service, and generate DDO from it let did_document_temp = did_doc_from_keys(Default::default(), key_ver.clone(), key_enc.clone(), service.clone())?; let peer_did = generate_numalgo2(did_document_temp.into())?; - let vm_id = peer_did.to_numalgo3(); Ok(( did_doc_from_keys(peer_did.clone().into(), key_ver, key_enc.clone(), service)?, @@ -161,7 +160,7 @@ pub fn attach_to_ddo_sov(attachment: Attachment) -> Result(&bytes) { Ok(ddo) => Ok(ddo), Err(err) => { - println!("Error deserializing to new DDO: {err}"); + error!("Error deserializing to new DDO: {err}"); let res: AriesDidDoc = serde_json::from_slice(&bytes).map_err(|err| { AriesVcxError::from_msg( AriesVcxErrorKind::SerializationError, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs b/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs index 4e68f09260..93a811d099 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs @@ -16,7 +16,6 @@ use uuid::Uuid; use std::marker::PhantomData; use did_doc_sov::DidDocumentSov; -use public_key::Key; use super::{ states::{abandoned::Abandoned, traits::ThreadId}, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/config.rs b/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/config.rs index a02de56888..709a3a372b 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/config.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/config.rs @@ -7,7 +7,6 @@ use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OobI use url::Url; pub struct PairwiseConstructRequestConfig { - pub ledger: Arc, pub wallet: Arc, pub invitation: OobInvitation, pub resolver_registry: Arc, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs b/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs index 6cdbaf8e2e..d2cb390300 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs @@ -7,21 +7,15 @@ use did_resolver::{error::GenericError, traits::resolvable::DidResolvable}; use messages::msg_fields::protocols::did_exchange::{ complete::Complete as CompleteMessage, request::Request, response::Response, }; -use public_key::{Key, KeyType}; use crate::{ - common::{ - keys::get_verkey_from_ledger, - ledger::transactions::{into_did_doc, resolve_oob_invitation}, - }, + common::ledger::transactions::resolve_oob_invitation, errors::error::{AriesVcxError, AriesVcxErrorKind}, - handlers::util::AnyInvitation, protocols::did_exchange::{ state_machine::helpers::{attach_to_ddo_sov, create_our_did_document, ddo_sov_to_attach, jws_sign_attach}, states::{completed::Completed, requester::request_sent::RequestSent}, transition::{transition_error::TransitionError, transition_result::TransitionResult}, }, - utils::from_legacy_did_doc_to_sov, }; use helpers::{construct_complete_message, construct_request, did_doc_from_did, verify_handshake_protocol}; @@ -33,7 +27,6 @@ use super::DidExchangeRequester; impl DidExchangeRequester { async fn construct_request_pairwise( PairwiseConstructRequestConfig { - ledger, wallet, service_endpoint, routing_keys, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/helpers.rs b/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/helpers.rs index 4cda735ec8..8a47ac9b81 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/helpers.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/helpers.rs @@ -10,10 +10,7 @@ use messages::{ }, }; -use crate::{ - errors::error::AriesVcxError, - protocols::did_exchange::state_machine::helpers::{attach_to_ddo_sov, ddo_sov_to_attach}, -}; +use crate::{errors::error::AriesVcxError, protocols::did_exchange::state_machine::helpers::attach_to_ddo_sov}; pub async fn resolve_their_ddo( resolver_registry: &Arc, diff --git a/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs b/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs index df273783ed..d5951c8ac7 100644 --- a/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs +++ b/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs @@ -32,7 +32,7 @@ impl DidExchangeResponder { }: ReceiveRequestConfig, ) -> Result, Response>, AriesVcxError> { let their_ddo = resolve_their_ddo(&resolver_registry, &request).await?; - let (our_did_document, 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( diff --git a/aries_vcx/src/utils/mod.rs b/aries_vcx/src/utils/mod.rs index 217151eb2f..9a62f09b65 100644 --- a/aries_vcx/src/utils/mod.rs +++ b/aries_vcx/src/utils/mod.rs @@ -12,7 +12,6 @@ use did_key::DidKey; use did_parser::Did; use diddoc_legacy::aries::diddoc::AriesDidDoc; use diddoc_legacy::aries::service::AriesService; -use public_key::{Key, KeyType}; use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult}; use crate::utils::encryption_envelope::EncryptionEnvelope; @@ -112,21 +111,6 @@ pub async fn send_message_anonymously( Ok(()) } -fn vm_method_type_to_key_type(vm_type: &VerificationMethodType) -> VcxResult { - match vm_type { - VerificationMethodType::Ed25519VerificationKey2018 | VerificationMethodType::Ed25519VerificationKey2020 => { - Ok(KeyType::Ed25519) - } - VerificationMethodType::X25519KeyAgreementKey2019 | VerificationMethodType::X25519KeyAgreementKey2020 => { - Ok(KeyType::X25519) - } - a @ _ => Err(AriesVcxError::from_msg( - AriesVcxErrorKind::InvalidState, - format!("Unable to convert ver. method type {a} to key type"), - )), - } -} - // TODO: Get rid of this please!!! pub fn from_did_doc_sov_to_legacy(ddo: DidDocumentSov) -> VcxResult { let mut new_ddo = AriesDidDoc::default(); @@ -151,13 +135,12 @@ pub fn from_did_doc_sov_to_legacy(ddo: DidDocumentSov) -> VcxResult recipient_keys.push(key.key().base58()); } KeyKind::Reference(_) => {} - KeyKind::Value(value) => todo!(), + KeyKind::Value(_) => {} } } } } new_ddo.set_recipient_keys(recipient_keys); - println!("Converted their ddo {ddo:?} to legacy ddo: {new_ddo:?}"); Ok(new_ddo) } diff --git a/aries_vcx/tests/test_did_exchange.rs b/aries_vcx/tests/test_did_exchange.rs index 6637543362..e1460a031c 100644 --- a/aries_vcx/tests/test_did_exchange.rs +++ b/aries_vcx/tests/test_did_exchange.rs @@ -5,6 +5,7 @@ mod utils; use std::sync::Arc; +use aries_vcx::protocols::did_exchange::resolve_key_from_invitation; use aries_vcx::protocols::did_exchange::state_machine::requester::{ ConstructRequestConfig, DidExchangeRequester, PairwiseConstructRequestConfig, }; @@ -31,21 +32,24 @@ async fn did_exchange_test() { let institution = create_faber(setup.genesis_file_path.clone()).await; let consumer = create_alice(setup.genesis_file_path).await; - let url: Url = "http://dummyurl.org".parse().unwrap(); - 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::("peer".into(), did_peer_resolver.into()), ); + let url: Url = "http://dummyurl.org".parse().unwrap(); + // TODO: Create invite manually + let invitation: Invitation = serde_json::from_str(fixtures::OOB_INVITE).unwrap(); + let invitation_id = invitation.id.clone(); + let invitation_key = resolve_key_from_invitation(&invitation, &resolver_registry) + .await + .unwrap(); + let TransitionResult { state: requester, output: request, } = DidExchangeRequester::::construct_request(ConstructRequestConfig::Pairwise( PairwiseConstructRequestConfig { - ledger: consumer.profile.inject_indy_ledger_read(), wallet: consumer.profile.inject_wallet(), invitation, service_endpoint: url.clone(), @@ -69,6 +73,7 @@ async fn did_exchange_test() { service_endpoint: url.clone(), routing_keys: vec![], invitation_id, + invitation_key, }) .await .unwrap(); @@ -86,8 +91,8 @@ async fn did_exchange_test() { .first() .unwrap() .public_key() - .base58() - .unwrap(); + .unwrap() + .base58(); assert_eq!( requester .their_did_doc() @@ -95,8 +100,8 @@ async fn did_exchange_test() { .first() .unwrap() .public_key() - .base58() - .unwrap(), + .unwrap() + .base58(), responder_key ); @@ -106,8 +111,8 @@ async fn did_exchange_test() { .first() .unwrap() .public_key() - .base58() - .unwrap(); + .unwrap() + .base58(); assert_eq!( responder .their_did_doc() @@ -115,8 +120,8 @@ async fn did_exchange_test() { .first() .unwrap() .public_key() - .base58() - .unwrap(), + .unwrap() + .base58(), requester_key ); }) diff --git a/did_doc_sov/src/extra_fields/didcommv1.rs b/did_doc_sov/src/extra_fields/didcommv1.rs index 5b5d418a20..393cbb1821 100644 --- a/did_doc_sov/src/extra_fields/didcommv1.rs +++ b/did_doc_sov/src/extra_fields/didcommv1.rs @@ -2,16 +2,12 @@ use serde::{Deserialize, Serialize}; use super::{AcceptType, KeyKind}; -// TODO: Remove these crazy defaults!!! #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] #[serde(rename_all = "camelCase")] #[serde(deny_unknown_fields)] pub struct ExtraFieldsDidCommV1 { - #[serde(default)] priority: u32, - #[serde(default)] recipient_keys: Vec, - #[serde(default)] routing_keys: Vec, #[serde(default)] accept: Vec,