diff --git a/app/core/Vault.js b/app/core/Vault.js index cde50ff38b7..0276ab21614 100644 --- a/app/core/Vault.js +++ b/app/core/Vault.js @@ -28,11 +28,15 @@ export const restoreQRKeyring = async () => { /** * Restores the Ledger keyring if it exists. */ -export const restoreLedgerKeyring = async () => { +export const restoreLedgerKeyring = async (keyring) => { const { KeyringController } = Engine.context; - const keyring = await getLedgerKeyring(); + if (keyring) { try { + const serializedLedgerKeyring = await keyring.serialize(); + //This will regenerate a new ledger keyring after `createNewVaultAndRestore` is called + (await getLedgerKeyring()).deserialize(serializedLedgerKeyring); + await KeyringController.persistAllKeyrings(); KeyringController.updateIdentities(await KeyringController.getAccounts()); } catch (e) { @@ -98,11 +102,13 @@ export const recreateVaultWithNewPassword = async ( const hdKeyring = KeyringController.state.keyrings[0]; const existingAccountCount = hdKeyring.accounts.length; + const oldLedgerKeyring = await getLedgerKeyring(); + // Recreate keyring with password given to this method await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase); await restoreQRKeyring(); - await restoreLedgerKeyring(); + await restoreLedgerKeyring(oldLedgerKeyring); // Create previous accounts again for (let i = 0; i < existingAccountCount - 1; i++) { diff --git a/app/core/Vault.test.ts b/app/core/Vault.test.ts index 3e51f5c6358..494dfb76339 100644 --- a/app/core/Vault.test.ts +++ b/app/core/Vault.test.ts @@ -73,9 +73,17 @@ describe('Vault', () => { describe('restoreLedgerKeyring', () => { it('should restore ledger keyring if it exists', async () => { const { KeyringController } = Engine.context; - (getLedgerKeyring as jest.Mock).mockResolvedValue('foo'); - await restoreLedgerKeyring(); + + const mockSerialisedKeyring = jest.fn(); + const mockDeserializedKeyring = jest.fn(); + (getLedgerKeyring as jest.Mock).mockResolvedValue({ + deserialize: mockDeserializedKeyring, + }); + await restoreLedgerKeyring({ serialize: mockSerialisedKeyring }); + + expect(mockSerialisedKeyring).toHaveBeenCalled(); expect(getLedgerKeyring).toHaveBeenCalled(); + expect(mockDeserializedKeyring).toHaveBeenCalled(); expect(KeyringController.persistAllKeyrings).toHaveBeenCalled(); expect(KeyringController.updateIdentities).toHaveBeenCalled(); expect(KeyringController.getAccounts).toHaveBeenCalled(); @@ -83,7 +91,7 @@ describe('Vault', () => { it('should not restore ledger keyring if it does not exist', async () => { const { KeyringController } = Engine.context; - (getLedgerKeyring as jest.Mock).mockResolvedValue(''); + await restoreLedgerKeyring(); expect(KeyringController.persistAllKeyrings).not.toHaveBeenCalled(); expect(KeyringController.updateIdentities).not.toHaveBeenCalled(); @@ -92,10 +100,16 @@ describe('Vault', () => { it('should log error if an exception is thrown', async () => { const { KeyringController } = Engine.context; - (getLedgerKeyring as jest.Mock).mockResolvedValue('foo'); + + (getLedgerKeyring as jest.Mock).mockResolvedValue({ + deserialize: jest.fn(), + }); + const error = new Error('Test error'); KeyringController.persistAllKeyrings.mockRejectedValue(error); - await restoreLedgerKeyring(); + + await restoreLedgerKeyring({ serialize: jest.fn() }); + expect(Logger.error).toHaveBeenCalledWith( error, 'error while trying to restore Ledger accounts on recreate vault',