diff --git a/did_peer/src/error.rs b/did_peer/src/error.rs index 62d9d9be12..d47b5b74fa 100644 --- a/did_peer/src/error.rs +++ b/did_peer/src/error.rs @@ -3,7 +3,7 @@ use std::convert::Infallible; use did_doc::schema::verification_method::VerificationMethodType; use thiserror::Error; -use crate::peer_did::numalgos::NumalgoKind; +use crate::peer_did::numalgos::kind::NumalgoKind; #[derive(Debug, Error)] pub enum DidPeerError { diff --git a/did_peer/src/lib.rs b/did_peer/src/lib.rs index cc297e941b..462a582971 100644 --- a/did_peer/src/lib.rs +++ b/did_peer/src/lib.rs @@ -1,6 +1,5 @@ extern crate display_as_json; pub mod error; -mod numalgos; pub mod peer_did; -pub mod peer_did_resolver; +pub mod resolver; diff --git a/did_peer/src/numalgos/mod.rs b/did_peer/src/numalgos/mod.rs deleted file mode 100644 index 0f8a93b0a0..0000000000 --- a/did_peer/src/numalgos/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod numalgo2; -pub mod numalgo3; diff --git a/did_peer/src/numalgos/numalgo3/mod.rs b/did_peer/src/numalgos/numalgo3/mod.rs deleted file mode 100644 index cbe6753597..0000000000 --- a/did_peer/src/numalgos/numalgo3/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod generate; diff --git a/did_peer/src/peer_did/generic.rs b/did_peer/src/peer_did/generic.rs index 0c5cc9fb09..aa34844282 100644 --- a/did_peer/src/peer_did/generic.rs +++ b/did_peer/src/peer_did/generic.rs @@ -5,20 +5,20 @@ use super::PeerDid; use crate::{ error::DidPeerError, peer_did::{ - numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3, NumalgoKind}, + numalgos::{kind::NumalgoKind, numalgo2::Numalgo2, numalgo3::Numalgo3}, parse::parse_numalgo, validate::validate, }, }; #[derive(Clone, Debug, PartialEq)] -pub enum GenericPeerDid { +pub enum AnyPeerDid { Numalgo2(PeerDid), Numalgo3(PeerDid), } -impl GenericPeerDid { - pub fn parse(did: T) -> Result +impl AnyPeerDid { + pub fn parse(did: T) -> Result where Did: TryFrom, >::Error: Into, @@ -28,9 +28,9 @@ impl GenericPeerDid { validate(&did)?; let parsed = match numalgo { NumalgoKind::MultipleInceptionKeys(numalgo) => { - GenericPeerDid::Numalgo2(PeerDid { did, numalgo }) + AnyPeerDid::Numalgo2(PeerDid { did, numalgo }) } - _ => GenericPeerDid::Numalgo3(PeerDid { + _ => AnyPeerDid::Numalgo3(PeerDid { did, numalgo: Numalgo3, }), @@ -40,27 +40,25 @@ impl GenericPeerDid { pub fn numalgo(&self) -> NumalgoKind { match self { - GenericPeerDid::Numalgo2(peer_did) => { - NumalgoKind::MultipleInceptionKeys(peer_did.numalgo) - } - GenericPeerDid::Numalgo3(peer_did) => NumalgoKind::DidShortening(peer_did.numalgo), + AnyPeerDid::Numalgo2(peer_did) => NumalgoKind::MultipleInceptionKeys(peer_did.numalgo), + AnyPeerDid::Numalgo3(peer_did) => NumalgoKind::DidShortening(peer_did.numalgo), } } } -impl Serialize for GenericPeerDid { +impl Serialize for AnyPeerDid { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match &self { - GenericPeerDid::Numalgo2(peer_did) => serializer.serialize_str(peer_did.did().did()), - GenericPeerDid::Numalgo3(peer_did) => serializer.serialize_str(peer_did.did().did()), + AnyPeerDid::Numalgo2(peer_did) => serializer.serialize_str(peer_did.did().did()), + AnyPeerDid::Numalgo3(peer_did) => serializer.serialize_str(peer_did.did().did()), } } } -impl<'de> Deserialize<'de> for GenericPeerDid { +impl<'de> Deserialize<'de> for AnyPeerDid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, @@ -88,15 +86,15 @@ mod tests { const INVALID_PEER_DID_NUMALGO3: &str = "did:peer:3.d8da5079c166b183cfz15ee27747f34e116977103d8b23c96dcba9a9d9429689"; - fn generic_peer_did_numalgo2() -> GenericPeerDid { - GenericPeerDid::Numalgo2(PeerDid { + fn generic_peer_did_numalgo2() -> AnyPeerDid { + AnyPeerDid::Numalgo2(PeerDid { did: VALID_PEER_DID_NUMALGO2.parse().unwrap(), numalgo: Numalgo2, }) } - fn generic_peer_did_numalgo3() -> GenericPeerDid { - GenericPeerDid::Numalgo3(PeerDid { + fn generic_peer_did_numalgo3() -> AnyPeerDid { + AnyPeerDid::Numalgo3(PeerDid { did: VALID_PEER_DID_NUMALGO3.parse().unwrap(), numalgo: Numalgo3, }) @@ -123,28 +121,28 @@ mod tests { #[test] fn numalgo2() { - let deserialized: GenericPeerDid = + let deserialized: AnyPeerDid = serde_json::from_str(&format!("\"{}\"", VALID_PEER_DID_NUMALGO2)).unwrap(); assert_eq!(deserialized, generic_peer_did_numalgo2()); } #[test] fn numalgo2_invalid() { - let deserialized: Result = + let deserialized: Result = serde_json::from_str(&format!("\"{}\"", INVALID_PEER_DID_NUMALGO2)); assert!(deserialized.is_err()); } #[test] fn numalgo3() { - let deserialized: GenericPeerDid = + let deserialized: AnyPeerDid = serde_json::from_str(&format!("\"{}\"", VALID_PEER_DID_NUMALGO3)).unwrap(); assert_eq!(deserialized, generic_peer_did_numalgo3()); } #[test] fn numalgo3_invalid() { - let deserialized: Result = + let deserialized: Result = serde_json::from_str(&format!("\"{}\"", INVALID_PEER_DID_NUMALGO3)); assert!(deserialized.is_err()); } diff --git a/did_peer/src/peer_did/mod.rs b/did_peer/src/peer_did/mod.rs index 25cefa8130..aaaa56550e 100644 --- a/did_peer/src/peer_did/mod.rs +++ b/did_peer/src/peer_did/mod.rs @@ -1,7 +1,6 @@ pub mod numalgos; mod parse; -mod regex; mod validate; pub mod generic; @@ -12,7 +11,7 @@ use std::{fmt::Display, marker::PhantomData}; use did_doc::schema::did_doc::DidDocument; use did_doc_sov::extra_fields::ExtraFieldsSov; use did_parser::Did; -use numalgos::traits::Numalgo; +use numalgos::Numalgo; use serde::{ de::{self, Visitor}, Deserialize, Deserializer, Serialize, Serializer, diff --git a/did_peer/src/peer_did/numalgos/kind.rs b/did_peer/src/peer_did/numalgos/kind.rs new file mode 100644 index 0000000000..732ea2fa7c --- /dev/null +++ b/did_peer/src/peer_did/numalgos/kind.rs @@ -0,0 +1,47 @@ +use std::fmt::Display; + +use crate::{ + error::DidPeerError, + peer_did::numalgos::{ + numalgo0::Numalgo0, numalgo1::Numalgo1, numalgo2::Numalgo2, numalgo3::Numalgo3, Numalgo, + }, +}; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum NumalgoKind { + InceptionKeyWithoutDoc(Numalgo0), + GenesisDoc(Numalgo1), + MultipleInceptionKeys(Numalgo2), + DidShortening(Numalgo3), +} + +impl NumalgoKind { + pub fn to_char(&self) -> char { + match self { + NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR, + NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR, + NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR, + NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_CHAR, + } + } +} + +impl Display for NumalgoKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.to_char().fmt(f) + } +} + +impl TryFrom for NumalgoKind { + type Error = DidPeerError; + + fn try_from(value: char) -> Result { + match value { + Numalgo0::NUMALGO_CHAR => Ok(NumalgoKind::InceptionKeyWithoutDoc(Numalgo0)), + Numalgo1::NUMALGO_CHAR => Ok(NumalgoKind::GenesisDoc(Numalgo1)), + Numalgo2::NUMALGO_CHAR => Ok(NumalgoKind::MultipleInceptionKeys(Numalgo2)), + Numalgo3::NUMALGO_CHAR => Ok(NumalgoKind::DidShortening(Numalgo3)), + c => Err(DidPeerError::InvalidNumalgoCharacter(c)), + } + } +} diff --git a/did_peer/src/peer_did/numalgos/mod.rs b/did_peer/src/peer_did/numalgos/mod.rs index d816f9a468..6da0b86779 100644 --- a/did_peer/src/peer_did/numalgos/mod.rs +++ b/did_peer/src/peer_did/numalgos/mod.rs @@ -1,55 +1,43 @@ +pub mod kind; pub mod numalgo0; pub mod numalgo1; pub mod numalgo2; pub mod numalgo3; -pub(super) mod traits; - use std::fmt::Display; -use numalgo0::Numalgo0; -use numalgo1::Numalgo1; -use numalgo2::Numalgo2; -use numalgo3::Numalgo3; - -use self::traits::Numalgo; -use crate::error::DidPeerError; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum NumalgoKind { - InceptionKeyWithoutDoc(Numalgo0), - GenesisDoc(Numalgo1), - MultipleInceptionKeys(Numalgo2), - DidShortening(Numalgo3), -} - -impl NumalgoKind { - pub fn to_char(&self) -> char { - match self { - NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR, - NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR, - NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR, - NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_CHAR, +use did_doc::schema::did_doc::DidDocument; +use did_doc_sov::extra_fields::ExtraFieldsSov; +use did_parser::Did; + +use crate::{ + error::DidPeerError, + peer_did::{parse::parse_numalgo, validate::validate, FromDidDoc, PeerDid}, + resolver::options::PublicKeyEncoding, +}; + +pub trait Numalgo: Sized + Default { + const NUMALGO_CHAR: char; + + fn parse(did: T) -> Result, DidPeerError> + where + Did: TryFrom, + >::Error: Into, + { + let did: Did = did.try_into().map_err(Into::into)?; + let numalgo_char = parse_numalgo(&did)?.to_char(); + if numalgo_char != Self::NUMALGO_CHAR { + return Err(DidPeerError::InvalidNumalgoCharacter(numalgo_char)); } + validate(&did)?; + Ok(PeerDid::from_parts(did, Self::default())) } } -impl Display for NumalgoKind { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.to_char().fmt(f) - } -} - -impl TryFrom for NumalgoKind { - type Error = DidPeerError; - - fn try_from(value: char) -> Result { - match value { - Numalgo0::NUMALGO_CHAR => Ok(NumalgoKind::InceptionKeyWithoutDoc(Numalgo0)), - Numalgo1::NUMALGO_CHAR => Ok(NumalgoKind::GenesisDoc(Numalgo1)), - Numalgo2::NUMALGO_CHAR => Ok(NumalgoKind::MultipleInceptionKeys(Numalgo2)), - Numalgo3::NUMALGO_CHAR => Ok(NumalgoKind::DidShortening(Numalgo3)), - c => Err(DidPeerError::InvalidNumalgoCharacter(c)), - } - } +pub trait ResolvableNumalgo: Numalgo { + fn resolve( + &self, + did: &Did, + public_key_encoding: PublicKeyEncoding, + ) -> Result, DidPeerError>; } diff --git a/did_peer/src/peer_did/numalgos/numalgo0.rs b/did_peer/src/peer_did/numalgos/numalgo0/mod.rs similarity index 77% rename from did_peer/src/peer_did/numalgos/numalgo0.rs rename to did_peer/src/peer_did/numalgos/numalgo0/mod.rs index 469949e76d..2b5e81e91e 100644 --- a/did_peer/src/peer_did/numalgos/numalgo0.rs +++ b/did_peer/src/peer_did/numalgos/numalgo0/mod.rs @@ -1,4 +1,4 @@ -use super::traits::Numalgo; +use crate::peer_did::numalgos::Numalgo; #[derive(Clone, Copy, Default, Debug, PartialEq)] pub struct Numalgo0; diff --git a/did_peer/src/peer_did/numalgos/numalgo1.rs b/did_peer/src/peer_did/numalgos/numalgo1/mod.rs similarity index 77% rename from did_peer/src/peer_did/numalgos/numalgo1.rs rename to did_peer/src/peer_did/numalgos/numalgo1/mod.rs index 0405dafea9..8a70427459 100644 --- a/did_peer/src/peer_did/numalgos/numalgo1.rs +++ b/did_peer/src/peer_did/numalgos/numalgo1/mod.rs @@ -1,4 +1,4 @@ -use super::traits::Numalgo; +use crate::peer_did::numalgos::Numalgo; #[derive(Clone, Copy, Default, Debug, PartialEq)] pub struct Numalgo1; diff --git a/did_peer/src/peer_did/numalgos/numalgo2.rs b/did_peer/src/peer_did/numalgos/numalgo2.rs deleted file mode 100644 index 7990ef1497..0000000000 --- a/did_peer/src/peer_did/numalgos/numalgo2.rs +++ /dev/null @@ -1,26 +0,0 @@ -use did_doc::schema::did_doc::DidDocument; -use did_doc_sov::extra_fields::ExtraFieldsSov; -use did_parser::Did; - -use super::traits::{Numalgo, ResolvableNumalgo}; -use crate::{ - error::DidPeerError, numalgos::numalgo2::resolve_numalgo2, - peer_did_resolver::options::PublicKeyEncoding, -}; - -#[derive(Clone, Copy, Default, Debug, PartialEq)] -pub struct Numalgo2; - -impl Numalgo for Numalgo2 { - const NUMALGO_CHAR: char = '2'; -} - -impl ResolvableNumalgo for Numalgo2 { - fn resolve( - &self, - did: &Did, - public_key_encoding: PublicKeyEncoding, - ) -> Result, DidPeerError> { - resolve_numalgo2(did, public_key_encoding).map(|builder| builder.build()) - } -} diff --git a/did_peer/src/numalgos/numalgo2/encoding.rs b/did_peer/src/peer_did/numalgos/numalgo2/encoding.rs similarity index 97% rename from did_peer/src/numalgos/numalgo2/encoding.rs rename to did_peer/src/peer_did/numalgos/numalgo2/encoding.rs index df4d9fcd96..53c6576059 100644 --- a/did_peer/src/numalgos/numalgo2/encoding.rs +++ b/did_peer/src/peer_did/numalgos/numalgo2/encoding.rs @@ -12,7 +12,7 @@ use public_key::Key; use crate::{ error::DidPeerError, - numalgos::numalgo2::{ + peer_did::numalgos::numalgo2::{ purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated, verification_method::get_key_by_verification_method, }, @@ -163,13 +163,16 @@ fn abbreviate_service( #[cfg(test)] mod tests { use did_doc::schema::{ + did_doc::DidDocument, service::Service, verification_method::{VerificationMethod, VerificationMethodType}, }; - use did_doc_sov::extra_fields::{didcommv2::ExtraFieldsDidCommV2, KeyKind}; + use did_doc_sov::extra_fields::{didcommv2::ExtraFieldsDidCommV2, ExtraFieldsSov, KeyKind}; use did_parser::DidUrl; - use super::*; + use crate::peer_did::numalgos::numalgo2::encoding::{ + append_encoded_key_segments, append_encoded_service_segment, + }; fn create_verification_method( did_full: String, diff --git a/did_peer/src/numalgos/numalgo2/mod.rs b/did_peer/src/peer_did/numalgos/numalgo2/mod.rs similarity index 61% rename from did_peer/src/numalgos/numalgo2/mod.rs rename to did_peer/src/peer_did/numalgos/numalgo2/mod.rs index 2b2231497d..6eee8dc6e3 100644 --- a/did_peer/src/numalgos/numalgo2/mod.rs +++ b/did_peer/src/peer_did/numalgos/numalgo2/mod.rs @@ -1,23 +1,26 @@ -mod encoding; -mod purpose; -mod resolve; -mod service_abbreviated; -mod verification_method; - use did_doc::schema::did_doc::DidDocument; use did_doc_sov::extra_fields::ExtraFieldsSov; -pub use resolve::resolve_numalgo2; +use did_parser::Did; +use encoding::{append_encoded_key_segments, append_encoded_service_segment}; use sha256::digest; use crate::{ error::DidPeerError, - numalgos::numalgo2::encoding::{append_encoded_key_segments, append_encoded_service_segment}, peer_did::{ - numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3}, + numalgos::{ + numalgo2::resolve::resolve_numalgo2, numalgo3::Numalgo3, Numalgo, ResolvableNumalgo, + }, FromDidDoc, PeerDid, }, + resolver::options::PublicKeyEncoding, }; +mod encoding; +mod purpose; +pub mod resolve; +mod service_abbreviated; +mod verification_method; + impl FromDidDoc for Numalgo2 { fn from_did_doc( did_document: DidDocument, @@ -40,3 +43,20 @@ impl PeerDid { PeerDid::::parse(format!("did:peer:3.{}", numalgoless_id_hashed)) } } + +#[derive(Clone, Copy, Default, Debug, PartialEq)] +pub struct Numalgo2; + +impl Numalgo for Numalgo2 { + const NUMALGO_CHAR: char = '2'; +} + +impl ResolvableNumalgo for Numalgo2 { + fn resolve( + &self, + did: &Did, + public_key_encoding: PublicKeyEncoding, + ) -> Result, DidPeerError> { + resolve_numalgo2(did, public_key_encoding).map(|builder| builder.build()) + } +} diff --git a/did_peer/src/numalgos/numalgo2/purpose.rs b/did_peer/src/peer_did/numalgos/numalgo2/purpose.rs similarity index 100% rename from did_peer/src/numalgos/numalgo2/purpose.rs rename to did_peer/src/peer_did/numalgos/numalgo2/purpose.rs diff --git a/did_peer/src/numalgos/numalgo2/resolve/helpers.rs b/did_peer/src/peer_did/numalgos/numalgo2/resolve/helpers.rs similarity index 99% rename from did_peer/src/numalgos/numalgo2/resolve/helpers.rs rename to did_peer/src/peer_did/numalgos/numalgo2/resolve/helpers.rs index 1ea38dc516..fdddb11082 100644 --- a/did_peer/src/numalgos/numalgo2/resolve/helpers.rs +++ b/did_peer/src/peer_did/numalgos/numalgo2/resolve/helpers.rs @@ -10,11 +10,11 @@ use public_key::Key; use crate::{ error::DidPeerError, - numalgos::numalgo2::{ + peer_did::numalgos::numalgo2::{ purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated, verification_method::get_verification_methods_by_key, }, - peer_did_resolver::options::PublicKeyEncoding, + resolver::options::PublicKeyEncoding, }; pub fn process_elements( diff --git a/did_peer/src/numalgos/numalgo2/resolve/mod.rs b/did_peer/src/peer_did/numalgos/numalgo2/resolve/mod.rs similarity index 87% rename from did_peer/src/numalgos/numalgo2/resolve/mod.rs rename to did_peer/src/peer_did/numalgos/numalgo2/resolve/mod.rs index 76160e2f88..daea3c7a41 100644 --- a/did_peer/src/numalgos/numalgo2/resolve/mod.rs +++ b/did_peer/src/peer_did/numalgos/numalgo2/resolve/mod.rs @@ -5,7 +5,7 @@ use did_doc_sov::extra_fields::ExtraFieldsSov; use did_parser::Did; use self::helpers::process_elements; -use crate::{error::DidPeerError, peer_did_resolver::options::PublicKeyEncoding}; +use crate::{error::DidPeerError, resolver::options::PublicKeyEncoding}; pub fn resolve_numalgo2( did: &Did, diff --git a/did_peer/src/numalgos/numalgo2/service_abbreviated.rs b/did_peer/src/peer_did/numalgos/numalgo2/service_abbreviated.rs similarity index 100% rename from did_peer/src/numalgos/numalgo2/service_abbreviated.rs rename to did_peer/src/peer_did/numalgos/numalgo2/service_abbreviated.rs diff --git a/did_peer/src/numalgos/numalgo2/verification_method.rs b/did_peer/src/peer_did/numalgos/numalgo2/verification_method.rs similarity index 86% rename from did_peer/src/numalgos/numalgo2/verification_method.rs rename to did_peer/src/peer_did/numalgos/numalgo2/verification_method.rs index 4e98a74014..1efca5b3e1 100644 --- a/did_peer/src/numalgos/numalgo2/verification_method.rs +++ b/did_peer/src/peer_did/numalgos/numalgo2/verification_method.rs @@ -4,7 +4,7 @@ use did_doc::schema::verification_method::{ use did_parser::{Did, DidUrl}; use public_key::{Key, KeyType}; -use crate::{error::DidPeerError, peer_did_resolver::options::PublicKeyEncoding}; +use crate::{error::DidPeerError, resolver::options::PublicKeyEncoding}; pub fn get_verification_methods_by_key( key: &Key, @@ -49,7 +49,7 @@ pub fn get_key_by_verification_method(vm: &VerificationMethod) -> Result { return Err(DidPeerError::UnsupportedVerificationMethodType( t.to_owned(), - )) + )); } }; Ok(Key::new(vm.public_key_field().key_decoded()?, key_type)?) @@ -118,7 +118,9 @@ fn to_did_url_reference(key: &Key) -> Result { #[cfg(test)] mod tests { - use super::*; + use did_doc::schema::verification_method::{VerificationMethod, VerificationMethodType}; + use did_parser::Did; + use public_key::Key; fn did() -> Did { "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.\ @@ -172,11 +174,18 @@ mod tests { mod get_verification_methods_by_key { use super::*; + use crate::{ + peer_did::numalgos::numalgo2::verification_method, resolver::options::PublicKeyEncoding, + }; // Multibase encoded keys are multicodec-prefixed by their encoding type ... fn test_get_verification_methods_by_key_multibase(key: &Key) { - let vms = - get_verification_methods_by_key(key, &did(), PublicKeyEncoding::Multibase).unwrap(); + let vms = verification_method::get_verification_methods_by_key( + key, + &did(), + PublicKeyEncoding::Multibase, + ) + .unwrap(); assert_eq!(vms.len(), 1); assert_eq!( vms[0].public_key_field().key_decoded().unwrap(), @@ -187,8 +196,12 @@ mod tests { // ... and base58 encoded keys are not fn test_get_verification_methods_by_key_base58(key: &Key) { - let vms = - get_verification_methods_by_key(key, &did(), PublicKeyEncoding::Base58).unwrap(); + let vms = verification_method::get_verification_methods_by_key( + key, + &did(), + PublicKeyEncoding::Base58, + ) + .unwrap(); assert_eq!(vms.len(), 1); assert_ne!( vms[0].public_key_field().key_decoded().unwrap(), @@ -230,11 +243,13 @@ mod tests { mod get_key_by_verification_method { use super::*; + use crate::peer_did::numalgos::numalgo2::verification_method; #[test] fn test_get_key_by_verification_method_0() { assert_eq!( - get_key_by_verification_method(&verification_method_0()).unwrap(), + verification_method::get_key_by_verification_method(&verification_method_0()) + .unwrap(), key_0() ); } @@ -242,7 +257,8 @@ mod tests { #[test] fn test_get_key_by_verification_method_1() { assert_eq!( - get_key_by_verification_method(&verification_method_1()).unwrap(), + verification_method::get_key_by_verification_method(&verification_method_1()) + .unwrap(), key_1() ); } @@ -250,7 +266,8 @@ mod tests { #[test] fn test_get_key_by_verification_method_2() { assert_eq!( - get_key_by_verification_method(&verification_method_2()).unwrap(), + verification_method::get_key_by_verification_method(&verification_method_2()) + .unwrap(), key_2() ); } diff --git a/did_peer/src/peer_did/numalgos/numalgo3.rs b/did_peer/src/peer_did/numalgos/numalgo3.rs deleted file mode 100644 index a7982dc9c6..0000000000 --- a/did_peer/src/peer_did/numalgos/numalgo3.rs +++ /dev/null @@ -1,8 +0,0 @@ -use super::traits::Numalgo; - -#[derive(Clone, Copy, Default, Debug, PartialEq)] -pub struct Numalgo3; - -impl Numalgo for Numalgo3 { - const NUMALGO_CHAR: char = '3'; -} diff --git a/did_peer/src/numalgos/numalgo3/generate.rs b/did_peer/src/peer_did/numalgos/numalgo3/mod.rs similarity index 84% rename from did_peer/src/numalgos/numalgo3/generate.rs rename to did_peer/src/peer_did/numalgos/numalgo3/mod.rs index d3b5de42fd..576838b30d 100644 --- a/did_peer/src/numalgos/numalgo3/generate.rs +++ b/did_peer/src/peer_did/numalgos/numalgo3/mod.rs @@ -4,11 +4,18 @@ use did_doc_sov::extra_fields::ExtraFieldsSov; use crate::{ error::DidPeerError, peer_did::{ - numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3}, + numalgos::{numalgo2::Numalgo2, Numalgo}, FromDidDoc, PeerDid, }, }; +#[derive(Clone, Copy, Default, Debug, PartialEq)] +pub struct Numalgo3; + +impl Numalgo for Numalgo3 { + const NUMALGO_CHAR: char = '3'; +} + impl FromDidDoc for Numalgo3 { fn from_did_doc( did_document: DidDocument, @@ -19,7 +26,10 @@ impl FromDidDoc for Numalgo3 { #[cfg(test)] mod tests { - use super::*; + use crate::peer_did::{ + numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3}, + PeerDid, + }; #[test] fn test_generate_numalgo3() { diff --git a/did_peer/src/peer_did/numalgos/traits.rs b/did_peer/src/peer_did/numalgos/traits.rs deleted file mode 100644 index 4aad8609c2..0000000000 --- a/did_peer/src/peer_did/numalgos/traits.rs +++ /dev/null @@ -1,35 +0,0 @@ -use did_doc::schema::did_doc::DidDocument; -use did_doc_sov::extra_fields::ExtraFieldsSov; -use did_parser::Did; - -use crate::{ - error::DidPeerError, - peer_did::{parse::parse_numalgo, validate::validate, PeerDid}, - peer_did_resolver::options::PublicKeyEncoding, -}; - -pub trait Numalgo: Sized + Default { - const NUMALGO_CHAR: char; - - fn parse(did: T) -> Result, DidPeerError> - where - Did: TryFrom, - >::Error: Into, - { - let did: Did = did.try_into().map_err(Into::into)?; - let numalgo_char = parse_numalgo(&did)?.to_char(); - if numalgo_char != Self::NUMALGO_CHAR { - return Err(DidPeerError::InvalidNumalgoCharacter(numalgo_char)); - } - validate(&did)?; - Ok(PeerDid::from_parts(did, Self::default())) - } -} - -pub trait ResolvableNumalgo: Numalgo { - fn resolve( - &self, - did: &Did, - public_key_encoding: PublicKeyEncoding, - ) -> Result, DidPeerError>; -} diff --git a/did_peer/src/peer_did/parse.rs b/did_peer/src/peer_did/parse.rs index a69a0ecd06..f105479c29 100644 --- a/did_peer/src/peer_did/parse.rs +++ b/did_peer/src/peer_did/parse.rs @@ -1,7 +1,6 @@ use did_parser::Did; -use super::numalgos::NumalgoKind; -use crate::error::DidPeerError; +use crate::{error::DidPeerError, peer_did::numalgos::kind::NumalgoKind}; pub fn parse_numalgo(did: &Did) -> Result { did.id() diff --git a/did_peer/src/peer_did/regex.rs b/did_peer/src/peer_did/regex.rs deleted file mode 100644 index ac92dd7074..0000000000 --- a/did_peer/src/peer_did/regex.rs +++ /dev/null @@ -1,14 +0,0 @@ -use once_cell::sync::Lazy; -use regex::Regex; - -static GROUP_NUMALGO_0_AND_1: &str = r"([01](z)([1-9a-km-zA-HJ-NP-Z]{5,200}))"; -static GROUP_NUMALGO_2: &str = - r"(2((.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]{5,200}))+(.(S)[0-9a-zA-Z=]*)?))"; -static GROUP_NUMALGO_3: &str = r"(3\.[0-9a-fA-F]{64})"; - -pub static PEER_DID_REGEX: Lazy = Lazy::new(|| { - Regex::new(&format!( - r"^did:peer:({GROUP_NUMALGO_0_AND_1}|{GROUP_NUMALGO_2}|{GROUP_NUMALGO_3})$" - )) - .unwrap() -}); diff --git a/did_peer/src/peer_did/validate.rs b/did_peer/src/peer_did/validate.rs index aca41197ac..6d80c22d61 100644 --- a/did_peer/src/peer_did/validate.rs +++ b/did_peer/src/peer_did/validate.rs @@ -1,8 +1,21 @@ use did_parser::Did; +use once_cell::sync::Lazy; +use regex::Regex; -use super::regex::PEER_DID_REGEX; use crate::error::DidPeerError; +static GROUP_NUMALGO_0_AND_1: &str = r"([01](z)([1-9a-km-zA-HJ-NP-Z]{5,200}))"; +static GROUP_NUMALGO_2: &str = + r"(2((.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]{5,200}))+(.(S)[0-9a-zA-Z=]*)?))"; +static GROUP_NUMALGO_3: &str = r"(3\.[0-9a-fA-F]{64})"; + +pub static PEER_DID_REGEX: Lazy = Lazy::new(|| { + Regex::new(&format!( + r"^did:peer:({GROUP_NUMALGO_0_AND_1}|{GROUP_NUMALGO_2}|{GROUP_NUMALGO_3})$" + )) + .unwrap() +}); + pub fn validate(did: &Did) -> Result<(), DidPeerError> { if !PEER_DID_REGEX.is_match(did.did()) { Err(DidPeerError::DidValidationError(format!( diff --git a/did_peer/src/peer_did_resolver/mod.rs b/did_peer/src/resolver/mod.rs similarity index 100% rename from did_peer/src/peer_did_resolver/mod.rs rename to did_peer/src/resolver/mod.rs diff --git a/did_peer/src/peer_did_resolver/options.rs b/did_peer/src/resolver/options.rs similarity index 100% rename from did_peer/src/peer_did_resolver/options.rs rename to did_peer/src/resolver/options.rs diff --git a/did_peer/src/peer_did_resolver/resolver.rs b/did_peer/src/resolver/resolver.rs similarity index 87% rename from did_peer/src/peer_did_resolver/resolver.rs rename to did_peer/src/resolver/resolver.rs index 859f104d31..dfd47a3c46 100644 --- a/did_peer/src/peer_did_resolver/resolver.rs +++ b/did_peer/src/resolver/resolver.rs @@ -11,7 +11,8 @@ use did_resolver::{ use super::options::ExtraFieldsOptions; use crate::{ - error::DidPeerError, numalgos::numalgo2::resolve_numalgo2, peer_did::generic::GenericPeerDid, + error::DidPeerError, + peer_did::{generic::AnyPeerDid, numalgos::numalgo2::resolve::resolve_numalgo2}, }; pub struct PeerDidResolver; @@ -26,9 +27,9 @@ impl DidResolvable for PeerDidResolver { did: &Did, options: &DidResolutionOptions, ) -> Result, GenericError> { - let peer_did = GenericPeerDid::parse(did.to_owned())?; + let peer_did = AnyPeerDid::parse(did.to_owned())?; match peer_did { - GenericPeerDid::Numalgo2(peer_did) => { + AnyPeerDid::Numalgo2(peer_did) => { let did_doc = resolve_numalgo2(peer_did.did(), options.extra().public_key_encoding())? .add_also_known_as(peer_did.to_numalgo3()?.to_string().parse()?) diff --git a/did_peer/tests/resolve_negative.rs b/did_peer/tests/resolve_negative.rs index 3d9ee81fae..26b0fd8859 100644 --- a/did_peer/tests/resolve_negative.rs +++ b/did_peer/tests/resolve_negative.rs @@ -2,7 +2,7 @@ mod fixtures; use did_peer::{ error::DidPeerError, - peer_did_resolver::{ + resolver::{ options::{ExtraFieldsOptions, PublicKeyEncoding}, resolver::PeerDidResolver, }, diff --git a/did_peer/tests/resolve_positive.rs b/did_peer/tests/resolve_positive.rs index f7e0ce57c6..445ddc0ceb 100644 --- a/did_peer/tests/resolve_positive.rs +++ b/did_peer/tests/resolve_positive.rs @@ -2,7 +2,7 @@ mod fixtures; use did_doc::schema::did_doc::DidDocument; use did_doc_sov::extra_fields::ExtraFieldsSov; -use did_peer::peer_did_resolver::{ +use did_peer::resolver::{ options::{ExtraFieldsOptions, PublicKeyEncoding}, resolver::PeerDidResolver, };