Skip to content

Commit

Permalink
Fix service deabbreviation, add test
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas committed Dec 7, 2023
1 parent 001519f commit daff7fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
4 changes: 2 additions & 2 deletions aries/aries_vcx/src/common/ledger/service_didsov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mod unit_tests {
"Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR",
"3LYuxJBJkngDbvJj4zjx13DBUdZ2P96eNybwd2n9L9AU"
],
"types": ["endpoint", "did-communication", "DIDComm"]
"types": ["endpoint", "did-communication", "DIDCommMessaging"]
});
assert_eq!(expected, json!(&service1));
}
Expand All @@ -111,7 +111,7 @@ mod unit_tests {
"Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR",
"3LYuxJBJkngDbvJj4zjx13DBUdZ2P96eNybwd2n9L9AU"
],
"types": ["endpoint", "did-communication", "DIDComm"]
"types": ["endpoint", "did-communication", "DIDCommMessaging"]
})
.to_string();

Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {

let (our_did_document, _our_verkey) =
create_our_did_document(&agent_invitee.wallet, dummy_url.clone(), vec![]).await?;
info!("Inviter prepares did document: {our_did_document}");
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}");
let did_inviter: Did = invitation_get_first_did_service(&invitation)?;
Expand Down
104 changes: 56 additions & 48 deletions did_core/did_doc/src/schema/service/typed/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
pub mod didcommv1;
pub mod didcommv2;

use std::{fmt, fmt::Display};
use std::{fmt::Display, str::FromStr};

use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use url::Url;

use crate::schema::{types::uri::Uri, utils::OneOrList};
use crate::{
error::DidDocumentBuilderError,
schema::{types::uri::Uri, utils::OneOrList},
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TypedService<E> {
Expand Down Expand Up @@ -50,6 +53,30 @@ pub enum ServiceType {
Other(String),
}

impl FromStr for ServiceType {
type Err = DidDocumentBuilderError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
SERVICE_TYPE_AIP1 => Ok(ServiceType::AIP1),
SERVICE_TYPE_DIDCOMMV1 => Ok(ServiceType::DIDCommV1),
SERVICE_TYPE_DIDCOMMV2 => Ok(ServiceType::DIDCommV2),
SERVICE_TYPE_LEGACY => Ok(ServiceType::Legacy),
_ => Ok(ServiceType::Other(s.to_owned())),
}
}
}

impl<'de> Deserialize<'de> for ServiceType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(de::Error::custom)
}
}

impl Display for ServiceType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -84,38 +111,6 @@ impl Serialize for ServiceType {
}
}

impl<'de> Deserialize<'de> for ServiceType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct StringVisitor;

impl<'de> Visitor<'de> for StringVisitor {
type Value = ServiceType;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match value {
SERVICE_TYPE_AIP1 => Ok(ServiceType::AIP1),
SERVICE_TYPE_DIDCOMMV1 => Ok(ServiceType::DIDCommV1),
SERVICE_TYPE_DIDCOMMV2 => Ok(ServiceType::DIDCommV2),
SERVICE_TYPE_LEGACY => Ok(ServiceType::Legacy),
_ => Ok(ServiceType::Other(value.to_owned())),
}
}
}

deserializer.deserialize_str(StringVisitor)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -138,31 +133,44 @@ mod tests {
let serialized = serde_json::to_string(&service_type).unwrap();
assert_eq!(serialized, "\"IndyAgent\"");

let service_type = ServiceType::Other("Other".to_string());
let service_type = ServiceType::Other("foobar".to_string());
let serialized = serde_json::to_string(&service_type).unwrap();
assert_eq!(serialized, "\"Other\"");
assert_eq!(serialized, "\"foobar\"");
}

#[test]
fn test_service_type_deserialize() {
let service_type = ServiceType::AIP1;
let deserialized: ServiceType = serde_json::from_str("\"endpoint\"").unwrap();
assert_eq!(deserialized, service_type);
assert_eq!(deserialized, ServiceType::AIP1);

let service_type = ServiceType::DIDCommV1;
let deserialized: ServiceType = serde_json::from_str("\"did-communication\"").unwrap();
assert_eq!(deserialized, service_type);
assert_eq!(deserialized, ServiceType::DIDCommV1);

let service_type = ServiceType::DIDCommV2;
let deserialized: ServiceType = serde_json::from_str("\"DIDCommMessaging\"").unwrap();
assert_eq!(deserialized, service_type);
assert_eq!(deserialized, ServiceType::DIDCommV2);

let service_type = ServiceType::Legacy;
let deserialized: ServiceType = serde_json::from_str("\"IndyAgent\"").unwrap();
assert_eq!(deserialized, service_type);
assert_eq!(deserialized, ServiceType::Legacy);

let deserialized: ServiceType = serde_json::from_str("\"foobar\"").unwrap();
assert_eq!(deserialized, ServiceType::Other("foobar".to_string()));
}

#[test]
fn test_service_from_unquoted_string() {
let service = ServiceType::from_str("endpoint").unwrap();
assert_eq!(service, ServiceType::AIP1);

let service = ServiceType::from_str("did-communication").unwrap();
assert_eq!(service, ServiceType::DIDCommV1);

let service = ServiceType::from_str("DIDCommMessaging").unwrap();
assert_eq!(service, ServiceType::DIDCommV2);

let service = ServiceType::from_str("IndyAgent").unwrap();
assert_eq!(service, ServiceType::Legacy);

let service_type = ServiceType::Other("Other".to_string());
let deserialized: ServiceType = serde_json::from_str("\"Other\"").unwrap();
assert_eq!(deserialized, service_type);
let service = ServiceType::from_str("foobar").unwrap();
assert_eq!(service, ServiceType::Other("foobar".to_string()));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::str::FromStr;
use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine};
use did_doc::schema::{
did_doc::DidDocumentBuilder,
Expand Down Expand Up @@ -123,7 +124,10 @@ fn process_key_element(
fn deabbreviate_service(service: ServiceAbbreviatedDidPeer2, index: usize) -> Service {
let service_type = match service.service_type().clone() {
OneOrList::One(service_type) => {
let typed: ServiceType = serde_json::from_str(&service_type).unwrap(); // todo: patrik: address unwrap
let typed = match service_type.as_str() {
"dm" => ServiceType::DIDCommV2,
_ => ServiceType::from_str(&service_type).unwrap() // todo: patrik: the unwrap
};
OneOrList::One(typed)
}
OneOrList::List(service_types) => {
Expand Down

0 comments on commit daff7fb

Please sign in to comment.