@keypo/typescript-sdk
Version:
A TypeScript SDK for using Keypo
53 lines (52 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = decrypt;
const authenticateLitSession_1 = require("./utils/authenticateLitSession");
const ethersAdapter_1 = require("./utils/ethersAdapter");
async function decrypt(dataIdentifier, wallet, config, debug) {
if (debug) {
console.log("[DEBUG] decrypt() called with:");
console.log(" dataIdentifier:", dataIdentifier);
console.log(" wallet.address:", wallet.account?.address);
console.log(" config:", config);
}
// Convert viem client to ethers signer
const signer = (0, ethersAdapter_1.clientToSigner)(wallet);
// Authenticate lit session with ethers wallet
if (debug) {
console.log("[DEBUG] Calling authenticateLitSession...");
}
const { sessionSigs, dataMetadata } = await (0, authenticateLitSession_1.authenticateLitSession)(signer, config.chain, config.expiration, config.registryContractAddress, dataIdentifier, config.apiUrl, debug);
if (debug) {
console.log("[DEBUG] authenticateLitSession result:", {
sessionSigs,
dataMetadata,
});
console.log("dataMetadata", dataMetadata);
}
// Call the API endpoint to handle decryption
const response = await fetch(`${config.apiUrl}/decryption`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
dataIdentifier,
sessionSigs,
dataMetadata: JSON.stringify(dataMetadata),
})
});
if (!response.ok) {
throw new Error(`Decryption failed: ${response.statusText}`);
}
const result = await response.json();
if (debug) {
console.log("[DEBUG] API response:", result);
}
// Convert the decrypted data from a plain object to a Uint8Array
const decryptedDataArray = new Uint8Array(Object.values(result.decryptedData));
return {
decryptedData: decryptedDataArray,
metadata: result.metadata,
};
}