Skip to Content

DID API

Low-level DID (Decentralized Identifier) operations. Used internally by createAgent and the handshake, but available for advanced use cases.


resolveDID

Universal DID resolver. Detects the method and delegates to the correct resolver.

function resolveDID(did: string): Promise<DIDResolutionResult>

Parameters

ParameterTypeDescription
didstringAny supported DID (e.g. did:web:acme.com, did:key:z6Mkh...)

Returns

DIDResolutionResult

FieldTypeDescription
didDocumentDIDDocument | nullThe resolved DID Document, or null on failure
didResolutionMetadata{ error?: string }Metadata about the resolution (contains error on failure)
didDocumentMetadataRecord<string, unknown>Additional metadata

Example

import { resolveDID } from "@credat/sdk"; // Resolve did:web (makes HTTP request) const webResult = await resolveDID("did:web:acme.com"); if (webResult.didDocument) { console.log(webResult.didDocument.verificationMethod); } // Resolve did:key (no network request) const keyResult = await resolveDID("did:key:z6MkhaXg..."); if (keyResult.didDocument) { console.log(keyResult.didDocument.id); }

Errors

ErrorWhen
DIDError with DID_RESOLUTION_FAILEDInvalid DID format (doesn’t start with did:)
DIDError with DID_METHOD_UNSUPPORTEDDID method not supported (only key and web are supported)

Supported methods

MethodResolutionNetwork
did:webFetches https://{domain}/.well-known/did.jsonYes
did:keyExtracts key from the DID stringNo

did:web functions

createDidWeb

Creates a did:web identifier from a domain and optional path.

function createDidWeb(domain: string, path?: string): string

Examples

import { createDidWeb } from "@credat/sdk"; createDidWeb("acme.com"); // "did:web:acme.com" createDidWeb("acme.com", "agents/bot-1"); // "did:web:acme.com:agents:bot-1"

didWebToUrl

Converts a did:web to its HTTP resolution URL.

function didWebToUrl(did: string): string

Examples

import { didWebToUrl } from "@credat/sdk"; didWebToUrl("did:web:acme.com"); // "https://acme.com/.well-known/did.json" didWebToUrl("did:web:acme.com:agents:bot-1"); // "https://acme.com/agents/bot-1/did.json"

resolveDidWeb

Resolves a did:web by fetching its DID Document over HTTPS.

function resolveDidWeb( did: string, options?: { timeout?: number }, ): Promise<DIDResolutionResult>
ParameterTypeRequiredDefaultDescription
didstringYes---A did:web identifier
options.timeoutnumberNo10000HTTP request timeout in milliseconds

Returns { didDocument: null, didResolutionMetadata: { error: "notFound" } } on HTTP errors, timeouts, or network failures.


did:key functions

createDidKey

Creates a did:key identifier from a raw public key.

function createDidKey(publicKey: Uint8Array, algorithm: Algorithm): string

Example

import { createDidKey, generateKeyPair } from "@credat/sdk"; const keys = generateKeyPair("EdDSA"); const did = createDidKey(keys.publicKey, "EdDSA"); // "did:key:z6Mkh..."

resolveDidKey

Resolves a did:key by extracting the public key from the identifier. No network request.

function resolveDidKey(did: string): DIDResolutionResult

Example

import { resolveDidKey } from "@credat/sdk"; const result = resolveDidKey("did:key:z6Mkh..."); if (result.didDocument) { const jwk = result.didDocument.verificationMethod?.[0]?.publicKeyJwk; console.log(jwk); // { kty: "OKP", crv: "Ed25519", x: "..." } }

Types

DIDDocument

interface DIDDocument { id: string; verificationMethod?: VerificationMethod[]; authentication?: string[]; assertionMethod?: string[]; service?: ServiceEndpoint[]; }

VerificationMethod

interface VerificationMethod { id: string; type: string; controller: string; publicKeyJwk?: JsonWebKey; publicKeyMultibase?: string; }

ServiceEndpoint

interface ServiceEndpoint { id: string; type: string; serviceEndpoint: string; }
Last updated on