Skip to content

Commit

Permalink
Merge branch 'main' into fix/8822-network-filtered-list-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
tommasini authored Mar 5, 2024
2 parents b4b863a + d669eb3 commit 17d36c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
12 changes: 7 additions & 5 deletions app/core/Vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import { getLedgerKeyring } from './Ledger/Ledger';
/**
* Restores the QR keyring if it exists.
*/
export const restoreQRKeyring = async () => {
export const restoreQRKeyring = async (qrKeyring) => {
const { KeyringController } = Engine.context;
const qrKeyring = (
await KeyringController.getKeyringsByType(KeyringTypes.qr)
)[0];

if (qrKeyring) {
try {
const serializedQRKeyring = await qrKeyring.serialize();
Expand Down Expand Up @@ -98,10 +96,14 @@ export const recreateVaultWithNewPassword = async (
const hdKeyring = KeyringController.state.keyrings[0];
const existingAccountCount = hdKeyring.accounts.length;

const qrKeyring = (
await KeyringController.getKeyringsByType(KeyringTypes.qr)
)[0];

// Recreate keyring with password given to this method
await KeyringController.createNewVaultAndRestore(newPassword, seedPhrase);

await restoreQRKeyring();
await restoreQRKeyring(qrKeyring);
await restoreLedgerKeyring();

// Create previous accounts again
Expand Down
26 changes: 10 additions & 16 deletions app/core/Vault.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Engine from './Engine';
import Logger from '../util/Logger';
import { KeyringTypes } from '@metamask/keyring-controller';
import Engine from './Engine';

import { restoreQRKeyring, restoreLedgerKeyring } from './Vault';
import { getLedgerKeyring } from './Ledger/Ledger';
import { restoreLedgerKeyring, restoreQRKeyring } from './Vault';

jest.mock('./Engine', () => ({
context: {
Expand Down Expand Up @@ -36,11 +35,9 @@ describe('Vault', () => {
const mockQRKeyring = {
serialize: jest.fn().mockResolvedValue('serialized-keyring-data'),
};
KeyringController.getKeyringsByType.mockResolvedValue([mockQRKeyring]);
await restoreQRKeyring();
expect(KeyringController.getKeyringsByType).toHaveBeenCalledWith(
KeyringTypes.qr,
);

await restoreQRKeyring(mockQRKeyring);

expect(mockQRKeyring.serialize).toHaveBeenCalled();
expect(KeyringController.restoreQRKeyring).toHaveBeenCalledWith(
'serialized-keyring-data',
Expand All @@ -49,20 +46,17 @@ describe('Vault', () => {

it('should not restore QR keyring if it does not exist', async () => {
const { KeyringController } = Engine.context;
KeyringController.getKeyringsByType.mockResolvedValue([]);
await restoreQRKeyring();
expect(KeyringController.getKeyringsByType).toHaveBeenCalledWith(
KeyringTypes.qr,
);

await restoreQRKeyring([]);

expect(KeyringController.restoreQRKeyring).not.toHaveBeenCalled();
});

it('should log error if an exception is thrown', async () => {
const { KeyringController } = Engine.context;
const error = new Error('Test error');
const mockKeyring = { serialize: jest.fn().mockRejectedValue(error) };
KeyringController.getKeyringsByType.mockResolvedValue([mockKeyring]);
await restoreQRKeyring();

await restoreQRKeyring(mockKeyring);
expect(Logger.error).toHaveBeenCalledWith(
error,
'error while trying to get qr accounts on recreate vault',
Expand Down

0 comments on commit 17d36c8

Please sign in to comment.