diff --git a/src/hooks/useStash.ts b/src/hooks/useStash.ts index 2957a80..4659ca5 100644 --- a/src/hooks/useStash.ts +++ b/src/hooks/useStash.ts @@ -50,11 +50,17 @@ export function useStash() { const queryClient = useQueryClient(); const { mutateAsync: createEvent } = useNostrPublish(); + // Safely check if nip44 is available without throwing (some signers throw + // "Browser extension not available" when the getter is accessed with no login) + const hasNip44 = (() => { + try { return !!user?.signer?.nip44; } catch { return false; } + })(); + // Fetch and decrypt stash const { data: stash, isLoading, error } = useQuery({ queryKey: ['stash', user?.pubkey], queryFn: async ({ signal }): Promise => { - if (!user?.pubkey || !user.signer?.nip44) { + if (!user?.pubkey || !hasNip44) { return { items: [], updatedAt: 0 }; } @@ -76,7 +82,7 @@ export function useStash() { try { // Decrypt the content (encrypted to self) - const decrypted = await user.signer.nip44.decrypt(user.pubkey, event.content); + const decrypted = await user.signer!.nip44!.decrypt(user.pubkey, event.content); const stashData = JSON.parse(decrypted) as Stash; return stashData; } catch (err) { @@ -84,18 +90,18 @@ export function useStash() { return { items: [], updatedAt: 0 }; } }, - enabled: !!user?.pubkey && !!user.signer?.nip44, + enabled: !!user?.pubkey && hasNip44, staleTime: 60000, // 1 minute }); // Save stash (encrypt and publish) const saveStash = useCallback(async (newStash: Stash) => { - if (!user?.pubkey || !user.signer?.nip44) { + if (!user?.pubkey || !hasNip44) { throw new Error('Encryption not available'); } // Encrypt to self - const encrypted = await user.signer.nip44.encrypt(user.pubkey, JSON.stringify(newStash)); + const encrypted = await user.signer!.nip44!.encrypt(user.pubkey, JSON.stringify(newStash)); // Publish as replaceable event await createEvent({