Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use addNewAccount from core KeyringController #19814

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions app/scripts/metamask-controller.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
44 changes: 10 additions & 34 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2943,12 +2943,10 @@ export default class MetamaskController extends EventEmitter {

// seek out the first zero balance
while (lastBalance !== '0x0') {
await this.coreKeyringController.addNewAccount(accounts.length);
const { addedAccountAddress } =
await this.coreKeyringController.addNewAccount(accounts.length);
accounts = await this.coreKeyringController.getAccounts();
lastBalance = await this.getBalance(
accounts[accounts.length - 1],
ethQuery,
);
lastBalance = await this.getBalance(addedAccountAddress, ethQuery);
}

// remove extra zero balance account potentially created from seeking ahead
Expand Down Expand Up @@ -3394,7 +3392,7 @@ export default class MetamaskController extends EventEmitter {
* Adds a new account to the default (first) HD seed phrase Keyring.
*
* @param accountCount
* @returns {} keyState
* @returns {Promise<string>} The address of the newly-created account.
*/
async addNewAccount(accountCount) {
const isActionMetricsQueueE2ETest =
Expand All @@ -3404,38 +3402,16 @@ export default class MetamaskController extends EventEmitter {
await new Promise((resolve) => setTimeout(resolve, 5_000));
}

const [primaryKeyring] = this.coreKeyringController.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 oldAccounts = await this.coreKeyringController.getAccounts();

await this.verifySeedPhrase();
const { addedAccountAddress } =
await this.coreKeyringController.addNewAccount(accountCount);

this.preferencesController.setAddresses(newAccounts);
newAccounts.forEach((address) => {
if (!oldAccounts.includes(address)) {
this.preferencesController.setSelectedAddress(address);
}
});

const { identities } = this.preferencesController.store.getState();
return { ...keyState, identities };
if (!oldAccounts.includes(addedAccountAddress)) {
this.preferencesController.setSelectedAddress(addedAccountAddress);
}

return {
...keyringController.memStore.getState(),
identities: oldIdentities,
};
return addedAccountAddress;
mikesposito marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ describe('MetaMaskController', function () {
metamaskController.keyringController,
'addNewAccount',
);
addNewAccountStub.returns({});
addNewAccountStub.returns('0x123');

getAccountsStub = sinon.stub(
metamaskController.keyringController,
Expand Down Expand Up @@ -818,7 +818,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');
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion ui/store/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('Actions', () => {

const addNewAccount = background.addNewAccount.callsFake((_, cb) =>
cb(null, {
identities: {},
addedAccountAddress: '0x123',
}),
);

Expand Down
10 changes: 3 additions & 7 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,24 +432,20 @@ 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;
} finally {
dispatch(hideLoadingIndication());
}

const newAccountAddress = Object.keys(newIdentities).find(
(address) => !oldIdentities[address],
);
await forceUpdateMetamaskState(dispatch);
return newAccountAddress;
return addedAccountAddress;
};
}

Expand Down
Loading