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

SK-246 added encryptionService #131

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"react-scroll-to-bottom": "^4.2.0",
"react-virtualized": "^9.22.5",
"recyclerlistview": "^4.2.0",
"web-vitals": "^2.1.4",
"vodozemac-javascript": "file:vendors/vodozemac"
"vodozemac-javascript": "file:vendors/vodozemac",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-app-rewired start",
Expand Down
10 changes: 2 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ContextMenuHub from "@components/context/ContextMenuHub";
import activityService from "@services/activityService";
import autoLoginService from "@services/autoLoginService.js";
import conversationService from "@services/conversationsService";
import encryptionService from "@services/encryptionService";
import globalConstants from "@helpers/constants";
import messagesService from "@services/messagesService";
import navigateTo from "@utils/navigation/navigate_to";
Expand All @@ -12,9 +13,9 @@ import { Suspense, lazy, useEffect, useRef } from "react";
import { getIsMobileView, setIsMobileView } from "@store/values/IsMobileView";
import { getIsTabletView, setIsTabletView } from "@store/values/IsTabletView";
import { history } from "@helpers/history";
import { setSelectedConversation } from "@store/values/SelectedConversation";
import { selectIsClicked, setClicked } from "@store/values/ContextMenu";
import { setIsTabInFocus } from "@store/values/IsTabInFocus";
import { setSelectedConversation } from "@store/values/SelectedConversation";
import { updateNetworkState } from "@store/values/NetworkState";
import { useDispatch, useSelector } from "react-redux";

Expand All @@ -23,19 +24,12 @@ import SPageLoader from "@skeletons/SPageLoader";

import "@styles/GlobalParam.css";

// import initVodozemac, { Account } from "vodozemac-javascript";

const Main = lazy(() => import("@components/Main"));
const AuthorizationHub = lazy(() =>
import("@components/auth/AuthorizationHub")
);

export default function App() {
// initVodozemac().then((res) => {
// const a = new Account();
// console.log(a);
// });

const dispatch = useDispatch();
history.location = useLocation();
history.navigate = useNavigate();
Expand Down
15 changes: 15 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ class Api {
return this.sendPromise(requestData, resObjKey);
}

async encryptedDeviceCreate(data) {
const requestData = {
request: {
device_register: {
identity_key: data.identity_key,
signed_key: data.signed_key,
one_time_pre_keys: data.one_time_pre_keys,
},
id: getUniqueId("encryptedDeviceCreate"),
},
};
const resObjKey = "success";
return this.sendPromise(requestData, resObjKey);
}

async userEdit(data) {
const requestData = {
request: {
Expand Down
3 changes: 3 additions & 0 deletions src/services/autoLoginService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api from "@api/api";
import encryptionService from "./encryptionService";
import navigateTo from "@utils/navigation/navigate_to";
import showCustomAlert from "@utils/show_alert";
import store from "@store/store";
Expand Down Expand Up @@ -42,6 +43,8 @@ class AutoLoginService {
subscribeForNotifications();
store.dispatch(upsertUser(userData));

await encryptionService.registerDevice();

store.dispatch(setUserIsLoggedIn(true));

const { pathname, hash } = history.location;
Expand Down
38 changes: 38 additions & 0 deletions src/services/encryptionService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import api from "@api/api";
import initVodozemac, { Account } from "vodozemac-javascript";
import { setEncryptedUser } from "@src/store/values/EncryptedUser";

class EncryptionService {
constructor() {
this.vodozemac = initVodozemac();
Oleksandr1414 marked this conversation as resolved.
Show resolved Hide resolved
this.account = null;
}

#getAccount() {
if (this.account) {
return this.account;
}

this.account = new Account();

return this.account;
}

async registerDevice() {
const user = this.#getAccount();
user.generate_one_time_keys(50);

setEncryptedUser(user);

const data = {
identity_key: user.curve25519_key,
signed_key: user.ed25519_key,
one_time_pre_keys: Object.fromEntries(user.one_time_keys.entries()),
};
await api.encryptedDeviceCreate(data);
}
}

const encryptionService = new EncryptionService();

export default encryptionService;
4 changes: 4 additions & 0 deletions src/services/usersService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import DownloadManager from "@src/adapters/downloadManager";
import api from "@api/api";
import encryptionService from "./encryptionService";
import isHeic from "@utils/media/is_heic";
import processFile from "@utils/media/process_file";
import showCustomAlert from "@utils/show_alert";
Expand All @@ -12,6 +13,7 @@ import validateIsEmptyObject from "@validations/validateIsEmtpyObject";
import validateLogin from "@validations/user/validateLogin";
import validatePassword from "@validations/user/validatePassword";
import validatePhone from "@validations/user/validatePhone";
import { setEncryptedUser } from "@src/store/values/EncryptedUser";

class UsersService {
async login(data) {
Expand Down Expand Up @@ -39,6 +41,7 @@ class UsersService {
});
localStorage.setItem("sessionId", userToken);
api.curerntUserId = userData._id;
await encryptionService.registerDevice();

return userData;
}
Expand Down Expand Up @@ -126,6 +129,7 @@ class UsersService {
sub.unsubscribe().then(async () => {
await api.pushSubscriptionDelete();
await api.userLogout();
setEncryptedUser({});
localStorage.removeItem("sessionId");
})
)
Expand Down
4 changes: 4 additions & 0 deletions src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextMenuReducer from "@store/values/ContextMenu";
import conversationsReducer from "@store/values/Conversations";
import currentUserIdReducer from "@store/values/CurrentUserId";
import encryptedUserReducer from "@store/values/EncryptedUser";
import isMobileViewReducer from "@store/values/IsMobileView";
import isTabInFocusReducer from "@store/values/IsTabInFocus";
import isTabletViewReducer from "@store/values/IsTabletView";
Expand All @@ -15,6 +16,7 @@ const appReducer = combineReducers({
contextMenu: contextMenuReducer,
conversations: conversationsReducer,
currentUserId: currentUserIdReducer,
encryptedUser: encryptedUserReducer,
isMobileView: isMobileViewReducer,
isTabInFocus: isTabInFocusReducer,
isTabletView: isTabletViewReducer,
Expand All @@ -33,13 +35,15 @@ const rootReducer = (state, action) => {
isTabInFocus,
networkState,
currentUserId,
encryptedUser,
} = state;
state = {
isMobileView,
isTabletView,
isTabInFocus,
networkState,
currentUserId,
encryptedUser,
};
}

Expand Down
17 changes: 17 additions & 0 deletions src/store/values/EncryptedUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createSlice } from "@reduxjs/toolkit";

export const EncryptedUser = createSlice({
name: "EncryptedUser",
initialState: {
value: {},
},
reducers: {
setEncryptedUser: (state, action) => void (state.value = action.payload),
},
});

export const selectEncryptedUser = (state) => state.EncryptedUser.value;

export const { setEncryptedUser } = EncryptedUser.actions;

export default EncryptedUser.reducer;