Skip to content

Commit

Permalink
Diddoc/remove generics updates (#1097)
Browse files Browse the repository at this point in the history
* Enhancement: find first comptabile key agreement verification method

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Used TypedBulder for didcomm-service data models

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Do not leak DidDocumentBuilder out of did_doc crate

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Refactor construct_request didexchange transition

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Reformat

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Rename resolve_their_ddo -> resolve_ddo_from_request

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Reorganize typed didcomm service models

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Make encryption envelope api more general and safer

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Change MissingField to wrap 'str rather than String

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Reduce use of OneOrList

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove DidDocumentSovError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Add negative test cases for verification method

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Do not return DidDocumentBuilderError from JsonWebKey methods

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Multibase wrapper use custom error instead of DidDocumentBuilderError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Add todo about using ? with Jwk decoding

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Handle jwk error explicitly

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove now unnecessary serde:error->DidDocumentBuilderError mapping

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Uri wrapper use custom error instead of DidDocumentBuilderError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Create custom error for KeyDecoding errors

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove unused InvalidInput error variant

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Use PeerDid resolver in favor of peer_did.to_did_doc

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Typed service object represents service with particular value of types attribute

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix didpeer test, fix did:peer:2 regex validation

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix didpeer regex, fix test test_peer_did_2_encode_decode, fix service id deabbreviation

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Add todo note

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix invalid peer did fixture, remove comments

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove forgotten logs

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Use global uri error mapping in aries-vcx

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Create DidDocumentLookupError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove getters of DidResolutionOutput in favor of public fields (commonly used for destructuring pattern)

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Replace for cycles by find()

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Tweak send_message to take Url by reference

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Change to_did_doc to to_did_doc_builder

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Add source attribute to MultibaseWrapperError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Create JsonWebKeyError

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Turn KeyDecodingError enum into struct with source err as trait object

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Define peer2 resolution on peer_did itself

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Refactor service_types()

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix formatting

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* refactor: keep single way to resolve peer dids (#1106)

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

---------

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jan 18, 2024
1 parent 4a3a300 commit e0c163e
Show file tree
Hide file tree
Showing 67 changed files with 863 additions and 774 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aries/agents/node/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ typings/
# End of https://www.gitignore.io/api/node


vcxagent-core/storage-proofs/
vcxagent-core/storage-*

**/test/tmp
19 changes: 9 additions & 10 deletions aries/agents/rust/aries-vcx-agent/src/error/convertors.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::convert::From;

use aries_vcx::{
did_doc::error::{DidDocumentBuilderError, DidDocumentSovError},
did_doc::error::DidDocumentBuilderError,
errors::error::{AriesVcxError, AriesVcxErrorKind},
protocols::did_exchange::state_machine::generic::GenericDidExchange,
};
use aries_vcx_core::errors::error::AriesVcxCoreError;
use did_resolver_sov::did_resolver::did_doc::schema::utils::error::DidDocumentLookupError;

use crate::error::*;

Expand All @@ -29,7 +30,6 @@ impl From<serde_json::Error> for AgentError {
}
}

// TODO
impl From<AriesVcxCoreError> for AgentError {
fn from(err: AriesVcxCoreError) -> Self {
let kind = AgentErrorKind::GenericAriesVcxError;
Expand All @@ -38,14 +38,6 @@ impl From<AriesVcxCoreError> for AgentError {
}
}

impl From<DidDocumentSovError> for AgentError {
fn from(err: DidDocumentSovError) -> Self {
let kind = AgentErrorKind::GenericAriesVcxError;
let message = format!("DidDocumentSovError; err: {:?}", err.to_string());
AgentError { message, kind }
}
}

impl From<DidDocumentBuilderError> for AgentError {
fn from(err: DidDocumentBuilderError) -> Self {
let kind = AgentErrorKind::GenericAriesVcxError;
Expand Down Expand Up @@ -93,3 +85,10 @@ impl From<(GenericDidExchange, AriesVcxError)> for AgentError {
AgentError { message, kind }
}
}
impl From<DidDocumentLookupError> for AgentError {
fn from(err: DidDocumentLookupError) -> Self {
let kind = AgentErrorKind::GenericAriesVcxError;
let message = format!("DidDocumentLookupError; err: {:?}", err.to_string());
AgentError { message, kind }
}
}
2 changes: 1 addition & 1 deletion aries/agents/rust/aries-vcx-agent/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct VcxHttpClient;

#[async_trait]
impl Transport for VcxHttpClient {
async fn send_message(&self, msg: Vec<u8>, service_endpoint: Url) -> VcxResult<()> {
async fn send_message(&self, msg: Vec<u8>, service_endpoint: &Url) -> VcxResult<()> {
shared::http_client::post_message(msg, service_endpoint).await?;
Ok(())
}
Expand Down
51 changes: 33 additions & 18 deletions aries/agents/rust/aries-vcx-agent/src/services/did_exchange.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use aries_vcx::{
did_doc::schema::{service::typed::ServiceType, types::uri::Uri},
messages::{
msg_fields::protocols::{
did_exchange::{
Expand All @@ -25,7 +26,6 @@ use aries_vcx_core::wallet::{base_wallet::BaseWallet, indy::IndySdkWallet};
use did_peer::peer_did::{numalgos::numalgo2::Numalgo2, PeerDid};
use did_resolver_registry::ResolverRegistry;
use did_resolver_sov::did_resolver::did_doc::schema::did_doc::DidDocument;
use url::Url;

use super::connection::ServiceEndpoint;
use crate::{
Expand Down Expand Up @@ -83,10 +83,17 @@ impl ServiceDidExchange {
.thid;
let ddo_their = requester.their_did_doc();
let ddo_our = requester.our_did_document();
let encryption_envelope =
pairwise_encrypt(ddo_our, ddo_their, self.wallet.as_ref(), &request.into()).await?;
let service = ddo_their.get_service_of_type(&ServiceType::DIDCommV1)?;
let encryption_envelope = pairwise_encrypt(
ddo_our,
ddo_their,
self.wallet.as_ref(),
&request.into(),
service.id(),
)
.await?;
VcxHttpClient
.send_message(encryption_envelope.0, get_first_endpoint(ddo_their)?)
.send_message(encryption_envelope.0, service.service_endpoint())
.await?;
self.did_exchange.insert(&request_id, requester.clone())
}
Expand Down Expand Up @@ -127,10 +134,17 @@ impl ServiceDidExchange {
.await?;
let ddo_their = responder.their_did_doc();
let ddo_our = responder.our_did_document();
let encryption_envelope =
pairwise_encrypt(ddo_our, ddo_their, self.wallet.as_ref(), &response.into()).await?;
let service = ddo_their.get_service_of_type(&ServiceType::DIDCommV1)?;
let encryption_envelope = pairwise_encrypt(
ddo_our,
ddo_their,
self.wallet.as_ref(),
&response.into(),
service.id(),
)
.await?;
VcxHttpClient
.send_message(encryption_envelope.0, get_first_endpoint(ddo_their)?)
.send_message(encryption_envelope.0, service.service_endpoint())
.await?;
self.did_exchange.insert(&request_id, responder.clone())
}
Expand All @@ -144,10 +158,17 @@ impl ServiceDidExchange {
.await?;
let ddo_their = requester.their_did_doc();
let ddo_our = requester.our_did_document();
let encryption_envelope =
pairwise_encrypt(ddo_our, ddo_their, self.wallet.as_ref(), &complete.into()).await?;
let service = ddo_their.get_service_of_type(&ServiceType::DIDCommV1)?;
let encryption_envelope = pairwise_encrypt(
ddo_our,
ddo_their,
self.wallet.as_ref(),
&complete.into(),
service.id(),
)
.await?;
VcxHttpClient
.send_message(encryption_envelope.0, get_first_endpoint(ddo_their)?)
.send_message(encryption_envelope.0, service.service_endpoint())
.await?;
self.did_exchange.insert(&thread_id, requester.clone())
}
Expand Down Expand Up @@ -187,25 +208,19 @@ impl ServiceDidExchange {
}
}

pub fn get_first_endpoint(did_document: &DidDocument) -> AgentResult<Url> {
let service = did_document.service().first().ok_or(AgentError::from_msg(
AgentErrorKind::InvalidState,
"No service found",
))?;
Ok(service.service_endpoint().clone())
}

pub async fn pairwise_encrypt(
our_did_doc: &DidDocument,
their_did_doc: &DidDocument,
wallet: &impl BaseWallet,
message: &AriesMessage,
their_service_id: &Uri,
) -> AgentResult<EncryptionEnvelope> {
EncryptionEnvelope::create(
wallet,
serde_json::json!(message).to_string().as_bytes(),
our_did_doc,
their_did_doc,
their_service_id,
)
.await
.map_err(|err| {
Expand Down
2 changes: 1 addition & 1 deletion aries/agents/rust/mediator/src/aries_agent/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct MockTransport;

#[async_trait]
impl Transport for MockTransport {
async fn send_message(&self, _msg: Vec<u8>, _service_endpoint: url::Url) -> VcxResult<()> {
async fn send_message(&self, _msg: Vec<u8>, _service_endpoint: &url::Url) -> VcxResult<()> {
Ok(())
}
}
12 changes: 9 additions & 3 deletions aries/aries_vcx/src/errors/mapping_others.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::PoisonError;

use aries_vcx_core::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind};
use did_doc::schema::{types::uri::UriWrapperError, utils::error::DidDocumentLookupError};
use shared::errors::http_error::HttpError;

use crate::{
Expand Down Expand Up @@ -45,8 +46,8 @@ impl From<did_doc::error::DidDocumentBuilderError> for AriesVcxError {
}
}

impl From<did_doc::error::DidDocumentSovError> for AriesVcxError {
fn from(err: did_doc::error::DidDocumentSovError) -> Self {
impl From<DidDocumentLookupError> for AriesVcxError {
fn from(err: DidDocumentLookupError) -> Self {
AriesVcxError::from_msg(AriesVcxErrorKind::InvalidState, err.to_string())
}
}
Expand Down Expand Up @@ -75,7 +76,12 @@ impl From<did_key::error::DidKeyError> for AriesVcxError {
}
}

// TODO
impl From<UriWrapperError> for AriesVcxError {
fn from(err: UriWrapperError) -> Self {
AriesVcxError::from_msg(AriesVcxErrorKind::InvalidInput, err.to_string())
}
}

impl From<AriesVcxCoreError> for AriesVcxError {
fn from(err: AriesVcxCoreError) -> Self {
let kind = match err.kind() {
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx/src/handlers/mediated_connection/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn send_message(

post_message(
envelope,
did_doc.get_endpoint().ok_or_else(|| {
&did_doc.get_endpoint().ok_or_else(|| {
AriesVcxError::from_msg(AriesVcxErrorKind::InvalidUrl, "No URL in DID Doc")
})?,
)
Expand Down
4 changes: 2 additions & 2 deletions aries/aries_vcx/src/protocols/connection/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl GenericConnection {
let service_endpoint = did_doc.get_endpoint().ok_or_else(|| {
AriesVcxError::from_msg(AriesVcxErrorKind::InvalidUrl, "No URL in DID Doc")
})?;
transport.send_message(msg, service_endpoint).await
transport.send_message(msg, &service_endpoint).await
}
}

Expand Down Expand Up @@ -341,7 +341,7 @@ mod connection_serde_tests {

#[async_trait]
impl Transport for MockTransport {
async fn send_message(&self, _msg: Vec<u8>, _service_endpoint: Url) -> VcxResult<()> {
async fn send_message(&self, _msg: Vec<u8>, _service_endpoint: &Url) -> VcxResult<()> {
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx/src/protocols/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
let service_endpoint = self.their_did_doc().get_endpoint().ok_or_else(|| {
AriesVcxError::from_msg(AriesVcxErrorKind::InvalidUrl, "No URL in DID Doc")
})?;
transport.send_message(msg, service_endpoint).await
transport.send_message(msg, &service_endpoint).await
}
}

Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx/src/protocols/did_exchange/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn resolve_enc_key_from_invitation(
})?;
info!("DID resolution output {:?}", output);
Ok(output
.did_document()
.did_document
.verification_method()
.first()
.ok_or_else(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use chrono::Utc;
use did_doc::schema::{
did_doc::DidDocument,
service::{extra_fields::ServiceKeyKind, typed::didcommv1::ServiceDidCommV1, Service},
service::{service_key_kind::ServiceKeyKind, typed::didcommv1::ServiceDidCommV1, Service},
types::uri::Uri,
verification_method::{VerificationMethod, VerificationMethodType},
};
Expand Down Expand Up @@ -246,17 +246,9 @@ mod tests {
assert_eq!(&ddo_service.id().to_string(), "#service-0");
assert_eq!(
ddo_service.service_type(),
&OneOrList::List(vec![ServiceType::DIDCommV1])
&OneOrList::One(ServiceType::DIDCommV1)
);
assert_eq!(ddo_service.service_endpoint(), &service_endpoint);
let recipient_keys = ddo_service
.extra_field_recipient_keys()
.map(|keys| {
keys.into_iter()
.map(|key| key.to_string())
.collect::<Vec<_>>()
})
.unwrap();
assert_eq!(
ddo_service.extra_field_routing_keys().unwrap(),
vec![
Expand All @@ -265,7 +257,6 @@ mod tests {
]
);

println!("did_doc: {}", did_doc);
assert_eq!(did_doc.key_agreement().len(), 1);
match did_doc.key_agreement().first().unwrap() {
VerificationMethodKind::Resolved(key_agreement) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use messages::{
timing::Timing,
},
msg_fields::protocols::{
did_exchange::request::{Request, RequestContent, RequestDecorators},
did_exchange::{
complete::{Complete, CompleteDecorators},
request::{Request, RequestContent, RequestDecorators},
},
out_of_band::invitation::{Invitation, OobService},
},
};
Expand Down Expand Up @@ -40,6 +43,22 @@ pub fn construct_request(invitation_id: String, our_did: String) -> Request {
.build()
}

pub fn construct_didexchange_complete(request_id: String, invitation_id: String) -> Complete {
let decorators = CompleteDecorators::builder()
.thread(
Thread::builder()
.thid(request_id)
.pthid(invitation_id)
.build(),
)
.timing(Timing::builder().out_time(Utc::now()).build())
.build();
Complete::builder()
.id(Uuid::new_v4().to_string())
.decorators(decorators)
.build()
}

/// We are going to support only DID service values in did-exchange protocol unless there's explicit
/// good reason to keep support for "embedded" type of service value.
/// This function returns first found DID based service value from invitation.
Expand Down
Loading

0 comments on commit e0c163e

Please sign in to comment.