1 import type { UseCaseInterface } from '../Domain/UseCase/UseCaseInterface'
2 import type { Result } from '@proton/docs-shared'
3 import type { EncryptionService } from '../Services/Encryption/EncryptionService'
4 import type { EncryptionContext } from '../Services/Encryption/EncryptionContext'
5 import type { DocumentKeys } from '@proton/drive-store'
6 import { GetAssociatedEncryptionDataForRealtimeMessage } from './GetAdditionalEncryptionData'
7 import type { AnonymousEncryptionMetadata, EncryptionMetadata } from '../Types/EncryptionMetadata'
8 import { isPrivateDocumentKeys, type PublicDocumentKeys } from '../Types/DocumentEntitlements'
11 * Encrypts a message directed towards the RTS.
13 export class EncryptMessage implements UseCaseInterface<Uint8Array> {
14 constructor(private encryption: EncryptionService<EncryptionContext.RealtimeMessage>) {}
18 metadata: EncryptionMetadata | AnonymousEncryptionMetadata,
19 keys: DocumentKeys | PublicDocumentKeys,
20 ): Promise<Result<Uint8Array>> {
21 const aad = GetAssociatedEncryptionDataForRealtimeMessage(metadata)
23 if (isPrivateDocumentKeys(keys)) {
24 const result = await this.encryption.signAndEncryptData(
27 keys.documentContentKey,
28 keys.userAddressPrivateKey,
33 const result = await this.encryption.encryptAnonymousData(update, aad, keys.documentContentKey)