Skip to content

Commit

Permalink
Refactor service de/abbreviation, make conversion API <did:peer:2 -->…
Browse files Browse the repository at this point in the history
… ddo> user friendlier

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jan 4, 2024
1 parent 1f67b95 commit 004e629
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::sync::Arc;
use chrono::Utc;
use did_parser::Did;
use did_peer::{
peer_did::{
numalgos::numalgo2::{resolve::resolve_numalgo2, Numalgo2},
PeerDid,
},
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding, PeerDidResolver},
};
use did_resolver::traits::resolvable::DidResolvable;
Expand Down Expand Up @@ -45,7 +42,9 @@ impl DidExchangeRequester<RequestSent> {
.await?
.did_document()
.clone();
let our_did_document = resolve_numalgo2(our_peer_did, PublicKeyEncoding::Base58)?.build();
let our_did_document = our_peer_did
.to_did_doc_builder(PublicKeyEncoding::Base58)?
.build();
let invitation_id = Uuid::new_v4().to_string();

let request = construct_request(invitation_id.clone(), our_peer_did.to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::sync::Arc;
use aries_vcx_core::wallet::base_wallet::BaseWallet;
use did_doc::schema::did_doc::DidDocument;
use did_peer::{
peer_did::{
numalgos::numalgo2::{resolve::resolve_numalgo2, Numalgo2},
PeerDid,
},
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::options::PublicKeyEncoding,
};
use did_resolver_registry::ResolverRegistry;
Expand Down Expand Up @@ -36,7 +33,9 @@ impl DidExchangeResponder<ResponseSent> {
invitation_key: Key,
) -> Result<TransitionResult<DidExchangeResponder<ResponseSent>, Response>, AriesVcxError> {
let their_ddo = resolve_their_ddo(&resolver_registry, &request).await?;
let our_did_document = resolve_numalgo2(our_peer_did, PublicKeyEncoding::Base58)?.build();
let our_did_document = our_peer_did
.to_did_doc_builder(PublicKeyEncoding::Base58)?
.build();

// 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
Expand Down
52 changes: 37 additions & 15 deletions aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use aries_vcx::{
},
states::{requester::request_sent::RequestSent, responder::response_sent::ResponseSent},
transition::transition_result::TransitionResult,
},
}
};
use aries_vcx_core::ledger::indy_vdr_ledger::DefaultIndyLedgerRead;
use did_doc::schema::{
Expand All @@ -22,7 +22,7 @@ use did_doc::schema::{
use did_parser::Did;
use did_peer::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::PeerDidResolver,
resolver::{options::PublicKeyEncoding, PeerDidResolver},
};
use did_resolver_registry::ResolverRegistry;
use did_resolver_sov::resolution::DidSovResolver;
Expand Down Expand Up @@ -118,11 +118,11 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
invitation
);

let (our_did_document, _our_verkey) =
let (requesters_did_document, _our_verkey) =
create_our_did_document(&agent_invitee.wallet, dummy_url.clone(), vec![]).await?;
info!("Invitee prepares did document: {our_did_document}");
let peer_did_invitee = PeerDid::<Numalgo2>::from_did_doc(our_did_document.clone())?;
info!("Invitee prepares their peer:did: {peer_did_invitee}");
info!("Requester prepares did document: {requesters_did_document}");
let requesters_peer_did = PeerDid::<Numalgo2>::from_did_doc(requesters_did_document.clone())?;
info!("Requester prepares their peer:did: {requesters_peer_did}");
let did_inviter: Did = invitation_get_first_did_service(&invitation)?;

let TransitionResult {
Expand All @@ -131,7 +131,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
} = DidExchangeRequester::<RequestSent>::construct_request(
resolver_registry.clone(),
&did_inviter,
&peer_did_invitee,
&requesters_peer_did,
)
.await
.unwrap();
Expand All @@ -140,10 +140,16 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
&request
);

let (our_did_document, _our_verkey) =
let (responders_did_document, _our_verkey) =
create_our_did_document(&agent_invitee.wallet, dummy_url.clone(), vec![]).await?;
let peer_did_inviter = PeerDid::<Numalgo2>::from_did_doc(our_did_document.clone())?;
info!("Inviter prepares their peer:did: {peer_did_inviter}");
info!("Responder prepares did document: {responders_did_document}");
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: DidDocument = responders_peer_did
.to_did_doc_builder(PublicKeyEncoding::Base58)?
.build();
info!("Responder decodes constructed peer:did as did document: {check_diddoc}");

let TransitionResult {
output: response,
Expand All @@ -152,7 +158,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
&agent_inviter.wallet,
resolver_registry,
request,
&peer_did_inviter,
&responders_peer_did,
invitation_key,
)
.await
Expand All @@ -175,13 +181,29 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
);

info!(
"Requester look of counterparty diddoc {}",
requester.their_did_doc()
"Requesters did document (requesters view): {}",
requester.our_did_doc()
);
info!(
"Responder look of counterparty diddoc {}",
responder.their_did_doc()
"Responders did document (requesters view): {}",
requester.their_did_doc()
);

// let data= "Hello world";
// let m = EncryptionEnvelope::create(
// &agent_invitee.wallet,
// data.as_bytes(),
// &requester.our_did_doc(),
// &requester.their_did_doc()
// ).await?;
//
// info!("Encrypted message: {:?}", m);
//
// let unpacked = EncryptionEnvelope::auth_unpack(
// &agent_invitee.wallet,
// m.0,
// "disabled"
// ).await?;

Ok(())
}
16 changes: 8 additions & 8 deletions did_core/did_methods/did_peer/examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use did_doc::schema::{
};
use did_parser::{Did, DidUrl};
use did_peer::peer_did::{
numalgos::{
numalgo2::{resolve::resolve_numalgo2, Numalgo2},
numalgo3::Numalgo3,
},
numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3},
PeerDid,
};

Expand Down Expand Up @@ -45,10 +42,13 @@ fn demo() -> Result<(), Box<dyn Error>> {
peer_did_3_v2
);

resolve_numalgo2(
&peer_did_2,
did_peer::resolver::options::PublicKeyEncoding::Base58,
)?;
let decoded_did_doc = peer_did_2
.to_did_doc_builder(did_peer::resolver::options::PublicKeyEncoding::Base58)?
.build();
println!(
"Decoded did document: \n{}",
serde_json::to_string_pretty(&decoded_did_doc)?
);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ use std::cmp::Ordering;
use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine};
use did_doc::schema::{
did_doc::DidDocument,
service::{
extra_fields::{ServiceAcceptType, ServiceKeyKind},
typed::ServiceType,
Service,
},
utils::OneOrList,
verification_method::{VerificationMethod, VerificationMethodKind},
};
use public_key::Key;
use serde_json::from_value;

use crate::{
error::DidPeerError,
peer_did::numalgos::numalgo2::{
purpose::ElementPurpose, service_abbreviated::ServiceAbbreviatedDidPeer2,
purpose::ElementPurpose,
service_abbreviation::{abbreviate_service, ServiceAbbreviatedDidPeer2},
verification_method::get_key_by_verification_method,
},
};
Expand Down Expand Up @@ -129,69 +123,6 @@ fn append_key_to_did(mut did: String, key: Key, purpose: ElementPurpose) -> Stri
did
}

fn abbreviate_service(service: &Service) -> Result<ServiceAbbreviatedDidPeer2, DidPeerError> {
let service_endpoint = service.service_endpoint().clone();
let routing_keys = {
service
.extra()
.get("routingKeys")
.map(|value| {
from_value::<Vec<ServiceKeyKind>>(value.clone()).map_err(|_| {
DidPeerError::ParsingError(format!(
"Could not parse routing keys as Vector of Strings. Value of \
routing_keys: {}",
value
))
})
})
.unwrap_or_else(|| Ok(vec![]))
}?;
let accept = {
service
.extra()
.get("accept")
.map(|value| {
from_value::<Vec<ServiceAcceptType>>(value.clone()).map_err(|_| {
DidPeerError::ParsingError(format!(
"Could not parse accept as Vector of Strings. Value of accept: {}",
value
))
})
})
.unwrap_or_else(|| Ok(vec![]))
}?;
let service_type = service.service_type().clone();
let service_types_abbreviated = match service_type {
OneOrList::List(service_types) => {
let abbreviated_list = service_types
.iter()
.map(|value| {
if value == &ServiceType::DIDCommV2 {
"dm".to_string()
} else {
value.to_string()
}
})
.collect();
OneOrList::List(abbreviated_list)
}
OneOrList::One(service_type) => {
if service_type == ServiceType::DIDCommV2 {
OneOrList::One("dm".to_string())
} else {
OneOrList::One(service_type.to_string())
}
}
};
Ok(ServiceAbbreviatedDidPeer2::new(
Some(service.id().to_string()),
service_types_abbreviated,
service_endpoint,
routing_keys,
accept,
))
}

#[cfg(test)]
mod tests {
use did_doc::schema::{
Expand All @@ -202,12 +133,15 @@ mod tests {
types::uri::Uri,
verification_method::{VerificationMethod, VerificationMethodType},
};
use did_parser::{Did, DidUrl};
use did_parser::DidUrl;
use pretty_assertions::assert_eq;
use did_doc::schema::service::typed::ServiceType;
use did_doc::schema::utils::OneOrList;

use super::*;
use crate::{
helpers::convert_to_hashmap, peer_did::numalgos::numalgo2::resolve::resolve_numalgo2,
helpers::convert_to_hashmap,
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::options::PublicKeyEncoding,
};

Expand Down Expand Up @@ -278,16 +212,17 @@ mod tests {

let did = append_encoded_service_segment(did.to_string(), &did_document).unwrap();

let ddo = resolve_numalgo2(&did.parse::<Did>().unwrap(), PublicKeyEncoding::Base58)
let did_parsed = PeerDid::<Numalgo2>::parse(did.clone()).unwrap();
let ddo = did_parsed
.to_did_doc_builder(PublicKeyEncoding::Base58)
.unwrap()
.build();

let ddo_expected = resolve_numalgo2(
&did_expected.parse::<Did>().unwrap(),
PublicKeyEncoding::Base58,
)
.unwrap()
.build();
let did_expected_parsed = PeerDid::<Numalgo2>::parse(did_expected.clone()).unwrap();
let ddo_expected = did_expected_parsed
.to_did_doc_builder(PublicKeyEncoding::Base58)
.unwrap()
.build();

assert_eq!(ddo, ddo_expected);
assert_eq!(did, did_expected);
Expand Down
Loading

0 comments on commit 004e629

Please sign in to comment.