@tidecloak/js
Version:
TideCloak client side JS SDK
77 lines • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const BaseComponent_js_1 = require("./Components/BaseComponent.js");
const ComponentRegistry_js_1 = require("./Components/ComponentRegistry.js");
const Ed25519Components_js_1 = require("./Components/Schemes/Ed25519/Ed25519Components.js");
const Ed25519Scheme_js_1 = __importDefault(require("./Components/Schemes/Ed25519/Ed25519Scheme.js"));
const SchemeRegistry_js_1 = require("./Components/Schemes/SchemeRegistry.js");
const DH_js_1 = require("./Encryption/DH.js");
const Math_js_1 = require("./Math.js");
const Serialization_js_1 = require("./Serialization.js");
class TideKey {
static NewKey(scheme) {
const seedFactory = ComponentRegistry_js_1.Registery[scheme.Name][BaseComponent_js_1.Seed];
return new TideKey(seedFactory.Create(undefined));
}
static FromSerializedComponent(c) {
return new TideKey(BaseComponent_js_1.BaseComponent.DeserializeComponent(c));
}
constructor(c) {
/**@type { BaseComponent } */
this.component = undefined;
if (c instanceof BaseComponent_js_1.BaseComponent)
this.component = c;
else
throw Error("Expecting object derived from BaseComponent");
}
/**
*
* @returns {BasePrivateComponent}
*/
get_private_component() {
if (!hasOwnInstanceMethod(this.component, "GetPrivate") && !(this.component instanceof BaseComponent_js_1.BasePrivateComponent))
throw Error("Cannot generate or find private component");
this.privateComponent = this.component instanceof BaseComponent_js_1.BasePrivateComponent ? this.component : this.component.GetPrivate();
return this.privateComponent;
}
/**
* @returns {BasePublicComponent}
*/
get_public_component() {
if (!hasOwnInstanceMethod(this.component, "GetPublic") && !(this.component instanceof BaseComponent_js_1.BasePublicComponent))
throw Error("Cannot generate or find public component");
this.publicComponent = this.component instanceof BaseComponent_js_1.BasePublicComponent ? this.component : this.component.GetPublic();
return this.publicComponent;
}
async sign(message) {
const f = this.component.Scheme.GetSigningFunction();
return await f(message, this.get_private_component());
}
async verify(message, signature) {
const f = this.component.Scheme.GetVerifyingFunction();
return await f(message, signature, this.get_public_component());
}
async prepVouchersReq(gORKn) {
// Ensure scheme is Ed25519 for tide vouchers
if (this.component.Scheme !== Ed25519Scheme_js_1.default)
throw Error("Cannot execute prepVouchersReq on a non Ed25519 key");
let blurKeyPub = [];
for (let i = 0; i < gORKn.length; i++) {
const z = (0, Math_js_1.mod)((0, Serialization_js_1.BigIntFromByteArray)(await (0, DH_js_1.computeSharedKey)(gORKn[i], this.get_private_component().priv)));
blurKeyPub[i] = gORKn[i].mul(z);
}
return blurKeyPub;
}
}
exports.default = TideKey;
function hasOwnInstanceMethod(obj, methodName) {
// get the “own” prototype of this object’s class
const proto = Object.getPrototypeOf(obj);
// check it has its own property of that name, and that it’s a function
return Object.prototype.hasOwnProperty.call(proto, methodName)
&& typeof proto[methodName] === 'function';
}
//# sourceMappingURL=TideKey.js.map