Skip to content

Commit

Permalink
Fix method_id validation
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Feb 13, 2024
1 parent d0f663b commit 4635a47
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions identity_did/src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,7 @@ impl CoreDID {
///
/// Returns `Err` if the input is not a valid [`DID`].
pub fn parse(input: impl AsRef<str>) -> Result<Self, Error> {
let base_did_url: BaseDIDUrl = BaseDIDUrl::parse(input).map_err(Error::from)?;
Self::try_from_base_did(base_did_url)
}

/// Try convert a [`BaseDIDUrl`] into a [`CoreDID`].
fn try_from_base_did(base_did_url: BaseDIDUrl) -> Result<Self, Error> {
Self::check_validity(&base_did_url)?;
Ok(Self(base_did_url))
BaseDIDUrl::parse(input).map(Self).map_err(Error::from)
}

/// Set the method name of the [`DID`].
Expand All @@ -145,9 +138,23 @@ impl CoreDID {

/// Validates whether a string is a valid [`DID`] method-id.
pub fn valid_method_id(value: &str) -> Result<(), Error> {
if !value.chars().all(is_char_method_id) {
return Err(Error::InvalidMethodId);
// if !value.chars().all(is_char_method_id) {
// return Err(Error::InvalidMethodId);
// }
let mut chars = value.chars();
while let Some(c) = chars.next() {
match c {
'%' => {
let digits = chars.clone().take(2).collect::<String>();
u8::from_str_radix(&digits, 16).map_err(|_| Error::InvalidMethodId)?;
chars.next();
chars.next();
}
c if is_char_method_id(c) => (),
_ => return Err(Error::InvalidMethodId),
}
}

Ok(())
}

Expand Down Expand Up @@ -185,7 +192,7 @@ impl TryFrom<BaseDIDUrl> for CoreDID {
type Error = Error;

fn try_from(base_did_url: BaseDIDUrl) -> Result<Self, Self::Error> {
Self::try_from_base_did(base_did_url)
Ok(Self(base_did_url))
}
}

Expand Down

0 comments on commit 4635a47

Please sign in to comment.