Resolve DID documents from Decentralized Identifiers according to the W3C DID specification
npx @tessl/cli install tessl/npm-did-resolver@4.1.0DID Resolver is a TypeScript library that provides a simple common interface for JavaScript applications to resolve DID documents from Decentralized Identifiers (DIDs). It supports the W3C DID specification and allows DID method implementors to release npm packages that applications can add through a pluggable resolver registry system.
npm install did-resolverimport { Resolver, parse, DIDResolutionResult, DIDDocument } from "did-resolver";For CommonJS:
const { Resolver, parse, DIDResolutionResult, DIDDocument } = require("did-resolver");import { Resolver } from "did-resolver";
import ethr from "ethr-did-resolver";
import web from "web-did-resolver";
// Configure resolver with method implementations
const resolver = new Resolver({
...ethr.getResolver(),
...web.getResolver(),
});
// Resolve a DID document
const result = await resolver.resolve("did:ethr:0xF3beAC30C498D9E26865F34fCAa57dBB935b0D74");
console.log(result.didDocument);
// Parse DID URLs
const parsed = parse("did:example:123456789?service=messaging#key-1");
console.log(parsed.method, parsed.id, parsed.query, parsed.fragment);DID Resolver is built around several key components:
Core functionality for resolving DID documents from various decentralized storage mechanisms through pluggable method resolvers.
class Resolver implements Resolvable {
constructor(registry: ResolverRegistry = {}, options: ResolverOptions = {});
resolve(didUrl: string, options?: DIDResolutionOptions): Promise<DIDResolutionResult>;
}
interface Resolvable {
resolve: (didUrl: string, options?: DIDResolutionOptions) => Promise<DIDResolutionResult>;
}
interface DIDResolutionResult {
'@context'?: 'https://w3id.org/did-resolution/v1' | string | string[];
didResolutionMetadata: DIDResolutionMetadata;
didDocument: DIDDocument | null;
didDocumentMetadata: DIDDocumentMetadata;
}
interface DIDResolutionOptions extends Extensible {
accept?: string;
}
interface ResolverOptions {
cache?: DIDCache | boolean | undefined;
legacyResolvers?: LegacyResolverRegistry;
}
type ResolverRegistry = Record<string, DIDResolver>;
type DIDResolver = (
did: string,
parsed: ParsedDID,
resolver: Resolvable,
options: DIDResolutionOptions
) => Promise<DIDResolutionResult>;Parses DID URL strings into structured components for processing by resolver implementations.
function parse(didUrl: string): ParsedDID | null;
interface ParsedDID {
did: string;
didUrl: string;
method: string;
id: string;
path?: string;
fragment?: string;
query?: string;
params?: Params;
}
interface Params {
[index: string]: string;
}Built-in and customizable caching for DID resolution results to improve performance and reduce network requests.
function inMemoryCache(): DIDCache;
function noCache(parsed: ParsedDID, resolve: WrappedResolver): Promise<DIDResolutionResult>;
type DIDCache = (parsed: ParsedDID, resolve: WrappedResolver) => Promise<DIDResolutionResult>;
type WrappedResolver = () => Promise<DIDResolutionResult>;Backward compatibility support for DID method resolvers created before version 3.0.0.
function wrapLegacyResolver(resolve: LegacyDIDResolver): DIDResolver;
type LegacyDIDResolver = (did: string, parsed: ParsedDID, resolver: Resolvable) => Promise<DIDDocument>;
interface LegacyResolverRegistry {
[index: string]: LegacyDIDResolver;
}Complete TypeScript definitions for W3C DID document structure and associated metadata.
type DIDDocument = {
'@context'?: 'https://www.w3.org/ns/did/v1' | string | string[];
id: string;
alsoKnownAs?: string[];
controller?: string | string[];
verificationMethod?: VerificationMethod[];
service?: Service[];
publicKey?: VerificationMethod[]; // deprecated
} & {
[x in KeyCapabilitySection]?: (string | VerificationMethod)[];
};
interface VerificationMethod {
id: string;
type: string;
controller: string;
publicKeyBase58?: string;
publicKeyBase64?: string;
publicKeyJwk?: JsonWebKey;
publicKeyHex?: string;
publicKeyMultibase?: string;
blockchainAccountId?: string;
ethereumAddress?: string;
// ConditionalProof2022 properties
conditionOr?: VerificationMethod[];
conditionAnd?: VerificationMethod[];
threshold?: number;
conditionThreshold?: VerificationMethod[];
conditionWeightedThreshold?: ConditionWeightedThreshold[];
conditionDelegated?: string;
relationshipParent?: string[];
relationshipChild?: string[];
relationshipSibling?: string[];
}
interface Service {
id: string;
type: string;
serviceEndpoint: ServiceEndpoint | ServiceEndpoint[];
[x: string]: any;
}
interface JsonWebKey extends Extensible {
alg?: string;
crv?: string;
e?: string;
ext?: boolean;
key_ops?: string[];
kid?: string;
kty: string;
n?: string;
use?: string;
x?: string;
y?: string;
}
type KeyCapabilitySection =
| 'authentication'
| 'assertionMethod'
| 'keyAgreement'
| 'capabilityInvocation'
| 'capabilityDelegation';
type ServiceEndpoint = string | Record<string, any>;
interface ConditionWeightedThreshold {
condition: VerificationMethod;
weight: number;
}Metadata structures for DID resolution operations and results.
interface DIDResolutionMetadata extends Extensible {
contentType?: string;
error?: 'invalidDid' | 'notFound' | 'representationNotSupported' | 'unsupportedDidMethod' | string;
}
interface DIDDocumentMetadata extends Extensible {
created?: string;
updated?: string;
deactivated?: boolean;
versionId?: string;
nextUpdate?: string;
nextVersionId?: string;
equivalentId?: string;
canonicalId?: string;
}
type Extensible = Record<string, any>;