// Debug and fix Matrix key sharing issues (async () => { console.log('šŸ” MATRIX KEY SHARING DIAGNOSTIC START'); const matrixService = window.matrixService; if (!matrixService || !matrixService.client) { console.log('āŒ Matrix service not available'); return; } const client = matrixService.client; console.log('āœ… Matrix client available'); console.log('šŸ” Crypto module active:', !!client.crypto); if (!client.crypto) { console.log('āŒ Crypto module not active'); return; } // Check device info const deviceId = client.getDeviceId(); const userId = client.getUserId(); console.log('šŸ‘¤ User ID:', userId); console.log('šŸ“± Device ID:', deviceId); // Check if we have our own device keys try { const ownDevice = client.crypto.deviceList.getStoredDevice(userId, deviceId); console.log('šŸ”‘ Own device info:', ownDevice ? 'Found' : 'Missing'); if (ownDevice) { console.log('šŸ”‘ Own device keys:', { verified: ownDevice.isVerified(), blocked: ownDevice.isBlocked(), known: ownDevice.isKnown() }); } } catch (error) { console.log('āŒ Error checking own device:', error.message); } // Check encrypted rooms and their key status const rooms = client.getRooms(); const encryptedRooms = rooms.filter(r => r.hasEncryptionStateEvent && r.hasEncryptionStateEvent()); console.log(`šŸ” Found ${encryptedRooms.length} encrypted rooms`); for (const room of encryptedRooms.slice(0, 3)) { // Check first 3 encrypted rooms console.log(`\nšŸ  Room: ${room.name || room.roomId.substring(0, 20)}`); // Get room members and their devices const members = room.getMembers(); console.log(`šŸ‘„ Members: ${members.length}`); for (const member of members.slice(0, 5)) { // Check first 5 members const memberUserId = member.userId; console.log(` šŸ‘¤ ${memberUserId === userId ? 'YOU' : 'Member'}: ${memberUserId}`); try { // Get devices for this user const devices = client.crypto.deviceList.getStoredDevicesForUser(memberUserId); console.log(` šŸ“± Devices: ${devices ? devices.length : 0}`); if (devices && devices.length > 0) { devices.forEach((device, index) => { console.log(` Device ${index + 1}: ${device.deviceId} (verified: ${device.isVerified()}, blocked: ${device.isBlocked()})`); }); } } catch (error) { console.log(` āŒ Error getting devices for ${memberUserId}:`, error.message); } } // Check if we can encrypt for this room try { const canEncrypt = await client.crypto.isEncryptionEnabledInRoom(room.roomId); console.log(` šŸ” Encryption enabled: ${canEncrypt}`); } catch (error) { console.log(` āŒ Error checking encryption status:`, error.message); } } // Try to request keys for recent encrypted events console.log('\nšŸ”‘ Attempting to request missing keys...'); try { // Force a key request for any pending events if (client.crypto.requestRoomKey) { console.log('šŸ”‘ Requesting room keys...'); // This is a bit advanced, but let's try to trigger key requests } // Try to download keys from server if (client.crypto.downloadKeys) { console.log('šŸ”‘ Downloading keys from server...'); const userIds = [userId]; // Start with just our own keys await client.crypto.downloadKeys(userIds); console.log('āœ… Key download completed'); } } catch (error) { console.log('āŒ Error requesting keys:', error.message); } // Suggest solutions console.log('\nšŸ’” POTENTIAL SOLUTIONS:'); console.log('1. šŸ”„ Try logging out and back in to reset device state'); console.log('2. šŸ”‘ Ask other users to send a new message (this will share new keys)'); console.log('3. šŸ“± Verify your device with another Matrix client'); console.log('4. šŸ—‚ļø Check if you have key backup enabled in other Matrix clients'); // Try to send a message to establish new keys if (encryptedRooms.length > 0) { console.log('\nšŸ”„ Attempting to send a message to establish new keys...'); try { const testRoom = encryptedRooms[0]; await matrixService.sendMessage(testRoom.roomId, 'šŸ”‘ Key establishment test - this should create new encryption keys for this device'); console.log('āœ… Test message sent - this should help establish keys for future messages'); } catch (error) { console.log('āŒ Failed to send test message:', error.message); } } console.log('\nšŸ” MATRIX KEY SHARING DIAGNOSTIC END'); })();