UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

82 lines 3.21 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 }); /** * Represents a new (i.e pending, unsigned) commit which will create, update, or delete an object in * a user's Identity Hub. */ class Commit { constructor(fields) { this.fields = fields; } /** * Verifies whether the currently set fields constitute a valid commit which can be * signed/encrypted and stored in an Identity Hub. * * Throws an error if the commit is not valid. * * need: Move validation logic to hub-common-js repository to be shared with hub-node-core. */ validate() { if (!this.fields.iss) { throw new Error("Commit must specify the iss field."); } const requiredStrings = ['interface', 'context', 'type', 'committed_at', 'commit_strategy', 'sub']; requiredStrings.forEach((field) => { if (!this.fields[field] || typeof this.fields[field] !== 'string' || this.fields[field].length === 0) { throw new Error(`Commit '${field}' field must be a non-zero-length string.`); } }); if (!this.fields.operation) { throw new Error("Commit 'operation' field must be specified."); } if (['create', 'update', 'delete'].indexOf(this.fields.operation) === -1) { throw new Error("Commit 'operation' field must be one of create, update, or delete."); } if (this.fields.operation === 'create') { if (this.fields.object_id !== undefined) { throw new Error("Commit 'object_id' field must not be specified when operation is 'create'."); } } else { if (!this.fields.object_id) { throw new Error(`Commit 'object_id' field must be specified when operation is '${this.fields.operation}'.`); } } if (!this.fields.payload) { throw new Error("Commit must specify the 'payload' field."); } if (['string', 'object'].indexOf(typeof this.fields.payload) === -1) { throw new Error(`Commit payload must be string or object, ${typeof this.fields.payload} given.`); } } /** * Returns true if the validate() method would pass without error. */ isValid() { try { this.validate(); return true; } catch (err) { return false; } } /** * Returns the fields of the commit. */ getCommitFields() { return this.fields; } /** * Returns the application-specific payload for this commit. */ getPayload() { return this.fields.payload; } } exports.default = Commit; //# sourceMappingURL=Commit.js.map