From ab9f74cc2bd20e654bd9125155140d7c89a11a6c Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 25 Jul 2023 00:23:53 +0200 Subject: [PATCH] refactor: use addNewAccount from core KeyringController --- .../metamask-controller.actions.test.js | 12 ++----- app/scripts/metamask-controller.js | 34 +++---------------- app/scripts/metamask-controller.test.js | 4 +-- ui/store/actions.test.js | 2 +- ui/store/actions.ts | 10 ++---- 5 files changed, 13 insertions(+), 49 deletions(-) diff --git a/app/scripts/metamask-controller.actions.test.js b/app/scripts/metamask-controller.actions.test.js index 5e7c771a1a6b..397648c371da 100644 --- a/app/scripts/metamask-controller.actions.test.js +++ b/app/scripts/metamask-controller.actions.test.js @@ -145,27 +145,21 @@ describe('MetaMaskController', function () { metamaskController.addNewAccount(1), metamaskController.addNewAccount(1), ]); - assert.deepEqual( - Object.keys(addNewAccountResult1.identities), - Object.keys(addNewAccountResult2.identities), - ); + assert.equal(addNewAccountResult1, addNewAccountResult2); }); it('two successive calls with same accountCount give same result', async function () { await metamaskController.createNewVaultAndKeychain('test@123'); const addNewAccountResult1 = await metamaskController.addNewAccount(1); const addNewAccountResult2 = await metamaskController.addNewAccount(1); - assert.deepEqual( - Object.keys(addNewAccountResult1.identities), - Object.keys(addNewAccountResult2.identities), - ); + assert.equal(addNewAccountResult1, addNewAccountResult2); }); it('two successive calls with different accountCount give different results', async function () { await metamaskController.createNewVaultAndKeychain('test@123'); const addNewAccountResult1 = await metamaskController.addNewAccount(1); const addNewAccountResult2 = await metamaskController.addNewAccount(2); - assert.notDeepEqual(addNewAccountResult1, addNewAccountResult2); + assert.notEqual(addNewAccountResult1, addNewAccountResult2); }); }); diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 9efbb8447fd2..1abe48eb0eff 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -3387,38 +3387,12 @@ export default class MetamaskController extends EventEmitter { await new Promise((resolve) => setTimeout(resolve, 5_000)); } - const [primaryKeyring] = this.keyringController.getKeyringsByType( - KeyringType.hdKeyTree, - ); - if (!primaryKeyring) { - throw new Error('MetamaskController - No HD Key Tree found'); - } - const { keyringController } = this; - const { identities: oldIdentities } = - this.preferencesController.store.getState(); - - if (Object.keys(oldIdentities).length === accountCount) { - const oldAccounts = await keyringController.getAccounts(); - const keyState = await keyringController.addNewAccount(primaryKeyring); - const newAccounts = await keyringController.getAccounts(); + const { addedAccountAddress } = + await this.coreKeyringController.addNewAccount(accountCount); - await this.verifySeedPhrase(); + this.preferencesController.setSelectedAddress(addedAccountAddress); - this.preferencesController.setAddresses(newAccounts); - newAccounts.forEach((address) => { - if (!oldAccounts.includes(address)) { - this.preferencesController.setSelectedAddress(address); - } - }); - - const { identities } = this.preferencesController.store.getState(); - return { ...keyState, identities }; - } - - return { - ...keyringController.memStore.getState(), - identities: oldIdentities, - }; + return addedAccountAddress; } /** diff --git a/app/scripts/metamask-controller.test.js b/app/scripts/metamask-controller.test.js index 5bed8c899d51..59a84ff2360f 100644 --- a/app/scripts/metamask-controller.test.js +++ b/app/scripts/metamask-controller.test.js @@ -716,7 +716,7 @@ describe('MetaMaskController', function () { metamaskController.keyringController, 'addNewAccount', ); - addNewAccountStub.returns({}); + addNewAccountStub.returns('0x123'); getAccountsStub = sinon.stub( metamaskController.keyringController, @@ -797,7 +797,7 @@ describe('MetaMaskController', function () { await addNewAccount; assert.fail('should throw'); } catch (e) { - assert.equal(e.message, 'MetamaskController - No HD Key Tree found'); + assert.equal(e.message, 'No HD keyring found'); } }); }); diff --git a/ui/store/actions.test.js b/ui/store/actions.test.js index 91e2c2ef763d..67d5d097bee8 100644 --- a/ui/store/actions.test.js +++ b/ui/store/actions.test.js @@ -398,7 +398,7 @@ describe('Actions', () => { const addNewAccount = background.addNewAccount.callsFake((_, cb) => cb(null, { - identities: {}, + addedAccountAddress: '0x123', }), ); diff --git a/ui/store/actions.ts b/ui/store/actions.ts index af3b677c246d..7fe99c9ccd03 100644 --- a/ui/store/actions.ts +++ b/ui/store/actions.ts @@ -432,12 +432,11 @@ export function addNewAccount(): ThunkAction< const oldIdentities = getState().metamask.identities; dispatch(showLoadingIndication()); - let newIdentities; + let addedAccountAddress; try { - const { identities } = await submitRequestToBackground('addNewAccount', [ + addedAccountAddress = await submitRequestToBackground('addNewAccount', [ Object.keys(oldIdentities).length, ]); - newIdentities = identities; } catch (error) { dispatch(displayWarning(error)); throw error; @@ -445,11 +444,8 @@ export function addNewAccount(): ThunkAction< dispatch(hideLoadingIndication()); } - const newAccountAddress = Object.keys(newIdentities).find( - (address) => !oldIdentities[address], - ); await forceUpdateMetamaskState(dispatch); - return newAccountAddress; + return addedAccountAddress; }; }