Page MenuHomePhabricator

D9001.id30732.diff
No OneTemporary

D9001.id30732.diff

diff --git a/native/backup/constants.js b/native/backup/constants.js
new file mode 100644
--- /dev/null
+++ b/native/backup/constants.js
@@ -0,0 +1,3 @@
+// @flow
+
+export const BACKUP_ID_LENGTH = 32; //256 bits
diff --git a/native/backup/use-client-backup.js b/native/backup/use-client-backup.js
new file mode 100644
--- /dev/null
+++ b/native/backup/use-client-backup.js
@@ -0,0 +1,69 @@
+// @flow
+
+import * as React from 'react';
+import { useSelector } from 'react-redux';
+
+import { uintArrayToHexString } from 'lib/media/data-utils.js';
+import { isLoggedIn } from 'lib/selectors/user-selectors.js';
+import type { BackupAuth, UserData, UserKeys } from 'lib/types/backup-types.js';
+
+import { uploadBackup } from './api.js';
+import { BACKUP_ID_LENGTH } from './constants.js';
+import { encryptBackup } from './encryption.js';
+import { commCoreModule } from '../native-modules.js';
+import { generateKey } from '../utils/aes-crypto-module.js';
+
+type ClientBackup = {
+ +uploadBackupProtocol: (userData: UserData) => Promise<void>,
+};
+
+function useClientBackup(): ClientBackup {
+ const accessToken = useSelector(state => state.commServicesAccessToken);
+ const currentUserID = useSelector(
+ state => state.currentUserInfo && state.currentUserInfo.id,
+ );
+ const loggedIn = useSelector(isLoggedIn);
+
+ const uploadBackupProtocol = React.useCallback(
+ async (userData: UserData) => {
+ if (!loggedIn || !currentUserID) {
+ throw new Error('Attempt to upload backup for not logged in user.');
+ }
+ console.info('Start uploading backup...');
+
+ const backupDataKey = generateKey();
+ await commCoreModule.initializeCryptoAccount();
+ const {
+ primaryIdentityPublicKeys: { ed25519 },
+ } = await commCoreModule.getUserPublicKey();
+ const userKeys: UserKeys = {
+ backupDataKey: uintArrayToHexString(backupDataKey),
+ ed25519,
+ };
+
+ const backupID = await commCoreModule.generateRandomString(
+ BACKUP_ID_LENGTH,
+ );
+
+ const encryptedBackup = await encryptBackup({
+ backupID,
+ userKeys,
+ userData,
+ });
+
+ const backupAuth: BackupAuth = {
+ userID: currentUserID,
+ accessToken: accessToken ? accessToken : '',
+ deviceID: ed25519,
+ };
+
+ await uploadBackup(encryptedBackup, backupAuth);
+ console.info('Backup uploaded.');
+ },
+ [accessToken, currentUserID, loggedIn],
+ );
+
+ return { uploadBackupProtocol };
+}
+
+export { useClientBackup };

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 7:00 AM (20 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2698269
Default Alt Text
D9001.id30732.diff (2 KB)

Event Timeline