UNPKG

@keypo/typescript-sdk

Version:

A TypeScript SDK for using Keypo

50 lines (49 loc) 1.91 kB
import { authenticateLitSession } from './utils/authenticateLitSession.js'; import { clientToSigner } from './utils/ethersAdapter.js'; export 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 = clientToSigner(wallet); // Authenticate lit session with ethers wallet if (debug) { console.log("[DEBUG] Calling authenticateLitSession..."); } const { sessionSigs, dataMetadata } = await 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, }; }