|
243 | 243 | }
|
244 | 244 | };
|
245 | 245 |
|
| 246 | + /** |
| 247 | + * Disables decryption capabilities, clears Lit Protocol session data, and resets UI state |
| 248 | + */ |
| 249 | + const resetDecryptionState = () => { |
| 250 | + try { |
| 251 | + localStorage?.removeItem("lit-wallet-sig"); |
| 252 | + localStorage?.removeItem("lit-session-key"); |
| 253 | + localStorage?.setItem("isDecryptionEnabled", "false"); |
| 254 | + sliderValueForDecryption = "off"; |
| 255 | +
|
| 256 | + if (cipherProvider) { |
| 257 | + try { |
| 258 | + cipherProvider.enableDecryption(false); |
| 259 | + } catch (error) { |
| 260 | + console.error("Error while disabling cipher provider decryption:", error); |
| 261 | + // Continue execution even if this fails |
| 262 | + } |
| 263 | + } |
| 264 | + } catch (storageError) { |
| 265 | + console.error("Error while clearing storage:", storageError); |
| 266 | + // We still want to update the UI state even if storage operations fail |
| 267 | + sliderValueForDecryption = "off"; |
| 268 | + } |
| 269 | + }; |
| 270 | +
|
246 | 271 | /**
|
247 | 272 | * Handles wallet disconnection cleanup
|
248 | 273 | * Resets all wallet-related state and clears storage
|
|
255 | 280 | activeRequest = undefined;
|
256 | 281 | initialized = false;
|
257 | 282 |
|
258 |
| - // Clear Lit Protocol session data |
259 |
| - clearLitStorage(); |
| 283 | + // Reset decryption state |
| 284 | + resetDecryptionState(); |
260 | 285 |
|
261 | 286 | if (cipherProvider?.disconnectWallet) {
|
262 |
| - cipherProvider.disconnectWallet(); |
| 287 | + try { |
| 288 | + cipherProvider.disconnectWallet(); |
| 289 | + } catch (error) { |
| 290 | + console.error("Error during cipher provider disconnection:", error); |
| 291 | + } |
263 | 292 | }
|
264 | 293 |
|
265 | 294 | // Reset state
|
|
291 | 320 | litInitializationAttempted = false;
|
292 | 321 | initialized = false;
|
293 | 322 | previousAddress = undefined;
|
294 |
| - clearLitStorage(); |
| 323 | + resetDecryptionState(); |
295 | 324 |
|
296 | 325 | loading = true;
|
297 | 326 | try {
|
|
442 | 471 | /**
|
443 | 472 | * Initializes Lit Protocol session for encrypted requests
|
444 | 473 | * Attempts to restore existing session if available
|
| 474 | + * @returns {Promise<boolean>} Success status of the initialization |
445 | 475 | */
|
446 | 476 | const initializeLitSession = async (
|
447 | 477 | currentAccount: GetAccountReturnType | undefined
|
448 |
| - ) => { |
449 |
| - if (!currentAccount?.address || !cipherProvider || litInitializationAttempted) { |
450 |
| - console.error( |
451 |
| - "Initialization skipped: Missing account, cipherProvider, or already attempted." |
452 |
| - ); |
453 |
| - return; |
| 478 | + ): Promise<boolean> => { |
| 479 | + if (!currentAccount?.address) { |
| 480 | + console.error("Cannot initialize Lit session: Missing account address"); |
| 481 | + return false; |
| 482 | + } |
| 483 | +
|
| 484 | + if (!cipherProvider) { |
| 485 | + console.error("Cannot initialize Lit session: Missing cipher provider"); |
| 486 | + return false; |
| 487 | + } |
| 488 | +
|
| 489 | + if (litInitializationAttempted) { |
| 490 | + console.log("Lit initialization already attempted for this session"); |
| 491 | + return false; |
454 | 492 | }
|
455 | 493 |
|
456 | 494 | litInitializationAttempted = true;
|
457 | 495 |
|
458 | 496 | try {
|
459 | 497 | const storedSig = localStorage?.getItem("lit-wallet-sig");
|
460 | 498 | const sessionKey = localStorage?.getItem("lit-session-key");
|
| 499 | + const isEnabled = localStorage?.getItem("isDecryptionEnabled") === "true"; |
461 | 500 |
|
462 |
| - if (storedSig && sessionKey) { |
463 |
| - const parsedSig = JSON.parse(storedSig); |
| 501 | + // If we have all necessary session data and they match the current account |
| 502 | + if (storedSig && sessionKey && isEnabled) { |
| 503 | + try { |
| 504 | + const parsedSig = JSON.parse(storedSig); |
464 | 505 |
|
465 |
| - if ( |
466 |
| - parsedSig.address?.toLowerCase() === |
467 |
| - currentAccount.address?.toLowerCase() |
468 |
| - ) { |
469 |
| - try { |
| 506 | + if (parsedSig.address?.toLowerCase() === currentAccount.address?.toLowerCase()) { |
470 | 507 | // Use the stored session key and signature to restore the session
|
471 |
| - cipherProvider.enableDecryption(true); |
| 508 | + await cipherProvider.enableDecryption(true); |
472 | 509 | sliderValueForDecryption = "on";
|
| 510 | +
|
| 511 | + // Ensure our UI state reflects the storage state |
473 | 512 | localStorage.setItem("isDecryptionEnabled", "true");
|
474 |
| - await getRequests(currentAccount, requestNetwork); |
| 513 | +
|
| 514 | + console.log("Successfully restored Lit session for account:", parsedSig.address); |
475 | 515 | return true;
|
476 |
| - } catch (error) { |
477 |
| - console.error("Failed to restore Lit session:", error); |
478 |
| - clearLitStorage(); |
| 516 | + } else { |
| 517 | + console.log("Stored signature address doesn't match current account, resetting"); |
| 518 | + resetDecryptionState(); |
479 | 519 | }
|
480 |
| - } else { |
481 |
| - clearLitStorage(); |
| 520 | + } catch (sessionError) { |
| 521 | + console.error("Failed to restore Lit session:", sessionError); |
| 522 | + resetDecryptionState(); |
482 | 523 | }
|
| 524 | + } else { |
| 525 | + console.log("Incomplete session data, cannot restore Lit session"); |
483 | 526 | }
|
484 | 527 | } catch (error) {
|
485 | 528 | console.error("Failed to initialize Lit session:", error);
|
486 |
| - clearLitStorage(); |
| 529 | + resetDecryptionState(); |
487 | 530 | }
|
488 | 531 | return false;
|
489 | 532 | };
|
490 | 533 |
|
491 |
| - const clearLitStorage = () => { |
492 |
| - localStorage?.removeItem("lit-wallet-sig"); |
493 |
| - localStorage?.removeItem("lit-session-key"); |
494 |
| - localStorage?.setItem("isDecryptionEnabled", "false"); |
495 |
| - sliderValueForDecryption = "off"; |
496 |
| - if (cipherProvider) { |
497 |
| - cipherProvider.enableDecryption(false); |
498 |
| - } |
499 |
| - }; |
500 |
| -
|
501 | 534 | const getRequestsQueryKey = (address: string, currentPage: number) => [
|
502 | 535 | "requestsData",
|
503 | 536 | address,
|
|
846 | 879 | const previousNetworks = [...selectedNetworks]; // Store current selection
|
847 | 880 |
|
848 | 881 | try {
|
| 882 | + // Handle decryption state changes based on slider value |
849 | 883 | if (sliderValue === "on") {
|
850 |
| - if (localStorage?.getItem("isDecryptionEnabled") === "false") { |
| 884 | + const currentDecryptionState = localStorage?.getItem("isDecryptionEnabled") === "true"; |
| 885 | +
|
| 886 | + // Only invalidate queries if we're changing the decryption state |
| 887 | + if (!currentDecryptionState) { |
851 | 888 | queryClient.invalidateQueries();
|
852 | 889 | }
|
| 890 | +
|
853 | 891 | try {
|
854 | 892 | const signer = await getEthersSigner(wagmiConfig);
|
855 | 893 | if (signer && currentAccount?.address) {
|
856 |
| - loadSessionSignatures = |
857 |
| - localStorage?.getItem("lit-wallet-sig") === null; |
858 |
| - await cipherProvider?.getSessionSignatures( |
859 |
| - signer, |
860 |
| - currentAccount.address, |
861 |
| - window.location.host, |
862 |
| - "Sign in to Lit Protocol through Request Network" |
863 |
| - ); |
864 |
| - cipherProvider?.enableDecryption(true); |
865 |
| - localStorage?.setItem("isDecryptionEnabled", JSON.stringify(true)); |
| 894 | + // Check if we need to get signatures |
| 895 | + loadSessionSignatures = localStorage?.getItem("lit-wallet-sig") === null; |
| 896 | +
|
| 897 | + if (loadSessionSignatures) { |
| 898 | + await cipherProvider?.getSessionSignatures( |
| 899 | + signer, |
| 900 | + currentAccount.address, |
| 901 | + window.location.host, |
| 902 | + "Sign in to Lit Protocol through Request Network" |
| 903 | + ); |
| 904 | + } |
| 905 | +
|
| 906 | + await cipherProvider?.enableDecryption(true); |
| 907 | + localStorage?.setItem("isDecryptionEnabled", "true"); |
866 | 908 | }
|
867 | 909 | } catch (error) {
|
868 | 910 | console.error("Failed to enable decryption:", error);
|
869 | 911 | toast.error("Failed to enable decryption.");
|
870 |
| - sliderValueForDecryption = "off"; |
| 912 | + resetDecryptionState(); |
871 | 913 | return;
|
872 | 914 | } finally {
|
873 | 915 | loadSessionSignatures = false;
|
874 | 916 | }
|
875 | 917 | } else {
|
876 |
| - if (localStorage?.getItem("isDecryptionEnabled") === "true") { |
| 918 | + const currentDecryptionState = localStorage?.getItem("isDecryptionEnabled") === "true"; |
| 919 | +
|
| 920 | + // Only invalidate queries if we're changing the decryption state |
| 921 | + if (currentDecryptionState) { |
877 | 922 | queryClient.invalidateQueries();
|
878 | 923 | }
|
879 |
| - cipherProvider?.enableDecryption(false); |
880 |
| - localStorage?.setItem("isDecryptionEnabled", JSON.stringify(false)); |
| 924 | +
|
| 925 | + resetDecryptionState(); |
881 | 926 | }
|
882 | 927 |
|
883 | 928 | // Always load requests regardless of decryption state
|
|
0 commit comments