Skip to content

Commit

Permalink
fix: added chat.decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 21, 2023
1 parent 880a181 commit d6398ab
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/examples/sdk-backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "^1.4.17",
"@pushprotocol/restapi": "@latest",
"@pushprotocol/socket": "^0.5.2"
}
}
21 changes: 21 additions & 0 deletions packages/examples/sdk-backend-node/pushAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,6 +136,26 @@ export const runPushAPICases = async (): Promise<void> => {
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) => {
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) {
Expand Down
38 changes: 23 additions & 15 deletions packages/restapi/src/lib/chat/helpers/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Message,
ProgressHookType,
IUser,
IMessageIPFS,
} from '../types';
import {
GroupUpdateOptions,
Expand Down Expand Up @@ -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<string> => {
return await PUSH_CHAT.approve({
senderAddress: target,
Expand Down
11 changes: 11 additions & 0 deletions packages/restapi/tests/lib/pushapi/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit d6398ab

Please sign in to comment.