Skip to main content

Module did

Module did 

Source
Expand description

Decentralized identifiers (DIDs), DID URLs, DID documents and resolution types.

Pure data: no I/O, no async, no crypto. The module has three parts:

§DID syntax

Did and DidUrl are validated when constructed and otherwise keep the input unchanged. They implement the syntax of W3C DID Core 1.0 §3.1 and §3.2 together with the RFC 3986 productions it references:

did                = "did:" method-name ":" method-specific-id
method-name        = 1*method-char
method-char        = %x61-7A / DIGIT                 ; lowercase a-z and 0-9 only
method-specific-id = *( *idchar ":" ) 1*idchar       ; colons allowed, last segment non-empty
idchar             = ALPHA / DIGIT / "." / "-" / "_" / pct-encoded
pct-encoded        = "%" HEXDIG HEXDIG

did-url            = did path-abempty [ "?" query ] [ "#" fragment ]

Consequences worth knowing:

  • did: and the method name are lowercase and case-sensitive: DID:example:123 and did:Example:123 are invalid.
  • The method-specific id may contain : and empty inner segments (did:example:a::b is valid) but must not end with : (did:example:a: is invalid).
  • Percent-encoding is checked for completeness (%3A valid, %3 and %G1 invalid) but never decoded, and nothing is normalized. Two DIDs are equal exactly when their strings are equal.
  • A space, /, ?, # or non-ASCII character ends the DID. Did::parse rejects it; DidUrl::parse takes /, ? and # as the start of the path, query or fragment.
  • Parsing rejects input longer than DEFAULT_MAX_LEN bytes; use Did::parse_with_max_len or DidUrl::parse_with_max_len to change the limit.

Both types implement Display, FromStr, TryFrom<&str>, TryFrom<String>, AsRef<str>, Clone, Eq, Ord, Hash, and serde Serialize/Deserialize as a plain string, where deserialization validates.

§Examples

use identity_kit::did::{Did, DidUrl};

let did: Did = "did:example:123".parse()?;
let key = did.join("#key-1")?;
assert_eq!(key.to_string(), "did:example:123#key-1");

let url = DidUrl::parse("did:example:123/path?versionId=1#key-1")?;
assert_eq!(url.did(), &did);
assert_eq!(url.path(), Some("/path"));
assert_eq!(url.query(), Some("versionId=1"));
assert_eq!(url.fragment(), Some("key-1"));

Re-exports§

pub use crate::jose::OneOrMany;

Structs§

Context
The @context of a DID document, carried verbatim: a string, a map or a non-empty array of them.
Did
A decentralized identifier such as did:web:example.com.
DidDocument
A DID document (DID Core §5): the core properties as fields, everything else in properties.
DidDocumentBuilder
Builds a DidDocument, reporting the first invariant violation at build.
DidMethod
A DID method, named with an optional namespace: did:demo or did:demo:dev.
DidUrl
A DID URL such as did:example:123/path?versionId=1#key-1.
DocumentMetadata
Metadata about the DID document (DID Core §7.1.3 “DID document metadata”).
MethodType
The type of a verification method: an open string with constants for the types this crate produces.
ResolutionError
Why a DID could not be resolved: a ResolutionErrorKind keyword, and optionally a human-readable detail and the underlying error.
ResolutionMetadata
Metadata about the resolution process (DID Core §7.1.3 “DID resolution metadata”).
ResolutionOptions
Options passed to a resolver (DID Core §7.1.1 “DID resolution options”).
ResolutionResult
What a resolver returns: the document when resolution succeeded, and the two metadata structures (DID Core §7.1.1).
Service
A service (DID Core §5.4): a way of interacting with the DID subject.
Uri
A URI (RFC 3986) stored exactly as written: no normalization, no percent-decoding, no case folding.
VerificationMethod
A verification method (DID Core §5.2): a public key with an id, a type and a controller.

Enums§

DidError
Why a string is not a valid DID, DID URL or DID method.
DidUrlReference
A reference to a resource of a DID document: either an absolute DidUrl or a relative DID URL (DID Core §3.2.2) such as #key-1, which is resolved against the document’s id.
DocumentError
Why a DidDocument operation was refused.
MethodRef
An entry of a verification relationship: a reference to a method in verificationMethod, or a method embedded in the relationship and usable for that purpose only (DID Core §5.3).
ResolutionErrorKind
The error keywords of DID resolution (DID Core §7.1.2, DID Resolution).
ServiceEndpoint
The serviceEndpoint of a service (DID Core §5.4): a URI, a set of URIs, or a map.
VerificationMaterial
The verification material of a verification method (DID Core §5.2.1): exactly one of publicKeyJwk and publicKeyMultibase.
VerificationRelationship
A verification relationship (DID Core §5.3): the purpose a verification method may be used for. The Display and FromStr forms are the DID document property names.

Constants§

APPLICATION_DID_JSON
Media type of the plain JSON representation of a DID document (DID Core §6.2).
APPLICATION_DID_LD_JSON
Media type of the JSON-LD representation of a DID document (DID Core §6.3).
DEFAULT_MAX_LEN
Default maximum input length in bytes for Did::parse and DidUrl::parse: 2 KiB.