Skip to content

Commit

Permalink
Fix recreate vault import accounts (#1756)
Browse files Browse the repository at this point in the history
* fix recreate vault

* Improve getting simple key pair keys

* Introduce try catch around imported accounts

* Fix for multiple imported accounts

* remove unecessary async await

* Use old password

* use logger on recreate vault
  • Loading branch information
andrepimenta authored Aug 12, 2020
1 parent 9aa9629 commit 7288f5c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/components/Views/ChoosePassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Icon from 'react-native-vector-icons/FontAwesome';
import AppConstants from '../../../core/AppConstants';
import OnboardingProgress from '../../UI/OnboardingProgress';
import zxcvbn from 'zxcvbn';
import Logger from '../../../util/Logger';

const steps = [strings('choose_password.title'), strings('choose_password.secure'), strings('choose_password.confirm')];

Expand Down Expand Up @@ -355,6 +356,25 @@ class ChoosePassword extends PureComponent {
recreateVault = async password => {
const { KeyringController, PreferencesController } = Engine.context;
const seedPhrase = await this.getSeedPhrase();

let importedAccounts = [];
try {
const keychainPassword = this.keyringControllerPasswordSet ? this.state.password : '';
// 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(password, seedPhrase);
// Keyring is set with empty password or not
Expand All @@ -371,6 +391,15 @@ class ChoosePassword extends PureComponent {
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');
}

// Reset preferencesControllerState
preferencesControllerState = PreferencesController.state;

Expand Down

0 comments on commit 7288f5c

Please sign in to comment.