Skip to content

Commit

Permalink
refactor: keep single way to resolve peer dids (#1106)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jan 18, 2024
1 parent ec5bdd4 commit 49b3a10
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 25 deletions.
2 changes: 0 additions & 2 deletions aries/aries_vcx/src/errors/mapping_others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ impl From<did_key::error::DidKeyError> for AriesVcxError {
}
}

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

// TODO
impl From<AriesVcxCoreError> for AriesVcxError {
fn from(err: AriesVcxCoreError) -> Self {
let kind = match err.kind() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
states::{completed::Completed, requester::request_sent::RequestSent},
transition::{transition_error::TransitionError, transition_result::TransitionResult},
},
utils::didcomm_utils::resolve_didpeer2,
};

impl DidExchangeRequester<RequestSent> {
Expand All @@ -34,7 +35,7 @@ impl DidExchangeRequester<RequestSent> {
.resolve(their_did, &Default::default())
.await?
.did_document;
let did_document = our_peer_did.resolve(PublicKeyEncoding::Base58)?;
let our_did_document = resolve_didpeer2(our_peer_did, PublicKeyEncoding::Base58).await?;
let invitation_id = Uuid::new_v4().to_string();

let request = construct_request(invitation_id.clone(), our_peer_did.to_string());
Expand All @@ -46,7 +47,7 @@ impl DidExchangeRequester<RequestSent> {
invitation_id,
},
their_did_document,
did_document,
our_did_document,
),
output: request,
})
Expand All @@ -73,8 +74,8 @@ impl DidExchangeRequester<RequestSent> {
} else {
let peer_did = PeerDid::<Numalgo2>::parse(response.content.did)
.map_err(to_transition_error(self.clone()))?;
peer_did
.resolve(PublicKeyEncoding::Base58)
resolve_didpeer2(&peer_did, PublicKeyEncoding::Base58)
.await
.map_err(to_transition_error(self.clone()))?
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
states::{completed::Completed, responder::response_sent::ResponseSent},
transition::{transition_error::TransitionError, transition_result::TransitionResult},
},
utils::didcomm_utils::resolve_didpeer2,
};

impl DidExchangeResponder<ResponseSent> {
Expand All @@ -33,7 +34,7 @@ impl DidExchangeResponder<ResponseSent> {
invitation_key: Key,
) -> Result<TransitionResult<DidExchangeResponder<ResponseSent>, Response>, AriesVcxError> {
let their_ddo = resolve_ddo_from_request(&resolver_registry, &request).await?;
let our_did_document = our_peer_did.resolve(PublicKeyEncoding::Base58)?;
let our_did_document = resolve_didpeer2(our_peer_did, PublicKeyEncoding::Base58).await?;
// TODO: Response should sign the new *did* with invitation_key only if key was rotated
// In practice if the invitation was public, we definitely will be rotating to
// peer:did
Expand Down
23 changes: 23 additions & 0 deletions aries/aries_vcx/src/utils/didcomm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@ use did_doc::schema::{
did_doc::DidDocument, service::service_key_kind::ServiceKeyKind, types::uri::Uri,
verification_method::VerificationMethodType,
};
use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolutionOptions, PeerDidResolver},
};
use did_resolver::{
error::GenericError,
traits::resolvable::{resolution_output::DidResolutionOutput, DidResolvable},
};
use public_key::Key;

use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};

pub(crate) async fn resolve_didpeer2(
did_peer: &PeerDid<Numalgo2>,
encoding: PublicKeyEncoding,
) -> Result<DidDocument, GenericError> {
let DidResolutionOutput { did_document, .. } = PeerDidResolver::new()
.resolve(
did_peer.did(),
&PeerDidResolutionOptions {
encoding: Some(encoding),
},
)
.await?;
Ok(did_document)
}

fn resolve_service_key_to_typed_key(
key: &ServiceKeyKind,
did_document: &DidDocument,
Expand Down
21 changes: 19 additions & 2 deletions aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use did_doc::schema::{
use did_parser::Did;
use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolver},
resolver::{options::PublicKeyEncoding, PeerDidResolutionOptions, PeerDidResolver},
};
use did_resolver::traits::resolvable::{resolution_output::DidResolutionOutput, DidResolvable};
use did_resolver_registry::ResolverRegistry;
use did_resolver_sov::resolution::DidSovResolver;
use log::info;
Expand All @@ -42,6 +43,22 @@ use crate::utils::test_agent::{

pub mod utils;

pub(crate) async fn resolve_didpeer2(
did_peer: &PeerDid<Numalgo2>,
encoding: PublicKeyEncoding,
) -> DidDocument {
let DidResolutionOutput { did_document, .. } = PeerDidResolver::new()
.resolve(
did_peer.did(),
&PeerDidResolutionOptions {
encoding: Some(encoding),
},
)
.await
.unwrap();
did_document
}

fn assert_key_agreement(a: DidDocument, b: DidDocument) {
let a_key = resolve_base58_key_agreement(&a).unwrap();
let b_key = resolve_base58_key_agreement(&b).unwrap();
Expand Down Expand Up @@ -137,7 +154,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
let responders_peer_did = PeerDid::<Numalgo2>::from_did_doc(responders_did_document.clone())?;
info!("Responder prepares their peer:did: {responders_peer_did}");

let check_diddoc = responders_peer_did.resolve(PublicKeyEncoding::Base58)?;
let check_diddoc = resolve_didpeer2(&responders_peer_did, PublicKeyEncoding::Base58).await;
info!("Responder decodes constructed peer:did as did document: {check_diddoc}");

let TransitionResult {
Expand Down
13 changes: 11 additions & 2 deletions did_core/did_methods/did_peer/examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use did_peer::{
numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3},
PeerDid,
},
resolver::options::PublicKeyEncoding,
resolver::{options::PublicKeyEncoding, PeerDidResolutionOptions, PeerDidResolver},
};
use did_resolver::traits::resolvable::{resolution_output::DidResolutionOutput, DidResolvable};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -46,7 +47,15 @@ async fn demo() -> Result<(), Box<dyn Error>> {
peer_did_3_v2
);

let did_document = peer_did_2.resolve(PublicKeyEncoding::Base58)?;
let DidResolutionOutput { did_document, .. } = PeerDidResolver::new()
.resolve(
peer_did_2.did(),
&PeerDidResolutionOptions {
encoding: Some(PublicKeyEncoding::Base58),
},
)
.await
.unwrap();
println!(
"Decoded did document: \n{}",
serde_json::to_string_pretty(&did_document)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ impl PeerDid<Numalgo2> {
didpeer_elements_to_diddoc(did_doc_builder, self.did(), public_key_encoding)?;
Ok(did_doc_builder)
}

pub fn resolve(
&self,
public_key_encoding: PublicKeyEncoding,
) -> Result<DidDocument, DidPeerError> {
let builder: DidDocumentBuilder = self.to_did_doc_builder(public_key_encoding)?;
let did_doc = builder
.add_also_known_as(self.to_numalgo3()?.to_string().parse()?)
.build();
Ok(did_doc)
}
}

#[derive(Clone, Copy, Default, Debug, PartialEq)]
Expand Down
9 changes: 6 additions & 3 deletions did_core/did_methods/did_peer/src/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_trait::async_trait;
use did_doc::schema::did_doc::DidDocumentBuilder;
use did_parser::Did;
use did_resolver::{
error::GenericError,
Expand Down Expand Up @@ -28,7 +29,6 @@ impl PeerDidResolver {
pub struct PeerDidResolutionOptions {
pub encoding: Option<PublicKeyEncoding>,
}

#[async_trait]
impl DidResolvable for PeerDidResolver {
type DidResolutionOptions = PeerDidResolutionOptions;
Expand All @@ -41,8 +41,11 @@ impl DidResolvable for PeerDidResolver {
let peer_did = AnyPeerDid::parse(did.to_owned())?;
match peer_did {
AnyPeerDid::Numalgo2(peer_did) => {
let did_doc =
peer_did.resolve(options.encoding.unwrap_or(PublicKeyEncoding::Base58))?;
let encoding = options.encoding.unwrap_or(PublicKeyEncoding::Multibase);
let builder: DidDocumentBuilder = peer_did.to_did_doc_builder(encoding)?;
let did_doc = builder
.add_also_known_as(peer_did.to_numalgo3()?.to_string().parse()?)
.build();
let resolution_metadata = DidResolutionMetadata::builder()
.content_type("application/did+json".to_string())
.build();
Expand Down

0 comments on commit 49b3a10

Please sign in to comment.