MetaMask / MetaMask/metamask-extension

Bug: TypeError in getSelectedMultichainAccount when account.scopes is undefined (Solana network)

Open
#41,962 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

INVALID-ISSUE-TEMPLATE Sev2-normal team-accounts-framework type-bug
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

## Description

During controller initialization in `setupControllerEventSubscriptions()`, calling `getSelectedMultichainAccount` for the Solana network (`solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp`) throws a `TypeError` because `account.scopes` is `undefined` for at least one account, causing `isScopeEqualToAny` to fail when it tries to call `.some()` on an undefined value.

**Sentry Issue**: [METAMASK-YHAH](https://metamask.sentry.io/issues/7239174035/)

## Error

```
Error: Failed to get selected multichain account for network: solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
Caused by: TypeError: can't access property "some", t is undefined
```

## Stack Trace

```
app/scripts/metamask-controller.js — setupControllerEventSubscriptions
NON_EVM_ACCOUNT_CHANGED_CONFIGS.forEach(({ network }) => { ... })

@metamask/accounts-controller — AccountsController.getSelectedMultichainAccount
const accounts = this.listMultichainAccounts(chainId);

@metamask/accounts-controller — AccountsController.listMultichainAccounts
return accounts.filter((account) => isScopeEqualToAny(chainId, account.scopes));

@metamask/keyring-utils — isScopeEqualToAny
return scopes.some((other) => isScopeEqual(scope, other));
// ^^^ `scopes` (i.e. account.scopes) is undefined
```

## Root Cause

`AccountsController.listMultichainAccounts` filters internal accounts by calling `isScopeEqualToAny(chainId, account.scopes)`. The `isScopeEqualToAny` function in `@metamask/keyring-utils` calls `scopes.some(...)` without guarding against `scopes` being `undefined`. If any internal account has an undefined `scopes` field (e.g. a corrupted or partially-migrated account), this throws a `TypeError`.

The error is caught in `setupControllerEventSubscriptions()` (~line 1841) and sent to Sentry, so it does not crash the extension — but it indicates a data integrity issue and prevents proper initialization of `lastSelectedAccountAddressByNetwork` for Solana.

## Relevant Code

`app/scripts/metamask-controller.js` (~line 1832):
```js
NON_EVM_ACCOUNT_CHANGED_CONFIGS.forEach(({ network }) => {
try {
lastSelectedAccountAddressByNetwork[network] =
this.accountsController.getSelectedMultichainAccount(network)?.address;
} catch (err) {
// This scenario shouldn't occur, but if it does, we track it for debugging
const error = new Error(
`Failed to get selected multichain account for network: ${network}`,
{ cause: err },
);
captureException(error);
}
});
```

## Environment

- **Extension version**: 13.20.1
- **Manifest**: MV2
- **Browser**: Firefox 149.0
- **OS**: Windows ≥10
- **Environment**: Production

## Possible Fix (AI suggested)

The fix could be applied at one or more levels:
1. **`@metamask/keyring-utils` — `isScopeEqualToAny`**: Guard against `scopes` being `undefined`/`null` (e.g. `return Array.isArray(scopes) && scopes.some(...)`).
2. **`@metamask/accounts-controller` — `listMultichainAccounts`**: Filter out accounts with undefined/empty `scopes` before calling `isScopeEqualToAny`.
3. **Migration**: Add a state migration to ensure all existing internal accounts have a valid (possibly empty) `scopes` array.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in app/scripts/metamask-controller.js at setupControllerEventSubscriptions(), then trace AccountsController.getSelectedMultichainAccount and listMultichainAccounts into isScopeEqualToAny in @metamask/keyring-utils. Determine where undefined account.scopes should be handled, including whether existing accounts need migration. Done means Solana initialization no longer throws or reports this Sentry error when an account lacks scopes.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, blockchain
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.