Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3522995
D9001.id30480.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.id30480.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,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
Details
Attached
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)
Attached To
Mode
D9001: [client-backup] implement initial backup protocol
Attached
Detach File
Event Timeline
Log In to Comment