@@ -69,6 +69,11 @@ export type AccountsControllerSetAccountNameAction = {
69
69
handler : AccountsController [ 'setAccountName' ] ;
70
70
} ;
71
71
72
+ export type AccountsControllerSetAccountNameAndSelectAccountAction = {
73
+ type : `${typeof controllerName } :setAccountNameAndSelectAccount`;
74
+ handler : AccountsController [ 'setAccountNameAndSelectAccount' ] ;
75
+ } ;
76
+
72
77
export type AccountsControllerListAccountsAction = {
73
78
type : `${typeof controllerName } :listAccounts`;
74
79
handler : AccountsController [ 'listAccounts' ] ;
@@ -124,6 +129,7 @@ export type AccountsControllerActions =
124
129
| AccountsControllerListAccountsAction
125
130
| AccountsControllerListMultichainAccountsAction
126
131
| AccountsControllerSetAccountNameAction
132
+ | AccountsControllerSetAccountNameAndSelectAccountAction
127
133
| AccountsControllerUpdateAccountsAction
128
134
| AccountsControllerGetAccountByAddressAction
129
135
| AccountsControllerGetSelectedAccountAction
@@ -437,6 +443,56 @@ export class AccountsController extends BaseController<
437
443
} ) ;
438
444
}
439
445
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
+
440
496
/**
441
497
* Updates the metadata of the account with the given ID.
442
498
*
@@ -449,15 +505,8 @@ export class AccountsController extends BaseController<
449
505
) : void {
450
506
const account = this . getAccountExpect ( accountId ) ;
451
507
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 ) ;
461
510
}
462
511
463
512
const internalAccount = {
@@ -1197,6 +1246,11 @@ export class AccountsController extends BaseController<
1197
1246
this . setAccountName . bind ( this ) ,
1198
1247
) ;
1199
1248
1249
+ this . messagingSystem . registerActionHandler (
1250
+ `${ controllerName } :setAccountNameAndSelectAccount` ,
1251
+ this . setAccountNameAndSelectAccount . bind ( this ) ,
1252
+ ) ;
1253
+
1200
1254
this . messagingSystem . registerActionHandler (
1201
1255
`${ controllerName } :updateAccounts` ,
1202
1256
this . updateAccounts . bind ( this ) ,
0 commit comments