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: throw explicit error when KeyringController is locked #5172

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions packages/keyring-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 95.51,
branches: 94.26,
functions: 100,
lines: 99.07,
statements: 99.08,
lines: 98.96,
statements: 98.98,
},
},

Expand Down
87 changes: 65 additions & 22 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,28 @@ describe('KeyringController', () => {
);
});
});

it('should throw an error if there is no primary keyring', async () => {
await withController(async ({ controller, encryptor }) => {
await controller.setLocked();
jest
.spyOn(encryptor, 'decrypt')
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]);
await controller.submitPassword('123');

await expect(controller.addNewAccount()).rejects.toThrow(
'No HD keyring found',
);
});
});
});

it('should throw error with no HD keyring', async () => {
it('should throw error when the controller is locked', async () => {
await withController(
{ skipVaultCreation: true },
async ({ controller }) => {
await expect(controller.addNewAccount()).rejects.toThrow(
'No HD keyring found',
KeyringControllerError.ControllerLocked,
);
},
);
Expand Down Expand Up @@ -889,7 +903,7 @@ describe('KeyringController', () => {
});

describe('when non-existing account is provided', () => {
it('should throw error', async () => {
it('should throw error if no account matches the address', async () => {
await withController(async ({ controller }) => {
await expect(
controller.getKeyringForAccount(
Expand All @@ -901,19 +915,34 @@ describe('KeyringController', () => {
});
});

it('should throw an error if there are no keyrings', async () => {
await withController(
{ skipVaultCreation: true },
async ({ controller }) => {
await expect(
controller.getKeyringForAccount(
'0x51253087e6f8358b5f10c0a94315d69db3357859',
),
).rejects.toThrow(
'KeyringController - No keyring found. Error info: There are no keyrings',
);
},
);
it('should throw an error if there is no keyring', async () => {
await withController(async ({ controller, encryptor }) => {
await controller.setLocked();
jest
.spyOn(encryptor, 'decrypt')
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]);
await controller.submitPassword('123');

await expect(
controller.getKeyringForAccount(
'0x0000000000000000000000000000000000000000',
),
).rejects.toThrow(
'KeyringController - No keyring found. Error info: There are no keyrings',
);
});
});

it('should throw an error if the controller is locked', async () => {
await withController(async ({ controller }) => {
await controller.setLocked();

await expect(
controller.getKeyringForAccount(
'0x51253087e6f8358b5f10c0a94315d69db3357859',
),
).rejects.toThrow(KeyringControllerError.ControllerLocked);
});
});
});
});
Expand Down Expand Up @@ -963,7 +992,7 @@ describe('KeyringController', () => {
await controller.setLocked();

await expect(controller.persistAllKeyrings()).rejects.toThrow(
KeyringControllerError.MissingCredentials,
KeyringControllerError.ControllerLocked,
);
});
});
Expand Down Expand Up @@ -2018,9 +2047,9 @@ describe('KeyringController', () => {
async ({ controller }) => {
await controller.setLocked();

await expect(controller.changePassword('')).rejects.toThrow(
KeyringControllerError.MissingCredentials,
);
await expect(async () =>
controller.changePassword(''),
).rejects.toThrow(KeyringControllerError.ControllerLocked);
},
);
});
Expand Down Expand Up @@ -2265,16 +2294,30 @@ describe('KeyringController', () => {
});
});

it('should throw error with no HD keyring', async () => {
it('should throw error if the controller is locked', async () => {
await withController(
{ skipVaultCreation: true },
async ({ controller }) => {
await expect(controller.verifySeedPhrase()).rejects.toThrow(
'No HD keyring found',
KeyringControllerError.ControllerLocked,
);
},
);
});

it('should throw an error if there is no primary keyring', async () => {
await withController(async ({ controller, encryptor }) => {
await controller.setLocked();
jest
.spyOn(encryptor, 'decrypt')
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]);
await controller.submitPassword('123');

await expect(controller.verifySeedPhrase()).rejects.toThrow(
'No HD keyring found',
);
});
});
});

describe('verifyPassword', () => {
Expand Down
Loading
Loading