diff --git a/lib/shared/crypto-utils.js b/lib/shared/crypto-utils.js --- a/lib/shared/crypto-utils.js +++ b/lib/shared/crypto-utils.js @@ -1,8 +1,62 @@ //@flow -import type { OLMOneTimeKeys } from '../types/crypto-types'; +import * as React from 'react'; + +import { + getOlmSessionInitializationData, + getOlmSessionInitializationDataActionTypes, +} from '../actions/user-actions.js'; +import type { OLMIdentityKeys, OLMOneTimeKeys } from '../types/crypto-types'; +import type { OlmSessionInitializationInfo } from '../types/request-types'; +import { + useServerCall, + useDispatchActionPromise, +} from '../utils/action-utils.js'; +import type { CallServerEndpointOptions } from '../utils/call-server-endpoint.js'; import { values } from '../utils/objects.js'; +function useInitialNotificationsEncryptedMessage( + platformSpecificSessionCreator: ( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, + ) => Promise, +): (callServerEndpointOptions?: ?CallServerEndpointOptions) => Promise { + const callGetOlmSessionInitializationData = useServerCall( + getOlmSessionInitializationData, + ); + const dispatchActionPromise = useDispatchActionPromise(); + + return React.useCallback( + async callServerEndpointOptions => { + const olmSessionDataPromise = callGetOlmSessionInitializationData( + callServerEndpointOptions, + ); + + dispatchActionPromise( + getOlmSessionInitializationDataActionTypes, + olmSessionDataPromise, + ); + + const { signedIdentityKeysBlob, notifInitializationInfo } = + await olmSessionDataPromise; + + const { notificationIdentityPublicKeys } = JSON.parse( + signedIdentityKeysBlob.payload, + ); + + return await platformSpecificSessionCreator( + notificationIdentityPublicKeys, + notifInitializationInfo, + ); + }, + [ + callGetOlmSessionInitializationData, + dispatchActionPromise, + platformSpecificSessionCreator, + ], + ); +} + function getOneTimeKeyValues( oneTimeKeys: OLMOneTimeKeys, ): $ReadOnlyArray { @@ -14,4 +68,8 @@ return getOneTimeKeyValues(oneTimeKeys); } -export { getOneTimeKeyValues, getOneTimeKeyValuesFromBlob }; +export { + getOneTimeKeyValues, + getOneTimeKeyValuesFromBlob, + useInitialNotificationsEncryptedMessage, +}; diff --git a/native/account/log-in-panel.react.js b/native/account/log-in-panel.react.js --- a/native/account/log-in-panel.react.js +++ b/native/account/log-in-panel.react.js @@ -18,6 +18,7 @@ validEmailRegex, oldValidUsernameRegex, } from 'lib/shared/account-utils.js'; +import { useInitialNotificationsEncryptedMessage } from 'lib/shared/crypto-utils.js'; import { type LogInInfo, type LogInExtraInfo, @@ -45,7 +46,7 @@ import { nativeLogInExtraInfoSelector } from '../selectors/account-selectors.js'; import type { KeyPressEvent } from '../types/react-native.js'; import Alert from '../utils/alert.js'; -import { useInitialNotificationsEncryptedMessage } from '../utils/crypto-utils.js'; +import { nativeNotificationsSessionCreator } from '../utils/crypto-utils.js'; import type { StateContainer } from '../utils/state-container.js'; export type LogInState = { @@ -388,7 +389,9 @@ const dispatchActionPromise = useDispatchActionPromise(); const callLogIn = useServerCall(logIn); const getInitialNotificationsEncryptedMessage = - useInitialNotificationsEncryptedMessage(); + useInitialNotificationsEncryptedMessage( + nativeNotificationsSessionCreator, + ); return ( { diff --git a/native/socket.react.js b/native/socket.react.js --- a/native/socket.react.js +++ b/native/socket.react.js @@ -14,6 +14,7 @@ } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; +import { useInitialNotificationsEncryptedMessage } from 'lib/shared/crypto-utils.js'; import Socket, { type BaseSocketProps } from 'lib/socket/socket.react.js'; import { logInActionSources } from 'lib/types/account-types.js'; import { @@ -37,7 +38,7 @@ nativeSessionStateFuncSelector, } from './selectors/socket-selectors.js'; import Alert from './utils/alert.js'; -import { useInitialNotificationsEncryptedMessage } from './utils/crypto-utils.js'; +import { nativeNotificationsSessionCreator } from './utils/crypto-utils.js'; import { decompressMessage } from './utils/decompress.js'; const NativeSocket: React.ComponentType = @@ -65,7 +66,9 @@ const preRequestUserState = useSelector(preRequestUserStateSelector); const getInitialNotificationsEncryptedMessage = - useInitialNotificationsEncryptedMessage(); + useInitialNotificationsEncryptedMessage( + nativeNotificationsSessionCreator, + ); const getClientResponses = useSelector(state => nativeGetClientResponsesSelector({ diff --git a/native/utils/crypto-utils.js b/native/utils/crypto-utils.js --- a/native/utils/crypto-utils.js +++ b/native/utils/crypto-utils.js @@ -1,54 +1,21 @@ // @flow -import * as React from 'react'; - -import { - getOlmSessionInitializationData, - getOlmSessionInitializationDataActionTypes, -} from 'lib/actions/user-actions.js'; -import { - useServerCall, - useDispatchActionPromise, -} from 'lib/utils/action-utils.js'; -import type { CallServerEndpointOptions } from 'lib/utils/call-server-endpoint.js'; +import type { OLMIdentityKeys } from 'lib/types/crypto-types'; +import type { OlmSessionInitializationInfo } from 'lib/types/request-types'; import { commCoreModule } from '../native-modules.js'; -function useInitialNotificationsEncryptedMessage(): ( - callServerEndpointOptions?: ?CallServerEndpointOptions, -) => Promise { - const callGetOlmSessionInitializationData = useServerCall( - getOlmSessionInitializationData, - ); - const dispatchActionPromise = useDispatchActionPromise(); - - return React.useCallback( - async callServerEndpointOptions => { - const olmSessionDataPromise = callGetOlmSessionInitializationData( - callServerEndpointOptions, - ); - - dispatchActionPromise( - getOlmSessionInitializationDataActionTypes, - olmSessionDataPromise, - ); - - const { signedIdentityKeysBlob, notifInitializationInfo } = - await olmSessionDataPromise; - - const { notificationIdentityPublicKeys } = JSON.parse( - signedIdentityKeysBlob.payload, - ); - - const { prekey, prekeySignature, oneTimeKey } = notifInitializationInfo; - return await commCoreModule.initializeNotificationsSession( - JSON.stringify(notificationIdentityPublicKeys), - prekey, - prekeySignature, - oneTimeKey, - ); - }, - [callGetOlmSessionInitializationData, dispatchActionPromise], +function nativeNotificationsSessionCreator( + notificationsIdentityKeys: OLMIdentityKeys, + notificationsInitializationInfo: OlmSessionInitializationInfo, +): Promise { + const { prekey, prekeySignature, oneTimeKey } = + notificationsInitializationInfo; + return commCoreModule.initializeNotificationsSession( + JSON.stringify(notificationsIdentityKeys), + prekey, + prekeySignature, + oneTimeKey, ); } @@ -60,4 +27,4 @@ return ed25519; } -export { useInitialNotificationsEncryptedMessage, getContentSigningKey }; +export { getContentSigningKey, nativeNotificationsSessionCreator };