Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added chat.decrypt #726

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
22 changes: 22 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,27 @@ 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) => {
// 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) {
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');
});
});
Loading