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

Use KeyringController messenger events (#20341)

* refactor: use keyring-controller events and unlock methods

* test: add tests for setLocked

* rollback: call trezor dispose directly

* Update app/scripts/metamask-controller.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
This commit is contained in:
Michele Esposito 2023-08-19 00:07:38 +02:00 committed by GitHub
parent 07abc53cce
commit efd34343c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 20 deletions

View File

@ -220,6 +220,14 @@ describe('MetaMaskController', function () {
}); });
}); });
describe('#setLocked', function () {
it('should lock the wallet', async function () {
const { isUnlocked, keyrings } = await metamaskController.setLocked();
assert(!isUnlocked);
assert.deepEqual(keyrings, []);
});
});
describe('#addToken', function () { describe('#addToken', function () {
const address = '0x514910771af9ca656af840dff83e8264ecf986ca'; const address = '0x514910771af9ca656af840dff83e8264ecf986ca';
const symbol = 'LINK'; const symbol = 'LINK';

View File

@ -877,15 +877,21 @@ export default class MetamaskController extends EventEmitter {
), ),
}); });
this.keyringController = this.controllerMessenger.subscribe('KeyringController:unlock', () =>
this.coreKeyringController.getEthKeyringController(); this._onUnlock(),
);
this.keyringController.memStore.subscribe((state) => this.controllerMessenger.subscribe('KeyringController:lock', () =>
this._onKeyringControllerUpdate(state), this._onLock(),
);
this.controllerMessenger.subscribe(
'KeyringController:stateChange',
(state) => {
this._onKeyringControllerUpdate(state);
},
); );
this.keyringController.on('unlock', () => this._onUnlock()); this.keyringController =
this.keyringController.on('lock', () => this._onLock()); this.coreKeyringController.getEthKeyringController();
const getIdentities = () => const getIdentities = () =>
this.preferencesController.store.getState().identities; this.preferencesController.store.getState().identities;
@ -3060,10 +3066,9 @@ export default class MetamaskController extends EventEmitter {
* is up to date with known accounts once the vault is decrypted. * is up to date with known accounts once the vault is decrypted.
* *
* @param {string} password - The user's password * @param {string} password - The user's password
* @returns {Promise<object>} The keyringController update.
*/ */
async submitPassword(password) { async submitPassword(password) {
await this.keyringController.submitPassword(password); await this.coreKeyringController.submitPassword(password);
///: BEGIN:ONLY_INCLUDE_IN(build-mmi) ///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.mmiController.onSubmitPassword(); this.mmiController.onSubmitPassword();
@ -3083,8 +3088,6 @@ export default class MetamaskController extends EventEmitter {
this.preferencesController.getLedgerTransportPreference(); this.preferencesController.getLedgerTransportPreference();
this.setLedgerTransportPreference(transportPreference); this.setLedgerTransportPreference(transportPreference);
return this.keyringController.fullUpdate();
} }
async _loginUser() { async _loginUser() {
@ -3123,7 +3126,7 @@ export default class MetamaskController extends EventEmitter {
const { loginToken, loginSalt } = const { loginToken, loginSalt } =
await this.extension.storage.session.get(['loginToken', 'loginSalt']); await this.extension.storage.session.get(['loginToken', 'loginSalt']);
if (loginToken && loginSalt) { if (loginToken && loginSalt) {
const { vault } = this.keyringController.store.getState(); const { vault } = this.coreKeyringController.state;
const jsonVault = JSON.parse(vault); const jsonVault = JSON.parse(vault);
@ -3135,7 +3138,10 @@ export default class MetamaskController extends EventEmitter {
return; return;
} }
await this.keyringController.submitEncryptionKey(loginToken, loginSalt); await this.coreKeyringController.submitEncryptionKey(
loginToken,
loginSalt,
);
} }
} catch (e) { } catch (e) {
// If somehow this login token doesn't work properly, // If somehow this login token doesn't work properly,
@ -4709,16 +4715,11 @@ export default class MetamaskController extends EventEmitter {
trezorKeyring.dispose(); trezorKeyring.dispose();
} }
const [ledgerKeyring] = this.coreKeyringController.getKeyringsByType(
KeyringType.ledger,
);
ledgerKeyring?.destroy?.();
if (isManifestV3) { if (isManifestV3) {
this.clearLoginArtifacts(); this.clearLoginArtifacts();
} }
return this.keyringController.setLocked(); return this.coreKeyringController.setLocked();
} }
removePermissionsFor = (subjects) => { removePermissionsFor = (subjects) => {

View File

@ -370,7 +370,7 @@ describe('MetaMaskController', function () {
metamaskController.preferencesController.store.getState().identities, metamaskController.preferencesController.store.getState().identities,
); );
const addresses = const addresses =
await metamaskController.keyringController.getAccounts(); await metamaskController.coreKeyringController.getAccounts();
identities.forEach((identity) => { identities.forEach((identity) => {
assert.ok( assert.ok(
@ -388,6 +388,20 @@ describe('MetaMaskController', function () {
}); });
}); });
describe('setLocked', function () {
it('should lock KeyringController', async function () {
sandbox.spy(metamaskController.coreKeyringController, 'setLocked');
await metamaskController.setLocked();
assert(metamaskController.coreKeyringController.setLocked.called);
assert.equal(
metamaskController.coreKeyringController.state.isUnlocked,
false,
);
});
});
describe('#createNewVaultAndKeychain', function () { describe('#createNewVaultAndKeychain', function () {
it('can only create new vault on keyringController once', async function () { it('can only create new vault on keyringController once', async function () {
const selectStub = sandbox.stub( const selectStub = sandbox.stub(