diff --git a/native/account/restore-backup-screen.react.js b/native/account/restore-backup-screen.react.js --- a/native/account/restore-backup-screen.react.js +++ b/native/account/restore-backup-screen.react.js @@ -16,7 +16,7 @@ }; export type RestoreBackupScreenParams = { - +username: string, + +userIdentifier: string, +credentials: | { +type: 'password', @@ -24,6 +24,7 @@ } | { +type: 'siwe', + +secret: string, +message: string, +signature: string, }, diff --git a/native/account/restore-password-account-screen.react.js b/native/account/restore-password-account-screen.react.js --- a/native/account/restore-password-account-screen.react.js +++ b/native/account/restore-password-account-screen.react.js @@ -31,7 +31,7 @@ const onProceed = React.useCallback(() => { if (areCredentialsPresent) { props.navigation.navigate(RestoreBackupScreenRouteName, { - username, + userIdentifier: username, credentials: { type: 'password', password, diff --git a/native/account/restore-prompt-screen.react.js b/native/account/restore-prompt-screen.react.js --- a/native/account/restore-prompt-screen.react.js +++ b/native/account/restore-prompt-screen.react.js @@ -3,6 +3,9 @@ import * as React from 'react'; import { Text, View } from 'react-native'; +import type { SIWEResult } from 'lib/types/siwe-types.js'; +import { getMessageForException } from 'lib/utils/errors.js'; + import PromptButton from './prompt-button.react.js'; import RegistrationButtonContainer from './registration/registration-button-container.react.js'; import RegistrationContainer from './registration/registration-container.react.js'; @@ -10,9 +13,15 @@ import type { SignInNavigationProp } from './sign-in-navigator.react'; import { useSIWEPanelState } from './siwe-hooks.js'; import SIWEPanel from './siwe-panel.react.js'; +import { useClientBackup } from '../backup/use-client-backup.js'; import type { NavigationRoute } from '../navigation/route-names'; -import { RestorePasswordAccountScreenRouteName } from '../navigation/route-names.js'; +import { + RestoreSIWEBackupRouteName, + RestorePasswordAccountScreenRouteName, +} from '../navigation/route-names.js'; import { useColors, useStyles } from '../themes/colors.js'; +import { unknownErrorAlertDetails } from '../utils/alert-messages.js'; +import Alert from '../utils/alert.js'; import RestoreIcon from '../vectors/restore-icon.react.js'; type Props = { @@ -31,6 +40,53 @@ props.navigation.navigate(RestorePasswordAccountScreenRouteName); }, [props.navigation]); + const goBack = React.useCallback(() => { + props.navigation.goBack(); + }, [props.navigation]); + + const { retrieveLatestBackupInfo } = useClientBackup(); + const onSIWESuccess = React.useCallback( + async (result: SIWEResult) => { + try { + const { address, signature, message } = result; + const backupInfo = await retrieveLatestBackupInfo(address); + const { siweBackupData } = backupInfo; + + if (!siweBackupData) { + throw new Error('Missing SIWE message for Wallet user backup'); + } + + const { + siweBackupMsgNonce, + siweBackupMsgIssuedAt, + siweBackupMsgStatement, + } = siweBackupData; + + props.navigation.navigate(RestoreSIWEBackupRouteName, { + siweNonce: siweBackupMsgNonce, + siweStatement: siweBackupMsgStatement, + siweIssuedAt: siweBackupMsgIssuedAt, + userIdentifier: address, + signature, + message, + }); + } catch (e) { + const messageForException = getMessageForException(e); + console.log( + `SIWE restore error: ${messageForException ?? 'unknown error'}`, + ); + const alertDetails = unknownErrorAlertDetails; + Alert.alert( + alertDetails.title, + alertDetails.message, + [{ text: 'OK', onPress: goBack }], + { cancelable: false }, + ); + } + }, + [goBack, props.navigation, retrieveLatestBackupInfo], + ); + const { panelState, openPanel, @@ -45,7 +101,7 @@ onClosing={onPanelClosing} onClosed={onPanelClosed} closing={panelState === 'closing'} - onSuccessfulWalletSignature={() => {}} + onSuccessfulWalletSignature={onSIWESuccess} siweSignatureRequestData={siweSignatureRequestData} setLoading={siwePanelSetLoading} /> diff --git a/native/backup/restore-siwe-backup.react.js b/native/backup/restore-siwe-backup.react.js --- a/native/backup/restore-siwe-backup.react.js +++ b/native/backup/restore-siwe-backup.react.js @@ -1,24 +1,23 @@ // @flow import * as React from 'react'; -import { Alert } from 'react-native'; import { type SIWEResult } from 'lib/types/siwe-types.js'; -import { getMessageForException } from 'lib/utils/errors.js'; -import { useClientBackup } from './use-client-backup.js'; import { SignSIWEBackupMessageForRestore } from '../account/registration/siwe-backup-message-creation.react.js'; -import { commCoreModule } from '../native-modules.js'; import { type RootNavigationProp } from '../navigation/root-navigator.react.js'; -import { type NavigationRoute } from '../navigation/route-names.js'; -import { persistConfig } from '../redux/persist.js'; +import { + type NavigationRoute, + RestoreBackupScreenRouteName, +} from '../navigation/route-names.js'; export type RestoreSIWEBackupParams = { - +backupID: string, +siweNonce: string, +siweStatement: string, +siweIssuedAt: string, +userIdentifier: string, + +signature: string, + +message: string, }; type Props = { @@ -31,44 +30,28 @@ const { route } = props; const { params: { - backupID, siweStatement, siweIssuedAt, siweNonce, userIdentifier, + signature, + message, }, } = route; - const { getBackupUserKeys } = useClientBackup(); - const onSuccessfulWalletSignature = React.useCallback( (result: SIWEResult) => { - void (async () => { - const { signature } = result; - let message = 'success'; - try { - const { backupDataKey, backupLogDataKey } = await getBackupUserKeys( - userIdentifier, - signature, - backupID, - ); - await commCoreModule.restoreBackupData( - backupID, - backupDataKey, - backupLogDataKey, - persistConfig.version.toString(), - ); - } catch (e) { - message = `Backup restore error: ${String( - getMessageForException(e), - )}`; - console.error(message); - } - Alert.alert('Restore protocol result', message); - goBack(); - })(); + props.navigation.navigate(RestoreBackupScreenRouteName, { + userIdentifier, + credentials: { + type: 'siwe', + secret: result.signature, + message, + signature, + }, + }); }, - [backupID, getBackupUserKeys, goBack, userIdentifier], + [message, props.navigation, signature, userIdentifier], ); return ( diff --git a/native/profile/backup-menu.react.js b/native/profile/backup-menu.react.js --- a/native/profile/backup-menu.react.js +++ b/native/profile/backup-menu.react.js @@ -229,8 +229,7 @@ const testRestoreForSIWEUser = React.useCallback(async () => { let message = 'success'; try { - const { siweBackupData, backupID } = - await retrieveLatestBackupInfo(userIdentifier); + const { siweBackupData } = await retrieveLatestBackupInfo(userIdentifier); if (!siweBackupData) { throw new Error('Missing SIWE message for Wallet user backup'); @@ -245,11 +244,12 @@ navigation.navigate<'RestoreSIWEBackup'>({ name: RestoreSIWEBackupRouteName, params: { - backupID, siweNonce: siweBackupMsgNonce, siweStatement: siweBackupMsgStatement, siweIssuedAt: siweBackupMsgIssuedAt, userIdentifier, + signature: '', + message: '', }, }); } catch (e) {