diff --git a/native/backup/use-client-backup.js b/native/backup/use-client-backup.js --- a/native/backup/use-client-backup.js +++ b/native/backup/use-client-backup.js @@ -5,8 +5,10 @@ import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; import { - type SIWEBackupData, + latestBackupInfoResponseValidator, siweBackupDataValidator, + type LatestBackupInfo, + type SIWEBackupData, } from 'lib/types/backup-types.js'; import type { SIWEBackupSecrets } from 'lib/types/siwe-types.js'; import { assertWithValidator } from 'lib/utils/validation-utils.js'; @@ -20,6 +22,7 @@ +uploadBackupProtocol: () => Promise, +restorePasswordUserBackupProtocol: () => Promise, +retrieveLatestSIWEBackupData: () => Promise, + +retrieveLatestBackupInfo: () => Promise, }; async function getBackupSecret(): Promise { @@ -105,11 +108,35 @@ ); }, [currentUserID, currentUserInfo, loggedIn]); - return { - uploadBackupProtocol, - restorePasswordUserBackupProtocol, - retrieveLatestSIWEBackupData, - }; + const retrieveLatestBackupInfo = React.useCallback(async () => { + if (!loggedIn || !currentUserID || !currentUserInfo?.username) { + throw new Error('Attempt to restore backup for not logged in user.'); + } + const userIdentitifer = currentUserInfo?.username; + + const response = + await commCoreModule.retrieveLatestBackupInfo(userIdentitifer); + + return assertWithValidator( + JSON.parse(response), + latestBackupInfoResponseValidator, + ); + }, [currentUserID, currentUserInfo, loggedIn]); + + return React.useMemo( + () => ({ + uploadBackupProtocol, + restorePasswordUserBackupProtocol, + retrieveLatestSIWEBackupData, + retrieveLatestBackupInfo, + }), + [ + restorePasswordUserBackupProtocol, + retrieveLatestBackupInfo, + retrieveLatestSIWEBackupData, + uploadBackupProtocol, + ], + ); } export { getBackupSecret, useClientBackup }; diff --git a/native/profile/backup-menu.react.js b/native/profile/backup-menu.react.js --- a/native/profile/backup-menu.react.js +++ b/native/profile/backup-menu.react.js @@ -39,6 +39,7 @@ uploadBackupProtocol, restorePasswordUserBackupProtocol, retrieveLatestSIWEBackupData, + retrieveLatestBackupInfo, } = useClientBackup(); const uploadBackup = React.useCallback(async () => { @@ -63,6 +64,25 @@ Alert.alert('Restore protocol result', message); }, [restorePasswordUserBackupProtocol]); + const testLatestBackupInfo = React.useCallback(async () => { + let message; + try { + const backupInfo = await retrieveLatestBackupInfo(); + const { backupID, userID } = backupInfo; + message = + `Success!\n` + + `Backup ID: ${backupID},\n` + + `userID: ${userID},\n` + + `userID check: ${currentUserInfo?.id === userID ? 'true' : 'false'}`; + } catch (e) { + message = `Latest backup info error: ${String( + getMessageForException(e), + )}`; + console.error(message); + } + Alert.alert('Latest backup info result', message); + }, [currentUserInfo?.id, retrieveLatestBackupInfo]); + const testRestoreForSIWEUser = React.useCallback(async () => { let message = 'success'; try { @@ -140,6 +160,19 @@ Test backup restore protocol + + + ); }