UNPKG

lighthouse-encryption-sdk-browser

Version:

Encryption SDK: Build your trustless, decentralized and fault resistance Application using distributed key shades with threshold cryptography

49 lines (48 loc) 1.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.shardKey = void 0; const browser_1 = __importDefault(require("bls-eth-wasm/browser")); const shardKey = async (key, threshold = 3, keyCount = 5) => { try { const msk = []; const idVec = []; const secVec = []; await browser_1.default.init(browser_1.default.BLS12_381).then(() => browser_1.default.getCurveOrder()); const masterKey = new browser_1.default.SecretKey(); masterKey.deserializeHexStr(key); msk.push(masterKey); for (let i = 0; i < threshold; i++) { const sk = new browser_1.default.SecretKey(); sk.setByCSPRNG(); msk.push(sk); } /* key sharing */ for (let i = 0; i < keyCount; i++) { //create random Vector ID(points on the ECC) const id = new browser_1.default.Id(); id.setByCSPRNG(); idVec.push(id); //Create a secKey Shard const sk = new browser_1.default.SecretKey(); sk.share(msk, idVec[i]); secVec.push(sk); } //Convert vector in to readable hex values return { isShardable: true, keyShards: secVec?.map((sk, index) => ({ key: sk.serializeToHexStr(), index: idVec[index].serializeToHexStr(), })) ?? [], }; } catch (e) { return { isShardable: false, keyShards: [] }; } }; exports.shardKey = shardKey;