UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

76 lines 3.68 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 Identifier_1 = require("../../../Identifier"); const JwsToken_1 = require("../jose/jws/JwsToken"); const JoseConstants_1 = require("../jose/JoseConstants"); const CryptoProtocolError_1 = require("../CryptoProtocolError"); /** * Hub Protocol for decrypting/verifying and encrypting/signing payloads */ class DidProtocol { /** * Authentication constructor * @param options Arguments to a constructor in a named object */ constructor(options) { this.sender = options.sender; this.resolver = options.resolver; this.options = this.sender.options; this.cryptoFactory = this.options.cryptoFactory; this.keyStore = this.options.keyStore; } /** * Unwrapping method for unwrapping requests/responses. * Decrypt a cipher using [PrivateKey] of Client Identifier and then verify payload. * @param encryptionKeyReference key reference of private key in keystore used to decrypt. * @param cipher the cipher to be decrypted and verified by client Identifier. */ async decryptAndVerify(encryptionKeyReference, cipher) { // decrypt payload. const jws = await this.sender.decrypt(cipher, encryptionKeyReference); // get identifier id from key id in header. const options = { cryptoFactory: this.cryptoFactory }; const token = await JwsToken_1.default.deserialize(jws, options); let kid = undefined; if (token.signatures[0].protected && token.signatures[0].protected.get(JoseConstants_1.default.Kid)) { kid = token.signatures[0].protected.get(JoseConstants_1.default.Kid); } else if (token.signatures[0].header && token.signatures[0].header.get(JoseConstants_1.default.Kid)) { kid = token.signatures[0].header.get(JoseConstants_1.default.Kid); } if (!kid) { throw new CryptoProtocolError_1.default('DidProtocol', 'The jws token does not contain a kid in protected or header'); } const id = kid.split('#')[0]; const identifier = new Identifier_1.default(id, this.options); let payload = await identifier.verify(jws); return JSON.parse(payload); } /** * Wrapping method for wrapping requests/responses. * Sign a payload using [PrivateKey] in client's keystore and encrypt payload using [PublicKey] * @param payload the payload to be signed and encrypted by client Identifier. * #param receiverId identifier for the receiver. */ async signAndEncrypt(payload, receiverId) { let stringifiedPayload; if (typeof (payload) === 'string') { stringifiedPayload = payload; } else { stringifiedPayload = JSON.stringify(payload); } const jws = await this.sender.sign(stringifiedPayload, this.sender.options.cryptoOptions.signingKeyReference); // create User Agent Options for Identifier const receiverIdentifier = new Identifier_1.default(receiverId, this.options); return receiverIdentifier.encrypt(jws); } } exports.default = DidProtocol; //# sourceMappingURL=DidProtocol.js.map