feat(chat): Enhance message decryption with improved key management and debug logging

- Add recipient public key fetching in messages API endpoint for sent message decryption
- Include `isE2E` flag in message response to indicate end-to-end encryption status
- Simplify encrypted content handling by always returning `encryptedContent` field
- Add comprehensive debug logging throughout message loading pipeline for troubleshooting
- Improve key resolution logic to fetch recipient keys when not available in conversation data
- Add detailed logging for each message processing step including key availability and decryption status
- Enhance error handling with more descriptive error messages in key fetching operations
- Better distinguish between sent and received message decryption requirements with clear comments
This commit is contained in:
Christomatt
2026-01-27 06:18:20 +01:00
parent 863cbc8dce
commit 7d1e22e457
3 changed files with 65 additions and 16 deletions
+15 -1
View File
@@ -172,9 +172,16 @@ export function useChatEncryption() {
senderPublicKey: string
): Promise<string> => {
if (!keys?.privateKey) {
console.error('[Decrypt] No private key available');
throw new Error('No chat keys available');
}
console.log('[Decrypt] Starting decryption', {
hasPrivateKey: !!keys.privateKey,
hasSenderPublicKey: !!senderPublicKey,
encryptedLength: encryptedMessage.length
});
const myPrivateKey = await importPrivateKey(keys.privateKey);
const theirPublicKey = await importPublicKey(senderPublicKey);
const sharedKey = await deriveSharedKey(myPrivateKey, theirPublicKey);
@@ -183,6 +190,11 @@ export function useChatEncryption() {
const iv = combined.slice(0, 12);
const ciphertext = combined.slice(12);
console.log('[Decrypt] Decrypting with shared key', {
ivLength: iv.byteLength,
ciphertextLength: ciphertext.byteLength
});
const decrypted = await window.crypto.subtle.decrypt(
{ name: 'AES-GCM', iv },
sharedKey,
@@ -190,7 +202,9 @@ export function useChatEncryption() {
);
const decoder = new TextDecoder();
return decoder.decode(decrypted);
const result = decoder.decode(decrypted);
console.log('[Decrypt] Success:', result.substring(0, 50));
return result;
}, [keys]);
// Clear keys (on logout)