UNPKG

@digitalcredentials/issuer-registry-client

Version:

Known issuers / verifiers registry client, for use in Typescript, browser, and React Native.

131 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegistryClient = exports.DidMapRegistryEntry = exports.DidMapRegistry = exports.KnownDidRegistry = void 0; /*! * Copyright (c) 2023 Digital Credentials Consortium. All rights reserved. */ const http_client_1 = require("@digitalcredentials/http-client"); /** * Example registry: * @example * ``` * { * "name": "DCC Sandbox Registry", * "url": "https://digitalcredentials.github.io/sandbox-registry/registry.json" * } * ``` */ class KnownDidRegistry { name; url; constructor(name, url) { this.name = name; this.url = url; } } exports.KnownDidRegistry = KnownDidRegistry; const DID_MAP_REGISTRY_SPEC_V01 = '0.1.0'; class DidMapRegistry extends KnownDidRegistry { version = DID_MAP_REGISTRY_SPEC_V01; /** * @example rawContents * { * did:key:z6MkpLDL3RoAoMRTwTgo3rs39ZwssfaPKtGdZw7AGRN7CK4W: { * name: "(Example) My University", * location: "Cambridge, MA, USA", * url: "https://digitalcredentials.mit.edu" * } * } */ rawContents; constructor({ name, url, rawContents }) { super(name, url); this.rawContents = rawContents; } } exports.DidMapRegistry = DidMapRegistry; /** * @example * ``` * // did:key:z6MkpLDL3RoAoMRTwTgo3rs39ZwssfaPKtGdZw7AGRN7CK4W * { * name: "(Example) My University", * location: "Cambridge, MA, USA", * url: "https://digitalcredentials.mit.edu", * inRegistries: ["DCC Community Registry", "DCC Sandbox Registry"] * } * ``` */ class DidMapRegistryEntry { name; url; location; inRegistries = new Set(); constructor({ name, url, location }) { this.name = name; this.url = url; this.location = location; } } exports.DidMapRegistryEntry = DidMapRegistryEntry; class RegistryClient { registries; didMap = new Map(); /** * @example * ``` * const config = [ * { * "name": "DCC Sandbox Registry", * "url": "https://digitalcredentials.github.io/sandbox-registry/registry.json" * } * ] * const client = new RegistryClient() * await client.load({ config }) * ``` * @param registries - Config object with a list of registries to load */ async load({ config }) { // Clear previous DID map and entries this.didMap = new Map(); this.registries = config; const registryLoadResult = JSON.parse(JSON.stringify(this.registries)); await Promise.all(this.registries.map(async (registry) => { const resultEntry = registryLoadResult.find((entry) => entry.url === registry.url); try { // fetch registry contents const contents = await http_client_1.httpClient.get(registry.url); registry.rawContents = contents.data.registry; // discard contents.meta, not needed at this point // cycle through each DID in the registry, add to DID Map for (const did in registry.rawContents) { const entry = new DidMapRegistryEntry(registry.rawContents[did]); const existingEntry = this.didMap.get(did); if (existingEntry != null) { existingEntry.inRegistries?.add(registry); } else { entry.inRegistries?.add(registry); this.didMap.set(did, entry); } } if (resultEntry != null) resultEntry.loaded = true; } catch (e) { console.log(`Could not load registry from url "${registry.url}":`, e); // no DIDs are added from that registry if (resultEntry != null) resultEntry.loaded = false; if (resultEntry != null) resultEntry.error = e; } })); return registryLoadResult; } didEntry(did) { return this.didMap.get(did); } } exports.RegistryClient = RegistryClient; //# sourceMappingURL=index.js.map