Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee committed Jun 27, 2023
1 parent 17581aa commit d75d554
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 135 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
push:
branches:
- main
pull_request:
branches:
- "**"

env:
DOCKER_BUILDKIT: 1
Expand Down
4 changes: 2 additions & 2 deletions 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::Numalgo;
use crate::peer_did::NumalgoKind;

#[derive(Debug, Error)]
pub enum DidPeerError {
Expand All @@ -20,7 +20,7 @@ pub enum DidPeerError {
#[error("Sovrin DID document builder error: {0}")]
DidDocumentSovBuilderError(#[from] did_doc_sov::error::DidDocumentSovError),
#[error("Unsupported numalgo: {0}")]
UnsupportedNumalgo(Numalgo),
UnsupportedNumalgo(NumalgoKind),
#[error("Invalid numalgo character: {0}")]
InvalidNumalgoCharacter(char),
#[error("Unsupported purpose character: {0}")]
Expand Down
9 changes: 6 additions & 3 deletions did_peer/src/numalgos/numalgo2/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ mod helpers;
use did_doc::schema::did_doc::DidDocument;
use did_doc_sov::extra_fields::ExtraFieldsSov;

use crate::{error::DidPeerError, peer_did::PeerDid};
use crate::{
error::DidPeerError,
peer_did::{numalgo2::Numalgo2, PeerDid},
};

use self::helpers::{append_encoded_key_segments, append_encoded_service_segment};

pub fn generate_numalgo2(did_document: DidDocument<ExtraFieldsSov>) -> Result<PeerDid, DidPeerError> {
pub fn generate_numalgo2(did_document: DidDocument<ExtraFieldsSov>) -> Result<PeerDid<Numalgo2>, DidPeerError> {
let mut did = String::from("did:peer:2");

did = append_encoded_key_segments(did, &did_document)?;
did = append_encoded_service_segment(did, &did_document)?;

PeerDid::parse(did)
PeerDid::<Numalgo2>::from_did(did)
}
15 changes: 10 additions & 5 deletions did_peer/src/numalgos/numalgo3/generate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use did_parser::Did;
use sha256::digest;

use crate::{error::DidPeerError, peer_did::PeerDid};
use crate::{
error::DidPeerError,
peer_did::{numalgo3::Numalgo3, PeerDid},
};

pub fn generate_numalgo3(did: &Did) -> Result<PeerDid, DidPeerError> {
pub fn generate_numalgo3(did: &Did) -> Result<PeerDid<Numalgo3>, DidPeerError> {
let numalgoless_id = did.id().chars().skip(2).collect::<String>();
let numalgoless_id_hashed = digest(numalgoless_id);
PeerDid::parse(format!("did:peer:3.{}", numalgoless_id_hashed))
PeerDid::<Numalgo3>::from_did(format!("did:peer:3.{}", numalgoless_id_hashed))
}

#[cfg(test)]
Expand All @@ -21,8 +24,10 @@ mod tests {
.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0".to_string()).unwrap();
assert_eq!(
PeerDid::parse("did:peer:3.0e857e93798921e83cfc2ef8bee9cafc25f15f4c9c7bee5ed9a9c62b56a62cca".to_string())
.unwrap(),
PeerDid::<Numalgo3>::from_did(
"did:peer:3.0e857e93798921e83cfc2ef8bee9cafc25f15f4c9c7bee5ed9a9c62b56a62cca".to_string()
)
.unwrap(),
generate_numalgo3(&peer_did_2).unwrap()
);
}
Expand Down
9 changes: 4 additions & 5 deletions did_peer/src/peer_did/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod numalgo;
mod numalgos;
mod peer_did;
mod regex;
mod transform;
pub(super) mod traits;

pub use numalgo::Numalgo;
pub use peer_did::PeerDid;
pub use numalgos::{numalgo0, numalgo1, numalgo2, numalgo3, NumalgoKind};
pub use peer_did::{GenericPeerDid, PeerDid};
36 changes: 0 additions & 36 deletions did_peer/src/peer_did/numalgo.rs

This file was deleted.

43 changes: 43 additions & 0 deletions did_peer/src/peer_did/numalgos/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub mod numalgo0;
pub mod numalgo1;
pub mod numalgo2;
pub mod numalgo3;

use std::fmt::Display;

use crate::error::DidPeerError;

use super::{numalgo0::Numalgo0, numalgo1::Numalgo1, numalgo2::Numalgo2, numalgo3::Numalgo3, traits::Numalgo};

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NumalgoKind {
InceptionKeyWithoutDoc(Numalgo0),
GenesisDoc(Numalgo1),
MultipleInceptionKeys(Numalgo2),
DidShortening(Numalgo3),
}

impl Display for NumalgoKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR.fmt(f),
NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR.fmt(f),
NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR.fmt(f),
NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_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)),
}
}
}
12 changes: 12 additions & 0 deletions did_peer/src/peer_did/numalgos/numalgo0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::peer_did::traits::Numalgo;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Numalgo0;

impl Numalgo for Numalgo0 {
const NUMALGO_CHAR: char = '0';

fn default() -> Self {
Self
}
}
12 changes: 12 additions & 0 deletions did_peer/src/peer_did/numalgos/numalgo1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::peer_did::traits::Numalgo;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Numalgo1;

impl Numalgo for Numalgo1 {
const NUMALGO_CHAR: char = '1';

fn default() -> Self {
Self
}
}
36 changes: 36 additions & 0 deletions did_peer/src/peer_did/numalgos/numalgo2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use did_doc::schema::did_doc::DidDocument;
use did_doc_sov::extra_fields::ExtraFieldsSov;
use did_parser::Did;

use crate::{
error::DidPeerError,
peer_did::{
traits::{Numalgo, ResolvablePeerDid, ToNumalgo3},
PeerDid,
},
};

use super::numalgo3::Numalgo3;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Numalgo2;

impl Numalgo for Numalgo2 {
const NUMALGO_CHAR: char = '2';

fn default() -> Self {
Self
}
}

impl ResolvablePeerDid for Numalgo2 {
fn resolve(&self) -> Result<DidDocument<ExtraFieldsSov>, DidPeerError> {
todo!()
}
}

impl ToNumalgo3 for Numalgo2 {
fn to_numalgo3(&self, did: &Did) -> Result<PeerDid<Numalgo3>, DidPeerError> {
todo!()
}
}
26 changes: 26 additions & 0 deletions did_peer/src/peer_did/numalgos/numalgo3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use did_parser::Did;

use crate::{
error::DidPeerError,
peer_did::{
traits::{Numalgo, ToNumalgo3},
PeerDid,
},
};

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Numalgo3;

impl Numalgo for Numalgo3 {
const NUMALGO_CHAR: char = '3';

fn default() -> Self {
Self
}
}

impl ToNumalgo3 for Numalgo3 {
fn to_numalgo3(&self, did: &Did) -> Result<PeerDid<Numalgo3>, DidPeerError> {
todo!()
}
}
63 changes: 63 additions & 0 deletions did_peer/src/peer_did/peer_did/generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::{
error::DidPeerError,
peer_did::{numalgo2::Numalgo2, numalgo3::Numalgo3, NumalgoKind},
};
use did_parser::Did;
use serde::{Deserialize, Deserializer};

use super::{
parsing::{parse_numalgo, parse_transform, validate},
PeerDid,
};

#[derive(Clone, Debug, PartialEq)]
pub enum GenericPeerDid {
Numalgo2(PeerDid<Numalgo2>),
Numalgo3(PeerDid<Numalgo3>),
}

impl GenericPeerDid {
pub fn parse<T>(did: T) -> Result<GenericPeerDid, DidPeerError>
where
Did: TryFrom<T>,
<Did as TryFrom<T>>::Error: Into<DidPeerError>,
{
let did: Did = did.try_into().map_err(Into::into)?;
let numalgo = parse_numalgo(&did)?;
let transform = match numalgo {
NumalgoKind::InceptionKeyWithoutDoc(_) | NumalgoKind::GenesisDoc(_) => Some(parse_transform(&did)?),
_ => None,
};
validate(&did)?;
let parsed = match numalgo {
NumalgoKind::MultipleInceptionKeys(numalgo) => GenericPeerDid::Numalgo2(PeerDid {
did,
numalgo,
transform,
}),
_ => GenericPeerDid::Numalgo3(PeerDid {
did,
numalgo: Numalgo3,
transform,
}),
};
Ok(parsed)
}

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),
}
}
}

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

0 comments on commit d75d554

Please sign in to comment.