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 = { @@ -27,6 +28,7 @@ +calendarRangeInactivityLimit: ?number, +platformDetails: PlatformDetails, +authoritativeKeyserverID: string, + +olmAPI: OlmAPI, }; let registeredConfig: ?Config = null; diff --git a/native/config.js b/native/config.js --- a/native/config.js +++ b/native/config.js @@ -6,6 +6,7 @@ import { resolveKeyserverSessionInvalidationUsingNativeCredentials } from './account/legacy-recover-keyserver-session.js'; import { authoritativeKeyserverID } from './authoritative-keyserver.js'; +import { olmAPI } from './crypto/olm-api.js'; import { persistConfig, codeVersion } from './redux/persist.js'; registerConfig({ @@ -18,4 +19,5 @@ stateVersion: persistConfig.version, }, authoritativeKeyserverID, + 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,15 @@ +// @flow + +import type { OlmAPI } from 'lib/types/crypto-types'; + +import { commCoreModule } from '../native-modules.js'; + +const olmAPI: OlmAPI = { + async initializeCryptoAccount(): Promise { + await commCoreModule.initializeCryptoAccount(); + }, + encrypt: commCoreModule.encrypt, + decrypt: commCoreModule.decrypt, +}; + +export { olmAPI }; diff --git a/web/app.react.js b/web/app.react.js --- a/web/app.react.js +++ b/web/app.react.js @@ -45,6 +45,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'; @@ -104,6 +105,7 @@ ...desktopDetails, }, authoritativeKeyserverID, + 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 };