From caaf834f37a403b67ecb9e887f2ee8030f991471 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Sat, 21 Sep 2024 13:25:06 +0400 Subject: [PATCH] Add decrypt and encrypt message to chat service --- src/app/components/chat/chat.service.ts | 57 +++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/app/components/chat/chat.service.ts b/src/app/components/chat/chat.service.ts index 02f73e4..135fa1f 100644 --- a/src/app/components/chat/chat.service.ts +++ b/src/app/components/chat/chat.service.ts @@ -6,7 +6,7 @@ import { Chat, Contact, Profile } from 'app/components/chat/chat.types'; import { IndexedDBService } from 'app/services/indexed-db.service'; import { MetadataService } from 'app/services/metadata.service'; import { SignerService } from 'app/services/signer.service'; -import { Filter, NostrEvent } from 'nostr-tools'; +import { Filter, nip04, NostrEvent } from 'nostr-tools'; import { RelayService } from 'app/services/relay.service'; import { EncryptedDirectMessage } from 'nostr-tools/kinds'; import { getEventHash } from 'nostr-tools'; @@ -304,10 +304,59 @@ export class ChatService implements OnDestroy { } // Decrypt received message - private async decryptReceivedMessage(event: NostrEvent, useExtension: boolean, decryptedSenderPrivateKey: string, otherPartyPubKey: string): Promise { - return 'Decrypted message'; // Implement decryption logic here - } + private async decryptReceivedMessage( + event: NostrEvent, + useExtension: boolean, + decryptedSenderPrivateKey: string, + recipientPublicKey: string + ): Promise { + if (useExtension) { + return await this.decryptMessageWithExtension(event.content, recipientPublicKey); + } else { + return await this.decryptMessage(decryptedSenderPrivateKey, recipientPublicKey, event.content); + } + } + // Messaging (NIP-04) + async decryptMessageWithExtension(encryptedContent: string, senderPubKey: string): Promise { + try { + const gt = globalThis as any; + const decryptedMessage = await gt.nostr.nip04.decrypt(senderPubKey, encryptedContent); + return decryptedMessage; + } catch (error) { + console.error('Error decrypting message with extension:', error); + throw new Error('Failed to decrypt message with Nostr extension.'); + } + } + + + async encryptMessageWithExtension(content: string, pubKey: string): Promise { + const gt = globalThis as any; + const encryptedMessage = await gt.nostr.nip04.encrypt(pubKey, content); + return encryptedMessage; + } + + async encryptMessage(privateKey: string, recipientPublicKey: string, message: string): Promise { + console.log(message); + try { + const encryptedMessage = await nip04.encrypt(privateKey, recipientPublicKey, message); + return encryptedMessage; + } catch (error) { + console.error('Error encrypting message:', error); + throw error; + } + } + + // NIP-04: Decrypting Direct Messages + async decryptMessage(privateKey: string, senderPublicKey: string, encryptedMessage: string): Promise { + try { + const decryptedMessage = await nip04.decrypt(privateKey, senderPublicKey, encryptedMessage); + return decryptedMessage; + } catch (error) { + console.error('Error decrypting message:', error); + throw error; + } + } // Update chat in the chat list updateChat(id: string, chat: Chat): Observable { return this.chats$.pipe(