diff --git a/native/account/fullscreen-siwe-panel.react.js b/native/account/fullscreen-siwe-panel.react.js index f1fce696f..a52807c51 100644 --- a/native/account/fullscreen-siwe-panel.react.js +++ b/native/account/fullscreen-siwe-panel.react.js @@ -1,124 +1,161 @@ // @flow import { useNavigation } from '@react-navigation/native'; import invariant from 'invariant'; import * as React from 'react'; import { ActivityIndicator, View } from 'react-native'; import { setDataLoadedActionType } from 'lib/actions/client-db-store-actions.js'; import type { SIWEResult } from 'lib/types/siwe-types.js'; import { ServerError } from 'lib/utils/errors.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; +import { usingCommServicesAccessToken } from 'lib/utils/services-utils.js'; import { useGetEthereumAccountFromSIWEResult } from './registration/ethereum-utils.js'; import { RegistrationContext } from './registration/registration-context.js'; import { enableNewRegistrationMode } from './registration/registration-types.js'; -import { useLegacySIWEServerCall } from './siwe-hooks.js'; +import { + useLegacySIWEServerCall, + useIdentityWalletLogInCall, + useIdentityWalletRegisterCall, +} from './siwe-hooks.js'; import SIWEPanel from './siwe-panel.react.js'; +import { commRustModule } from '../native-modules.js'; import { AccountDoesNotExistRouteName, RegistrationRouteName, } from '../navigation/route-names.js'; import { UnknownErrorAlertDetails } from '../utils/alert-messages.js'; import Alert from '../utils/alert.js'; type Props = { +goBackToPrompt: () => mixed, +closing: boolean, }; function FullscreenSIWEPanel(props: Props): React.Node { const [loading, setLoading] = React.useState(true); const activity = loading ? : null; const activityContainer = React.useMemo( () => ({ flex: 1, }), [], ); const registrationContext = React.useContext(RegistrationContext); invariant(registrationContext, 'registrationContext should be set'); const { setSkipEthereumLoginOnce } = registrationContext; const getEthereumAccountFromSIWEResult = useGetEthereumAccountFromSIWEResult(); const { navigate } = useNavigation(); const { goBackToPrompt } = props; const onAccountDoesNotExist = React.useCallback( async (result: SIWEResult) => { await getEthereumAccountFromSIWEResult(result); setSkipEthereumLoginOnce(true); goBackToPrompt(); navigate<'Registration'>(RegistrationRouteName, { screen: AccountDoesNotExistRouteName, }); }, [ getEthereumAccountFromSIWEResult, navigate, goBackToPrompt, setSkipEthereumLoginOnce, ], ); const legacySiweServerCall = useLegacySIWEServerCall(); + const identityWalletLogInCall = useIdentityWalletLogInCall(); + const identityWalletRegisterCall = useIdentityWalletRegisterCall(); const successRef = React.useRef(false); const dispatch = useDispatch(); const onSuccess = React.useCallback( async (result: SIWEResult) => { successRef.current = true; - try { - await legacySiweServerCall({ - ...result, - doNotRegister: enableNewRegistrationMode, - }); - } catch (e) { - if ( - e instanceof ServerError && - e.message === 'account_does_not_exist' - ) { - await onAccountDoesNotExist(result); - return; + if (usingCommServicesAccessToken) { + try { + const findUserIDResponse = + await commRustModule.findUserIDForWalletAddress(result.address); + if (JSON.parse(findUserIDResponse).userID) { + await identityWalletLogInCall(result); + } else if (enableNewRegistrationMode) { + await onAccountDoesNotExist(result); + } else { + await identityWalletRegisterCall(result); + } + } catch (e) { + Alert.alert( + UnknownErrorAlertDetails.title, + UnknownErrorAlertDetails.message, + [{ text: 'OK', onPress: goBackToPrompt }], + { cancelable: false }, + ); + throw e; } - Alert.alert( - UnknownErrorAlertDetails.title, - UnknownErrorAlertDetails.message, - [{ text: 'OK', onPress: goBackToPrompt }], - { cancelable: false }, - ); - throw e; + } else { + try { + await legacySiweServerCall({ + ...result, + doNotRegister: enableNewRegistrationMode, + }); + } catch (e) { + if ( + e instanceof ServerError && + e.message === 'account_does_not_exist' + ) { + await onAccountDoesNotExist(result); + return; + } + Alert.alert( + UnknownErrorAlertDetails.title, + UnknownErrorAlertDetails.message, + [{ text: 'OK', onPress: goBackToPrompt }], + { cancelable: false }, + ); + throw e; + } + dispatch({ + type: setDataLoadedActionType, + payload: { + dataLoaded: true, + }, + }); } - dispatch({ - type: setDataLoadedActionType, - payload: { - dataLoaded: true, - }, - }); }, - [legacySiweServerCall, dispatch, goBackToPrompt, onAccountDoesNotExist], + [ + identityWalletLogInCall, + identityWalletRegisterCall, + goBackToPrompt, + dispatch, + legacySiweServerCall, + onAccountDoesNotExist, + ], ); const ifBeforeSuccessGoBackToPrompt = React.useCallback(() => { if (!successRef.current) { goBackToPrompt(); } }, [goBackToPrompt]); const { closing } = props; return ( <> {activity} ); } export default FullscreenSIWEPanel; diff --git a/native/account/siwe-hooks.js b/native/account/siwe-hooks.js index 506d676b3..1878bac33 100644 --- a/native/account/siwe-hooks.js +++ b/native/account/siwe-hooks.js @@ -1,96 +1,135 @@ // @flow import * as React from 'react'; import { siweAuth, siweAuthActionTypes } from 'lib/actions/siwe-actions.js'; +import { + identityLogInActionTypes, + useIdentityWalletLogIn, + identityRegisterActionTypes, + useIdentityWalletRegister, +} from 'lib/actions/user-actions.js'; import { useInitialNotificationsEncryptedMessage } from 'lib/shared/crypto-utils.js'; import type { LogInStartingPayload, LogInExtraInfo, } from 'lib/types/account-types.js'; +import type { SIWEResult } from 'lib/types/siwe-types.js'; import { useLegacyAshoatKeyserverCall } from 'lib/utils/action-utils.js'; import type { CallSingleKeyserverEndpointOptions } from 'lib/utils/call-single-keyserver-endpoint.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import { useSelector } from '../redux/redux-utils.js'; import { nativeLogInExtraInfoSelector } from '../selectors/account-selectors.js'; import { nativeNotificationsSessionCreator } from '../utils/crypto-utils.js'; type SIWEServerCallParams = { +message: string, +signature: string, +doNotRegister?: boolean, ... }; function useLegacySIWEServerCall(): ( SIWEServerCallParams, ?CallSingleKeyserverEndpointOptions, ) => Promise { const siweAuthCall = useLegacyAshoatKeyserverCall(siweAuth); const callSIWE = React.useCallback( ( message: string, signature: string, extraInfo: $ReadOnly<{ ...LogInExtraInfo, +doNotRegister?: boolean }>, callSingleKeyserverEndpointOptions: ?CallSingleKeyserverEndpointOptions, ) => siweAuthCall( { message, signature, ...extraInfo, }, callSingleKeyserverEndpointOptions, ), [siweAuthCall], ); const logInExtraInfo = useSelector(nativeLogInExtraInfoSelector); const getInitialNotificationsEncryptedMessage = useInitialNotificationsEncryptedMessage(nativeNotificationsSessionCreator); const dispatchActionPromise = useDispatchActionPromise(); return React.useCallback( async ( { message, signature, doNotRegister }, callSingleKeyserverEndpointOptions, ) => { const extraInfo = await logInExtraInfo(); const initialNotificationsEncryptedMessage = await getInitialNotificationsEncryptedMessage(ashoatKeyserverID, { callSingleKeyserverEndpointOptions, }); const siwePromise = callSIWE( message, signature, { ...extraInfo, initialNotificationsEncryptedMessage, doNotRegister, }, callSingleKeyserverEndpointOptions, ); void dispatchActionPromise( siweAuthActionTypes, siwePromise, undefined, ({ calendarQuery: extraInfo.calendarQuery }: LogInStartingPayload), ); await siwePromise; }, [ logInExtraInfo, dispatchActionPromise, callSIWE, getInitialNotificationsEncryptedMessage, ], ); } -export { useLegacySIWEServerCall }; +function useIdentityWalletLogInCall(): SIWEResult => Promise { + const identityWalletLogIn = useIdentityWalletLogIn(); + const dispatchActionPromise = useDispatchActionPromise(); + return React.useCallback( + async ({ address, message, signature }) => { + const siwePromise = identityWalletLogIn(address, message, signature); + void dispatchActionPromise(identityLogInActionTypes, siwePromise); + + await siwePromise; + }, + [dispatchActionPromise, identityWalletLogIn], + ); +} + +function useIdentityWalletRegisterCall(): SIWEResult => Promise { + const identityWalletRegister = useIdentityWalletRegister(); + const dispatchActionPromise = useDispatchActionPromise(); + return React.useCallback( + async ({ address, message, signature }) => { + const siwePromise = identityWalletRegister(address, message, signature); + void dispatchActionPromise(identityRegisterActionTypes, siwePromise); + + await siwePromise; + }, + [dispatchActionPromise, identityWalletRegister], + ); +} + +export { + useLegacySIWEServerCall, + useIdentityWalletLogInCall, + useIdentityWalletRegisterCall, +};