UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

169 lines 7.41 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); const UserAgentError_1 = require("./UserAgentError"); const HostServiceEndpoint_1 = require("./serviceEndpoints/HostServiceEndpoint"); const UserServiceEndpoint_1 = require("./serviceEndpoints/UserServiceEndpoint"); const cloneDeep = require('lodash/fp/cloneDeep'); /** * Class for creating and managing identifiers, * retrieving identifier documents. */ class IdentifierDocument { /** * Constructs an instance of the identifier * document. * @param document from which to create the identifier document. * @param options for configuring how to register and resolve identifiers. */ constructor(document) { /** * Array of service entries added to the document. */ this.publicKeys = []; /** * Array of authentication entries added to the document. */ this.authenticationReferences = []; /** * Array of service entries added to the document. */ this.serviceReferences = []; // Populate the base properties this.id = document.id; this.publicKeys = document.publicKeys; if (document.created) { this.created = new Date(document.created); } this.authenticationReferences = document.authentication || []; this.serviceReferences = document.service || []; if (document.service && document.service.length > 0) { document.service.forEach((serviceRef) => { if (serviceRef.serviceEndpoint['@type'] === 'HostServiceEndpoint') { serviceRef.serviceEndpoint = HostServiceEndpoint_1.default.fromJSON(serviceRef.serviceEndpoint); } else { serviceRef.serviceEndpoint = UserServiceEndpoint_1.default.fromJSON(serviceRef.serviceEndpoint); } }); } } /** * Creates a new instance of an identifier document using the * provided public keys. * @param publicKeys to include in the document. */ static create(id, publicKeys) { const createdDate = new Date(Date.now()).toISOString(); return new IdentifierDocument({ id: id, created: createdDate, publicKeys: publicKeys }); } /** * Creates a new instance of an identifier document using the * provided public keys. * The id is generated. * @param idBase The base id in format did:{method}:{id}. {id} will be filled in by this method * @param publicKeys to include in the document. * @param options User agent options containing the crypto Api */ static async createAndGenerateId(idBase, publicKeys, options) { const document = IdentifierDocument.create(idBase, publicKeys); const identifier = await options.registrar.generateIdentifier(document); document.id = identifier.id; return document; } /** * Adds an authentication reference to the document. * @param authenticationReference to add to the document. */ addAuthenticationReference(authenticationReference) { this.authenticationReferences.push(authenticationReference); } /** * Adds a service reference to the document. * @param serviceReference to add to the document. */ addServiceReference(serviceReference) { this.serviceReferences.push(serviceReference); } /** * Get Hub Instances from Identity Service Reference. */ getHubInstances() { const filteredServiceReferences = this.serviceReferences.filter(reference => reference.type === 'IdentityHub'); if (filteredServiceReferences.length === 0 || filteredServiceReferences[0].serviceEndpoint.type !== 'UserServiceEndpoint') { throw new UserAgentError_1.default(`No Hub Instances for ${this.id}`); } const serviceEndpoint = filteredServiceReferences[0].serviceEndpoint; return serviceEndpoint.instances; } /** * Get Hub Locations from Identity Service Reference. */ getHubLocations() { const filteredServiceReferences = this.serviceReferences.filter(reference => reference.type === 'IdentityHub'); if (filteredServiceReferences.length === 0 || filteredServiceReferences[0].serviceEndpoint.type !== 'HostServiceEndpoint') { throw new UserAgentError_1.default(`No Hub Locations for ${this.id}`); } const serviceEndpoint = filteredServiceReferences[0].serviceEndpoint; return serviceEndpoint.locations; } getPublicKeysFromDocument() { return this.publicKeys.map(key => key.publicKeyJwk); } /** * Used to control the the properties that are * output by JSON.parse. */ static fromJSON(obj) { const document = Object.create(IdentifierDocument.prototype); const result = Object.assign(document, obj, { publicKeys: obj.publicKey }); delete result.publicKey; return new IdentifierDocument(document); } /** * Used to control the the properties that are * output by JSON.stringify. */ toJSON() { // Clone the current instance. Note the use of // a deep clone to ensure immutability of // the instance being cloned for serialization const clonedDocument = cloneDeep(this); // Add the JSON-LD context clonedDocument['@context'] = 'https://w3id.org/did/v1'; // switch authentication references to authentication. if (this.authenticationReferences && this.authenticationReferences.length > 0) { clonedDocument.authentication = this.authenticationReferences; } clonedDocument.authenticationReferences = undefined; // switch service references to service. if (this.serviceReferences && this.serviceReferences.length > 0) { clonedDocument.service = this.serviceReferences; clonedDocument.service.forEach((serviceRef) => { if (serviceRef.serviceEndpoint['type'] === 'HostServiceEndpoint') { serviceRef.serviceEndpoint = serviceRef.serviceEndpoint.toJSON(); } else { serviceRef.serviceEndpoint = serviceRef.serviceEndpoint.toJSON(); } }); } clonedDocument.serviceReferences = undefined; if (!this.publicKeys || this.publicKeys.length === 0) { clonedDocument.publicKeys = undefined; } else { clonedDocument.publicKey = this.publicKeys; delete clonedDocument.publicKeys; } // Now return the cloned document for serialization return clonedDocument; } } exports.default = IdentifierDocument; //# sourceMappingURL=IdentifierDocument.js.map