diff --git a/native/backup/conversion-utils.js b/native/backup/conversion-utils.js --- a/native/backup/conversion-utils.js +++ b/native/backup/conversion-utils.js @@ -16,4 +16,9 @@ return new Uint8Array(objBuffer); } -export { getBackupBytesFromBlob, convertObjToBytes }; +function convertBytesToObj(bytes: Uint8Array): T { + const str = commUtilsModule.decodeUTF8ArrayBufferToString(bytes.buffer); + return JSON.parse(str); +} + +export { getBackupBytesFromBlob, convertObjToBytes, convertBytesToObj }; diff --git a/native/backup/encryption.js b/native/backup/encryption.js --- a/native/backup/encryption.js +++ b/native/backup/encryption.js @@ -1,9 +1,14 @@ // @flow import { hexToUintArray } from 'lib/media/data-utils.js'; -import type { Backup, BackupEncrypted } from 'lib/types/backup-types.js'; +import type { + Backup, + BackupEncrypted, + UserData, + UserKeys, +} from 'lib/types/backup-types.js'; -import { convertObjToBytes } from './conversion-utils.js'; +import { convertBytesToObj, convertObjToBytes } from './conversion-utils.js'; import { fetchNativeKeychainCredentials } from '../account/native-credentials.js'; import { commCoreModule } from '../native-modules.js'; import * as AES from '../utils/aes-crypto-module.js'; @@ -31,4 +36,27 @@ return { backupID, userKeys: ct1, userData: ct2 }; } -export { getBackupKey, encryptBackup }; +async function decryptUserKeys( + backupID: string, + userKeysBytes: ArrayBuffer, +): Promise { + const backupKey = await getBackupKey(backupID); + const decryptedUserKeys = AES.decrypt( + backupKey, + new Uint8Array(userKeysBytes), + ); + return convertBytesToObj(decryptedUserKeys); +} + +async function decryptUserData( + backupDataKey: string, + userDataBytes: ArrayBuffer, +): Promise { + const decryptedUserData = AES.decrypt( + hexToUintArray(backupDataKey), + new Uint8Array(userDataBytes), + ); + return convertBytesToObj(decryptedUserData); +} + +export { getBackupKey, encryptBackup, decryptUserKeys, decryptUserData };