UNPKG

@keypo/typescript-sdk-server

Version:

Server-side SDK for Keypo with custom decrypt and proxy execute implementations

268 lines 9.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.authenticateLitSessionServer = exports.genSessionServer = exports.genAuthSigServer = void 0; const auth_helpers_1 = require("@lit-protocol/auth-helpers"); const constants_1 = require("@lit-protocol/constants"); const lit_node_client_1 = require("@lit-protocol/lit-node-client"); const typescript_sdk_1 = require("@keypo/typescript-sdk"); const getPermissionedFileMetadata_1 = require("./getPermissionedFileMetadata"); const ONE_DAY_FROM_NOW = new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(); const genAuthSigServer = async (wallet, client, uri, resources, expiration = ONE_DAY_FROM_NOW, debug) => { if (debug) { console.log("[DEBUG] genAuthSigServer called with:", { wallet, client, uri, resources, expiration, }); } const blockHash = await client.getLatestBlockhash(); if (debug) { console.log("[DEBUG] genAuthSigServer: blockHash:", blockHash); } const address = wallet.address; if (debug) { console.log("[DEBUG] genAuthSigServer: address:", address); } const message = await (0, auth_helpers_1.createSiweMessageWithRecaps)({ walletAddress: address, nonce: blockHash, litNodeClient: client, resources, expiration: expiration, uri, }); if (debug) { console.log("[DEBUG] genAuthSigServer: constructed message:", message); } const authSig = await (0, auth_helpers_1.generateAuthSig)({ signer: wallet, toSign: message, }); if (debug) { console.log("[DEBUG] genAuthSigServer: generated authSig:", authSig); } return authSig; }; exports.genAuthSigServer = genAuthSigServer; const genSessionServer = async (wallet, client, resources, expiration, chain, authSig, debug) => { if (debug) { console.log("[DEBUG] genSessionServer called with:", { wallet, client, resources, expiration, chain, authSig, }); } // Get the latest blockhash for the authNeededCallback const latestBlockhash = await client.getLatestBlockhash(); if (debug) { console.log("[DEBUG] genSessionServer: latestBlockhash:", latestBlockhash); } // Define the authNeededCallback function following the Lit Protocol server-side pattern const authNeededCallback = async (params) => { if (debug) { console.log("[DEBUG] authNeededCallback called with params:", params); } if (!params.uri) { throw new Error("uri is required"); } if (!params.expiration) { throw new Error("expiration is required"); } if (!params.resourceAbilityRequests) { throw new Error("resourceAbilityRequests is required"); } if (authSig) { if (debug) { console.log("[DEBUG] Returning provided authSig"); } return authSig; } // Create the SIWE message following the Lit Protocol pattern const toSign = await (0, auth_helpers_1.createSiweMessageWithRecaps)({ uri: params.uri, expiration: params.expiration, resources: params.resourceAbilityRequests, walletAddress: wallet.address, nonce: latestBlockhash, litNodeClient: client, }); if (debug) { console.log("[DEBUG] authNeededCallback: constructed message:", toSign); } // Generate the authSig following the Lit Protocol pattern const generatedAuthSig = await (0, auth_helpers_1.generateAuthSig)({ signer: wallet, toSign, }); if (debug) { console.log("[DEBUG] authNeededCallback: generated authSig:", generatedAuthSig); } return generatedAuthSig; }; return client.getSessionSigs({ chain: chain, resourceAbilityRequests: resources, authNeededCallback, }); }; exports.genSessionServer = genSessionServer; const authenticateLitSessionServer = async (wallet, chain, expiration, permissionsRegistryContractAddress, dataIdentifier, apiUrl, debug) => { let walletAddress; try { walletAddress = await wallet.getAddress(); if (debug) { console.log("[DEBUG] authenticateLitSessionServer called with:", { walletAddress, chain, expiration, permissionsRegistryContractAddress, dataIdentifier, }); } } catch (err) { console.error("[DEBUG] Error getting wallet address:", err); throw err; } // Use ethers wallet directly as signer const signer = wallet; let dataMetadata; try { if (debug) console.log("[DEBUG] Fetching permissioned file metadata..."); dataMetadata = await (0, getPermissionedFileMetadata_1.getPermissionedFileMetadata)(dataIdentifier, apiUrl, debug); if (debug) console.log("[DEBUG] dataMetadata (raw):", dataMetadata); if (!dataMetadata) { throw new Error("No data metadata found for the provided smart contract address"); } } catch (err) { console.error("[DEBUG] Error fetching permissioned file metadata:", err); throw err; } let dataMetadataObject; try { dataMetadataObject = JSON.parse(dataMetadata); if (debug) console.log("[DEBUG] dataMetadataObject (parsed):", dataMetadataObject); } catch (err) { console.error("[DEBUG] Error parsing dataMetadata:", err); throw err; } let litNodeClient; try { if (debug) console.log("[DEBUG] Creating LitNodeClient..."); litNodeClient = new lit_node_client_1.LitNodeClient({ litNetwork: constants_1.LIT_NETWORK.DatilDev, debug: false, }); if (debug) console.log("[DEBUG] Connecting LitNodeClient..."); await litNodeClient.connect(); if (debug) console.log("[DEBUG] LitNodeClient connected."); } catch (err) { console.error("[DEBUG] Error creating/connecting LitNodeClient:", err); throw err; } // if dataMetadataObject.proxyMetadata exists, use createEvmBalanceConditions instead of createEvmConditions let conditions = []; try { if (dataMetadataObject.proxyMetadata) { if (debug) console.log("[DEBUG] Using createEvmBalanceConditions..."); conditions = (0, typescript_sdk_1.createEvmBalanceConditions)(chain, dataMetadataObject.proxyMetadata.proxyAddress); } else { if (debug) console.log("[DEBUG] Using createEvmConditions..."); conditions = (0, typescript_sdk_1.createEvmConditions)(chain, permissionsRegistryContractAddress, dataIdentifier); } if (debug) console.log("[DEBUG] conditions:", conditions); } catch (err) { console.error("[DEBUG] Error creating conditions:", err); throw err; } let accsResourceString; try { if (debug) console.log("[DEBUG] Generating resource strings..."); accsResourceString = await auth_helpers_1.LitAccessControlConditionResource.generateResourceString(conditions, dataMetadataObject.encryptedData.dataToEncryptHash); if (debug) console.log("[DEBUG] accsResourceString:", accsResourceString); } catch (err) { console.error("[DEBUG] Error generating resource string:", err); throw err; } let resources; try { resources = [ { resource: new auth_helpers_1.LitActionResource("*"), ability: constants_1.LIT_ABILITY.LitActionExecution, }, { resource: new auth_helpers_1.LitAccessControlConditionResource(accsResourceString), ability: constants_1.LIT_ABILITY.AccessControlConditionDecryption, }, ]; if (debug) console.log("[DEBUG] resources:", resources); } catch (err) { console.error("[DEBUG] Error creating resources array:", err); throw err; } let sessionSigs; try { if (debug) console.log("[DEBUG] Calling genSessionServer..."); sessionSigs = await (0, exports.genSessionServer)(wallet, litNodeClient, resources, expiration, chain, undefined, debug); if (debug) console.log("[DEBUG] sessionSigs:", sessionSigs); } catch (err) { console.error("[DEBUG] Error in genSessionServer:", err); throw err; } let authSig; try { if (debug) console.log("[DEBUG] Calling genAuthSigServer..."); // Only get another authSig if proxyMetadata exists if (dataMetadataObject.proxyMetadata) { authSig = await (0, exports.genAuthSigServer)(wallet, litNodeClient, "https://www.keypo.io", resources, undefined, debug); if (debug) console.log("[DEBUG] authSig:", authSig); } else { authSig = null; } } catch (err) { console.error("[DEBUG] Error in genAuthSigServer:", err); throw err; } return { sessionSigs, authSig, dataToEncryptHash: dataMetadataObject.encryptedData.dataToEncryptHash, evmConditions: conditions, litNodeClient: litNodeClient, dataMetadata: dataMetadataObject, }; }; exports.authenticateLitSessionServer = authenticateLitSessionServer; //# sourceMappingURL=authenticateLitSessionServer.js.map