feat: add sortAccountIdsByLastSelected to getSessionScopes, wallet_getSession, and wallet_createSession#8255
feat: add sortAccountIdsByLastSelected to getSessionScopes, wallet_getSession, and wallet_createSession#8255
Conversation
|
@metamaskbot preview-build |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
| notifications = []; | ||
| } | ||
|
|
||
| const sortedAccounts = sortAccountIdsByLastSelected |
There was a problem hiding this comment.
[nit] this name can be misleading, since we are not always sorting these accounts, sortedAccounts could actually be unsorted if sortAccountIdsByLastSelected is not provided.
we could name the function param caipAccountIds, and this variable accounts for example (open to other suggestions)
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
|
|
||
| const sortedAccounts = sortAccountIdsByLastSelected | ||
| ? sortAccountIdsByLastSelected(accounts) | ||
| : accounts; |
There was a problem hiding this comment.
Sorting before merge loses order for overlapping scopes
Low Severity
sortAccountIdsByLastSelected is applied inside getNormalizedScopesObject (per-scope, before merge), but getSessionScopes then calls mergeNormalizedScopes which concatenates and deduplicates accounts from required and optional scopes via getUniqueArrayItems. When the same scope string appears in both requiredScopes and optionalScopes with different accounts, the merge disrupts the sorted order — the final array reflects required-first insertion order, not the result of sorting all accounts together.


Explanation
@metamask/chain-agnostic-permissionsortAccountIdsByLastSelectedparameter togetSessionScopesfunction(accounts: CaipAccountId[]) => CaipAccountId[]accountsarray within each scope (both required and optional scopes)@metamask/multichain-api-middlewaresortAccountIdsByLastSelectedhook towallet_getSessionhandlergetSessionScopeswhen building session responsesortAccountIdsByLastSelectedhook towallet_createSessionhandlergetSessionScopeswhen building session scopes for approvalReferences
See: MetaMask/metamask-extension#41068
Checklist
Note
Medium Risk
Medium risk because it changes the
wallet_getSession/wallet_createSessionhandler hook contract (new required hook) and can alter returned account ordering, which may impact downstream clients relying on current ordering.Overview
Adds an optional
sortAccountIdsByLastSelectedhook togetSessionScopesin@metamask/chain-agnostic-permission, allowing callers to reorderaccountswithin each scope (applied to both required and optional scopes, otherwise preserving existing order).Updates
@metamask/multichain-api-middlewareto require and pass this hook throughwallet_getSessionandwallet_createSessionwhen buildingsessionScopes, with accompanying test updates and changelog entries.Written by Cursor Bugbot for commit 7fd6922. This will update automatically on new commits. Configure here.