diff --git a/packages/examples/sdk-backend-node/package.json b/packages/examples/sdk-backend-node/package.json index 97b7d47cb..47b5d3196 100644 --- a/packages/examples/sdk-backend-node/package.json +++ b/packages/examples/sdk-backend-node/package.json @@ -11,7 +11,7 @@ "author": "", "license": "ISC", "dependencies": { - "@pushprotocol/restapi": "^1.4.17", + "@pushprotocol/restapi": "@latest", "@pushprotocol/socket": "^0.5.2" } } diff --git a/packages/examples/sdk-backend-node/pushAPI/index.ts b/packages/examples/sdk-backend-node/pushAPI/index.ts index e14d56659..60fdd8285 100644 --- a/packages/examples/sdk-backend-node/pushAPI/index.ts +++ b/packages/examples/sdk-backend-node/pushAPI/index.ts @@ -9,6 +9,7 @@ import { config } from '../config'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { createWalletClient, http } from 'viem'; import { goerli } from 'viem/chains'; +import { createSocketConnection, EVENTS } from '@pushprotocol/socket'; // CONFIGS const { env, showAPIResponse } = config; @@ -135,6 +136,27 @@ export const runPushAPICases = async (): Promise => { console.log('PushAPI.chat.send | Response - 200 OK\n\n'); // ------------------------------------------------------------------- // ------------------------------------------------------------------- + console.log('PushAPI.chat.decrypt'); + const pushSDKSocket = createSocketConnection({ + user: signerAddress, + socketType: 'chat', + socketOptions: { autoConnect: true, reconnectionAttempts: 3 }, + env: env, + }); + if (pushSDKSocket) { + await userAlice.chat.send(secondSignerAddress, { + content: 'Hello Bob!', + type: 'Text', + }); + pushSDKSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, async (message) => { + // uncomment after latest sdk deployment + // await userAlice.chat.decrypt([message]); + // pushSDKSocket.disconnect(); + }); + } + console.log('PushAPI.chat.decrypt | Response - 200 OK\n\n'); + // ------------------------------------------------------------------- + // ------------------------------------------------------------------- console.log('PushAPI.chat.accept'); const bobAcceptsRequest = await userBob.chat.accept(signerAddress); if (showAPIResponse) { diff --git a/packages/restapi/src/lib/chat/helpers/inbox.ts b/packages/restapi/src/lib/chat/helpers/inbox.ts index b243e2b78..f73f9a735 100644 --- a/packages/restapi/src/lib/chat/helpers/inbox.ts +++ b/packages/restapi/src/lib/chat/helpers/inbox.ts @@ -171,23 +171,31 @@ export const decryptConversation = async (options: DecryptConverationType) => { const message = messages[i]; let gotOtherPeer = false; if (message.encType !== 'PlainText') { - if (!pgpPrivateKey) { - throw Error('Decrypted private key is necessary'); - } - if (message.fromCAIP10 !== connectedUser.wallets.split(',')[0]) { - if (!gotOtherPeer) { - otherPeer = await getUser({ account: message.fromCAIP10, env }); - gotOtherPeer = true; + // check if message is already decrypted + if ( + // legacy messages ( no way to know if they are decrypted or not ) + message.messageObj === undefined || + // new messages ( if messageObj is string then it is not decrypted ) + typeof message.messageObj === 'string' + ) { + if (!pgpPrivateKey) { + throw Error('Decrypted private key is necessary'); + } + if (message.fromCAIP10 !== connectedUser.wallets.split(',')[0]) { + if (!gotOtherPeer) { + otherPeer = await getUser({ account: message.fromCAIP10, env }); + gotOtherPeer = true; + } + signatureValidationPubliKey = otherPeer!.publicKey; + } else { + signatureValidationPubliKey = connectedUser.publicKey; } - signatureValidationPubliKey = otherPeer!.publicKey; - } else { - signatureValidationPubliKey = connectedUser.publicKey; + messages[i] = await decryptAndVerifyMessage( + message, + signatureValidationPubliKey, + pgpPrivateKey + ); } - messages[i] = await decryptAndVerifyMessage( - message, - signatureValidationPubliKey, - pgpPrivateKey - ); } } return messages; diff --git a/packages/restapi/src/lib/pushapi/PushAPI.ts b/packages/restapi/src/lib/pushapi/PushAPI.ts index a41d25b80..610e6158d 100644 --- a/packages/restapi/src/lib/pushapi/PushAPI.ts +++ b/packages/restapi/src/lib/pushapi/PushAPI.ts @@ -9,6 +9,7 @@ import { Message, ProgressHookType, IUser, + IMessageIPFS, } from '../types'; import { GroupUpdateOptions, @@ -259,6 +260,15 @@ export class PushAPI { return await PUSH_CHAT.send(sendParams); }, + decrypt: async (messagePayloads: IMessageIPFS[]) => { + return await PUSH_CHAT.decryptConversation({ + pgpPrivateKey: this.decryptedPgpPvtKey, + env: this.env, + messages: messagePayloads, + connectedUser: await this.info(), + }); + }, + accept: async (target: string): Promise => { return await PUSH_CHAT.approve({ senderAddress: target, diff --git a/packages/restapi/tests/lib/pushapi/chat.test.ts b/packages/restapi/tests/lib/pushapi/chat.test.ts index b7ebbf683..c98e3e0ca 100644 --- a/packages/restapi/tests/lib/pushapi/chat.test.ts +++ b/packages/restapi/tests/lib/pushapi/chat.test.ts @@ -52,4 +52,15 @@ describe('PushAPI.chat functionality', () => { }); expect(response).to.be.an('object'); }); + it('Should decrypt message ', async () => { + await userAlice.chat.send(account2, { + content: 'Hello', + type: MessageType.TEXT, + }); + const messagePayloads = await userAlice.chat.history(account2); + const decryptedMessagePayloads = await userBob.chat.decrypt( + messagePayloads + ); + expect(decryptedMessagePayloads).to.be.an('array'); + }); });