diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -7,6 +7,8 @@ LogInResult, RegisterResult, RegisterInfo, + SIWEServerCall, + SIWEResult, UpdateUserSettingsRequest, } from '../types/account-types'; import type { GetSessionPublicKeysArgs } from '../types/request-types'; @@ -91,6 +93,31 @@ }; }; +const siweActionTypes = Object.freeze({ + started: 'SIWE_STARTED', + success: 'SIWE_SUCCESS', + failed: 'SIWE_FAILED', +}); +const siwe = ( + callServerEndpoint: CallServerEndpoint, +): ((siweInfo: SIWEServerCall) => Promise) => async siweInfo => { + const response = await callServerEndpoint( + 'siwe', + { + ...siweInfo, + platformDetails: getConfig().platformDetails, + }, + registerCallServerEndpointOptions, + ); + return { + currentUserInfo: response.currentUserInfo, + rawMessageInfos: response.rawMessageInfos, + threadInfos: response.cookieChange.threadInfos, + userInfos: response.cookieChange.userInfos, + calendarQuery: siweInfo.calendarQuery, + }; +}; + function mergeUserInfos(...userInfoArrays: UserInfo[][]): UserInfo[] { const merged = {}; for (const userInfoArray of userInfoArrays) { @@ -238,6 +265,8 @@ searchUsersActionTypes, setUserSettings, setUserSettingsActionTypes, + siwe, + siweActionTypes, updateSubscription, updateSubscriptionActionTypes, }; diff --git a/lib/types/account-types.js b/lib/types/account-types.js --- a/lib/types/account-types.js +++ b/lib/types/account-types.js @@ -140,6 +140,24 @@ +logInActionSource: LogInActionSource, }; +export type SIWERequest = { + +address: string, + +message: string, + +signature: string, +}; + +export type SIWEServerCall = { + ...SIWERequest, + ...LogInExtraInfo, +}; + +export type SIWEResponse = { + ...RegisterResponse, +}; + +export type SIWEResult = { + ...RegisterResult, +}; export type UpdatePasswordRequest = { code: string, password: string, diff --git a/lib/types/endpoints.js b/lib/types/endpoints.js --- a/lib/types/endpoints.js +++ b/lib/types/endpoints.js @@ -24,6 +24,7 @@ CREATE_ACCOUNT: 'create_account', LOG_IN: 'log_in', UPDATE_PASSWORD: 'update_password', + SIWE: 'siwe', }); type SessionChangingEndpoint = $Values; diff --git a/lib/types/redux-types.js b/lib/types/redux-types.js --- a/lib/types/redux-types.js +++ b/lib/types/redux-types.js @@ -7,6 +7,7 @@ LogInResult, RegisterResult, DefaultNotificationPayload, + SIWEResult, } from './account-types'; import type { ActivityUpdateSuccessPayload, @@ -818,6 +819,22 @@ +error: true, +payload: Error, +loadingInfo: LoadingInfo, + } + | { + +type: 'SIWE_STARTED', + +payload?: void, + +loadingInfo: LoadingInfo, + } + | { + +type: 'SIWE_SUCCESS', + +payload: SIWEResult, + +loadingInfo: LoadingInfo, + } + | { + +type: 'SIWE_FAILED', + +error: true, + +payload: Error, + +loadingInfo: LoadingInfo, }; export type ActionPayload = ?(Object | Array<*> | $ReadOnlyArray<*> | string); diff --git a/native/account/siwe-panel.react.js b/native/account/siwe-panel.react.js --- a/native/account/siwe-panel.react.js +++ b/native/account/siwe-panel.react.js @@ -3,11 +3,11 @@ import Animated from 'react-native-reanimated'; import WebView from 'react-native-webview'; -import { registerActionTypes, register } from 'lib/actions/user-actions'; +import { siweActionTypes, siwe } from 'lib/actions/user-actions'; import type { - RegisterInfo, + SIWEServerCall, + SIWEResult, LogInExtraInfo, - RegisterResult, LogInStartingPayload, } from 'lib/types/account-types'; import { @@ -35,23 +35,24 @@ // Redux dispatch functions +dispatchActionPromise: DispatchActionPromise, // async functions that hit server APIs - +registerAction: (registerInfo: RegisterInfo) => Promise, + +siweAction: (siweInfo: SIWEServerCall) => Promise, }; function SIWEPanel({ logInExtraInfo, dispatchActionPromise, - registerAction, + siweAction, }: Props) { const handleSIWE = React.useCallback( - ({ address, signature }) => { + ({ address, message, signature }) => { // this is all mocked from register-panel const extraInfo = logInExtraInfo(); dispatchActionPromise( - registerActionTypes, - registerAction({ - username: address, - password: signature, + siweActionTypes, + siweAction({ + address, + message, + signature, ...extraInfo, }), undefined, @@ -59,16 +60,16 @@ ); setNativeCredentials({ username: address, password: signature }); }, - [logInExtraInfo, dispatchActionPromise, registerAction], + [logInExtraInfo, dispatchActionPromise, siweAction], ); const handleMessage = React.useCallback( event => { const { nativeEvent: { data }, } = event; - const { address, signature } = JSON.parse(data); + const { address, message, signature } = JSON.parse(data); if (address && signature) { - handleSIWE({ address, signature }); + handleSIWE({ address, message, signature }); } }, [handleSIWE], @@ -87,14 +88,14 @@ ); const dispatchActionPromise = useDispatchActionPromise(); - const callRegister = useServerCall(register); + const callSiwe = useServerCall(siwe); return ( ); },