diff --git a/app/scripts/metamask-controller.actions.test.js b/app/scripts/metamask-controller.actions.test.js index a0dba444b2df..51b69a12008f 100644 --- a/app/scripts/metamask-controller.actions.test.js +++ b/app/scripts/metamask-controller.actions.test.js @@ -177,7 +177,7 @@ describe('MetaMaskController', function () { ]), Promise.resolve(1).then(() => { keyringControllerState1 = JSON.stringify( - metamaskController.keyringController.memStore.getState(), + metamaskController.coreKeyringController.state, ); metamaskController.importAccountWithStrategy('privateKey', [ importPrivkey, @@ -185,7 +185,7 @@ describe('MetaMaskController', function () { }), Promise.resolve(2).then(() => { keyringControllerState2 = JSON.stringify( - metamaskController.keyringController.memStore.getState(), + metamaskController.coreKeyringController.state, ); }), ]); diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f2754b7e22ec..a6411d8909ef 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -905,8 +905,8 @@ export default class MetamaskController extends EventEmitter { permissionSpecifications: { ...getPermissionSpecifications({ getIdentities, - getAllAccounts: this.keyringController.getAccounts.bind( - this.keyringController, + getAllAccounts: this.coreKeyringController.getAccounts.bind( + this.coreKeyringController, ), captureKeyringTypesWithMissingIdentities: ( identities = {}, @@ -1197,8 +1197,8 @@ export default class MetamaskController extends EventEmitter { this.networkController.state.providerConfig.chainId, preferencesStore: this.preferencesController.store, txHistoryLimit: 60, - signTransaction: this.keyringController.signTransaction.bind( - this.keyringController, + signTransaction: this.coreKeyringController.signTransaction.bind( + this.coreKeyringController, ), provider: this.provider, blockTracker: this.blockTracker, @@ -1695,7 +1695,7 @@ export default class MetamaskController extends EventEmitter { AppMetadataController: this.appMetadataController.store, NetworkController: this.networkController, CachedBalancesController: this.cachedBalancesController.store, - KeyringController: this.keyringController.memStore, + KeyringController: this.coreKeyringController, PreferencesController: this.preferencesController.store, MetaMetricsController: this.metaMetricsController.store, AddressBookController: this.addressBookController, @@ -1841,7 +1841,7 @@ export default class MetamaskController extends EventEmitter { KeyringType.snap, ); if (!snapKeyring) { - snapKeyring = await this.keyringController.addNewKeyring( + snapKeyring = await this.coreKeyringController.addNewKeyring( KeyringType.snap, ); } @@ -1944,7 +1944,7 @@ export default class MetamaskController extends EventEmitter { ///: BEGIN:ONLY_INCLUDE_IN(keyring-snaps) getSnapKeyring: this.getSnapKeyring.bind(this), saveSnapKeyring: async () => { - await this.keyringController.persistAllKeyrings(); + await this.coreKeyringController.persistAllKeyrings(); }, ///: END:ONLY_INCLUDE_IN ///: BEGIN:ONLY_INCLUDE_IN(snaps) @@ -2193,7 +2193,7 @@ export default class MetamaskController extends EventEmitter { * @returns {object} status */ getState() { - const { vault } = this.keyringController.store.getState(); + const { vault } = this.coreKeyringController.state; const isInitialized = Boolean(vault); const flatState = this.memStore.getFlatState(); @@ -2822,7 +2822,7 @@ export default class MetamaskController extends EventEmitter { async exportAccount(address, password) { await this.verifyPassword(password); - return this.keyringController.exportAccount(address, password); + return this.coreKeyringController.exportAccount(password, address); } async getTokenStandardAndDetails(address, userAddress, tokenId) { @@ -2917,25 +2917,7 @@ export default class MetamaskController extends EventEmitter { * @returns {object} vault */ async createNewVaultAndKeychain(password) { - const releaseLock = await this.createVaultMutex.acquire(); - try { - let vault; - const accounts = await this.keyringController.getAccounts(); - if (accounts.length > 0) { - vault = await this.keyringController.fullUpdate(); - } else { - vault = await this.keyringController.createNewVaultAndKeychain( - password, - ); - const addresses = await this.keyringController.getAccounts(); - this.preferencesController.setAddresses(addresses); - this.selectFirstIdentity(); - } - - return vault; - } finally { - releaseLock(); - } + return this.coreKeyringController.createNewVaultAndKeychain(password); } /** @@ -3171,7 +3153,7 @@ export default class MetamaskController extends EventEmitter { * @param {string} password - The user's password */ async verifyPassword(password) { - await this.keyringController.verifyPassword(password); + await this.coreKeyringController.verifyPassword(password); } /** @@ -3251,7 +3233,7 @@ export default class MetamaskController extends EventEmitter { keyringName, ); if (!keyring) { - keyring = await this.keyringController.addNewKeyring(keyringName); + keyring = await this.coreKeyringController.addNewKeyring(keyringName); } if (hdPath && keyring.setHdPath) { keyring.setHdPath(hdPath); @@ -3304,7 +3286,7 @@ export default class MetamaskController extends EventEmitter { // Merge with existing accounts // and make sure addresses are not repeated - const oldAccounts = await this.keyringController.getAccounts(); + const oldAccounts = await this.coreKeyringController.getAccounts(); const accountsToTrack = [ ...new Set( oldAccounts.concat(accounts.map((a) => a.address.toLowerCase())), @@ -3420,9 +3402,11 @@ export default class MetamaskController extends EventEmitter { const keyring = await this.getKeyringForDevice(deviceName, hdPath); keyring.setAccountToUnlock(index); - const oldAccounts = await this.keyringController.getAccounts(); - const keyState = await this.keyringController.addNewAccount(keyring); - const newAccounts = await this.keyringController.getAccounts(); + const oldAccounts = await this.coreKeyringController.getAccounts(); + const keyState = await this.coreKeyringController.addNewAccountForKeyring( + keyring, + ); + const newAccounts = await this.coreKeyringController.getAccounts(); this.preferencesController.setAddresses(newAccounts); newAccounts.forEach((address) => { if (!oldAccounts.includes(address)) { @@ -3571,7 +3555,7 @@ export default class MetamaskController extends EventEmitter { address, ); // Remove account from the keyring - await this.keyringController.removeAccount(address); + await this.coreKeyringController.removeAccount(address); const updatedKeyringAccounts = keyring ? await keyring.getAccounts() : {}; if (updatedKeyringAccounts?.length === 0) { keyring.destroy?.(); @@ -4516,7 +4500,7 @@ export default class MetamaskController extends EventEmitter { * @returns {boolean} Whether the extension is unlocked. */ isUnlocked() { - return this.keyringController.memStore.getState().isUnlocked; + return this.coreKeyringController.state.isUnlocked; } //============================================================================= diff --git a/app/scripts/metamask-controller.test.js b/app/scripts/metamask-controller.test.js index 316de29bb4e6..408371f8e3bf 100644 --- a/app/scripts/metamask-controller.test.js +++ b/app/scripts/metamask-controller.test.js @@ -318,7 +318,7 @@ describe('MetaMaskController', () => { }); jest.spyOn( - metamaskController.keyringController, + metamaskController.coreKeyringController, 'createNewVaultAndKeychain', ); jest.spyOn( @@ -363,7 +363,7 @@ describe('MetaMaskController', () => { it('adds 1 account', async () => { const keyringAccounts = - await metamaskController.keyringController.getAccounts(); + await metamaskController.coreKeyringController.getAccounts(); expect(keyringAccounts[keyringAccounts.length - 1]).toStrictEqual( '0xe18035bf8712672935fdb4e5e431b1a0183d2dfc', ); @@ -420,7 +420,7 @@ describe('MetaMaskController', () => { await metamaskController.createNewVaultAndKeychain(password); expect( - metamaskController.keyringController.createNewVaultAndKeychain, + metamaskController.coreKeyringController.createNewVaultAndKeychain, ).toHaveBeenCalledTimes(1); }); }); @@ -622,7 +622,7 @@ describe('MetaMaskController', () => { }); it('should add the Trezor Hardware keyring', async () => { - jest.spyOn(metamaskController.keyringController, 'addNewKeyring'); + jest.spyOn(metamaskController.coreKeyringController, 'addNewKeyring'); await metamaskController .connectHardware(HardwareDeviceNames.trezor, 0) .catch(() => null); @@ -631,13 +631,13 @@ describe('MetaMaskController', () => { KeyringType.trezor, ); expect( - metamaskController.keyringController.addNewKeyring, + metamaskController.coreKeyringController.addNewKeyring, ).toHaveBeenCalledWith(KeyringType.trezor); expect(keyrings).toHaveLength(1); }); it('should add the Ledger Hardware keyring', async () => { - jest.spyOn(metamaskController.keyringController, 'addNewKeyring'); + jest.spyOn(metamaskController.coreKeyringController, 'addNewKeyring'); await metamaskController .connectHardware(HardwareDeviceNames.ledger, 0) .catch(() => null); @@ -646,7 +646,7 @@ describe('MetaMaskController', () => { KeyringType.ledger, ); expect( - metamaskController.keyringController.addNewKeyring, + metamaskController.coreKeyringController.addNewKeyring, ).toHaveBeenCalledWith(KeyringType.ledger); expect(keyrings).toHaveLength(1); }); @@ -731,11 +731,11 @@ describe('MetaMaskController', () => { beforeEach(async () => { jest.spyOn(window, 'open').mockReturnValue(); jest - .spyOn(metamaskController.keyringController, 'addNewAccount') + .spyOn(metamaskController.coreKeyringController, 'addNewAccount') .mockReturnValue('0x123'); jest - .spyOn(metamaskController.keyringController, 'getAccounts') + .spyOn(metamaskController.coreKeyringController, 'getAccounts') .mockResolvedValueOnce(['0x1']) .mockResolvedValueOnce(['0x2']) .mockResolvedValueOnce(['0x3']); @@ -769,7 +769,7 @@ describe('MetaMaskController', () => { it('should call keyringController.addNewAccount', async () => { expect( - metamaskController.keyringController.addNewAccount, + metamaskController.coreKeyringController.addNewAccountForKeyring, ).toHaveBeenCalledTimes(1); });