Skip to content

Commit 2f9011c

Browse files
committed
fix: updated lit signature logic
1 parent 022f883 commit 2f9011c

File tree

3 files changed

+67
-99
lines changed

3 files changed

+67
-99
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/invoice-dashboard/src/lib/view-requests.svelte

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,30 @@
194194
currencyManager = await initializeCurrencyManager();
195195
});
196196
197-
const getRequestsQueryKey = (address: string, currentPage: number) => ["requestsData", address, currentPage];
197+
const getRequestsQueryKey = (address: string, currentPage: number) => [
198+
"requestsData",
199+
address,
200+
currentPage,
201+
];
198202
199-
const fetchRequests = async (address: string, page: number, pageSize: number) => {
203+
const fetchRequests = async (
204+
address: string,
205+
page: number,
206+
pageSize: number
207+
) => {
200208
if (!address || !requestNetwork) return null;
201209
try {
202-
const requestsData = await requestNetwork.fromIdentity({
203-
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
204-
value: address,
205-
}, undefined, {
206-
page: page,
207-
pageSize: pageSize,
208-
});
210+
const requestsData = await requestNetwork.fromIdentity(
211+
{
212+
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
213+
value: address,
214+
},
215+
undefined,
216+
{
217+
page: page,
218+
pageSize: pageSize,
219+
}
220+
);
209221
return requestsData;
210222
} catch (error) {
211223
console.error("Failed to fetch requests:", error);
@@ -222,12 +234,14 @@
222234
try {
223235
const data = await queryClient.fetchQuery({
224236
queryKey: getRequestsQueryKey(account.address, currentPage),
225-
queryFn: () => fetchRequests(account.address, currentPage, itemsPerPage)
237+
queryFn: () =>
238+
fetchRequests(account.address, currentPage, itemsPerPage),
226239
});
227240
228241
if (data) {
229-
requests = data.requests?.map((request) => request.getData())
230-
.sort((a, b) => b.timestamp - a.timestamp);
242+
requests = data.requests
243+
?.map((request) => request.getData())
244+
.sort((a, b) => b.timestamp - a.timestamp);
231245
hasMoreRequests = data?.meta?.pagination?.hasMore || false;
232246
} else {
233247
requests = [];
@@ -237,7 +251,8 @@
237251
if (hasMoreRequests) {
238252
queryClient.prefetchQuery({
239253
queryKey: getRequestsQueryKey(account.address, currentPage + 1),
240-
queryFn: () => fetchRequests(account.address, currentPage + 1, itemsPerPage)
254+
queryFn: () =>
255+
fetchRequests(account.address, currentPage + 1, itemsPerPage),
241256
});
242257
}
243258
@@ -492,24 +507,36 @@
492507
return;
493508
494509
loading = true;
495-
const previousNetworks = [...selectedNetworks]; // Store current selection
510+
const previousNetworks = [...selectedNetworks];
496511
497512
try {
498513
if (sliderValue === "on") {
499514
if (localStorage?.getItem("isDecryptionEnabled") === "false") {
500-
queryClient.invalidateQueries()
501-
}
515+
queryClient.invalidateQueries();
516+
}
502517
try {
503518
const signer = await getEthersSigner(wagmiConfig);
504519
if (signer && currentAccount?.address) {
505520
loadSessionSignatures =
506521
localStorage?.getItem("lit-wallet-sig") === null;
507-
await cipherProvider?.getSessionSignatures(
522+
const signatures = await cipherProvider?.getSessionSignatures(
508523
signer,
509524
currentAccount.address,
510525
window.location.host,
511526
"Sign in to Lit Protocol through Request Network"
512527
);
528+
529+
// Save both signatures
530+
localStorage?.setItem(
531+
"lit-wallet-sig",
532+
JSON.stringify({
533+
address: currentAccount.address,
534+
timestamp: Date.now(),
535+
sig: signatures.walletSig,
536+
})
537+
);
538+
localStorage?.setItem("lit-session-key", signatures.sessionKey);
539+
513540
cipherProvider?.enableDecryption(true);
514541
localStorage?.setItem("isDecryptionEnabled", JSON.stringify(true));
515542
}
@@ -522,7 +549,7 @@
522549
}
523550
} else {
524551
if (localStorage?.getItem("isDecryptionEnabled") === "true") {
525-
queryClient.invalidateQueries()
552+
queryClient.invalidateQueries();
526553
}
527554
cipherProvider?.enableDecryption(false);
528555
localStorage?.setItem("isDecryptionEnabled", JSON.stringify(false));

packages/single-invoice/src/lib/single-invoice.svelte

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -252,79 +252,26 @@
252252
};
253253
254254
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;
303256
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";
307260
261+
if (walletSig && sessionKey && isEnabled) {
308262
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
317264
cipherProvider.enableDecryption(true);
318-
return true;
319265
} 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");
324274
}
325-
} catch (error) {
326-
console.error("Failed to ensure decryption:", error);
327-
return false;
328275
}
329276
};
330277
@@ -471,22 +418,14 @@
471418
let unwatchAccount: WatchAccountReturnType | undefined;
472419
473420
const handleWalletConnection = async () => {
474-
account = getAccount(wagmiConfig);
421+
if (!account?.address) return;
475422
476-
// Reset decryption state
477-
if (cipherProvider) {
478-
cipherProvider.disconnectWallet();
479-
localStorage.removeItem("lit-wallet-sig");
480-
}
423+
account = getAccount(wagmiConfig);
481424
482-
// Only attempt decryption setup if needed
483425
if (isDecryptionEnabled && requestId) {
484426
const isEncrypted = await isRequestEncrypted(requestId);
485427
if (isEncrypted) {
486428
await ensureDecryption();
487-
} else {
488-
// For non-encrypted requests, just disable decryption
489-
cipherProvider?.enableDecryption(false);
490429
}
491430
}
492431
@@ -508,6 +447,8 @@
508447
if (cipherProvider) {
509448
cipherProvider.disconnectWallet();
510449
localStorage.removeItem("lit-wallet-sig");
450+
localStorage.removeItem("lit-session-key");
451+
localStorage.setItem("isDecryptionEnabled", "false");
511452
}
512453
};
513454

0 commit comments

Comments
 (0)