diff --git a/lib/types/crypto-types.js b/lib/types/crypto-types.js --- a/lib/types/crypto-types.js +++ b/lib/types/crypto-types.js @@ -121,3 +121,9 @@ PREKEY: 0, TEXT: 1, }); + +export type OlmAPI = { + +initializeCryptoAccount: () => Promise, + +encrypt: (content: string, deviceID: string) => Promise, + +decrypt: (encryptedContent: string, deviceID: string) => Promise, +}; diff --git a/lib/utils/config.js b/lib/utils/config.js --- a/lib/utils/config.js +++ b/lib/utils/config.js @@ -9,6 +9,7 @@ } from '../keyserver-conn/keyserver-conn-types.js'; import type { InitialNotifMessageOptions } from '../shared/crypto-utils.js'; import type { LogInActionSource } from '../types/account-types.js'; +import type { OlmAPI } from '../types/crypto-types.js'; import type { PlatformDetails } from '../types/device-types.js'; export type Config = { @@ -26,6 +27,7 @@ +setSessionIDOnRequest: boolean, +calendarRangeInactivityLimit: ?number, +platformDetails: PlatformDetails, + +olmAPI: OlmAPI, }; let registeredConfig: ?Config = null; diff --git a/native/config.js b/native/config.js --- a/native/config.js +++ b/native/config.js @@ -5,6 +5,7 @@ import { registerConfig } from 'lib/utils/config.js'; import { resolveKeyserverSessionInvalidationUsingNativeCredentials } from './account/legacy-recover-keyserver-session.js'; +import { olmAPI } from './crypto/olm-api.js'; import { persistConfig, codeVersion } from './redux/persist.js'; registerConfig({ @@ -16,4 +17,5 @@ codeVersion, stateVersion: persistConfig.version, }, + olmAPI, }); diff --git a/native/crypto/olm-api.js b/native/crypto/olm-api.js new file mode 100644 --- /dev/null +++ b/native/crypto/olm-api.js @@ -0,0 +1,19 @@ +// @flow + +import type { OlmAPI } from 'lib/types/crypto-types'; + +import { commCoreModule } from '../native-modules.js'; + +const olmAPI: OlmAPI = { + async initializeCryptoAccount(): Promise { + await commCoreModule.initializeCryptoAccount(); + }, + async encrypt(content: string, deviceID: string): Promise { + return await commCoreModule.encrypt(content, deviceID); + }, + async decrypt(encryptedContent: string, deviceID: string): Promise { + return await commCoreModule.decrypt(encryptedContent, deviceID); + }, +}; + +export { olmAPI }; diff --git a/web/app.react.js b/web/app.react.js --- a/web/app.react.js +++ b/web/app.react.js @@ -44,6 +44,7 @@ import { EditModalProvider } from './chat/edit-message-provider.js'; import { MemberListSidebarProvider } from './chat/member-list-sidebar/member-list-sidebar-provider.react.js'; import NavigationArrows from './components/navigation-arrows.react.js'; +import { olmAPI } from './crypto/olm-api.js'; import { initOpaque } from './crypto/opaque-utils.js'; import { getDatabaseModule } from './database/database-module-provider.js'; import electron from './electron.js'; @@ -102,6 +103,7 @@ stateVersion: persistConfig.version, ...desktopDetails, }, + olmAPI, }); const versionBroadcast = new BroadcastChannel('comm_version'); diff --git a/web/crypto/olm-api.js b/web/crypto/olm-api.js new file mode 100644 --- /dev/null +++ b/web/crypto/olm-api.js @@ -0,0 +1,47 @@ +// @flow + +import olm from '@commapp/olm'; +import type { Account, Session } from '@commapp/olm'; + +import { + type OlmAPI, + olmEncryptedMessageTypes, +} from 'lib/types/crypto-types.js'; + +// methods below are just mocks to SQLite API +// implement proper methods tracked in ENG-6462 +// eslint-disable-next-line no-unused-vars +function getOlmAccount(): Account { + return new olm.Account(); +} +// eslint-disable-next-line no-unused-vars +function getOlmSession(deviceID: string): Session { + return new olm.Session(); +} +// eslint-disable-next-line no-unused-vars +function storeOlmAccount(account: Account): void {} +// eslint-disable-next-line no-unused-vars +function storeOlmSession(session: Session): void {} + +const olmAPI: OlmAPI = { + async initializeCryptoAccount(): Promise { + await olm.init(); + }, + async encrypt(content: string, deviceID: string): Promise { + const session = getOlmSession(deviceID); + const { body } = session.encrypt(content); + storeOlmSession(session); + return body; + }, + async decrypt(encryptedContent: string, deviceID: string): Promise { + const session = getOlmSession(deviceID); + const result = session.decrypt( + olmEncryptedMessageTypes.TEXT, + encryptedContent, + ); + storeOlmSession(session); + return result; + }, +}; + +export { olmAPI };