@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
60 lines • 3 kB
JavaScript
"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 ProtectionStrategy_1 = require("../../crypto/strategies/ProtectionStrategy");
const PayloadSigningStrategy_1 = require("../../crypto/strategies/PayloadSigningStrategy");
const UserAgentError_1 = require("../../UserAgentError");
/**
* Class which can apply a signature to a commit.
*/
class CommitProtector {
constructor(options) {
this.did = options.did;
this.signingKeyReference = options.signingKeyReference;
this.payloadProtectionOptions = options.payloadProtectionOptions;
this.payloadProtection = options.payloadProtection;
this.hubProtectionStrategy = options.hubProtectionStrategy;
this.recipientsPublicKeys = options.recipientsPublicKeys;
}
/**
* Protect the given commit.
*
* @param commit The commit to protect.
*/
async protect(commit) {
let payload;
if (typeof (commit.getPayload()) === 'string') {
payload = commit.getPayload();
}
else {
payload = JSON.stringify(commit.getPayload());
}
commit.validate();
let protectedPayload = {};
if (!this.hubProtectionStrategy) {
// Set a default protection stategy
this.hubProtectionStrategy = new ProtectionStrategy_1.default();
this.hubProtectionStrategy.payloadSigningStrategy = new PayloadSigningStrategy_1.default();
}
// Do signature
if (this.hubProtectionStrategy.payloadSigningStrategy && this.hubProtectionStrategy.payloadSigningStrategy.enabled) {
if (!this.signingKeyReference) {
throw new UserAgentError_1.default(`The signing key reference is missing from the options`);
}
protectedPayload = await this.payloadProtection.sign(this.signingKeyReference, Buffer.from(payload), 'JwsCompactJson', this.payloadProtectionOptions);
}
// encrypt
if (this.hubProtectionStrategy.PayloadEncryptionStrategy && this.hubProtectionStrategy.PayloadEncryptionStrategy.enabled) {
if (!this.recipientsPublicKeys) {
throw new UserAgentError_1.default(`The encryption public keys are missing from the options`);
}
protectedPayload = await this.payloadProtection.encrypt(this.recipientsPublicKeys, Buffer.from(payload), 'JweCompactJson', this.payloadProtectionOptions);
}
return protectedPayload;
}
}
exports.default = CommitProtector;
//# sourceMappingURL=CommitProtector.js.map