Skip to content

Commit

Permalink
fix: Fix the issue ledger keyring account will disappear when passwor…
Browse files Browse the repository at this point in the history
…d reset.
  • Loading branch information
dawnseeker8 committed Feb 27, 2024
1 parent 07590a4 commit cdf675d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
12 changes: 9 additions & 3 deletions app/core/Vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++) {
Expand Down
24 changes: 19 additions & 5 deletions app/core/Vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,25 @@ 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();
});

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();
Expand All @@ -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',
Expand Down

0 comments on commit cdf675d

Please sign in to comment.