diff --git a/did_peer/src/peer_did/numalgos/mod.rs b/did_peer/src/peer_did/numalgos/mod.rs index 8c3c1efa25..d816f9a468 100644 --- a/did_peer/src/peer_did/numalgos/mod.rs +++ b/did_peer/src/peer_did/numalgos/mod.rs @@ -23,17 +23,23 @@ pub enum NumalgoKind { DidShortening(Numalgo3), } -impl Display for NumalgoKind { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl NumalgoKind { + pub fn to_char(&self) -> char { 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), + 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; diff --git a/did_peer/src/peer_did/numalgos/traits.rs b/did_peer/src/peer_did/numalgos/traits.rs index c7283422a5..67f988dd91 100644 --- a/did_peer/src/peer_did/numalgos/traits.rs +++ b/did_peer/src/peer_did/numalgos/traits.rs @@ -5,7 +5,7 @@ use did_parser::Did; use super::numalgo3::Numalgo3; use crate::{ error::DidPeerError, - peer_did::{validate::validate, PeerDid}, + peer_did::{parse::parse_numalgo, validate::validate, PeerDid}, peer_did_resolver::options::PublicKeyEncoding, }; @@ -18,20 +18,11 @@ pub trait Numalgo: Sized + Default { >::Error: Into, { let did: Did = did.try_into().map_err(Into::into)?; - - let numalgo_char = did.id().chars().next().ok_or_else(|| { - DidPeerError::DidValidationError(format!( - "Invalid did: unable to read numalgo character in did {}", - did.did() - )) - })?; - + 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())) } }