diff --git a/crates/did-simple/src/lib.rs b/crates/did-simple/src/lib.rs index b4bc996..e730eb2 100644 --- a/crates/did-simple/src/lib.rs +++ b/crates/did-simple/src/lib.rs @@ -24,12 +24,16 @@ use std::str::FromStr; -pub mod key_algos; +pub(crate) mod key_algos; pub mod methods; -pub mod uri; +pub mod url; pub mod utf8bytes; -pub mod varint; +mod varint; + +pub use crate::key_algos::KeyAlgo; +pub use crate::methods::DidDyn; +pub use crate::url::DidUrl; pub trait Did: FromStr { - fn uri(&self) -> self::uri::DidUri; + fn url(&self) -> self::url::DidUrl; } diff --git a/crates/did-simple/src/methods/key.rs b/crates/did-simple/src/methods/key.rs index ddda82b..5bbb8ad 100644 --- a/crates/did-simple/src/methods/key.rs +++ b/crates/did-simple/src/methods/key.rs @@ -6,7 +6,7 @@ use std::fmt::Display; use crate::{ key_algos::{Ed25519, KeyAlgo, StaticKeyAlgo}, - uri::{DidMethod, DidUri}, + url::{DidMethod, DidUrl}, utf8bytes::Utf8Bytes, varint::decode_varint, }; @@ -29,17 +29,17 @@ pub const PREFIX: &str = "did:key:"; impl DidKey { pub const PREFIX: &'static str = PREFIX; - /// Gets the buffer representing the did:key uri as a str. + /// Gets the buffer representing the did:key url as a str. pub fn as_str(&self) -> &str { self.s.as_str() } - /// Gets the buffer representing the did:key uri as a byte slice. + /// Gets the buffer representing the did:key url as a byte slice. pub fn as_slice(&self) -> &[u8] { self.s.as_slice() } - /// Gets the buffer representing the did:key uri as a reference counted slice + /// Gets the buffer representing the did:key url as a reference counted slice /// that is guaranteed to be utf8. pub fn as_utf8_bytes(&self) -> &Utf8Bytes { &self.s @@ -87,13 +87,13 @@ pub enum MultibaseDecodeError { Bs58(#[from] bs58::decode::Error), } -impl TryFrom for DidKey { - type Error = FromUriError; +impl TryFrom for DidKey { + type Error = FromUrlError; - fn try_from(value: DidUri) -> Result { + fn try_from(value: DidUrl) -> Result { let m = value.method(); if m != DidMethod::Key { - return Err(FromUriError::WrongMethod(m)); + return Err(FromUrlError::WrongMethod(m)); } debug_assert_eq!( value.as_slice().len() - value.method_specific_id().as_slice().len(), @@ -109,11 +109,11 @@ impl TryFrom for DidKey { let (multicodec_key_algo, tail_bytes) = decode_varint(&decoded_multibase)?; let (key_algo, pub_key_len) = match multicodec_key_algo { Ed25519::MULTICODEC_VALUE => (KeyAlgo::Ed25519, Ed25519::PUB_KEY_LEN), - _ => return Err(FromUriError::UnknownKeyAlgo(multicodec_key_algo)), + _ => return Err(FromUrlError::UnknownKeyAlgo(multicodec_key_algo)), }; if tail_bytes.len() != pub_key_len { - return Err(FromUriError::MismatchedPubkeyLen(key_algo, pub_key_len)); + return Err(FromUrlError::MismatchedPubkeyLen(key_algo, pub_key_len)); } let pubkey_bytes = (decoded_multibase.len() - pub_key_len)..; @@ -128,7 +128,7 @@ impl TryFrom for DidKey { } #[derive(thiserror::Error, Debug)] -pub enum FromUriError { +pub enum FromUrlError { #[error("Expected \"key\" method but got {0:?}")] WrongMethod(DidMethod), #[error(transparent)] @@ -165,15 +165,15 @@ mod test { } #[test] - fn test_try_from_uri() -> eyre::Result<()> { + fn test_try_from_url() -> eyre::Result<()> { for &example in ed25519_examples() { - let uri = DidUri::from_str(example) - .wrap_err_with(|| format!("failed to parse DidUri from {example}"))?; - assert_eq!(example, uri.as_str()); - let key_from_uri = DidKey::try_from(uri.clone()) - .wrap_err_with(|| format!("failed to parse DidKey from {uri}"))?; - assert_eq!(example, key_from_uri.as_str()); - assert_eq!(key_from_uri.key_algo(), Ed25519); + let url = Did::from_str(example) + .wrap_err_with(|| format!("failed to parse DidUrl from {example}"))?; + assert_eq!(example, url.as_str()); + let key_from_url = DidKey::try_from(url.clone()) + .wrap_err_with(|| format!("failed to parse DidKey from {url}"))?; + assert_eq!(example, key_from_url.as_str()); + assert_eq!(key_from_url.key_algo(), Ed25519); } Ok(()) } diff --git a/crates/did-simple/src/uri.rs b/crates/did-simple/src/url.rs similarity index 83% rename from crates/did-simple/src/uri.rs rename to crates/did-simple/src/url.rs index 726de29..2d93d92 100644 --- a/crates/did-simple/src/uri.rs +++ b/crates/did-simple/src/url.rs @@ -21,8 +21,8 @@ impl FromStr for DidMethod { } } -/// Helper type to access data in the method-specific-id of a [`DidUri`]. -pub struct MethodSpecificId<'a>(&'a DidUri); +/// Helper type to access data in the method-specific-id of a [`DidUrl`]. +pub struct MethodSpecificId<'a>(&'a DidUrl); impl MethodSpecificId<'_> { pub fn as_str(&self) -> &str { @@ -38,8 +38,9 @@ impl MethodSpecificId<'_> { } } +/// A Decentralized Identifier, including any path information, as a url. #[derive(Debug, Eq, PartialEq, Hash, Clone)] -pub struct DidUri { +pub struct DidUrl { method: DidMethod, /// The string representation of the DID. s: Utf8Bytes, @@ -47,18 +48,18 @@ pub struct DidUri { method_specific_id: std::ops::RangeFrom, } -impl DidUri { - /// Gets the buffer representing the uri as a str. +impl DidUrl { + /// Gets the buffer representing the url as a str. pub fn as_str(&self) -> &str { self.s.as_str() } - /// Gets the buffer representing the uri as a byte slice. + /// Gets the buffer representing the url as a byte slice. pub fn as_slice(&self) -> &[u8] { self.s.as_slice() } - /// Gets the buffer representing the uri as a reference counted slice that + /// Gets the buffer representing the url as a reference counted slice that /// is guaranteed to be utf8. pub fn as_utf8_bytes(&self) -> &Utf8Bytes { &self.s @@ -75,7 +76,7 @@ impl DidUri { } } -impl FromStr for DidUri { +impl FromStr for DidUrl { type Err = ParseError; fn from_str(s: &str) -> Result { @@ -87,7 +88,7 @@ impl FromStr for DidUri { let method = DidMethod::from_str(method)?; let start_idx = s.len() - remaining.len(); - Ok(DidUri { + Ok(DidUrl { method, s: Utf8Bytes::from(s.to_owned()), method_specific_id: (start_idx..), @@ -95,7 +96,7 @@ impl FromStr for DidUri { } } -impl TryFrom for DidUri { +impl TryFrom for DidUrl { type Error = ParseError; fn try_from(s: String) -> Result { @@ -107,7 +108,7 @@ impl TryFrom for DidUri { let method = DidMethod::from_str(method)?; let start_idx = s.len() - remaining.len(); - Ok(DidUri { + Ok(DidUrl { method, s: Utf8Bytes::from(s), method_specific_id: (start_idx..), @@ -125,7 +126,7 @@ pub enum ParseError { UnknownMethod, } -impl Display for DidUri { +impl Display for DidUrl { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.as_str().fmt(f) } @@ -136,8 +137,8 @@ mod test { use super::*; use eyre::{Result, WrapErr}; - fn common_test_cases() -> Vec { - vec![DidUri { + fn common_test_cases() -> Vec { + vec![DidUrl { method: DidMethod::Key, s: String::from("did:key:123456").into(), method_specific_id: (8..), @@ -148,8 +149,8 @@ mod test { fn test_parse() -> Result<()> { for expected in common_test_cases() { let s = expected.s.as_str().to_owned(); - let from_str = DidUri::from_str(&s).wrap_err("failed to from_str")?; - let try_from = DidUri::try_from(s).wrap_err("failed to try_from")?; + let from_str = DidUrl::from_str(&s).wrap_err("failed to from_str")?; + let try_from = DidUrl::try_from(s).wrap_err("failed to try_from")?; assert_eq!(from_str, try_from); assert_eq!(from_str, expected); }