metamask-mobile icon indicating copy to clipboard operation
metamask-mobile copied to clipboard

refactor: combine AccountsController listMultichainAccounts and listAccounts usage where appropriate

Open ffmcgee725 opened this issue 4 months ago • 6 comments

Description

In app/core/Permissions/index.ts, we have some utility functions which are looking to reduce repeated code on, such as getPermittedCaipAccountIdsByHostname and getPermittedEvmAddressesByHostname, which share very similar structure:

The helper functions getCaipAccountIdsFromSubject and getEvmAddessesFromSubject also share similar structure:

  • Both extract caveats from the subject's permissions
  • Both find a specific caveat by type
  • Both return empty arrays if no caveats are found

The refactoring I propose introduces two generic helper functions:

  • getDataFromSubject<T>: A generic function that handles the common pattern of extracting data from a subject's permissions and caveats. It takes a type parameter T and an extractor function that knows how to get the specific type of data from a caveat.
  • getPermittedDataByHostname<T>: A generic function that handles the common pattern of getting permitted data by hostname. It takes a type parameter T and an extractor function that knows how to get the specific type of data from a subject.

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/5115

Manual testing steps

There should be no user or dapp facing difference in behavior.

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

ffmcgee725 avatar Jun 13 '25 10:06 ffmcgee725