UNPKG

@safient/core

Version:

JavaScript SDK to manage safes and interact with Safient protocol.

145 lines (144 loc) 6.97 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Crypto = void 0; const encryption_1 = require("../utils/encryption"); const helpers_1 = require("../utils/helpers"); class Crypto { constructor() { /** * * @param safeData - Data that is stored on the safe * @param beneficiaryDid - DID of the beneficiary * @param creatorDid - DID of the creator * @param creator - Connection Object of the creator * @param guardians - Array of Guardian DIDs * @param signature - Signature of the message by creator * @param recoveryMessage - RecoveryMessage created for guardians * @param secrets - Array of secrets for guardians * @returns Encrypted Safe Data */ this.encryptSafeData = (safeData, creatorDid, creator, guardians, signature, recoveryMessage, secrets, beneficiaryDid) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; let shardData = []; let decSardData = []; let beneficiaryEncKey = null; try { //Generate AES key const aesKey = yield (0, encryption_1._generateCipherKey)(); // Encrypt data const encryptedData = yield (0, encryption_1._encryptData)(Buffer.from(JSON.stringify({ data: safeData })), aesKey); //Encrypt AES for creator const creatorEncKey = yield ((_b = (_a = creator.idx) === null || _a === void 0 ? void 0 : _a.ceramic.did) === null || _b === void 0 ? void 0 : _b.createDagJWE(aesKey, [creatorDid])); if (beneficiaryDid) { //Encrypt AES for beneficiary beneficiaryEncKey = yield ((_d = (_c = creator.idx) === null || _c === void 0 ? void 0 : _c.ceramic.did) === null || _d === void 0 ? void 0 : _d.createDagJWE(aesKey, [beneficiaryDid])); const ShareData = { beneficiaryEncKey: beneficiaryEncKey, message: JSON.parse(recoveryMessage), signature: signature }; const shards = (0, encryption_1._shamirSplit)(ShareData, 3, 2); for (let shardIndex = 0; shardIndex < shards.length; shardIndex++) { shardData.push({ data: yield ((_f = (_e = creator.idx) === null || _e === void 0 ? void 0 : _e.ceramic.did) === null || _f === void 0 ? void 0 : _f.createDagJWE({ share: shards[shardIndex], secret: secrets[shardIndex] }, [guardians[shardIndex]])), }); decSardData.push({ share: shards[shardIndex], secret: secrets[shardIndex] }); } } let result = { creatorEncKey: creatorEncKey, beneficiaryEncKey: beneficiaryEncKey, encryptedData: encryptedData, shardData: shardData, decSardData: decSardData, }; return result; } catch (err) { throw new Error(`Error while encrypting safe data`); } }); /** * * @param encKey - Encrypted AES key * @param connection - Connection object of the user * @param encryptedData - Encrypted Safe Data * @returns Decrypted Data */ this.decryptSafeData = (encKey, connection, encryptedData) => __awaiter(this, void 0, void 0, function* () { var _g, _h; const aesKey = yield ((_h = (_g = connection.idx) === null || _g === void 0 ? void 0 : _g.ceramic.did) === null || _h === void 0 ? void 0 : _h.decryptDagJWE(encKey)); const decryptedData = yield (0, encryption_1._decryptData)(encryptedData, aesKey); return decryptedData; }); /** * * @param shards - Recovered shards of the guardians * @returns Share pf the data */ this.reconstructSafeData = (shards) => __awaiter(this, void 0, void 0, function* () { try { //Reconstruct the shards from guardians const reconstructedData = (0, encryption_1._shamirCombine)(shards); //Get encrytped data const encryptedData = JSON.parse(reconstructedData.toString()); return encryptedData; } catch (err) { throw new Error(`Error while recontructing data`); } }); /** * * @param guardians - Array of Guardians of the Safes * @returns - Recovery message */ this.generateSecrets = (guardians) => { try { let guardiansSecrets = []; let hash; let secrets = []; guardians.map((guardian) => { const guardianSecret = (0, helpers_1.randomBytes)(4); secrets.push(guardianSecret.toString('hex')); guardiansSecrets.push({ secret: helpers_1.utils.solidityKeccak256(['string'], [guardianSecret.toString('hex')]), address: guardian.userAddress.toLowerCase() }); }); const recoveryMessage = JSON.stringify({ data: { guardians: guardiansSecrets } }); hash = helpers_1.utils.solidityKeccak256(["string"], [recoveryMessage]); const result = { guardians: guardiansSecrets, hash: hash, recoveryMessage: recoveryMessage, secrets: secrets }; return result; } catch (err) { throw new Error(`Error while creating recovery messages, ${err}`); } }; } } exports.Crypto = Crypto;