diff --git a/native/account/registration/registration-context-provider.react.js b/native/account/registration/registration-context-provider.react.js --- a/native/account/registration/registration-context-provider.react.js +++ b/native/account/registration/registration-context-provider.react.js @@ -2,9 +2,15 @@ import * as React from 'react'; +import { isLoggedIn } from 'lib/selectors/user-selectors.js'; +import type { SIWEBackupSecrets } from 'lib/types/siwe-types'; + import { RegistrationContext } from './registration-context.js'; import { useRegistrationServerCall } from './registration-server-call.js'; import type { CachedUserSelections } from './registration-types.js'; +import { commCoreModule } from '../../native-modules.js'; +import { useSelector } from '../../redux/redux-utils.js'; +import Alert from '../../utils/alert.js'; const emptyObj: CachedUserSelections = Object.freeze({}); @@ -20,6 +26,8 @@ const setSkipEthereumLoginOnce = React.useCallback((skip: boolean) => { baseSetSkipEthereumLoginOnce(skip || undefined); }, []); + const [siweBackupSecrets, setSIWEBackupSecrets] = + React.useState(undefined); const registrationServerCall = useRegistrationServerCall(); const contextValue = React.useMemo( @@ -29,15 +37,54 @@ setCachedSelections, skipEthereumLoginOnce, setSkipEthereumLoginOnce, + siweBackupSecrets, + setSIWEBackupSecrets, }), [ registrationServerCall, cachedSelections, skipEthereumLoginOnce, setSkipEthereumLoginOnce, + siweBackupSecrets, + setSIWEBackupSecrets, ], ); + const loggedIn = useSelector(isLoggedIn); + const persistSIWEBackupSecretsPromise = React.useRef>(null); + + React.useEffect(() => { + if (persistSIWEBackupSecretsPromise.current) { + return; + } + if (!siweBackupSecrets || !loggedIn) { + return; + } + + persistSIWEBackupSecretsPromise.current = void (async () => { + try { + await commCoreModule.setSIWEBackupSecrets(siweBackupSecrets); + } catch (e) { + console.log(e); + if (__DEV__) { + Alert.alert( + 'Failed to persist SIWE backup secrets to SQLite.', + e.message, + ); + } + } finally { + setSIWEBackupSecrets(undefined); + persistSIWEBackupSecretsPromise.current = null; + } + })(); + }, [siweBackupSecrets, loggedIn]); + + React.useEffect(() => { + if (!loggedIn) { + setSIWEBackupSecrets(undefined); + } + }, [loggedIn]); + return ( {props.children} diff --git a/native/account/registration/registration-context.js b/native/account/registration/registration-context.js --- a/native/account/registration/registration-context.js +++ b/native/account/registration/registration-context.js @@ -3,6 +3,7 @@ import * as React from 'react'; import type { SetState } from 'lib/types/hook-types.js'; +import type { SIWEBackupSecrets } from 'lib/types/siwe-types.js'; import type { RegistrationServerCallInput, @@ -18,6 +19,8 @@ // skipping the login flow once, so that the user can back out and change it. +skipEthereumLoginOnce?: ?true, +setSkipEthereumLoginOnce: boolean => void, + +siweBackupSecrets?: ?SIWEBackupSecrets, + +setSIWEBackupSecrets: SetState, }; const RegistrationContext: React.Context = diff --git a/native/account/registration/siwe-backup-message-creation.react.js b/native/account/registration/siwe-backup-message-creation.react.js --- a/native/account/registration/siwe-backup-message-creation.react.js +++ b/native/account/registration/siwe-backup-message-creation.react.js @@ -1,5 +1,6 @@ // @flow +import invariant from 'invariant'; import * as React from 'react'; import { Text } from 'react-native'; @@ -9,6 +10,7 @@ import RegistrationButton from './registration-button.react.js'; import RegistrationContainer from './registration-container.react.js'; import RegistrationContentContainer from './registration-content-container.react.js'; +import { RegistrationContext } from './registration-context.js'; import { type RegistrationNavigationProp } from './registration-navigator.react.js'; import type { CoolOrNerdMode, @@ -68,15 +70,23 @@ [panelState], ); + const registrationContext = React.useContext(RegistrationContext); + invariant(registrationContext, 'registrationContext should be set'); + const { setSIWEBackupSecrets } = registrationContext; + const onSuccessfulWalletSignature = React.useCallback( async (result: SIWEResult) => { - console.log(result); + const { message, signature } = result; + setSIWEBackupSecrets({ + message, + signature, + }); navigate<'RegistrationTerms'>({ name: RegistrationTermsRouteName, params, }); }, - [navigate, params], + [navigate, params, setSIWEBackupSecrets], ); let siwePanel;