Page MenuHomePhabricator

D9001.id30480.diff
No OneTemporary

D9001.id30480.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,68 @@
+// @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 { 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 = {
+ +uploadBackup: (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 uploadBackup = 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 { uploadBackup };
+}
+
+export { useClientBackup };

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 8:05 AM (14 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2699158
Default Alt Text
D9001.id30480.diff (2 KB)

Event Timeline