From 8e94216c02afce19095d9bd4c1e0e566f59ad557 Mon Sep 17 00:00:00 2001 From: padreug Date: Sun, 10 Aug 2025 12:11:57 +0200 Subject: [PATCH] feat: Enhance debugging and authentication in Nostr chat - Add detailed console logging in ChatComponent to track message sending attempts, selected peers, and authentication status. - Integrate authentication checks in useNostrChat, ensuring users have valid Nostr keypairs before sending messages. - Implement logic to generate and store new Nostr keys if none are found, improving user experience and key management. --- src/components/nostr/ChatComponent.vue | 13 +++ src/composables/useNostrChat.ts | 111 +++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 6 deletions(-) diff --git a/src/components/nostr/ChatComponent.vue b/src/components/nostr/ChatComponent.vue index b301806..35451b8 100644 --- a/src/components/nostr/ChatComponent.vue +++ b/src/components/nostr/ChatComponent.vue @@ -537,6 +537,19 @@ const sendMessage = async () => { if (!selectedPeer.value || !messageInput.value.trim()) return try { + // Add debugging information + console.log('🔍 ChatComponent: Attempting to send message...') + console.log('🔍 ChatComponent: Selected peer:', selectedPeer.value) + console.log('🔍 ChatComponent: Message content:', messageInput.value) + + // Check authentication status + const keyStatus = nostrChat.getNostrKeyStatus() + console.log('🔍 ChatComponent: Nostr key status:', keyStatus) + + // Check if user is logged in + console.log('🔍 ChatComponent: Is logged in:', nostrChat.isLoggedIn.value) + console.log('🔍 ChatComponent: Current user:', nostrChat.currentUser.value) + await sendNostrMessage(selectedPeer.value.pubkey, messageInput.value) messageInput.value = '' diff --git a/src/composables/useNostrChat.ts b/src/composables/useNostrChat.ts index 6046b9d..d539e02 100644 --- a/src/composables/useNostrChat.ts +++ b/src/composables/useNostrChat.ts @@ -1,10 +1,11 @@ import { ref, computed, readonly } from 'vue' -import { nip04, finalizeEvent, type EventTemplate } from 'nostr-tools' -import { hexToBytes } from '@/lib/utils/crypto' +import { nip04, finalizeEvent, type EventTemplate, generateSecretKey, getPublicKey } from 'nostr-tools' +import { hexToBytes, bytesToHex } from '@/lib/utils/crypto' import { getAuthToken } from '@/lib/config/lnbits' import { config } from '@/lib/config' import { useRelayHub } from './useRelayHub' +import { useAuth } from './useAuth' // Types export interface ChatMessage { @@ -67,9 +68,11 @@ export function useNostrChat() { // Use the centralized relay hub const relayHub = useRelayHub() + // Use the main authentication system + const auth = useAuth() + // State const messages = ref>(new Map()) - const currentUser = ref<{ pubkey: string; prvkey: string } | null>(null) const processedMessageIds = ref(new Set()) const onMessageAdded = ref<((peerPubkey: string) => void) | null>(null) @@ -82,9 +85,97 @@ export function useNostrChat() { // Store peers globally const peers = ref([]) - // Computed - use relay hub's connection status + // Computed - use relay hub's connection status and auth system const isConnected = computed(() => relayHub.isConnected.value) - const isLoggedIn = computed(() => !!currentUser.value) + const isLoggedIn = computed(() => auth.isAuthenticated.value) + + // Get current user from auth system + const currentUser = computed(() => { + const user = auth.currentUser.value + if (!user || !user.pubkey) { + console.log('🔍 useNostrChat: No user or no pubkey available:', user) + return null + } + + console.log('🔍 useNostrChat: User data available:', { + id: user.id, + username: user.username, + pubkey: user.pubkey, + hasExtensions: user.extensions, + extra: user.extra, + fullUser: user + }) + + // Check if we have a stored Nostr keypair for this user + const storageKey = `nostr_keys_${user.id}` + let storedKeys = null + + try { + const stored = localStorage.getItem(storageKey) + if (stored) { + storedKeys = JSON.parse(stored) + console.log('🔍 useNostrChat: Found stored Nostr keys:', storedKeys) + } + } catch (error) { + console.warn('Failed to parse stored Nostr keys:', error) + } + + // If we have stored keys, use them + if (storedKeys && storedKeys.prvkey && storedKeys.pubkey) { + console.log('🔍 useNostrChat: Using stored Nostr keys') + return { + pubkey: storedKeys.pubkey, + prvkey: storedKeys.prvkey + } + } + + // If no stored keys, generate new ones + console.log('🔍 useNostrChat: No stored keys found, generating new Nostr keypair') + try { + const privateKeyBytes = generateSecretKey() + const privateKey = bytesToHex(privateKeyBytes) + const publicKey = getPublicKey(privateKeyBytes) + + // Store the new keys + const newKeys = { pubkey: publicKey, prvkey: privateKey } + localStorage.setItem(storageKey, JSON.stringify(newKeys)) + + console.log('🔍 useNostrChat: Generated and stored new Nostr keypair') + + return newKeys + } catch (error) { + console.error('Failed to generate Nostr keypair:', error) + return null + } + }) + + // Check if user has complete Nostr keypair + const hasNostrKeys = computed(() => { + const user = currentUser.value + return user && user.pubkey && user.prvkey + }) + + // Get Nostr key status for debugging + const getNostrKeyStatus = () => { + const user = auth.currentUser.value + if (!user) { + return { hasUser: false, hasPubkey: false, hasPrvkey: false, message: 'No user logged in' } + } + + if (!user.pubkey) { + return { hasUser: true, hasPubkey: false, hasPrvkey: false, message: 'User logged in but no Nostr public key' } + } + + // Check if we can get the private key from somewhere + // For now, we don't have access to it + return { + hasUser: true, + hasPubkey: true, + hasPrvkey: false, + message: 'User has Nostr public key but private key not available', + pubkey: user.pubkey + } + } // Get unread count for a peer const getUnreadCount = (peerPubkey: string): number => { @@ -587,6 +678,11 @@ export function useNostrChat() { throw new Error('No user logged in - please authenticate first') } + // Check if we have the required Nostr keypair + if (!currentUser.value.prvkey) { + throw new Error('Nostr private key not available. Please ensure your LNBits account has Nostr keys configured.') + } + // Check if we have a pool and are connected if (!relayHub.isConnected.value) { console.warn('Not connected to relays - attempting to connect...') @@ -812,7 +908,10 @@ export function useNostrChat() { // Peer management methods loadPeers, - subscribeToAllPeersForNotifications + subscribeToAllPeersForNotifications, + currentUser, + hasNostrKeys, + getNostrKeyStatus } }