import type { DidKeyOptions, DidIonCreateOptions, DidMethodApi, DidMethodCreator, DidMethodResolver, DidResolverCache, DidResolutionResult, DidState } from '@tbd54566975/dids'; import { DidResolver } from '@tbd54566975/dids'; // Map method names to option types type CreateMethodOptions = { ion: DidIonCreateOptions; key: DidKeyOptions; }; // A conditional type for inferring options based on the method name type CreateOptions = CreateMethodOptions[M]; export type DidApiOptions = { didMethodApis: DidMethodApi[]; cache?: DidResolverCache; } export class DidApi { private didResolver: DidResolver; private methodCreatorMap: Map = new Map(); /** * returns the DID resolver created by this api. useful in scenarios where you want to pass around * the same resolver so that you can leverage the resolver's cache */ get resolver() { return this.didResolver; } constructor(options: DidApiOptions) { const { didMethodApis, cache } = options; this.didResolver = new DidResolver({ methodResolvers: options.didMethodApis, cache }); for (let methodApi of didMethodApis) { this.methodCreatorMap.set(methodApi.methodName, methodApi); } } /** * Creates a DID of the method provided * @param method - the method of DID to create * @param options - method-specific options * @returns the created DID */ create(method: M, options?: CreateOptions): Promise { const didMethodCreator = this.methodCreatorMap.get(method); if (!didMethodCreator) { throw new Error(`no creator available for ${method}`); } return didMethodCreator.create(options); } /** * Resolves the provided DID * @param did - the did to resolve * @see {@link https://www.w3.org/TR/did-core/#did-resolution | DID Resolution} * @returns DID Resolution Result */ resolve(did: string): Promise { return this.didResolver.resolve(did); } /** * can be used to add different did method resolvers * @param _resolver */ addMethodResolver(_resolver: DidMethodResolver) { throw new Error('not yet implemented'); } /** * can be used to add differed did method creators * @param _creator */ addMethodCreator(_creator: DidMethodCreator) { throw new Error('not yet implemented'); } }