Skip to content

Commit

Permalink
Fix didpeer regex, fix test test_peer_did_2_encode_decode, fix servic…
Browse files Browse the repository at this point in the history
…e id deabbreviation

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jan 8, 2024
1 parent 1e8bcad commit 9a5e54a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod tests {
use crate::{
helpers::convert_to_hashmap,
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
resolver::{options::PublicKeyEncoding},
resolver::options::PublicKeyEncoding,
};

fn create_verification_method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Numalgo for Numalgo2 {
#[cfg(test)]
mod test {
use did_doc::schema::did_doc::DidDocument;
use serde_json::{from_value, json};
use pretty_assertions::assert_eq;
use serde_json::{from_value, json};

use crate::{
peer_did::{numalgos::numalgo2::Numalgo2, PeerDid},
Expand All @@ -64,44 +64,43 @@ mod test {

#[test]
fn test_peer_did_2_encode_decode() {
let expected_did_peer = "did:peer:2.Ez6MkkukgyKAdBN46UAHvia2nxmioo74F6YdvW1nBT1wfKKha.Vz6MkfoapUdLHHgSMq5PYhdHYCoqGuRku2i17cQ9zAoR5cLSm.SeyJpZCI6IiNmb29iYXIiLCJ0IjpbImRpZC1jb21tdW5pY2F0aW9uIl0sInMiOiJodHRwOi8vZHVtbXl1cmwub3JnLyIsInIiOlsiIzZNa2t1a2d5Il0sImEiOlsiZGlkY29tbS9haXAyO2Vudj1yZmMxOSJdfQ";
let value = json!({
"id": "did:peer:2.Ez6MkkukgyKAdBN46UAHvia2nxmioo74F6YdvW1nBT1wfKKha.Vz6MkfoapUdLHHgSMq5PYhdHYCoqGuRku2i17cQ9zAoR5cLSm.SeyJpZCI6IiMwIiwidCI6WyJkaWQtY29tbXVuaWNhdGlvbiJdLCJzIjoiaHR0cDovL2R1bW15dXJsLm9yZy8iLCJhIjpbImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXX0",
"id": expected_did_peer,
"verificationMethod": [
{
"id": "#6MkfoapU",
"controller": "did:example:123456789abcdefghi",
"controller": expected_did_peer,
"type": "Ed25519VerificationKey2020",
"publicKeyBase58": "2MKmtP5qx8wtiaYr24KhMiHH5rV3cpkkvPF4LXT4h7fP"
}
],
"keyAgreement": [
{
"id": "#6Mkkukgy",
"controller": "did:example:123456789abcdefghi",
"controller": expected_did_peer,
"type": "Ed25519VerificationKey2020",
"publicKeyBase58": "7TVeP4vBqpZdMfTE314x7gAoyXnPgfPZozsFcjyeQ6vC"
}
],
"service": [
{
"id": "#0",
"id": "#foobar",
"type": [
"did-communication"
],
"serviceEndpoint": "http://dummyurl.org/",
"routingKeys": [],
"routingKeys": ["#6Mkkukgy"],
"accept": [
"didcomm/aip2;env=rfc19"
],
"priority": 0,
"recipientKeys": [
"did:key:z6MkkukgyKAdBN46UAHvia2nxmioo74F6YdvW1nBT1wfKKha"
]
}
]
});
let ddo_original: DidDocument = from_value(value).unwrap();
let did_peer: PeerDid<Numalgo2> = PeerDid::from_did_doc(ddo_original.clone()).unwrap();
assert_eq!(did_peer.to_string(), expected_did_peer);

let ddo_decoded: DidDocument = did_peer.to_did_doc(PublicKeyEncoding::Base58).unwrap();
assert_eq!(ddo_original, ddo_decoded);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use did_doc::schema::{
service_accept_type::ServiceAcceptType, service_key_kind::ServiceKeyKind,
typed::ServiceType, Service,
},
types::uri::Uri,
utils::OneOrList,
};
use serde::{Deserialize, Serialize};
Expand All @@ -18,7 +19,7 @@ pub struct ServiceAbbreviatedDidPeer2 {
// https://identity.foundation/peer-did-method-spec/#generating-a-didpeer2
// > For use with did:peer:2, service id attributes MUST be relative.
// > The service MAY omit the id; however, this is NOT RECOMMEDED (clarified).
id: Option<String>,
id: Option<Uri>,
#[serde(rename = "t")]
service_type: OneOrList<String>,
#[serde(rename = "s")]
Expand All @@ -35,7 +36,7 @@ pub struct ServiceAbbreviatedDidPeer2 {

impl ServiceAbbreviatedDidPeer2 {
pub fn new(
id: Option<String>,
id: Option<Uri>,
service_type: OneOrList<String>,
service_endpoint: Url,
routing_keys: Vec<ServiceKeyKind>,
Expand Down Expand Up @@ -124,7 +125,7 @@ pub(crate) fn abbreviate_service(
}
};
Ok(ServiceAbbreviatedDidPeer2::new(
Some(service.id().to_string()),
Some(service.id().clone()),
service_types_abbreviated,
service_endpoint,
routing_keys,
Expand Down Expand Up @@ -161,7 +162,10 @@ pub(crate) fn deabbreviate_service(

// todo: >>> we created custom error for uniresid wrapper, now we'll need conversion across the
// board.
let id = format!("#service-{}", index).parse()?;
let id = abbreviated
.id
.clone()
.unwrap_or(format!("#service-{}", index).parse()?);

let mut service = Service::new(
id,
Expand All @@ -187,6 +191,7 @@ mod tests {
service_accept_type::ServiceAcceptType, service_key_kind::ServiceKeyKind,
typed::ServiceType, Service,
},
types::uri::Uri,
utils::OneOrList,
};
use serde_json::json;
Expand All @@ -199,7 +204,7 @@ mod tests {
#[test]
fn test_deabbreviate_service_type_value_dm() {
let service_abbreviated = ServiceAbbreviatedDidPeer2 {
id: Some("#service-0".into()),
id: Some(Uri::new("#service-0").unwrap()),
service_type: OneOrList::One("dm".to_string()),
service_endpoint: Url::parse("https://example.org").unwrap(),
routing_keys: vec![],
Expand All @@ -220,9 +225,9 @@ mod tests {
let accept = vec![ServiceAcceptType::DIDCommV1];
let service_endpoint = Url::parse("https://example.com/endpoint").unwrap();
let service_type = OneOrList::One(ServiceType::Other("foobar".to_string()));
let service_id = "#service-0";
let service_id = Uri::new("#service-0").unwrap();
let service_abbreviated = ServiceAbbreviatedDidPeer2 {
id: Some(service_id.into()),
id: Some(service_id),
service_type: OneOrList::One("foobar".to_string()),
service_endpoint: service_endpoint.clone(),
routing_keys: routing_keys.clone(),
Expand Down

0 comments on commit 9a5e54a

Please sign in to comment.