From f293b203b7436abd764830c7037cfd65a90f238c Mon Sep 17 00:00:00 2001 From: Kirill Ageychenko Date: Sat, 5 Oct 2024 14:48:08 +0700 Subject: [PATCH] feat: reset wallet when keychain broken (#2121) Co-authored-by: iGroza --- src/contexts/app.ts | 55 ++++--------------------------- src/event-actions/on-app-reset.ts | 2 ++ 2 files changed, 8 insertions(+), 49 deletions(-) diff --git a/src/contexts/app.ts b/src/contexts/app.ts index e101fc2a9..1e2ad06a5 100644 --- a/src/contexts/app.ts +++ b/src/contexts/app.ts @@ -43,7 +43,7 @@ import {EventTracker} from '@app/services/event-tracker'; import {HapticEffects, vibrate} from '@app/services/haptic'; import {RemoteConfig} from '@app/services/remote-config'; -import {hideAll, showModal} from '../helpers'; +import {showModal} from '../helpers'; import {User} from '../models/user'; import { AppTheme, @@ -409,7 +409,7 @@ class App extends AsyncEventEmitter { return possibleToRestoreWallet; }; - async getPassword(pinCandidate?: string): Promise { + async getPassword(): Promise { const creds = await getGenericPassword(); // Detect keychain migration @@ -425,52 +425,9 @@ class App extends AsyncEventEmitter { !passwordEntity?.iv || !passwordEntity?.salt ) { - Logger.error('iOS Keychain Migration Error Found:', creds); - const walletToCheck = this.getWalletForPinRestore(); - // Save old biometry - const biomentryMigrationKey = 'biometry_before_migration'; - if (!VariablesBool.exists(biomentryMigrationKey)) { - VariablesBool.set(biomentryMigrationKey, this.biometry); - } - this.biometry = false; - - // Reset app if we have main Hardware Wallet - if (!walletToCheck) { - this.onboarded = false; - await onAppReset(); - hideAll(); - Alert.alert(getText(I18N.keychainMigrationNotPossible)); - return Promise.reject('password_migration_not_possible'); - } - - if (!pinCandidate) { - return Promise.reject('password_not_migrated'); - } - - // Try to verify old pin - try { - const isValid = await SecurePinUtils.checkPinCorrect( - walletToCheck, - pinCandidate, - ); - if (isValid) { - creds.password = await this.setPin(pinCandidate); - const uid = await getUid(); - const resp = await decryptPassworder<{password: string}>( - uid, - creds.password, - ); - - this.biometry = VariablesBool.get(biomentryMigrationKey); - VariablesBool.remove(biomentryMigrationKey); - return resp.password; - } else { - app.failureEnter(); - return Promise.reject('password_migration_not_matched'); - } - } catch (err) { - Logger.error('iOS Keychain Migration Error:', err); - } + Alert.alert(getText(I18N.keychainMigrationNotPossible)); + await onAppReset(); + return Promise.reject('password_migration_not_possible'); } } @@ -510,7 +467,7 @@ class App extends AsyncEventEmitter { const passwordsDoNotMatch = new Error('password_do_not_match'); if (this.canEnter) { try { - const password = await this.getPassword(pin); + const password = await this.getPassword(); return password === pin ? Promise.resolve() : Promise.reject(passwordsDoNotMatch); diff --git a/src/event-actions/on-app-reset.ts b/src/event-actions/on-app-reset.ts index 0134db744..5fdd97670 100644 --- a/src/event-actions/on-app-reset.ts +++ b/src/event-actions/on-app-reset.ts @@ -2,6 +2,7 @@ import RNAsyncStorage from '@react-native-async-storage/async-storage'; import BlastedImage from 'react-native-blasted-image'; import EncryptedStorage from 'react-native-encrypted-storage'; import {resetGenericPassword} from 'react-native-keychain'; +import RNRestart from 'react-native-restart'; import {app} from '@app/contexts'; import {Contact} from '@app/models/contact'; @@ -49,5 +50,6 @@ export async function onAppReset() { Logger.captureException(err, 'onAppReset'); } finally { app.onboarded = false; + RNRestart.restart(); } }