The Did class represents a Decentralized Identifier (DID) Uniform Resource Identifier (URI).

This class provides a method for parsing a DID URI string into its component parts, as well as a method for serializing a DID URI object into a string.

A DID URI is composed of the following components:

  • scheme
  • method
  • id
  • path
  • query
  • fragment
  • params

Constructors

  • Constructs a new Did instance from individual components.

    Parameters

    • params: {
          fragment?: string;
          id: string;
          method: string;
          params?: Record<string, string>;
          path?: string;
          query?: string;
      }

      An object containing the parameters to be included in the DID URI.

      • Optional fragment?: string

        Optional. The fragment component of the DID URI.

      • id: string

        The DID method identifier.

      • method: string

        The name of the DID method.

      • Optional params?: Record<string, string>

        Optional. The query parameters in the DID URI.

      • Optional path?: string

        Optional. The path component of the DID URI.

      • Optional query?: string

        Optional. The query component of the DID URI.

    Returns Did

Properties

fragment?: string

Optional fragment component of the DID URI.

Example

did:web:tbd.website#key-1
id: string

The DID method identifier.

Example

h4d3ixkwt6q5a455tucw7j14jmqyghdtbr6cpiz6on5oxj5bpr3o
method: string

The name of the DID method.

Examples of DID method names are dht, jwk, and web, among others.

params?: Record<string, string>

Optional query parameters in the DID URI.

Example

did:web:tbd.website?service=files&relativeRef=/whitepaper.pdf
path?: string

Optional path component of the DID URI.

Example

did:web:tbd.website/path
query?: string

Optional query component of the DID URI.

Example

did:web:tbd.website?versionId=1
uri: string

A string representation of the DID.

A DID is a URI composed of three parts: the scheme did:, a method identifier, and a unique, method-specific identifier specified by the DID method.

Example

did:dht:h4d3ixkwt6q5a455tucw7j14jmqyghdtbr6cpiz6on5oxj5bpr3o
DID_URI_PATTERN: RegExp = ...

Regular expression pattern for matching all of the components of a DID URI.

FRAGMENT_PATTERN: "(#.*)?" = ...

Regular expression pattern for matching the fragment component of a DID URI.

ID_CHAR_PATTERN: string = ...

Regular expression pattern for matching the characters allowed in a method identifier.

METHOD_ID_PATTERN: string = ...

Regular expression pattern for matching the method identifier component of a DID URI.

METHOD_PATTERN: "([a-z0-9]+)" = '([a-z0-9]+)'

Regular expression pattern for matching the method component of a DID URI.

PATH_PATTERN: "(/[^#?]*)?" = ...

Regular expression pattern for matching the path component of a DID URI.

PCT_ENCODED_PATTERN: "(?:%[0-9a-fA-F]{2})" = '(?:%[0-9a-fA-F]{2})'

Regular expression pattern for matching percent-encoded characters in a method identifier.

QUERY_PATTERN: "([?][^#]*)?" = ...

Regular expression pattern for matching the query component of a DID URI.

Methods

  • Parses a DID URI string into its individual components.

    Parameters

    • didUri: string

    Returns null | Did

    A Did object representing the parsed DID URI, or null if the input string is not a valid DID URI.

    Example

    const did = Did.parse('did:example:123?service=agent&relativeRef=/credentials#degree');

    console.log(did.uri) // Output: 'did:example:123'
    console.log(did.method) // Output: 'example'
    console.log(did.id) // Output: '123'
    console.log(did.query) // Output: 'service=agent&relativeRef=/credentials'
    console.log(did.fragment) // Output: 'degree'
    console.log(did.params) // Output: { service: 'agent', relativeRef: '/credentials' }

    Params

    didUri - The DID URI string to be parsed.