@tidecloak/js
Version:
TideCloak client side JS SDK
177 lines • 7.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HumanReadableModelBuilder = exports.ModelRegistry = void 0;
const Serialization_js_1 = require("../Cryptide/Serialization.js");
const InitializationCertificate_js_1 = __importDefault(require("./InitializationCertificate.js"));
const RuleSettings_js_1 = __importDefault(require("./Rules/RuleSettings.js"));
const CardanoTxBody_js_1 = __importDefault(require("./Cardano/CardanoTxBody.js"));
class ModelRegistry {
/**
*
* @param {string} modelId
* @returns {HumanReadableModelBuilder}
*/
static getHumanReadableModelBuilder(modelId, data, expiry) {
const c = modelBuildersMap[modelId];
if (!c)
throw Error("Could not find model: " + modelId);
return c.create(data, expiry);
}
}
exports.ModelRegistry = ModelRegistry;
class HumanReadableModelBuilder {
constructor(data, expiry) {
this._data = data;
this._expiry = expiry;
}
static create(data, expiry) {
return new this(data, expiry);
}
getHumanReadableObject() {
throw Error("Not implemented for this model");
}
}
exports.HumanReadableModelBuilder = HumanReadableModelBuilder;
// MODELS ----------------------------------------------------------------
class UserContextSignRequestBuilder extends HumanReadableModelBuilder {
get _id() { return this._name + ":" + this._version; }
constructor(data, expiry) {
super(data, expiry);
this._name = "UserContext"; // Model ID
this._humanReadableName = "Change Request";
this._version = "1";
}
static create(data, expiry) {
return super.create(data, expiry);
}
getHumanReadableObject() {
// deserialize draft here and return a pretty object for user
let prettyObject = {};
let draftIndex = 0;
const initCertPresent = (0, Serialization_js_1.GetValue)(this._data, 0)[0];
draftIndex++;
// determine if InitCert is present
switch (initCertPresent) {
case 0:
break;
case 1:
const initCert = (0, Serialization_js_1.GetValue)(this._data, draftIndex);
prettyObject.InitializationCertificate = new InitializationCertificate_js_1.default((0, Serialization_js_1.StringFromUint8Array)(initCert)).toPrettyObject();
draftIndex++;
break;
default:
throw Error("Unexpected value");
}
// make sure user context is JSON
let cont = true;
prettyObject.UserContexts = [];
while (cont) {
try {
prettyObject.UserContexts.push(JSON.parse((0, Serialization_js_1.StringFromUint8Array)((0, Serialization_js_1.GetValue)(this._data, draftIndex))));
draftIndex++;
}
catch {
cont = false;
}
}
// Create summary
let summary = [];
summary.push(["Admin related", initCertPresent == 1 ? "YES" : "no"]);
// Get the clients involved in this approval
// All clients will be either realm-management or under resource_management
let clients = [];
prettyObject.UserContexts.map(c => {
if (c.realm_access)
clients.push("realm_access");
if (typeof c.resource_access === "object") {
clients.push(...Object.keys(c.resource_access));
}
});
clients = [...new Set(clients)];
summary.push(["Applications affected", clients.join(", ")]);
summary.push(["Expiry", unixSecondsToLocaleString(this._expiry)]);
// return a nice object of InitCert? and usercontexts
return {
summary: summary,
pretty: prettyObject
};
}
}
class CardanoTxSignRequestBuilder extends HumanReadableModelBuilder {
get _id() { return this._name + ":" + this._version; }
constructor(data, expiry) {
//throw Error("Not implemented");
super(data, expiry);
this._name = "CardanoTx"; // Model ID
this._version = "1";
}
getHumanReadableObject() {
// deserialize draft here and return a pretty object for user
const txBytes = (0, Serialization_js_1.GetValue)(this._data, 0);
const body = new CardanoTxBody_js_1.default(txBytes);
let summary = [];
body.transaction.outputs.map(o => {
summary.push([`Outgoing ada to ${o.address}`, (o.amount / 1000000n).toString()]);
});
summary.push(["Fee", body.transaction.fee.toString()]);
return {
summary: summary,
pretty: body.toPrettyObject()
};
}
}
class RuleSettingSignRequestBuilder extends HumanReadableModelBuilder {
get _id() { return this._name + ":" + this._version; }
constructor(data, expiry) {
//throw Error("Not implemented");
super(data, expiry);
this._name = "Rules"; // Model ID
this._version = "1";
}
getHumanReadableObject() {
// deserialize draft here and return a pretty object for user
let prettyObject = {};
let draftIndex = 0;
const previousRulesPresent = (0, Serialization_js_1.GetValue)(this._data, 0)[0];
draftIndex++;
// determine if InitCert is present
switch (previousRulesPresent) {
case 0:
break;
case 1:
const previousRuleSettings = (0, Serialization_js_1.GetValue)(this._data, draftIndex);
prettyObject.RuleSettingToRevoke = new RuleSettings_js_1.default((0, Serialization_js_1.StringFromUint8Array)(previousRuleSettings)).toPrettyObject();
draftIndex += 2;
break;
default:
throw Error("Unexpected value");
}
const newRuleSettings = (0, Serialization_js_1.GetValue)(this._data, draftIndex);
prettyObject.NewRuleSetting = new RuleSettings_js_1.default((0, Serialization_js_1.StringFromUint8Array)(newRuleSettings)).toPrettyObject();
return {
summary: [["No summary for RuleSettings"]],
pretty: prettyObject
};
}
}
const modelBuildersMap = {
[new UserContextSignRequestBuilder()._id]: UserContextSignRequestBuilder,
[new CardanoTxSignRequestBuilder()._id]: CardanoTxSignRequestBuilder,
[new RuleSettingSignRequestBuilder()._id]: RuleSettingSignRequestBuilder
};
const unixSecondsToLocaleString = (unixSeconds) => {
const milliseconds = unixSeconds * 1000;
const date = new Date(milliseconds);
return date.toLocaleString('en-GB', {
day: 'numeric',
month: 'long',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
});
};
//# sourceMappingURL=ModelRegistry.js.map