Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3522544
D9001.id30732.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D9001.id30732.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D9001: [client-backup] implement initial backup protocol
Attached
Detach File
Event Timeline
Log In to Comment