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 recreate vault import accounts #1756

Merged
merged 9 commits into from
Aug 12, 2020
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