1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Use addNewAccount from core KeyringController (#19814)

* refactor: use addNewAccount from core KeyringController

* refactor: replace missed interaction

* refactor: select account only when is new

* refactor: use getAccounts to check if account is new
This commit is contained in:
Michele Esposito 2023-08-16 11:19:41 +02:00 committed by GitHub
parent fed047f88b
commit 42d05ef9cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 53 deletions

View File

@ -145,27 +145,21 @@ describe('MetaMaskController', function () {
metamaskController.addNewAccount(1), metamaskController.addNewAccount(1),
metamaskController.addNewAccount(1), metamaskController.addNewAccount(1),
]); ]);
assert.deepEqual( assert.equal(addNewAccountResult1, addNewAccountResult2);
Object.keys(addNewAccountResult1.identities),
Object.keys(addNewAccountResult2.identities),
);
}); });
it('two successive calls with same accountCount give same result', async function () { it('two successive calls with same accountCount give same result', async function () {
await metamaskController.createNewVaultAndKeychain('test@123'); await metamaskController.createNewVaultAndKeychain('test@123');
const addNewAccountResult1 = await metamaskController.addNewAccount(1); const addNewAccountResult1 = await metamaskController.addNewAccount(1);
const addNewAccountResult2 = await metamaskController.addNewAccount(1); const addNewAccountResult2 = await metamaskController.addNewAccount(1);
assert.deepEqual( assert.equal(addNewAccountResult1, addNewAccountResult2);
Object.keys(addNewAccountResult1.identities),
Object.keys(addNewAccountResult2.identities),
);
}); });
it('two successive calls with different accountCount give different results', async function () { it('two successive calls with different accountCount give different results', async function () {
await metamaskController.createNewVaultAndKeychain('test@123'); await metamaskController.createNewVaultAndKeychain('test@123');
const addNewAccountResult1 = await metamaskController.addNewAccount(1); const addNewAccountResult1 = await metamaskController.addNewAccount(1);
const addNewAccountResult2 = await metamaskController.addNewAccount(2); const addNewAccountResult2 = await metamaskController.addNewAccount(2);
assert.notDeepEqual(addNewAccountResult1, addNewAccountResult2); assert.notEqual(addNewAccountResult1, addNewAccountResult2);
}); });
}); });

View File

@ -2943,12 +2943,10 @@ export default class MetamaskController extends EventEmitter {
// seek out the first zero balance // seek out the first zero balance
while (lastBalance !== '0x0') { while (lastBalance !== '0x0') {
await this.coreKeyringController.addNewAccount(accounts.length); const { addedAccountAddress } =
await this.coreKeyringController.addNewAccount(accounts.length);
accounts = await this.coreKeyringController.getAccounts(); accounts = await this.coreKeyringController.getAccounts();
lastBalance = await this.getBalance( lastBalance = await this.getBalance(addedAccountAddress, ethQuery);
accounts[accounts.length - 1],
ethQuery,
);
} }
// remove extra zero balance account potentially created from seeking ahead // remove extra zero balance account potentially created from seeking ahead
@ -3394,7 +3392,7 @@ export default class MetamaskController extends EventEmitter {
* Adds a new account to the default (first) HD seed phrase Keyring. * Adds a new account to the default (first) HD seed phrase Keyring.
* *
* @param accountCount * @param accountCount
* @returns {} keyState * @returns {Promise<string>} The address of the newly-created account.
*/ */
async addNewAccount(accountCount) { async addNewAccount(accountCount) {
const isActionMetricsQueueE2ETest = const isActionMetricsQueueE2ETest =
@ -3404,38 +3402,16 @@ export default class MetamaskController extends EventEmitter {
await new Promise((resolve) => setTimeout(resolve, 5_000)); await new Promise((resolve) => setTimeout(resolve, 5_000));
} }
const [primaryKeyring] = this.coreKeyringController.getKeyringsByType( const oldAccounts = await this.coreKeyringController.getAccounts();
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 { addedAccountAddress } =
const oldAccounts = await keyringController.getAccounts(); await this.coreKeyringController.addNewAccount(accountCount);
const keyState = await keyringController.addNewAccount(primaryKeyring);
const newAccounts = await keyringController.getAccounts();
await this.verifySeedPhrase(); if (!oldAccounts.includes(addedAccountAddress)) {
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 { return addedAccountAddress;
...keyringController.memStore.getState(),
identities: oldIdentities,
};
} }
/** /**

View File

@ -737,7 +737,7 @@ describe('MetaMaskController', function () {
metamaskController.keyringController, metamaskController.keyringController,
'addNewAccount', 'addNewAccount',
); );
addNewAccountStub.returns({}); addNewAccountStub.returns('0x123');
getAccountsStub = sinon.stub( getAccountsStub = sinon.stub(
metamaskController.keyringController, metamaskController.keyringController,
@ -818,7 +818,7 @@ describe('MetaMaskController', function () {
await addNewAccount; await addNewAccount;
assert.fail('should throw'); assert.fail('should throw');
} catch (e) { } catch (e) {
assert.equal(e.message, 'MetamaskController - No HD Key Tree found'); assert.equal(e.message, 'No HD keyring found');
} }
}); });
}); });

View File

@ -398,7 +398,7 @@ describe('Actions', () => {
const addNewAccount = background.addNewAccount.callsFake((_, cb) => const addNewAccount = background.addNewAccount.callsFake((_, cb) =>
cb(null, { cb(null, {
identities: {}, addedAccountAddress: '0x123',
}), }),
); );

View File

@ -432,12 +432,11 @@ export function addNewAccount(): ThunkAction<
const oldIdentities = getState().metamask.identities; const oldIdentities = getState().metamask.identities;
dispatch(showLoadingIndication()); dispatch(showLoadingIndication());
let newIdentities; let addedAccountAddress;
try { try {
const { identities } = await submitRequestToBackground('addNewAccount', [ addedAccountAddress = await submitRequestToBackground('addNewAccount', [
Object.keys(oldIdentities).length, Object.keys(oldIdentities).length,
]); ]);
newIdentities = identities;
} catch (error) { } catch (error) {
dispatch(displayWarning(error)); dispatch(displayWarning(error));
throw error; throw error;
@ -445,11 +444,8 @@ export function addNewAccount(): ThunkAction<
dispatch(hideLoadingIndication()); dispatch(hideLoadingIndication());
} }
const newAccountAddress = Object.keys(newIdentities).find(
(address) => !oldIdentities[address],
);
await forceUpdateMetamaskState(dispatch); await forceUpdateMetamaskState(dispatch);
return newAccountAddress; return addedAccountAddress;
}; };
} }