Skip to content

Commit 3b817f0

Browse files
committed
fix: issue with lit-wallet-sig
1 parent bda56f6 commit 3b817f0

File tree

2 files changed

+160
-29
lines changed

2 files changed

+160
-29
lines changed

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

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
{ value: "pending", checked: false },
137137
];
138138
139+
let litSessionInitialized = false;
140+
let initializationAttempted = false;
141+
139142
const handleWalletConnection = async () => {
140143
account = getAccount(wagmiConfig);
141144
await loadRequests(sliderValueForDecryption, account, requestNetwork);
@@ -149,29 +152,36 @@
149152
cipherProvider = undefined;
150153
};
151154
152-
const handleWalletChange = (
155+
const handleWalletChange = async (
153156
account: GetAccountReturnType,
154157
previousAccount: GetAccountReturnType
155158
) => {
156159
if (account?.address !== previousAccount?.address) {
157-
handleWalletDisconnection();
158-
handleWalletConnection();
159-
} else if (account?.address) {
160-
handleWalletConnection();
161-
} else {
162-
handleWalletDisconnection();
160+
clearLitStorage();
161+
litSessionInitialized = false;
162+
initializationAttempted = false;
163+
if (account?.address) {
164+
await initializeLitSession(account);
165+
}
163166
}
164167
};
165168
166-
onMount(() => {
169+
onMount(async () => {
170+
try {
171+
currencyManager = await initializeCurrencyManager();
172+
if (account?.address) {
173+
await initializeLitSession(account);
174+
}
175+
} catch (error) {
176+
console.error("Failed to initialize:", error);
177+
}
178+
167179
unwatchAccount = watchAccount(wagmiConfig, {
168180
onChange(
169181
account: GetAccountReturnType,
170182
previousAccount: GetAccountReturnType
171183
) {
172-
tick().then(() => {
173-
handleWalletChange(account, previousAccount);
174-
});
184+
handleWalletChange(account, previousAccount);
175185
},
176186
});
177187
});
@@ -190,9 +200,57 @@
190200
191201
$: isRequestPayed, getOneRequest(activeRequest);
192202
193-
onMount(async () => {
194-
currencyManager = await initializeCurrencyManager();
195-
});
203+
const initializeLitSession = async (
204+
currentAccount: GetAccountReturnType | undefined
205+
) => {
206+
if (!currentAccount?.address || !cipherProvider || initializationAttempted)
207+
return;
208+
209+
initializationAttempted = true;
210+
211+
try {
212+
const storedSig = localStorage?.getItem("lit-wallet-sig");
213+
const sessionKey = localStorage?.getItem("lit-session-key");
214+
215+
if (storedSig && sessionKey) {
216+
const parsedSig = JSON.parse(storedSig);
217+
218+
if (
219+
parsedSig.address?.toLowerCase() ===
220+
currentAccount.address?.toLowerCase()
221+
) {
222+
try {
223+
// Use the stored session key and signature to restore the session
224+
await cipherProvider.enableDecryption(true);
225+
sliderValueForDecryption = "on";
226+
litSessionInitialized = true;
227+
localStorage.setItem("isDecryptionEnabled", "true");
228+
await getRequests(currentAccount, requestNetwork);
229+
return true;
230+
} catch (error) {
231+
console.error("Failed to restore Lit session:", error);
232+
clearLitStorage();
233+
}
234+
} else {
235+
clearLitStorage();
236+
}
237+
}
238+
} catch (error) {
239+
console.error("Failed to initialize Lit session:", error);
240+
clearLitStorage();
241+
}
242+
return false;
243+
};
244+
245+
const clearLitStorage = () => {
246+
localStorage?.removeItem("lit-wallet-sig");
247+
localStorage?.removeItem("lit-session-key");
248+
localStorage?.setItem("isDecryptionEnabled", "false");
249+
sliderValueForDecryption = "off";
250+
if (cipherProvider) {
251+
cipherProvider.enableDecryption(false);
252+
}
253+
};
196254
197255
const getRequestsQueryKey = (address: string, currentPage: number) => [
198256
"requestsData",
@@ -508,7 +566,7 @@
508566
return;
509567
510568
loading = true;
511-
const previousNetworks = [...selectedNetworks];
569+
const previousNetworks = [...selectedNetworks]; // Store current selection
512570
513571
try {
514572
if (sliderValue === "on") {
@@ -520,24 +578,12 @@
520578
if (signer && currentAccount?.address) {
521579
loadSessionSignatures =
522580
localStorage?.getItem("lit-wallet-sig") === null;
523-
const signatures = await cipherProvider?.getSessionSignatures(
581+
await cipherProvider?.getSessionSignatures(
524582
signer,
525583
currentAccount.address,
526584
window.location.host,
527585
"Sign in to Lit Protocol through Request Network"
528586
);
529-
530-
// Save both signatures
531-
localStorage?.setItem(
532-
"lit-wallet-sig",
533-
JSON.stringify({
534-
address: currentAccount.address,
535-
timestamp: Date.now(),
536-
sig: signatures.walletSig,
537-
})
538-
);
539-
localStorage?.setItem("lit-session-key", signatures.sessionKey);
540-
541587
cipherProvider?.enableDecryption(true);
542588
localStorage?.setItem("isDecryptionEnabled", JSON.stringify(true));
543589
}

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
localStorage?.getItem("isDecryptionEnabled") ?? "false"
125125
);
126126
127+
let litSessionInitialized = false;
128+
let initializationAttempted = false;
129+
127130
$: cipherProvider = requestNetwork?.getCipherProvider() as CipherProvider;
128131
129132
$: if (request && address) {
@@ -207,8 +210,78 @@
207210
unknownCurrency = currency ? currency.decimals === undefined : false;
208211
}
209212
213+
const initializeLitSession = async (
214+
currentAccount: GetAccountReturnType | undefined
215+
) => {
216+
if (
217+
!currentAccount?.address ||
218+
!cipherProvider ||
219+
initializationAttempted
220+
) {
221+
console.error(
222+
"Initialization skipped: Missing account, cipherProvider, or already attempted."
223+
);
224+
return;
225+
}
226+
227+
initializationAttempted = true;
228+
229+
try {
230+
const storedSig = localStorage?.getItem("lit-wallet-sig");
231+
const sessionKey = localStorage?.getItem("lit-session-key");
232+
233+
if (storedSig && sessionKey) {
234+
const parsedSig = JSON.parse(storedSig);
235+
236+
if (
237+
parsedSig.address?.toLowerCase() ===
238+
currentAccount.address?.toLowerCase()
239+
) {
240+
try {
241+
// Use the stored session key and signature to restore the session
242+
await cipherProvider.enableDecryption(true);
243+
litSessionInitialized = true;
244+
localStorage.setItem("isDecryptionEnabled", "true");
245+
console.log("Lit session restored successfully.");
246+
return true;
247+
} catch (error) {
248+
console.error("Failed to restore Lit session:", error);
249+
clearLitStorage();
250+
}
251+
} else {
252+
console.warn("Stored signature does not match current account.");
253+
clearLitStorage();
254+
}
255+
} else {
256+
console.warn("No stored session data found.");
257+
}
258+
} catch (error) {
259+
console.error("Failed to initialize Lit session:", error);
260+
clearLitStorage();
261+
}
262+
return false;
263+
};
264+
265+
const clearLitStorage = () => {
266+
localStorage?.removeItem("lit-wallet-sig");
267+
localStorage?.removeItem("lit-session-key");
268+
localStorage.setItem("isDecryptionEnabled", "false");
269+
if (cipherProvider) {
270+
cipherProvider.enableDecryption(false);
271+
}
272+
console.log("Cleared Lit session storage.");
273+
};
274+
210275
onMount(async () => {
211276
currencyManager = await initializeCurrencyManager();
277+
if (account?.address) {
278+
// Attempt to use existing session from the dashboard
279+
const sessionRestored = await initializeLitSession(account);
280+
if (!sessionRestored) {
281+
// If no session is restored, initialize a new session
282+
await initializeLitSession(account);
283+
}
284+
}
212285
});
213286
214287
onMount(() => {
@@ -252,16 +325,21 @@
252325
};
253326
254327
const ensureDecryption = async () => {
255-
if (!account?.address || !cipherProvider) return;
328+
if (!account?.address || !cipherProvider) return false;
256329
257330
const walletSig = localStorage.getItem("lit-wallet-sig");
258331
const sessionKey = localStorage.getItem("lit-session-key");
259332
const isEnabled = localStorage.getItem("isDecryptionEnabled") === "true";
260333
334+
console.log("walletSig", walletSig);
335+
console.log("sessionKey", sessionKey);
336+
console.log("isEnabled", isEnabled);
337+
261338
if (walletSig && sessionKey && isEnabled) {
262339
try {
263340
// Use existing signatures
264341
cipherProvider.enableDecryption(true);
342+
return true;
265343
} catch (error) {
266344
console.error(
267345
"Failed to enable decryption with existing signatures:",
@@ -271,8 +349,10 @@
271349
localStorage.removeItem("lit-wallet-sig");
272350
localStorage.removeItem("lit-session-key");
273351
localStorage.setItem("isDecryptionEnabled", "false");
352+
return false;
274353
}
275354
}
355+
return false;
276356
};
277357
278358
const getOneRequest = async (requestId: string) => {
@@ -281,10 +361,14 @@
281361
unsupportedNetwork = false;
282362
283363
// Only attempt decryption setup if needed
364+
console.log("isDecryptionEnabled", isDecryptionEnabled);
284365
if (isDecryptionEnabled) {
285366
const encrypted = await isRequestEncrypted(requestId);
367+
console.log("encrypted", encrypted);
286368
if (encrypted) {
369+
console.log("Ensuring decryption...");
287370
const decryptionReady = await ensureDecryption();
371+
console.log("decryptionReady", decryptionReady);
288372
if (!decryptionReady) {
289373
throw new Error("Failed to initialize decryption");
290374
}
@@ -295,6 +379,7 @@
295379
}
296380
297381
const singleRequest = await requestNetwork?.fromRequestId(requestId);
382+
console.log("singleRequest", singleRequest);
298383
if (!singleRequest) {
299384
console.log("No request found");
300385
return;

0 commit comments

Comments
 (0)