@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
69 lines • 2.62 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* 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 base64url_1 = require("base64url");
const crypto = require("crypto");
const JoseConstants_1 = require("../crypto/protocols/jose/JoseConstants");
/**
* Class representing a protected commit.
*/
class ProtectedCommit {
constructor(json) {
this.json = json;
}
/**
* Returns the protected commit data in the Flattened JWS JSON Serialization.
*/
toFlattenedJson() {
return this.json;
}
/**
* Returns the decoded protected headers for this commit.
*/
getProtectedHeaders() {
if (this.json && this.json.get(JoseConstants_1.default.tokenProtected)) {
return JSON.parse(base64url_1.default.decode(this.json.get(JoseConstants_1.default.tokenProtected)));
}
throw new Error('Commit does not have a protected field.');
}
/**
* Returns the decoded payload for this commit.
*/
getPayload() {
if (this.json && this.json.get(JoseConstants_1.default.tokenPayload)) {
const decoded = base64url_1.default.decode(this.json.get(JoseConstants_1.default.tokenPayload).toString());
try {
return JSON.parse(decoded);
}
catch (e) {
// Not JSON, return directly
return decoded;
}
}
throw new Error('Commit does not have a payload field.');
}
/**
* Retrieves the revision ID for this commit.
*/
getRevision() {
// NEED: Verify signature; cache result
const sha256 = crypto.createHash('sha256');
sha256.update(`${this.json.get(JoseConstants_1.default.tokenProtected)}.${this.json.get(JoseConstants_1.default.tokenPayload)}`);
return sha256.digest('hex');
}
/**
* Retrieves the ID of the object to which this commit belongs.
*/
getObjectId() {
const headers = this.getProtectedHeaders();
if (headers.operation === 'create') {
return this.getRevision();
}
return headers.object_id;
}
}
exports.default = ProtectedCommit;
//# sourceMappingURL=ProtectedCommit.js.map