1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

Destroy ledger keyring when removing last account in ledger keyring (#14993)

* Destroy ledger keyring when removing last account in ledger keyring

* Update unit tests
This commit is contained in:
Dan J Miller 2022-06-21 16:33:54 -02:30 committed by GitHub
parent 051254dffd
commit ac7245b50c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -2673,8 +2673,14 @@ export default class MetamaskController extends EventEmitter {
// Remove account from the account tracker controller
this.accountTracker.removeAccount([address]);
const keyring = await this.keyringController.getKeyringForAccount(address);
// Remove account from the keyring
await this.keyringController.removeAccount(address);
const updatedKeyringAccounts = keyring ? await keyring.getAccounts() : {};
if (updatedKeyringAccounts?.length === 0) {
keyring.destroy?.();
}
return address;
}

View File

@ -782,12 +782,20 @@ describe('MetaMaskController', function () {
describe('#removeAccount', function () {
let ret;
const addressToRemove = '0x1';
let mockKeyring;
beforeEach(async function () {
mockKeyring = {
getAccounts: sinon.stub().returns(Promise.resolve([])),
destroy: sinon.stub(),
};
sinon.stub(metamaskController.preferencesController, 'removeAddress');
sinon.stub(metamaskController.accountTracker, 'removeAccount');
sinon.stub(metamaskController.keyringController, 'removeAccount');
sinon.stub(metamaskController, 'removeAllAccountPermissions');
sinon
.stub(metamaskController.keyringController, 'getKeyringForAccount')
.returns(Promise.resolve(mockKeyring));
ret = await metamaskController.removeAccount(addressToRemove);
});
@ -797,6 +805,9 @@ describe('MetaMaskController', function () {
metamaskController.accountTracker.removeAccount.restore();
metamaskController.preferencesController.removeAddress.restore();
metamaskController.removeAllAccountPermissions.restore();
mockKeyring.getAccounts.resetHistory();
mockKeyring.destroy.resetHistory();
});
it('should call preferencesController.removeAddress', async function () {
@ -830,6 +841,16 @@ describe('MetaMaskController', function () {
it('should return address', async function () {
assert.equal(ret, '0x1');
});
it('should call keyringController.getKeyringForAccount', async function () {
assert(
metamaskController.keyringController.getKeyringForAccount.calledWith(
addressToRemove,
),
);
});
it('should call keyring.destroy', async function () {
assert(mockKeyring.destroy.calledOnce);
});
});
describe('#newUnsignedMessage', function () {