From 715991c5009eca4d6e9abd504e564b4eca6ef8ce Mon Sep 17 00:00:00 2001 From: soralit Date: Wed, 1 Jun 2022 16:58:04 +0800 Subject: [PATCH 1/3] fix: Keystone accounts page will increase 1 after scanning wrong wallet sync qrcode --- app/components/Views/ConnectQRHardware/index.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/components/Views/ConnectQRHardware/index.tsx b/app/components/Views/ConnectQRHardware/index.tsx index 8a82b93bccb..e74285bfd1a 100644 --- a/app/components/Views/ConnectQRHardware/index.tsx +++ b/app/components/Views/ConnectQRHardware/index.tsx @@ -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], ); const nextPage = useCallback(async () => { From 730afdda79554224104f18bc9d31a7bb3468096e Mon Sep 17 00:00:00 2001 From: soralit Date: Wed, 1 Jun 2022 16:59:33 +0800 Subject: [PATCH 2/3] fix: keystone accounts disappered after reset password --- app/components/Views/ResetPassword/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/components/Views/ResetPassword/index.js b/app/components/Views/ResetPassword/index.js index b575d8db99d..7aaf331d2e8 100644 --- a/app/components/Views/ResetPassword/index.js +++ b/app/components/Views/ResetPassword/index.js @@ -469,9 +469,14 @@ class ResetPassword extends PureComponent { ); } + const qrKeyring = await KeyringController.getOrAddQRKeyring(); + const serializedQRKeyring = await qrKeyring.serialize(); + // Recreate keyring with password given to this method await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase); + await KeyringController.restoreQRKeyring(serializedQRKeyring); + // Get props to restore vault const hdKeyring = KeyringController.state.keyrings[0]; const existingAccountCount = hdKeyring.accounts.length; From c18b6190e00946d23de2f8a680ee827fc7252502 Mon Sep 17 00:00:00 2001 From: soralit Date: Mon, 13 Jun 2022 08:20:45 +0800 Subject: [PATCH 3/3] code refactor --- app/components/Views/ResetPassword/index.js | 86 +++------------------ app/core/Vault.js | 24 ++++-- 2 files changed, 28 insertions(+), 82 deletions(-) diff --git a/app/components/Views/ResetPassword/index.js b/app/components/Views/ResetPassword/index.js index 7aaf331d2e8..03826f5bea5 100644 --- a/app/components/Views/ResetPassword/index.js +++ b/app/components/Views/ResetPassword/index.js @@ -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 { @@ -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({ @@ -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; @@ -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 { @@ -438,81 +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', - ); - } - - const qrKeyring = await KeyringController.getOrAddQRKeyring(); - const serializedQRKeyring = await qrKeyring.serialize(); - - // Recreate keyring with password given to this method - await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase); - - await KeyringController.restoreQRKeyring(serializedQRKeyring); - - // 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, + ); }; /** diff --git a/app/core/Vault.js b/app/core/Vault.js index 4d70613e09c..cf0462f0822 100644 --- a/app/core/Vault.js +++ b/app/core/Vault.js @@ -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 } = @@ -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]; @@ -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);