Skip to content

Commit f2d8196

Browse files
committed
feat(accounts-controller): add new setAccountNameAndSelectAccount action
1 parent ce61b87 commit f2d8196

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

packages/accounts-controller/src/AccountsController.ts

+63-9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export type AccountsControllerSetAccountNameAction = {
6969
handler: AccountsController['setAccountName'];
7070
};
7171

72+
export type AccountsControllerSetAccountNameAndSelectAccountAction = {
73+
type: `${typeof controllerName}:setAccountNameAndSelectAccount`;
74+
handler: AccountsController['setAccountNameAndSelectAccount'];
75+
};
76+
7277
export type AccountsControllerListAccountsAction = {
7378
type: `${typeof controllerName}:listAccounts`;
7479
handler: AccountsController['listAccounts'];
@@ -124,6 +129,7 @@ export type AccountsControllerActions =
124129
| AccountsControllerListAccountsAction
125130
| AccountsControllerListMultichainAccountsAction
126131
| AccountsControllerSetAccountNameAction
132+
| AccountsControllerSetAccountNameAndSelectAccountAction
127133
| AccountsControllerUpdateAccountsAction
128134
| AccountsControllerGetAccountByAddressAction
129135
| AccountsControllerGetSelectedAccountAction
@@ -437,6 +443,56 @@ export class AccountsController extends BaseController<
437443
});
438444
}
439445

446+
/**
447+
* Sets the name of the account with the given ID and select it.
448+
*
449+
* @param accountId - The ID of the account to set the name for and select.
450+
* @param accountName - The new name for the account.
451+
* @throws An error if an account with the same name already exists.
452+
*/
453+
setAccountNameAndSelectAccount(accountId: string, accountName: string): void {
454+
const account = this.getAccountExpect(accountId);
455+
456+
this.#assertAccountCanBeRenamed(account, accountName);
457+
458+
const internalAccount = {
459+
...account,
460+
metadata: {
461+
...account.metadata,
462+
name: accountName,
463+
},
464+
};
465+
466+
this.#update((state) => {
467+
const { internalAccounts } = state;
468+
// FIXME: Using the state as-is cause the following error: "Type instantiation is excessively
469+
// deep and possibly infinite.ts(2589)" (https://github.com/MetaMask/utils/issues/168)
470+
// Using a type-cast workaround this error and is slightly better than using a @ts-expect-error
471+
// which sometimes fail when compiling locally.
472+
internalAccounts.accounts[accountId] = internalAccount;
473+
474+
internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
475+
internalAccounts.selectedAccount = account.id;
476+
});
477+
478+
this.messagingSystem.publish(
479+
'AccountsController:accountRenamed',
480+
internalAccount,
481+
);
482+
}
483+
484+
#assertAccountCanBeRenamed(account: InternalAccount, accountName: string) {
485+
if (
486+
this.listMultichainAccounts().find(
487+
(internalAccount) =>
488+
internalAccount.metadata.name === accountName &&
489+
internalAccount.id !== account.id,
490+
)
491+
) {
492+
throw new Error('Account name already exists');
493+
}
494+
}
495+
440496
/**
441497
* Updates the metadata of the account with the given ID.
442498
*
@@ -449,15 +505,8 @@ export class AccountsController extends BaseController<
449505
): void {
450506
const account = this.getAccountExpect(accountId);
451507

452-
if (
453-
metadata.name &&
454-
this.listMultichainAccounts().find(
455-
(internalAccount) =>
456-
internalAccount.metadata.name === metadata.name &&
457-
internalAccount.id !== accountId,
458-
)
459-
) {
460-
throw new Error('Account name already exists');
508+
if (metadata.name) {
509+
this.#assertAccountCanBeRenamed(account, metadata.name);
461510
}
462511

463512
const internalAccount = {
@@ -1197,6 +1246,11 @@ export class AccountsController extends BaseController<
11971246
this.setAccountName.bind(this),
11981247
);
11991248

1249+
this.messagingSystem.registerActionHandler(
1250+
`${controllerName}:setAccountNameAndSelectAccount`,
1251+
this.setAccountNameAndSelectAccount.bind(this),
1252+
);
1253+
12001254
this.messagingSystem.registerActionHandler(
12011255
`${controllerName}:updateAccounts`,
12021256
this.updateAccounts.bind(this),

packages/accounts-controller/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type {
44
AccountsControllerGetStateAction,
55
AccountsControllerSetSelectedAccountAction,
66
AccountsControllerSetAccountNameAction,
7+
AccountsControllerSetAccountNameAndSelectAccountAction,
78
AccountsControllerListAccountsAction,
89
AccountsControllerListMultichainAccountsAction,
910
AccountsControllerUpdateAccountsAction,

0 commit comments

Comments
 (0)