Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vault recovery & invalid password error #6957

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions app/components/Views/ResetPassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import CheckBox from '@react-native-community/checkbox';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { connect } from 'react-redux';
import {
passwordSet,
passwordUnset,
seedphraseNotBackedUp,
} from '../../../actions/user';
import { passwordSet, seedphraseNotBackedUp } from '../../../actions/user';
import { setLockTime } from '../../../actions/settings';
import StyledButton from '../../UI/StyledButton';
import Engine from '../../../core/Engine';
Expand Down Expand Up @@ -837,7 +833,6 @@ const mapStateToProps = (state) => ({

const mapDispatchToProps = (dispatch) => ({
passwordSet: () => dispatch(passwordSet()),
passwordUnset: () => dispatch(passwordUnset()),
Cal-L marked this conversation as resolved.
Show resolved Hide resolved
setLockTime: (time) => dispatch(setLockTime(time)),
seedphraseNotBackedUp: () => dispatch(seedphraseNotBackedUp()),
});
Expand Down
20 changes: 19 additions & 1 deletion app/core/Authentication/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
SEED_PHRASE_HINTS,
} from '../../constants/storage';
import Logger from '../../util/Logger';
import { authSuccess, authError, logIn, logOut } from '../../actions/user';
import {
authSuccess,
authError,
logIn,
logOut,
passwordSet,
} from '../../actions/user';
import AUTHENTICATION_TYPE from '../../constants/userProperties';
import { Store } from 'redux';
import AuthenticationError from './AuthenticationError';
Expand Down Expand Up @@ -66,6 +72,16 @@ class AuthenticationService {
}
}

private dispatchPasswordSet(): void {
if (this.store) {
this.store.dispatch(passwordSet());
} else {
Logger.log(
'Attempted to dispatch passwordSet action but dispatch was not initialized',
);
}
}

private dispatchLogout(): void {
if (this.store) {
this.store.dispatch(logOut());
Expand Down Expand Up @@ -396,6 +412,7 @@ class AuthenticationService {
await this.storePassword(password, authData.currentAuthType);
this.dispatchLogin();
this.authData = authData;
this.dispatchPasswordSet();
} catch (e: any) {
this.lockApp(false);
throw new AuthenticationError(
Expand Down Expand Up @@ -435,6 +452,7 @@ class AuthenticationService {
await this.loginVaultCreation(password, selectedAddress);
this.dispatchLogin();
this.store?.dispatch(authSuccess(bioStateMachineId));
Cal-L marked this conversation as resolved.
Show resolved Hide resolved
this.dispatchPasswordSet();
} catch (e: any) {
this.store?.dispatch(authError(bioStateMachineId));
!disableAutoLogout && this.lockApp(false);
Expand Down
18 changes: 18 additions & 0 deletions app/core/EngineService/EngineService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import EngineService from './EngineService';
import Engine from '../Engine';
import { store } from '../../store';

jest.unmock('../Engine');

describe('EngineService', () => {
EngineService.initalizeEngine(store);
it('should have Engine initialized', () => {
expect(Engine.context).toBeDefined();
});
it('should have recovered vault on redux store ', async () => {
const { success } = await EngineService.initializeVaultFromBackup();
expect(success).toBeTruthy();
const { KeyringController } = store.getState().engine.backgroundState;
expect(KeyringController.vault).toBeDefined();
});
});
7 changes: 6 additions & 1 deletion app/core/EngineService/EngineService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ class EngineService {
const Engine = UntypedEngine as any;
// This ensures we create an entirely new engine
await Engine.destroyEngine();
this.engineInitialized = false;
if (keyringState) {
const instance = Engine.init(state, keyringState);
const newKeyringState = {
keyrings: [],
vault: keyringState.vault,
};
const instance = Engine.init(state, newKeyringState);
if (instance) {
this.updateControllers(importedStore, instance);
// this is a hack to give the engine time to reinitialize
Expand Down
5 changes: 4 additions & 1 deletion app/util/test/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ jest.mock('react-native-keychain', () => ({
setInternetCredentials: jest
.fn(('server', 'username', 'password'))
.mockResolvedValue({ service: 'metamask', storage: 'storage' }),
resetInternetCredentials: jest.fn(),
getInternetCredentials: jest
.fn()
.mockResolvedValue({ password: 'mock-credentials-password' }),
resetInternetCredentials: jest.fn().mockResolvedValue(),
ACCESSIBLE: {
WHEN_UNLOCKED: 'AccessibleWhenUnlocked',
AFTER_FIRST_UNLOCK: 'AccessibleAfterFirstUnlock',
Expand Down
Loading