|
252 | 252 | }; |
253 | 253 |
|
254 | 254 | const ensureDecryption = async () => { |
255 | | - if (!isDecryptionEnabled || !cipherProvider || !account?.address) { |
256 | | - return false; |
257 | | - } |
258 | | -
|
259 | | - try { |
260 | | - // First check if the request is actually encrypted |
261 | | - const encrypted = await isRequestEncrypted(requestId); |
262 | | - if (!encrypted) { |
263 | | - console.log("Request is not encrypted, skipping decryption setup"); |
264 | | - return true; |
265 | | - } |
266 | | -
|
267 | | - console.log("Request is encrypted, setting up decryption"); |
268 | | - const signer = await getEthersSigner(wagmiConfig); |
269 | | - if (!signer) return false; |
270 | | -
|
271 | | - // Check if we have a valid session first |
272 | | - const hasExistingSession = |
273 | | - localStorage.getItem("lit-wallet-sig") === "true"; |
274 | | -
|
275 | | - if (hasExistingSession) { |
276 | | - // Try to use existing session |
277 | | - try { |
278 | | - cipherProvider.enableDecryption(true); |
279 | | - // Test if current session works with a request fetch |
280 | | - const testRequest = await requestNetwork?.fromRequestId(requestId); |
281 | | -
|
282 | | - // Only consider the session valid if we can actually decrypt the request |
283 | | - if (testRequest?.getData()) { |
284 | | - console.log("Using existing Lit Protocol session"); |
285 | | - return true; |
286 | | - } |
287 | | - } catch (error) { |
288 | | - // If we get a decryption error, we need a new session |
289 | | - if ( |
290 | | - String(error).includes("Decryption is not available") || |
291 | | - String(error).includes("Failed to decrypt") || |
292 | | - String(error).includes("LitNodeClient is not ready") |
293 | | - ) { |
294 | | - console.log("Existing session invalid, clearing..."); |
295 | | - localStorage.removeItem("lit-wallet-sig"); |
296 | | - } else { |
297 | | - // If it's some other error, keep the session |
298 | | - console.log("Error fetching request, but keeping session:", error); |
299 | | - return true; |
300 | | - } |
301 | | - } |
302 | | - } |
| 255 | + if (!account?.address || !cipherProvider) return; |
303 | 256 |
|
304 | | - // If we get here, we need a new session |
305 | | - console.log("Getting new Lit Protocol session"); |
306 | | - loadSessionSignatures = true; |
| 257 | + const walletSig = localStorage.getItem("lit-wallet-sig"); |
| 258 | + const sessionKey = localStorage.getItem("lit-session-key"); |
| 259 | + const isEnabled = localStorage.getItem("isDecryptionEnabled") === "true"; |
307 | 260 |
|
| 261 | + if (walletSig && sessionKey && isEnabled) { |
308 | 262 | try { |
309 | | - await cipherProvider.getSessionSignatures( |
310 | | - signer, |
311 | | - account.address, |
312 | | - window.location.host, |
313 | | - "Sign in to Lit Protocol through Request Network" |
314 | | - ); |
315 | | -
|
316 | | - localStorage.setItem("lit-wallet-sig", "true"); |
| 263 | + // Use existing signatures |
317 | 264 | cipherProvider.enableDecryption(true); |
318 | | - return true; |
319 | 265 | } catch (error) { |
320 | | - console.error("Failed to get new session signatures:", error); |
321 | | - return false; |
322 | | - } finally { |
323 | | - loadSessionSignatures = false; |
| 266 | + console.error( |
| 267 | + "Failed to enable decryption with existing signatures:", |
| 268 | + error |
| 269 | + ); |
| 270 | + // Clear invalid signatures |
| 271 | + localStorage.removeItem("lit-wallet-sig"); |
| 272 | + localStorage.removeItem("lit-session-key"); |
| 273 | + localStorage.setItem("isDecryptionEnabled", "false"); |
324 | 274 | } |
325 | | - } catch (error) { |
326 | | - console.error("Failed to ensure decryption:", error); |
327 | | - return false; |
328 | 275 | } |
329 | 276 | }; |
330 | 277 |
|
|
471 | 418 | let unwatchAccount: WatchAccountReturnType | undefined; |
472 | 419 |
|
473 | 420 | const handleWalletConnection = async () => { |
474 | | - account = getAccount(wagmiConfig); |
| 421 | + if (!account?.address) return; |
475 | 422 |
|
476 | | - // Reset decryption state |
477 | | - if (cipherProvider) { |
478 | | - cipherProvider.disconnectWallet(); |
479 | | - localStorage.removeItem("lit-wallet-sig"); |
480 | | - } |
| 423 | + account = getAccount(wagmiConfig); |
481 | 424 |
|
482 | | - // Only attempt decryption setup if needed |
483 | 425 | if (isDecryptionEnabled && requestId) { |
484 | 426 | const isEncrypted = await isRequestEncrypted(requestId); |
485 | 427 | if (isEncrypted) { |
486 | 428 | await ensureDecryption(); |
487 | | - } else { |
488 | | - // For non-encrypted requests, just disable decryption |
489 | | - cipherProvider?.enableDecryption(false); |
490 | 429 | } |
491 | 430 | } |
492 | 431 |
|
|
508 | 447 | if (cipherProvider) { |
509 | 448 | cipherProvider.disconnectWallet(); |
510 | 449 | localStorage.removeItem("lit-wallet-sig"); |
| 450 | + localStorage.removeItem("lit-session-key"); |
| 451 | + localStorage.setItem("isDecryptionEnabled", "false"); |
511 | 452 | } |
512 | 453 | }; |
513 | 454 |
|
|
0 commit comments