The DidDht class provides an implementation of the did:dht DID method.

Features:

  • DID Creation: Create new did:dht DIDs.
  • DID Key Management: Instantiate a DID object from an existing verification method keys or or a key in a Key Management System (KMS). If supported by the KMS, a DID's key can be exported to a portable DID format.
  • DID Resolution: Resolve a did:dht to its corresponding DID Document stored in the DHT network.
  • Signature Operations: Sign and verify messages using keys associated with a DID.

Remarks

The did:dht method leverages the distributed nature of the Mainline DHT network for decentralized identity management. This method allows DIDs to be resolved without relying on centralized registries or ledgers, enhancing privacy and control for users. The DID Document is stored and retrieved from the DHT network, and the method includes optional mechanisms for discovering DIDs by type.

The DID URI in the did:dht method includes a method-specific identifier called the Identity Key which corresponds to the DID's entry in the DHT network. The Identity Key required to make changes to the DID Document since Mainline DHT nodes validate the signature of each message before storing the value in the DHT.

See

DID DHT Method Specification

Example

// DID Creation
const did = await DidDht.create();

// DID Creation with a KMS
const keyManager = new LocalKeyManager();
const did = await DidDht.create({ keyManager });

// DID Resolution
const resolutionResult = await DidDht.resolve({ did: did.uri });

// Signature Operations
const signer = await did.getSigner();
const signature = await signer.sign({ data: new TextEncoder().encode('Message') });
const isValid = await signer.verify({ data: new TextEncoder().encode('Message'), signature });

// Import / Export

// Export a BearerDid object to the PortableDid format.
const portableDid = await did.export();

// Reconstruct a BearerDid object from a PortableDid
const did = await DidDht.import(portableDid);

Hierarchy (view full)

Constructors

Properties

methodName: string = 'dht'

Name of the DID method, as defined in the DID DHT specification.

Methods

  • Creates a new DID using the did:dht method formed from a newly generated key.

    Type Parameters

    Parameters

    • params: {
          keyManager?: TKms;
          options?: DidDhtCreateOptions<TKms>;
      } = {}

      The parameters for the create operation.

      • Optional keyManager?: TKms

        Optionally specify a Key Management System (KMS) used to generate keys and sign data.

      • Optional options?: DidDhtCreateOptions<TKms>

        Optional parameters that can be specified when creating a new DID.

    Returns Promise<BearerDid>

    A Promise resolving to a BearerDid object representing the new DID.

    Remarks

    The DID URI is formed by z-base-32 encoding the Identity Key public key and prefixing with did:dht:.

    Notes:

    • If no options are given, by default a new Ed25519 key will be generated which serves as the Identity Key.

    Example

    // DID Creation
    const did = await DidDht.create();

    // DID Creation with a KMS
    const keyManager = new LocalKeyManager();
    const did = await DidDht.create({ keyManager });
  • Given the W3C DID Document of a did:dht DID, return the verification method that will be used for signing messages and credentials. If given, the methodId parameter is used to select the verification method. If not given, the Identity Key's verification method with an ID fragment of '#0' is used.

    Parameters

    • params: {
          didDocument: DidDocument;
          methodId?: string;
      }

      The parameters for the getSigningMethod operation.

      • didDocument: DidDocument

        DID Document to get the verification method from.

      • Optional methodId?: string

        ID of the verification method to use for signing.

    Returns Promise<DidVerificationMethod>

    Verification method to use for signing.

  • Publishes a DID to the DHT, making it publicly discoverable and resolvable.

    This method handles the publication of a DID Document associated with a did:dht DID to the Mainline DHT network. The publication process involves storing the DID Document in Mainline DHT via a Pkarr relay server.

    Parameters

    • params: {
          did: BearerDid;
          gatewayUri?: string;
      }

      The parameters for the publish operation.

      • did: BearerDid

        The BearerDid object representing the DID to be published.

      • Optional gatewayUri?: string

        Optional. The URI of a server involved in executing DID method operations. In the context of publishing, the endpoint is expected to be a DID DHT Gateway or Pkarr Relay. If not specified, a default gateway node is used.

    Returns Promise<DidRegistrationResult>

    A promise that resolves to a DidRegistrationResult object that contains the result of registering the DID with a DID DHT Gateway or Pkarr relay.

    Remarks

    • This method is typically invoked automatically during the creation of a new DID unless the publish option is set to false.
    • For existing, unpublished DIDs, it can be used to publish the DID Document to Mainline DHT.
    • The method relies on the specified Pkarr relay server to interface with the DHT network.

    Example

    // Generate a new DID and keys but explicitly disable publishing.
    const did = await DidDht.create({ options: { publish: false } });
    // Publish the DID to the DHT.
    const registrationResult = await DidDht.publish({ did });
    // `registrationResult.didDocumentMetadata.published` is true if the DID was successfully published.
  • Resolves a did:dht identifier to its corresponding DID document.

    This method performs the resolution of a did:dht DID, retrieving its DID Document from the Mainline DHT network. The process involves querying the DHT network via a Pkarr relay server to retrieve the DID Document that corresponds to the given DID identifier.

    Parameters

    • didUri: string

      The DID to be resolved.

    • options: DidResolutionOptions = {}

      Optional parameters for resolving the DID. Unused by this DID method.

    Returns Promise<DidResolutionResult>

    A Promise resolving to a DidResolutionResult object representing the result of the resolution.

    Remarks

    • If a gatewayUri option is not specified, a default Pkarr relay is used to access the DHT network.
    • It decodes the DID identifier and retrieves the associated DID Document and metadata.
    • In case of resolution failure, appropriate error information is returned.

    Example

    const resolutionResult = await DidDht.resolve('did:dht:example');