diff --git a/native/backup/api.js b/native/backup/api.js --- a/native/backup/api.js +++ b/native/backup/api.js @@ -7,6 +7,7 @@ import { makeBackupServiceEndpointURL } from 'lib/utils/backup-service.js'; import { toBase64URL } from 'lib/utils/base64.js'; +import { getBackupBytesFromBlob } from './conversion-utils.js'; import { commUtilsModule } from '../native-modules.js'; function getBackupFormData(backup: BackupEncrypted): FormData { @@ -72,4 +73,30 @@ return backupID; } -export { uploadBackup, getBackupID }; +async function getUserKeys( + backupID: string, + auth: BackupAuth, +): Promise { + const authHeader = getBackupAuthorizationHeader(auth); + + const getUserKeysEndpoint = backupService.httpEndpoints.GET_USER_KEYS; + const getUserKeysResponse = await fetch( + makeBackupServiceEndpointURL(getUserKeysEndpoint, { backupID }), + { + method: getUserKeysEndpoint.method, + headers: { + Authorization: authHeader, + }, + }, + ); + + if (!getUserKeysResponse.ok) { + const { status, statusText } = getUserKeysResponse; + throw new Error(`Server responded with HTTP ${status}: ${statusText}`); + } + + const blob = await getUserKeysResponse.blob(); + return getBackupBytesFromBlob(blob); +} + +export { uploadBackup, getBackupID, getUserKeys }; diff --git a/native/backup/conversion-utils.js b/native/backup/conversion-utils.js new file mode 100644 --- /dev/null +++ b/native/backup/conversion-utils.js @@ -0,0 +1,13 @@ +// @flow + +import { commUtilsModule } from '../native-modules.js'; +import { arrayBufferFromBlob } from '../utils/blob-utils-module.js'; + +function getBackupBytesFromBlob(blob: Blob): Uint8Array { + const buffer = arrayBufferFromBlob(blob); + const str = commUtilsModule.decodeUTF8ArrayBufferToString(buffer); + const decodedBuffer = commUtilsModule.base64DecodeBuffer(str); + return new Uint8Array(decodedBuffer); +} + +export { getBackupBytesFromBlob };