Skip to content

Commit

Permalink
Sign response attachment and other updates
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 23, 2023
1 parent cee9b94 commit ca83053
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 75 deletions.
1 change: 0 additions & 1 deletion agents/rust/aries-vcx-agent/src/agent/init.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
27 changes: 9 additions & 18 deletions agents/rust/aries-vcx-agent/src/services/did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,7 +81,6 @@ impl ServiceDidExchange {

pub async fn send_request_pairwise(&self, invitation: OobInvitation) -> AgentResult<String> {
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(),
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 0 additions & 3 deletions aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions aries_vcx/src/protocols/did_exchange/mod.rs
Original file line number Diff line number Diff line change
@@ -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<ResolverRegistry>,
) -> Result<Key, AriesVcxError> {
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,
)?),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand Down Expand Up @@ -161,7 +160,7 @@ pub fn attach_to_ddo_sov(attachment: Attachment) -> Result<DidDocumentSov, Aries
match serde_json::from_slice::<DidDocumentSov>(&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,
Expand Down
1 change: 0 additions & 1 deletion aries_vcx/src/protocols/did_exchange/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn IndyLedgerRead>,
pub wallet: Arc<dyn BaseWallet>,
pub invitation: OobInvitation,
pub resolver_registry: Arc<ResolverRegistry>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -33,7 +27,6 @@ use super::DidExchangeRequester;
impl DidExchangeRequester<RequestSent> {
async fn construct_request_pairwise(
PairwiseConstructRequestConfig {
ledger,
wallet,
service_endpoint,
routing_keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResolverRegistry>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl DidExchangeResponder<ResponseSent> {
}: ReceiveRequestConfig,
) -> Result<TransitionResult<DidExchangeResponder<ResponseSent>, 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(
Expand Down
19 changes: 1 addition & 18 deletions aries_vcx/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,21 +111,6 @@ pub async fn send_message_anonymously(
Ok(())
}

fn vm_method_type_to_key_type(vm_type: &VerificationMethodType) -> VcxResult<KeyType> {
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<AriesDidDoc> {
let mut new_ddo = AriesDidDoc::default();
Expand All @@ -151,13 +135,12 @@ pub fn from_did_doc_sov_to_legacy(ddo: DidDocumentSov) -> VcxResult<AriesDidDoc>
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)
}

Expand Down
31 changes: 18 additions & 13 deletions aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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::<PeerDidResolver>("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::<RequestSent>::construct_request(ConstructRequestConfig::Pairwise(
PairwiseConstructRequestConfig {
ledger: consumer.profile.inject_indy_ledger_read(),
wallet: consumer.profile.inject_wallet(),
invitation,
service_endpoint: url.clone(),
Expand All @@ -69,6 +73,7 @@ async fn did_exchange_test() {
service_endpoint: url.clone(),
routing_keys: vec![],
invitation_id,
invitation_key,
})
.await
.unwrap();
Expand All @@ -86,17 +91,17 @@ async fn did_exchange_test() {
.first()
.unwrap()
.public_key()
.base58()
.unwrap();
.unwrap()
.base58();
assert_eq!(
requester
.their_did_doc()
.verification_method()
.first()
.unwrap()
.public_key()
.base58()
.unwrap(),
.unwrap()
.base58(),
responder_key
);

Expand All @@ -106,17 +111,17 @@ async fn did_exchange_test() {
.first()
.unwrap()
.public_key()
.base58()
.unwrap();
.unwrap()
.base58();
assert_eq!(
responder
.their_did_doc()
.verification_method()
.first()
.unwrap()
.public_key()
.base58()
.unwrap(),
.unwrap()
.base58(),
requester_key
);
})
Expand Down
4 changes: 0 additions & 4 deletions did_doc_sov/src/extra_fields/didcommv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyKind>,
#[serde(default)]
routing_keys: Vec<KeyKind>,
#[serde(default)]
accept: Vec<AcceptType>,
Expand Down

0 comments on commit ca83053

Please sign in to comment.