Added end to end encryption

This commit is contained in:
Aslan 2026-01-13 17:33:23 -05:00
parent 9153ba841d
commit 575e9e2010
131 changed files with 2289 additions and 1670 deletions

View file

@ -4,29 +4,62 @@ import {
updateMessageApi,
removeMessageApi,
} from "../../api/message";
import { MessageActionTypes } from "../../store/message";
import { dispatch } from "../../store/state";
import { deleteMessage, setMessage } from "../../store/message";
import { state } from "../../store/state";
import {
bytesToHex,
decryptData,
encryptData,
generateIv,
hexToBytes,
} from "../crypto";
const fetchMessage = async (id: string) => {
const fetchMessage = async (id: string, communityId: string) => {
const data = await fetchMessageApi({
id: id,
});
dispatch({
type: MessageActionTypes.FETCH_MESSAGE_FINISH,
payload: data,
});
if (typeof data.error === "string") {
return;
}
try {
const decrypted = await decryptMessage(communityId, data.text, data.iv);
if (!decrypted) {
return;
}
setMessage({
...data,
text: decrypted,
});
} catch {}
};
const createMessage = async (text: string, channelId: string) => {
const createMessage = async (
communityId: string,
channelId: string,
text: string,
) => {
const encrypted = await encryptMessage(communityId, text);
if (!encrypted) {
return;
}
const [encryptedMessage, iv] = encrypted;
const data = await createMessageApi({
text: text,
channelId: channelId,
iv: iv,
text: encryptedMessage,
});
dispatch({
type: MessageActionTypes.CREATE_MESSAGE_FINISH,
payload: data,
if (typeof data.error === "string") {
return;
}
setMessage({
...data,
text: text,
});
};
@ -36,10 +69,11 @@ const updateMessage = async (id: string, text: string) => {
text: text,
});
dispatch({
type: MessageActionTypes.UPDATE_MESSAGE_FINISH,
payload: data,
});
if (typeof data.error === "string") {
return;
}
setMessage(data);
};
const removeMessage = async (id: string) => {
@ -47,10 +81,60 @@ const removeMessage = async (id: string) => {
id: id,
});
dispatch({
type: MessageActionTypes.REMOVE_MESSAGE_FINISH,
payload: data,
if (typeof data.error === "string") {
return;
}
deleteMessage();
};
const encryptMessage = async (
communityId: string,
text: string,
): Promise<[string, string] | undefined> => {
const key = state.community.communities[communityId]?.derivedKey;
if (!key) {
return;
}
const iv = generateIv();
const encrypted = await encryptData<string>({
key: key,
iv: iv,
data: text,
});
return [bytesToHex(encrypted), bytesToHex(iv.buffer)];
};
const decryptMessage = async (
communityId: string,
text: string,
iv: string,
): Promise<string | undefined> => {
const key = state.community.communities[communityId]?.derivedKey;
if (!key) {
return;
}
const ivBytes = hexToBytes(iv);
const textBytes = hexToBytes(text);
if (!ivBytes || !textBytes) {
return;
}
return await decryptData({
key: key,
iv: new Uint8Array(ivBytes),
encryptedData: textBytes,
});
};
export { fetchMessage, createMessage, updateMessage, removeMessage };
export {
fetchMessage,
createMessage,
updateMessage,
removeMessage,
encryptMessage,
decryptMessage,
};