Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn numalgos into child module of peer_did module #1035

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion did_peer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions did_peer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 0 additions & 2 deletions did_peer/src/numalgos/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion did_peer/src/numalgos/numalgo3/mod.rs

This file was deleted.

42 changes: 20 additions & 22 deletions did_peer/src/peer_did/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Numalgo2>),
Numalgo3(PeerDid<Numalgo3>),
}

impl GenericPeerDid {
pub fn parse<T>(did: T) -> Result<GenericPeerDid, DidPeerError>
impl AnyPeerDid {
pub fn parse<T>(did: T) -> Result<AnyPeerDid, DidPeerError>
where
Did: TryFrom<T>,
<Did as TryFrom<T>>::Error: Into<DidPeerError>,
Expand All @@ -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,
}),
Expand All @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -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,
})
Expand All @@ -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<GenericPeerDid, _> =
let deserialized: Result<AnyPeerDid, _> =
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<GenericPeerDid, _> =
let deserialized: Result<AnyPeerDid, _> =
serde_json::from_str(&format!("\"{}\"", INVALID_PEER_DID_NUMALGO3));
assert!(deserialized.is_err());
}
Expand Down
3 changes: 1 addition & 2 deletions did_peer/src/peer_did/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod numalgos;

mod parse;
mod regex;
mod validate;

pub mod generic;
Expand All @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions did_peer/src/peer_did/numalgos/kind.rs
Original file line number Diff line number Diff line change
@@ -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<char> for NumalgoKind {
type Error = DidPeerError;

fn try_from(value: char) -> Result<Self, Self::Error> {
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)),
}
}
}
76 changes: 31 additions & 45 deletions did_peer/src/peer_did/numalgos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
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, PeerDid},
resolver::options::PublicKeyEncoding,
};

pub trait Numalgo: Sized + Default {
const NUMALGO_CHAR: char;

fn parse<T>(did: T) -> Result<PeerDid<Self>, DidPeerError>
where
Did: TryFrom<T>,
<Did as TryFrom<T>>::Error: Into<DidPeerError>,
{
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<char> for NumalgoKind {
type Error = DidPeerError;

fn try_from(value: char) -> Result<Self, Self::Error> {
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<DidDocument<ExtraFieldsSov>, DidPeerError>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::traits::Numalgo;
use crate::peer_did::numalgos::Numalgo;

#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Numalgo0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::traits::Numalgo;
use crate::peer_did::numalgos::Numalgo;

#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Numalgo1;
Expand Down
26 changes: 0 additions & 26 deletions did_peer/src/peer_did/numalgos/numalgo2.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down
Loading