Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 13 additions & 19 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type { WritableDraft } from 'immer/dist/internal.js';
import { cloneDeep } from 'lodash';

import type { MultichainNetworkControllerNetworkDidChangeEvent } from './types';
import type { AccountsControllerStrictState } from './typing';
import type { HdSnapKeyringAccount } from './utils';
import {
getEvmDerivationPathForIndex,
Expand Down Expand Up @@ -479,14 +480,8 @@ export class AccountsController extends BaseController<
};

this.#update((state) => {
// FIXME: Using the state as-is cause the following error: "Type instantiation is excessively
// deep and possibly infinite.ts(2589)" (https://github.com/MetaMask/utils/issues/168)
// Using a type-cast workaround this error and is slightly better than using a @ts-expect-error
// which sometimes fail when compiling locally.
(state as AccountsControllerState).internalAccounts.accounts[account.id] =
internalAccount;
(state as AccountsControllerState).internalAccounts.selectedAccount =
account.id;
state.internalAccounts.accounts[account.id] = internalAccount;
state.internalAccounts.selectedAccount = account.id;
});

this.messagingSystem.publish(
Expand Down Expand Up @@ -529,12 +524,7 @@ export class AccountsController extends BaseController<
};

this.#update((state) => {
// FIXME: Using the state as-is cause the following error: "Type instantiation is excessively
// deep and possibly infinite.ts(2589)" (https://github.com/MetaMask/utils/issues/168)
// Using a type-cast workaround this error and is slightly better than using a @ts-expect-error
// which sometimes fail when compiling locally.
(state as AccountsControllerState).internalAccounts.accounts[accountId] =
internalAccount;
state.internalAccounts.accounts[accountId] = internalAccount;
});

if (metadata.name) {
Expand Down Expand Up @@ -615,9 +605,11 @@ export class AccountsController extends BaseController<
*/
loadBackup(backup: AccountsControllerState): void {
if (backup.internalAccounts) {
this.update((currentState) => {
currentState.internalAccounts = backup.internalAccounts;
});
this.update(
(currentState: WritableDraft<AccountsControllerStrictState>) => {
currentState.internalAccounts = backup.internalAccounts;
},
);
}
}

Expand Down Expand Up @@ -906,13 +898,15 @@ export class AccountsController extends BaseController<
*
* @param callback - Callback for updating state, passed a draft state object.
*/
#update(callback: (state: WritableDraft<AccountsControllerState>) => void) {
#update(
callback: (state: WritableDraft<AccountsControllerStrictState>) => void,
) {
// The currently selected account might get deleted during the update, so keep track
// of it before doing any change.
const previouslySelectedAccount =
this.state.internalAccounts.selectedAccount;

this.update((state) => {
this.update((state: WritableDraft<AccountsControllerStrictState>) => {
callback(state);

// If the account no longer exists (or none is selected), we need to re-select another one.
Expand Down
35 changes: 35 additions & 0 deletions packages/accounts-controller/src/typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { KeyringAccountEntropyOptions } from '@metamask/keyring-api';
import type { InternalAccount } from '@metamask/keyring-internal-api';

import type { AccountsControllerState } from './AccountsController';

/**
* Type constraint to ensure a type is compatible with {@link AccountsControllerState}.
* If the constraint is not matching, this type will resolve to `never` and thus, fails
* to compile.
*/
type IsAccountControllerState<Type extends AccountsControllerState> = Type;

/**
* A type compatible with {@link InternalAccount} which removes any use of recursive-type.
*/
export type StrictInternalAccount = Omit<InternalAccount, 'options'> & {
// Use stricter options, which are relying on `Json` (which sometimes
// cause compiler errors because of instanciation "too deep".
// In anyway, we should rarely have to use those "untyped" options.
options: {
entropy?: KeyringAccountEntropyOptions;
exportable?: boolean;
};
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Incompatible Account Types Cause Assignment Issues

The StrictInternalAccount type's options property is overly restrictive, only allowing entropy and exportable. However, InternalAccount objects, which are still used at runtime and in backups, contain additional options properties like entropySource, derivationPath, and groupIndex. This type incompatibility causes issues when assigning InternalAccount objects to StrictInternalAccount properties, notably in the #update and loadBackup methods.

Additional Locations (3)

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, but that's fine. We have no real use of those options, also, we can still update this type if we need too. As long as we don't use any recursive-type.

I'd just ignore this for now. Again, we're just narrowing a type, but we're not doing any runtime change at all, so all in all, this will behave the same.

Lastly, all those #update/update calls are done internally, so if we need to wider the type, it won't bleed out on the public interface of this controller. So we're good on that front too.


/**
* A type compatible with {@link AccountControllerState} which can be used to
* avoid recursive-type issue with `internalAccounts`.
*/
export type AccountsControllerStrictState = IsAccountControllerState<{
internalAccounts: {
accounts: Record<InternalAccount['id'], StrictInternalAccount>;
selectedAccount: InternalAccount['id'];
};
}>;
Loading