Skip to content

Commit

Permalink
Define peer2 resolution on peer_did itself
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 11, 2024
1 parent b70fb26 commit bb408ba
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::sync::Arc;
use did_parser::Did;
use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolver},
resolver::options::PublicKeyEncoding,
};
use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput;
use did_resolver_registry::ResolverRegistry;
use messages::msg_fields::protocols::did_exchange::{
complete::Complete as CompleteMessage, request::Request, response::Response,
Expand Down Expand Up @@ -34,10 +33,8 @@ impl DidExchangeRequester<RequestSent> {
let their_did_document = resolver_registry
.resolve(their_did, &Default::default())
.await?
.did_document
.clone();
let DidResolutionOutput { did_document, .. } =
PeerDidResolver::resolve_peerdid2(our_peer_did, PublicKeyEncoding::Base58).await?;
.did_document;
let did_document = our_peer_did.resolve(PublicKeyEncoding::Base58)?;
let invitation_id = Uuid::new_v4().to_string();

let request = construct_request(invitation_id.clone(), our_peer_did.to_string());
Expand Down Expand Up @@ -76,11 +73,9 @@ impl DidExchangeRequester<RequestSent> {
} else {
let peer_did = PeerDid::<Numalgo2>::parse(response.content.did)
.map_err(to_transition_error(self.clone()))?;
let DidResolutionOutput { did_document, .. } =
PeerDidResolver::resolve_peerdid2(&peer_did, PublicKeyEncoding::Base58)
.await
.map_err(to_transition_error(self.clone()))?;
did_document
peer_did
.resolve(PublicKeyEncoding::Base58)
.map_err(to_transition_error(self.clone()))?
};

let complete_message = construct_didexchange_complete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use aries_vcx_core::wallet::base_wallet::BaseWallet;
use did_doc::schema::did_doc::DidDocument;
use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolver},
resolver::options::PublicKeyEncoding,
};
use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput;
use did_resolver_registry::ResolverRegistry;
use messages::msg_fields::protocols::did_exchange::{
complete::Complete, request::Request, response::Response,
Expand Down Expand Up @@ -34,10 +33,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 DidResolutionOutput {
did_document: our_did_document,
..
} = PeerDidResolver::resolve_peerdid2(our_peer_did, PublicKeyEncoding::Base58).await?;
let our_did_document = our_peer_did.resolve(PublicKeyEncoding::Base58)?;
// 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
8 changes: 1 addition & 7 deletions aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolver},
};
use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput;
use did_resolver_registry::ResolverRegistry;
use did_resolver_sov::resolution::DidSovResolver;
use log::info;
Expand Down Expand Up @@ -138,12 +137,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 DidResolutionOutput {
did_document: check_diddoc,
..
} = PeerDidResolver::resolve_peerdid2(&responders_peer_did, PublicKeyEncoding::Base58)
.await
.unwrap();
let check_diddoc = responders_peer_did.resolve(PublicKeyEncoding::Base58)?;
info!("Responder decodes constructed peer:did as did document: {check_diddoc}");

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

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

let DidResolutionOutput { did_document, .. } =
PeerDidResolver::resolve_peerdid2(&peer_did_2, PublicKeyEncoding::Base58)
.await
.unwrap();
let did_document = peer_did_2.resolve(PublicKeyEncoding::Base58)?;
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,6 +42,17 @@ 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
36 changes: 9 additions & 27 deletions did_core/did_methods/did_peer/src/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_trait::async_trait;
use did_doc::schema::did_doc::DidDocumentBuilder;
use did_parser::Did;
use did_resolver::{
error::GenericError,
Expand All @@ -11,9 +10,7 @@ use did_resolver::{
use serde::{Deserialize, Serialize};

use crate::{
error::DidPeerError,
peer_did::{generic::AnyPeerDid, numalgos::numalgo2::Numalgo2, PeerDid},
resolver::options::PublicKeyEncoding,
error::DidPeerError, peer_did::generic::AnyPeerDid, resolver::options::PublicKeyEncoding,
};

pub mod options;
Expand All @@ -32,24 +29,6 @@ pub struct PeerDidResolutionOptions {
pub encoding: Option<PublicKeyEncoding>,
}

impl PeerDidResolver {
pub async fn resolve_peerdid2(
peer_did: &PeerDid<Numalgo2>,
public_key_encoding: PublicKeyEncoding,
) -> Result<DidResolutionOutput, GenericError> {
let builder: DidDocumentBuilder = peer_did.to_did_doc_builder(public_key_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();
let builder =
DidResolutionOutput::builder(did_doc).did_resolution_metadata(resolution_metadata);
Ok(builder.build())
}
}

#[async_trait]
impl DidResolvable for PeerDidResolver {
type DidResolutionOptions = PeerDidResolutionOptions;
Expand All @@ -62,11 +41,14 @@ impl DidResolvable for PeerDidResolver {
let peer_did = AnyPeerDid::parse(did.to_owned())?;
match peer_did {
AnyPeerDid::Numalgo2(peer_did) => {
Self::resolve_peerdid2(
&peer_did,
options.encoding.unwrap_or(PublicKeyEncoding::Multibase),
)
.await
let did_doc =
peer_did.resolve(options.encoding.unwrap_or(PublicKeyEncoding::Base58))?;
let resolution_metadata = DidResolutionMetadata::builder()
.content_type("application/did+json".to_string())
.build();
let builder = DidResolutionOutput::builder(did_doc)
.did_resolution_metadata(resolution_metadata);
Ok(builder.build())
}
n => Err(Box::new(DidPeerError::UnsupportedNumalgo(n.numalgo()))),
}
Expand Down

0 comments on commit bb408ba

Please sign in to comment.