Skip to content

Commit

Permalink
feat: reset wallet when keychain broken (#2121)
Browse files Browse the repository at this point in the history
Co-authored-by: iGroza <xneonxneon1@gmail.com>
  • Loading branch information
2 people authored and devkudasov committed Oct 6, 2024
1 parent 84434b4 commit f293b20
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 49 deletions.
55 changes: 6 additions & 49 deletions src/contexts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -409,7 +409,7 @@ class App extends AsyncEventEmitter {
return possibleToRestoreWallet;
};

async getPassword(pinCandidate?: string): Promise<string> {
async getPassword(): Promise<string> {
const creds = await getGenericPassword();

// Detect keychain migration
Expand All @@ -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');
}
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/event-actions/on-app-reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -49,5 +50,6 @@ export async function onAppReset() {
Logger.captureException(err, 'onAppReset');
} finally {
app.onboarded = false;
RNRestart.restart();
}
}

0 comments on commit f293b20

Please sign in to comment.