"use strict";function decryptEncryptionKey(e,r){if(!e||!r)throw(0,error_1.createError)({statusCode:500,message:"Encrypted key or passphrase is missing"});try{const[t,o,n,s]=e.split(":").map(e=>Buffer.from(e,"hex")),c=crypto_1.default.pbkdf2Sync(r,s,1e5,32,"sha512"),i=crypto_1.default.createDecipheriv("aes-256-gcm",c,t);i.setAuthTag(o);let y=i.update(n,void 0,"utf8");y+=i.final("utf8");return y}catch(e){console_1.logger.error("ENCRYPT","Decryption failed",e);throw(0,error_1.createError)({statusCode:500,message:"Decryption failed"})}}async function setEncryptionKey(e){if(!e)throw(0,error_1.createError)({statusCode:500,message:"Passphrase is required to set encryption key"});if(!encryptedKey)throw(0,error_1.createError)({statusCode:500,message:"Encrypted key is required to set encryption key"});try{setDynamicEncryptionKey(decryptEncryptionKey(encryptedKey,e));return!0}catch(e){console_1.logger.error("ENCRYPT","Failed to set the encryption key",e);return!1}}function setDynamicEncryptionKey(e){if(!e)throw(0,error_1.createError)({statusCode:500,message:"Encryption key is required"});dynamicEncryptionKey=Buffer.from(e,"hex")}function encrypt(e){if(!dynamicEncryptionKey)throw(0,error_1.createError)({statusCode:500,message:"Encryption key is not set"});const r=crypto_1.default.randomBytes(12),t=crypto_1.default.createCipheriv("aes-256-gcm",dynamicEncryptionKey,r);let o=t.update(e,"utf8","hex");o+=t.final("hex");const n=t.getAuthTag().toString("hex");return`${r.toString("hex")}:${n}:${o}`}function decrypt(e){if(!dynamicEncryptionKey){console_1.logger.error("ENCRYPT","Encryption key is not set");throw(0,error_1.createError)({statusCode:500,message:"Encryption key is not set"})}try{const r=e.split(":");if(3!==r.length){console_1.logger.error("ENCRYPT",`Invalid encrypted text format, expected 3 parts, got ${r.length}`);throw(0,error_1.createError)({statusCode:400,message:"Invalid encrypted text format"})}const[t,o,n]=r,s=Buffer.from(t,"hex"),c=Buffer.from(o,"hex"),i=crypto_1.default.createDecipheriv("aes-256-gcm",dynamicEncryptionKey,s);i.setAuthTag(c);let y=i.update(n,"hex","utf8");y+=i.final("utf8");return y}catch(e){console_1.logger.error("ENCRYPT","Decryption failed",e);if(e.message.includes("Unsupported state"))throw(0,error_1.createError)({statusCode:500,message:"Invalid encryption data or wrong encryption key"});throw e}}function isUnlockedEcosystemVault(){return!!dynamicEncryptionKey}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.setEncryptionKey=setEncryptionKey;exports.setDynamicEncryptionKey=setDynamicEncryptionKey;exports.encrypt=encrypt;exports.decrypt=decrypt;exports.isUnlockedEcosystemVault=isUnlockedEcosystemVault;const crypto_1=__importDefault(require("crypto")),console_1=require("@b/utils/console"),error_1=require("@b/utils/error");let dynamicEncryptionKey=null;const encryptedKey=process.env.ENCRYPTED_ENCRYPTION_KEY,passphrase=process.env.ENCRYPTION_KEY_PASSPHRASE;encryptedKey&&passphrase&&setEncryptionKey(passphrase);