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

[5.5] Keystone: fix 2 bugs #4430

Merged
merged 3 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions app/components/Views/ConnectQRHardware/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ const ConnectQRHardware = ({ navigation }: IConnectQRHardwareProps) => {
);

const onScanError = useCallback(
(error: string) => {
async (error: string) => {
hideScanner();
setErrorMsg(error);
const qrKeyring = await KeyringController.getOrAddQRKeyring();
qrKeyring.cancelSync();
},
[hideScanner],
[hideScanner, KeyringController],
gantunesr marked this conversation as resolved.
Show resolved Hide resolved
);

const nextPage = useCallback(async () => {
Expand Down
81 changes: 9 additions & 72 deletions app/components/Views/ResetPassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
passwordRequirementsMet,
} from '../../../util/password';
import NotificationManager from '../../../core/NotificationManager';
import { syncPrefs } from '../../../util/sync';
import { ThemeContext, mockTheme } from '../../../util/theme';
import AnimatedFox from 'react-native-animated-fox';
import {
Expand All @@ -57,6 +56,7 @@ import {
CONFIRM_CHANGE_PASSWORD_INPUT_BOX_ID,
} from '../../../constants/test-ids';
import { LoginOptionsSwitch } from '../../UI/LoginOptionsSwitch';
import { recreateVaultWithNewPassword } from '../../../core/Vault';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -371,8 +371,7 @@ class ResetPassword extends PureComponent {
};

onPressCreate = async () => {
const { loading, isSelected, password, confirmPassword, originalPassword } =
this.state;
const { loading, isSelected, password, confirmPassword } = this.state;
const passwordsMatch = password !== '' && password === confirmPassword;
const canSubmit = passwordsMatch && isSelected;

Expand All @@ -388,7 +387,7 @@ class ResetPassword extends PureComponent {
try {
this.setState({ loading: true });

await this.recreateVault(originalPassword);
await this.recreateVault();
// Set biometrics for new password
await SecureKeychain.resetGenericPassword();
try {
Expand Down Expand Up @@ -438,76 +437,14 @@ class ResetPassword extends PureComponent {
/**
* Recreates a vault
*
* @param password - Password to recreate and set the vault with
*/
recreateVault = async (password) => {
recreateVault = async () => {
const { originalPassword, password: newPassword } = this.state;
const { KeyringController, PreferencesController } = Engine.context;
const seedPhrase = await this.getSeedPhrase();
const oldPrefs = PreferencesController.state;

let importedAccounts = [];
try {
const keychainPassword = originalPassword;
// Get imported accounts
const simpleKeyrings = KeyringController.state.keyrings.filter(
(keyring) => keyring.type === 'Simple Key Pair',
);
for (let i = 0; i < simpleKeyrings.length; i++) {
const simpleKeyring = simpleKeyrings[i];
const simpleKeyringAccounts = await Promise.all(
simpleKeyring.accounts.map((account) =>
KeyringController.exportAccount(keychainPassword, account),
),
);
importedAccounts = [...importedAccounts, ...simpleKeyringAccounts];
}
} catch (e) {
Logger.error(
e,
'error while trying to get imported accounts on recreate vault',
);
}

// Recreate keyring with password given to this method
await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase);

// Get props to restore vault
const hdKeyring = KeyringController.state.keyrings[0];
const existingAccountCount = hdKeyring.accounts.length;
const selectedAddress = this.props.selectedAddress;

// Create previous accounts again
for (let i = 0; i < existingAccountCount - 1; i++) {
await KeyringController.addNewAccount();
}

try {
// Import imported accounts again
for (let i = 0; i < importedAccounts.length; i++) {
await KeyringController.importAccountWithStrategy('privateKey', [
importedAccounts[i],
]);
}
} catch (e) {
Logger.error(
e,
'error while trying to import accounts on recreate vault',
);
}

//Persist old account/identities names
const preferencesControllerState = PreferencesController.state;
const prefUpdates = syncPrefs(oldPrefs, preferencesControllerState);

// Set preferencesControllerState again
await PreferencesController.update(prefUpdates);
// Reselect previous selected account if still available
if (hdKeyring.accounts.includes(selectedAddress)) {
PreferencesController.setSelectedAddress(selectedAddress);
} else {
PreferencesController.setSelectedAddress(hdKeyring.accounts[0]);
}
await recreateVaultWithNewPassword(
originalPassword,
newPassword,
this.props.selectedAddress,
);
};

/**
Expand Down
24 changes: 19 additions & 5 deletions app/core/Vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export const getSeedPhrase = async (password = '') => {
};

/**
* Recreates a vault with the same password for the purpose of using the newest encryption methods
* Recreates a vault with the new password
*
* @param password - Password to recreate and set the vault with
* @param password - current password
* @param newPassword - new password
* @param selectedAddress
*/
export const recreateVaultWithSamePassword = async (
password = '',

export const recreateVaultWithNewPassword = async (
password,
newPassword,
selectedAddress,
) => {
const { KeyringController, PreferencesController, AccountTrackerController } =
Expand Down Expand Up @@ -57,7 +61,7 @@ export const recreateVaultWithSamePassword = async (
const serializedQRKeyring = await qrKeyring.serialize();

// Recreate keyring with password given to this method
await KeyringController.createNewVaultAndRestore(password, seedPhrase);
await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase);

// Get props to restore vault
const hdKeyring = KeyringController.state.keyrings[0];
Expand Down Expand Up @@ -105,3 +109,13 @@ export const recreateVaultWithSamePassword = async (
// Default to first account as fallback
PreferencesController.setSelectedAddress(hdKeyring.accounts[0]);
};

/**
* Recreates a vault with the same password for the purpose of using the newest encryption methods
*
* @param password - Password to recreate and set the vault with
*/
export const recreateVaultWithSamePassword = async (
password = '',
selectedAddress,
) => recreateVaultWithNewPassword(password, password, selectedAddress);