signal-desktop/ts/signal.ts

490 lines
15 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// The idea with this file is to make it webpackable for the style guide
import type { ReadonlyDeep } from 'type-fest';
import * as Crypto from './Crypto';
import * as Curve from './Curve';
import { start as conversationControllerStart } from './ConversationController';
import * as Groups from './groups';
import OS from './util/os/osMain';
2024-08-08 23:10:25 +00:00
import { isProduction } from './util/version';
import * as RemoteConfig from './RemoteConfig';
2024-08-08 23:10:25 +00:00
import { DataReader, DataWriter } from './sql/Client';
// Components
import { ConfirmationDialog } from './components/ConfirmationDialog';
// State
import { createApp } from './state/roots/createApp';
import { createSafetyNumberViewer } from './state/roots/createSafetyNumberViewer';
// Types
import * as TypesAttachment from './types/Attachment';
import * as VisualAttachment from './types/VisualAttachment';
import * as MessageType from './types/Message2';
import { Address } from './types/Address';
import { QualifiedAddress } from './types/QualifiedAddress';
// Processes / Services
import { initializeGroupCredentialFetcher } from './services/groupCredentialFetcher';
import { initializeNetworkObserver } from './services/networkObserver';
import { initializeUpdateListener } from './services/updateListener';
import { calling } from './services/calling';
import * as storage from './services/storage';
2024-03-15 14:20:33 +00:00
import { backupsService } from './services/backups';
import type { LoggerType } from './types/Logging';
import type {
AttachmentType,
AttachmentWithHydratedData,
2024-07-11 19:44:09 +00:00
AddressableAttachmentType,
LocalAttachmentV2Type,
} from './types/Attachment';
import type { MessageAttributesType, QuotedMessageType } from './model-types.d';
import type { SignalCoreType } from './window.d';
import type {
EmbeddedContactType,
EmbeddedContactWithHydratedAvatar,
} from './types/EmbeddedContact';
import type {
LinkPreviewType,
LinkPreviewWithHydratedData,
} from './types/message/LinkPreviews';
import type { StickerType, StickerWithHydratedData } from './types/Stickers';
2024-07-11 19:44:09 +00:00
type EncryptedReader = (
attachment: Partial<AddressableAttachmentType>
) => Promise<Uint8Array>;
type EncryptedWriter = (data: Uint8Array) => Promise<LocalAttachmentV2Type>;
type MigrationsModuleType = {
attachmentsPath: string;
copyIntoAttachmentsDirectory: (
path: string
) => Promise<{ path: string; size: number }>;
copyIntoTempDirectory: (
path: string
) => Promise<{ path: string; size: number }>;
deleteAttachmentData: (path: string) => Promise<void>;
deleteAvatar: (path: string) => Promise<void>;
2024-08-19 20:05:35 +00:00
deleteDownloadData: (path: string) => Promise<void>;
deleteDraftFile: (path: string) => Promise<void>;
deleteExternalMessageFiles: (
attributes: MessageAttributesType
) => Promise<void>;
deleteSticker: (path: string) => Promise<void>;
deleteTempFile: (path: string) => Promise<void>;
doesAttachmentExist: (path: string) => Promise<boolean>;
getAbsoluteAttachmentPath: (path: string) => string;
getAbsoluteAvatarPath: (src: string) => string;
getAbsoluteBadgeImageFilePath: (path: string) => string;
2024-08-19 20:05:35 +00:00
getAbsoluteDownloadsPath: (path: string) => string;
getAbsoluteDraftPath: (path: string) => string;
getAbsoluteStickerPath: (path: string) => string;
getAbsoluteTempPath: (path: string) => string;
loadAttachmentData: (
2024-07-11 19:44:09 +00:00
attachment: Partial<AttachmentType>
) => Promise<AttachmentWithHydratedData>;
loadContactData: (
contact: ReadonlyArray<ReadonlyDeep<EmbeddedContactType>> | undefined
) => Promise<Array<EmbeddedContactWithHydratedAvatar> | undefined>;
loadMessage: (
message: MessageAttributesType
) => Promise<MessageAttributesType>;
loadPreviewData: (
preview: ReadonlyArray<ReadonlyDeep<LinkPreviewType>> | undefined
) => Promise<Array<LinkPreviewWithHydratedData>>;
loadQuoteData: (
quote: QuotedMessageType | null | undefined
) => Promise<QuotedMessageType | null>;
loadStickerData: (
sticker: StickerType | undefined
) => Promise<StickerWithHydratedData | undefined>;
2024-07-11 19:44:09 +00:00
readAttachmentData: EncryptedReader;
readAvatarData: EncryptedReader;
readDraftData: EncryptedReader;
readStickerData: EncryptedReader;
readTempData: EncryptedReader;
saveAttachmentToDisk: (options: {
data: Uint8Array;
name: string;
}) => Promise<null | { fullPath: string; name: string }>;
processNewAttachment: (attachment: AttachmentType) => Promise<AttachmentType>;
2024-07-11 19:44:09 +00:00
processNewSticker: (stickerData: Uint8Array) => Promise<
LocalAttachmentV2Type & {
width: number;
height: number;
}
>;
processNewEphemeralSticker: (stickerData: Uint8Array) => Promise<
LocalAttachmentV2Type & {
width: number;
height: number;
}
>;
upgradeMessageSchema: (
attributes: MessageAttributesType,
options?: { maxVersion?: number }
) => Promise<MessageAttributesType>;
2024-07-11 19:44:09 +00:00
writeNewAttachmentData: EncryptedWriter;
writeNewDraftData: EncryptedWriter;
writeNewAvatarData: EncryptedWriter;
writeNewStickerData: EncryptedWriter;
writeNewBadgeImageFileData: (data: Uint8Array) => Promise<string>;
2024-07-11 19:44:09 +00:00
writeNewPlaintextTempData: (data: Uint8Array) => Promise<string>;
};
export function initializeMigrations({
getRegionCode,
Attachments,
Type,
VisualType,
logger,
userDataPath,
}: {
getRegionCode: () => string | undefined;
Attachments: AttachmentsModuleType;
Type: typeof TypesAttachment;
VisualType: typeof VisualAttachment;
logger: LoggerType;
userDataPath: string;
}): MigrationsModuleType {
if (!Attachments) {
throw new Error('initializeMigrations: Missing provided attachments!');
}
const {
createAbsolutePathGetter,
2024-07-11 19:44:09 +00:00
createPlaintextReader,
createWriterForNew,
createDoesExist,
getAvatarsPath,
getDraftPath,
2024-08-19 20:05:35 +00:00
getDownloadsPath,
getPath,
getStickersPath,
getBadgesPath,
getTempPath,
2024-07-11 19:44:09 +00:00
readAndDecryptDataFromDisk,
saveAttachmentToDisk,
} = Attachments;
const {
getImageDimensions,
makeImageThumbnail,
makeObjectUrl,
makeVideoScreenshot,
revokeObjectUrl,
} = VisualType;
const attachmentsPath = getPath(userDataPath);
2024-07-11 19:44:09 +00:00
function createEncryptedReader(basePath: string): EncryptedReader {
const fallbackReader = createPlaintextReader(basePath);
const pathGetter = createAbsolutePathGetter(basePath);
return async (
attachment: Partial<AddressableAttachmentType>
): Promise<Uint8Array> => {
// In-memory
if (attachment.data != null) {
return attachment.data;
}
if (attachment.path == null) {
throw new Error('Attachment was not downloaded yet');
}
if (attachment.version !== 2) {
return fallbackReader(attachment.path);
}
if (attachment.localKey == null || attachment.size == null) {
throw new Error('Failed to decrypt v2 attachment');
}
const absolutePath = pathGetter(attachment.path);
return readAndDecryptDataFromDisk({
absolutePath,
keysBase64: attachment.localKey,
size: attachment.size,
});
};
}
function createEncryptedWriterForNew(basePath: string): EncryptedWriter {
const pathGetter = createAbsolutePathGetter(basePath);
return data =>
Attachments.writeNewAttachmentData({
data,
getAbsoluteAttachmentPath: pathGetter,
});
}
const readAttachmentData = createEncryptedReader(attachmentsPath);
const loadAttachmentData = Type.loadData(readAttachmentData);
const loadContactData = MessageType.loadContactData(loadAttachmentData);
const loadPreviewData = MessageType.loadPreviewData(loadAttachmentData);
const loadQuoteData = MessageType.loadQuoteData(loadAttachmentData);
const loadStickerData = MessageType.loadStickerData(loadAttachmentData);
const getAbsoluteAttachmentPath = createAbsolutePathGetter(attachmentsPath);
const deleteOnDisk = Attachments.createDeleter(attachmentsPath);
2024-07-11 19:44:09 +00:00
const writeNewAttachmentData = createEncryptedWriterForNew(attachmentsPath);
const copyIntoAttachmentsDirectory =
Attachments.copyIntoAttachmentsDirectory(attachmentsPath);
const doesAttachmentExist = createDoesExist(attachmentsPath);
const stickersPath = getStickersPath(userDataPath);
const getAbsoluteStickerPath = createAbsolutePathGetter(stickersPath);
2024-07-11 19:44:09 +00:00
const writeNewStickerData = createEncryptedWriterForNew(stickersPath);
const deleteSticker = Attachments.createDeleter(stickersPath);
2024-07-11 19:44:09 +00:00
const readStickerData = createEncryptedReader(stickersPath);
const badgesPath = getBadgesPath(userDataPath);
const getAbsoluteBadgeImageFilePath = createAbsolutePathGetter(badgesPath);
const writeNewBadgeImageFileData = createWriterForNew(badgesPath, '.svg');
const tempPath = getTempPath(userDataPath);
const getAbsoluteTempPath = createAbsolutePathGetter(tempPath);
2024-07-11 19:44:09 +00:00
const writeNewTempData = createEncryptedWriterForNew(tempPath);
const writeNewPlaintextTempData = createWriterForNew(tempPath);
const deleteTempFile = Attachments.createDeleter(tempPath);
2024-07-11 19:44:09 +00:00
const readTempData = createEncryptedReader(tempPath);
const copyIntoTempDirectory =
Attachments.copyIntoAttachmentsDirectory(tempPath);
const draftPath = getDraftPath(userDataPath);
const getAbsoluteDraftPath = createAbsolutePathGetter(draftPath);
2024-07-11 19:44:09 +00:00
const writeNewDraftData = createEncryptedWriterForNew(draftPath);
const deleteDraftFile = Attachments.createDeleter(draftPath);
2024-07-11 19:44:09 +00:00
const readDraftData = createEncryptedReader(draftPath);
2024-08-19 20:05:35 +00:00
const downloadsPath = getDownloadsPath(userDataPath);
const getAbsoluteDownloadsPath = createAbsolutePathGetter(downloadsPath);
const deleteDownloadOnDisk = Attachments.createDeleter(downloadsPath);
const avatarsPath = getAvatarsPath(userDataPath);
2024-07-11 19:44:09 +00:00
const readAvatarData = createEncryptedReader(avatarsPath);
const getAbsoluteAvatarPath = createAbsolutePathGetter(avatarsPath);
2024-07-11 19:44:09 +00:00
const writeNewAvatarData = createEncryptedWriterForNew(avatarsPath);
const deleteAvatar = Attachments.createDeleter(avatarsPath);
return {
attachmentsPath,
copyIntoAttachmentsDirectory,
copyIntoTempDirectory,
deleteAttachmentData: deleteOnDisk,
deleteAvatar,
2024-08-19 20:05:35 +00:00
deleteDownloadData: deleteDownloadOnDisk,
deleteDraftFile,
deleteExternalMessageFiles: MessageType.deleteAllExternalFiles({
2024-08-19 20:05:35 +00:00
deleteAttachmentData: Type.deleteData({
deleteOnDisk,
deleteDownloadOnDisk,
}),
deleteOnDisk,
}),
deleteSticker,
deleteTempFile,
doesAttachmentExist,
getAbsoluteAttachmentPath,
getAbsoluteAvatarPath,
getAbsoluteBadgeImageFilePath,
2024-08-19 20:05:35 +00:00
getAbsoluteDownloadsPath,
getAbsoluteDraftPath,
getAbsoluteStickerPath,
getAbsoluteTempPath,
loadAttachmentData,
loadContactData,
loadMessage: MessageType.createAttachmentLoader(loadAttachmentData),
loadPreviewData,
loadQuoteData,
loadStickerData,
readAttachmentData,
2024-07-11 19:44:09 +00:00
readAvatarData,
readDraftData,
readStickerData,
readTempData,
saveAttachmentToDisk,
processNewAttachment: (attachment: AttachmentType) =>
MessageType.processNewAttachment(attachment, {
writeNewAttachmentData,
makeObjectUrl,
revokeObjectUrl,
getImageDimensions,
makeImageThumbnail,
makeVideoScreenshot,
2024-01-04 19:34:53 +00:00
deleteOnDisk,
logger,
}),
processNewSticker: (stickerData: Uint8Array) =>
2024-07-11 19:44:09 +00:00
MessageType.processNewSticker(stickerData, false, {
writeNewStickerData,
getImageDimensions,
logger,
}),
processNewEphemeralSticker: (stickerData: Uint8Array) =>
2024-07-11 19:44:09 +00:00
MessageType.processNewSticker(stickerData, true, {
writeNewStickerData: writeNewTempData,
getImageDimensions,
logger,
}),
upgradeMessageSchema: (
message: MessageAttributesType,
options: { maxVersion?: number } = {}
) => {
const { maxVersion } = options;
return MessageType.upgradeSchema(message, {
deleteOnDisk,
getImageDimensions,
getRegionCode,
makeImageThumbnail,
makeObjectUrl,
makeVideoScreenshot,
2024-07-11 19:44:09 +00:00
readAttachmentData,
revokeObjectUrl,
writeNewAttachmentData,
writeNewStickerData,
logger,
maxVersion,
});
},
2024-07-11 19:44:09 +00:00
writeNewAttachmentData,
writeNewAvatarData,
writeNewDraftData,
writeNewBadgeImageFileData,
2024-07-11 19:44:09 +00:00
writeNewPlaintextTempData,
writeNewStickerData,
};
}
type StringGetterType = (basePath: string) => string;
type AttachmentsModuleType = {
getAvatarsPath: StringGetterType;
getBadgesPath: StringGetterType;
2024-08-19 20:05:35 +00:00
getDownloadsPath: StringGetterType;
getDraftPath: StringGetterType;
getPath: StringGetterType;
getStickersPath: StringGetterType;
getTempPath: StringGetterType;
getUpdateCachePath: StringGetterType;
createDeleter: (root: string) => (relativePath: string) => Promise<void>;
2024-07-11 19:44:09 +00:00
createPlaintextReader: (
root: string
) => (relativePath: string) => Promise<Uint8Array>;
copyIntoAttachmentsDirectory: (
root: string
) => (sourcePath: string) => Promise<{ path: string; size: number }>;
createWriterForNew: (
root: string,
suffix?: string
) => (bytes: Uint8Array) => Promise<string>;
createAbsolutePathGetter: (
rootPath: string
) => (relativePath: string) => string;
createDoesExist: (root: string) => (relativePath: string) => Promise<boolean>;
saveAttachmentToDisk: ({
data,
name,
}: {
data: Uint8Array;
name: string;
}) => Promise<null | { fullPath: string; name: string }>;
2024-07-11 19:44:09 +00:00
readAndDecryptDataFromDisk: (options: {
absolutePath: string;
keysBase64: string;
size: number;
}) => Promise<Uint8Array>;
writeNewAttachmentData: (options: {
data: Uint8Array;
getAbsoluteAttachmentPath: (relativePath: string) => string;
}) => Promise<LocalAttachmentV2Type>;
};
export const setup = (options: {
Attachments: AttachmentsModuleType;
getRegionCode: () => string | undefined;
logger: LoggerType;
userDataPath: string;
}): SignalCoreType => {
const { Attachments, getRegionCode, logger, userDataPath } = options;
const Migrations = initializeMigrations({
getRegionCode,
Attachments,
Type: TypesAttachment,
VisualType: VisualAttachment,
logger,
userDataPath,
});
const Components = {
ConfirmationDialog,
};
const Roots = {
createApp,
createSafetyNumberViewer,
};
const Services = {
2024-03-15 14:20:33 +00:00
backups: backupsService,
calling,
initializeGroupCredentialFetcher,
initializeNetworkObserver,
initializeUpdateListener,
// Testing
storage,
};
const State = {
Roots,
};
const Types = {
Message: MessageType,
// Mostly for debugging
Address,
QualifiedAddress,
};
return {
Components,
Crypto,
Curve,
// Note: used in test/index.html, and not type-checked!
conversationControllerStart,
Groups,
Migrations,
OS,
RemoteConfig,
Services,
State,
Types,
2024-08-08 23:10:25 +00:00
...(isProduction(window.getVersion())
? {}
: {
DataReader,
DataWriter,
}),
};
};