signal-desktop/ts/services/backups/validator.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-05-14 17:04:50 +00:00
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as libsignal from '@signalapp/libsignal-client/dist/MessageBackup';
import { strictAssert } from '../../util/assert';
import { toAciObject } from '../../util/ServiceId';
import { isTestOrMockEnvironment } from '../../environment';
2024-09-03 17:18:15 +00:00
import { FileStream } from './util/FileStream';
2024-05-14 17:04:50 +00:00
export async function validateBackup(
filePath: string,
fileSize: number
): Promise<void> {
2024-10-31 17:01:03 +00:00
const accountEntropy = window.storage.get('accountEntropyPool');
strictAssert(accountEntropy, 'Account Entropy Pool not available');
2024-05-14 17:04:50 +00:00
const aci = toAciObject(window.storage.user.getCheckedAci());
2024-10-31 17:01:03 +00:00
const backupKey = new libsignal.MessageBackupKey({
accountEntropy,
aci,
});
2024-05-14 17:04:50 +00:00
const streams = new Array<FileStream>();
let outcome: libsignal.ValidationOutcome;
try {
outcome = await libsignal.validate(
backupKey,
libsignal.Purpose.RemoteBackup,
() => {
const stream = new FileStream(filePath);
streams.push(stream);
return stream;
},
BigInt(fileSize)
);
} finally {
await Promise.all(streams.map(stream => stream.close()));
}
if (isTestOrMockEnvironment()) {
strictAssert(
outcome.ok,
`Backup validation failed: ${outcome.errorMessage}`
);
} else {
strictAssert(outcome.ok, 'Backup validation failed');
}
}