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: use the decryptMessage function to get the original text #633

Merged
merged 2 commits into from
Aug 11, 2024
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
21 changes: 10 additions & 11 deletions client/src/components/Chat/DropDownOption.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import React from 'react';
import PropTypes from 'prop-types';
import Dropdown from 'rsuite/Dropdown';

import { BiDotsVerticalRounded } from 'react-icons/bi';

import Dropdown from 'rsuite/Dropdown';
import PropTypes from 'prop-types';
import React from 'react';
import chatHelper from 'src/lib/chatHelper';

import { useChat } from 'src/context/ChatContext';
import { useApp } from 'src/context/AppContext';
import { socket } from 'src/lib/socketConnection';

import { useApp } from 'src/context/AppContext';
import { useChat } from 'src/context/ChatContext';
import useChatUtils from 'src/lib/chatSocket';
import useCryptoKeys from 'src/hooks/useCryptoKeys';

const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setReplyId }) => {
const { app } = useApp();

const { importedPrivateKey, cryptoKey } = useCryptoKeys(app.currentChatId)
const { messages: state, updateMessage, removeMessage } = useChat();
const { getMessage, messageExists, handleCopyToClipBoard } = chatHelper(state, app);
const { deleteMessage } = useChatUtils(socket);
Expand Down Expand Up @@ -87,7 +85,8 @@ const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setRe
>
<Dropdown.Item onClick={() => handleEdit(id)}>Edit</Dropdown.Item>

<Dropdown.Item onClick={() => handleCopyToClipBoard(id, state, app)}>Copy</Dropdown.Item>
<Dropdown.Item onClick={() =>
handleCopyToClipBoard(id, importedPrivateKey)}>Copy</Dropdown.Item>
<Dropdown.Item onClick={() => setReplyId(id)}>Reply</Dropdown.Item>
<Dropdown.Item onClick={() => handleDelete(id)}>Delete</Dropdown.Item>
</Dropdown>
Expand All @@ -102,7 +101,7 @@ const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setRe
renderToggle={renderIconButtonReceiver}
NoCaret
>
<Dropdown.Item onClick={() => handleCopyToClipBoard(id, state, app)}>Copy</Dropdown.Item>
<Dropdown.Item onClick={() => handleCopyToClipBoard(id, cryptoKey)}>Copy</Dropdown.Item>
<Dropdown.Item onClick={() => setReplyId(id)}>Reply</Dropdown.Item>
</Dropdown>
);
Expand Down
15 changes: 9 additions & 6 deletions client/src/lib/chatHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import decryptMessage from './decryptMessage';

export default (state, app) => {
const getMessage = (id) => {
if (!state[app.currentChatId]) {
Expand Down Expand Up @@ -27,15 +29,16 @@ export default (state, app) => {
});
};

const handleCopyToClipBoard = async (id) => {
const handleCopyToClipBoard = async (id, key) => {
const { message } = getMessage(id, state, app);
const decryptedMessage = await decryptMessage(message, key);
if (message.includes('Warning Message')) {
return;
}
if ('clipboard' in navigator) {
return await navigator.clipboard.writeText(message);
return await navigator.clipboard.writeText(decryptedMessage);
} else {
return document.execCommand('copy', true, message);
return document.execCommand('copy', true, decryptedMessage);
}
};

Expand Down Expand Up @@ -104,7 +107,7 @@ export const isGreaterThan3Minutes = (interval, time) => {
return false;
};

// As we cant store data in array form directly in database
// As we cant store data in array form directly in database
// we need to convert it into string which is Base64 of Unit8Array
export const arrayBufferToBase64 = (arrayBuffer) => {
let binary = '';
Expand All @@ -116,15 +119,15 @@ export const arrayBufferToBase64 = (arrayBuffer) => {
return btoa(binary);
};

// This function is used keys which are in ArrayBuffer form to PEM
// This function is used keys which are in ArrayBuffer form to PEM
// because we cant sent the keys through socket.io in ArrayBuffer form
export const convertArrayBufferToPem = (arrayBuffer, type) => {
const buffer = new Uint8Array(arrayBuffer);
const base64String = btoa(String.fromCharCode.apply(null, buffer));
return `-----BEGIN ${type}-----\n${base64String}\n-----END ${type}-----`;
};

// This function does opposite of above function.
// This function does opposite of above function.
// After receiving keys from socket.io in PEM string we need to apply below function.
export const pemToArrayBuffer = (pemString) => {
const base64String =
Expand Down
Loading