UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

126 lines 6.17 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 CommitProtector_1 = require("../hubSession/crypto/CommitProtector"); const HubCommitWriteRequest_1 = require("../hubSession/requests/HubCommitWriteRequest"); const HubSession_1 = require("../hubSession/HubSession"); const HubObject_1 = require("./HubObject"); const JoseConstants_1 = require("../crypto/protocols/jose/JoseConstants"); const JoseProtocol_1 = require("../crypto/protocols/jose/JoseProtocol"); const typescript_map_1 = require("typescript-map"); /** * Class for doing CRUD operations to Actions, Collections, Permissions, and Profile * In a Hub. */ class HubClient { /** * Constructs an instance of the Hub Client Class for hub operations * @param hubClientOptions hub client options used to create instance. */ constructor(hubClientOptions) { if (!hubClientOptions.hubOwner || !hubClientOptions.clientIdentifier) { throw new UserAgentError_1.default(`HubClientOptions does not contain all properties`); } this.hubOwner = hubClientOptions.hubOwner; this.clientIdentifier = hubClientOptions.clientIdentifier; this.signingKeyReference = hubClientOptions.clientIdentifier.options.cryptoOptions.signingKeyReference; this.encryptionKeyReference = hubClientOptions.clientIdentifier.options.cryptoOptions.encryptionKeyReference; this.recipientsPublicKeys = hubClientOptions.recipientsPublicKeys; this.cryptoFactory = hubClientOptions.cryptoOptions.cryptoFactory; this.payloadProtection = hubClientOptions.cryptoOptions.payloadProtection; this.hubProtectionStrategy = hubClientOptions.hubProtectionStrategy; } /** * Protect and sends a commit to the hub owner's hub. * @param commit commit to be sent to hub owner's hub. */ async commit(commit) { if (commit.getCommitFields().iss !== this.clientIdentifier.id) { throw new UserAgentError_1.default(`Issuer, '${commit.getCommitFields().iss},' is not valid for this HubClient Instance.`); } if (commit.getCommitFields().sub !== this.hubOwner.id) { throw new UserAgentError_1.default(`Subject, '${commit.getCommitFields().sub},' is not valid for this HubClient Instance.`); } if (!this.clientIdentifier.options || !this.clientIdentifier.options.keyStore) { throw new UserAgentError_1.default(`No KeyStore defined for '${this.clientIdentifier}`); } const fields = commit.getCommitFields(); fields.kid = ''; const map = Object.keys(fields).map((field) => { return [field, fields[field]]; }); const options = { cryptoFactory: this.cryptoFactory, options: new typescript_map_1.TSMap([ [JoseConstants_1.default.optionProtectedHeader, new typescript_map_1.TSMap(map)] ]), payloadProtection: new JoseProtocol_1.default() }; const commitProtectOptions = { did: this.clientIdentifier.id, signingKeyReference: this.signingKeyReference, recipientsPublicKeys: this.recipientsPublicKeys, payloadProtection: this.payloadProtection, payloadProtectionOptions: options, hubProtectionStrategy: this.hubProtectionStrategy }; // protect the commit const commitProtector = new CommitProtector_1.default(commitProtectOptions); const protectedCommit = await commitProtector.protect(commit); const commitRequest = new HubCommitWriteRequest_1.default(protectedCommit); const session = await this.createHubSession(); return session.send(commitRequest); } /** * Query Objects of certain type in Hub. * @param queryRequest object that tells the hub what object to get. */ async queryObjects(queryRequest) { const session = await this.createHubSession(); const queryResponse = await session.send(queryRequest); const objects = queryResponse.getObjects(); let hubObjects = []; objects.forEach(object => { hubObjects.push(new HubObject_1.default(object)); }); return hubObjects; } /** * Query Object specified by certain id * @param commitQueryRequest HubCommitQueryRequest object to request object of specific id. * @param hubObject a HubObject containing metadata such as object id. */ async queryObject(commitQueryRequest, hubObject) { const session = await this.createHubSession(); await hubObject.hydrate(session, commitQueryRequest); return hubObject; } /** * Get all Hub Instances from hub owner's identifier document. */ async getHubInstances() { const identifierDocument = await this.hubOwner.getDocument(); return identifierDocument.getHubInstances(); } /** * Implement createHubSession method once HubSession is refactored. * creates a hubSession for hub instance that is available/online. */ async createHubSession() { const options = { client: this.clientIdentifier, hubOwner: this.hubOwner, hubId: 'did:ion:test:EiANVcAJKiAdVQRId8IYrQHxJE4nU4-_pW041qh-z5tVZQ', hubEndpoint: 'http://localhost:8080/', signingKeyReference: this.signingKeyReference, encryptionKeyReference: this.encryptionKeyReference }; return new HubSession_1.default(options); } } exports.default = HubClient; //# sourceMappingURL=HubClient.js.map