metamask-mobile
metamask-mobile copied to clipboard
refactor: combine AccountsController listMultichainAccounts and listAccounts usage where appropriate
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:
- Both take the same parameters (state and hostname)
- Both extract the subject from state
- Both use similar helper functions (getCaipAccountIdsFromSubject and getEvmAddessesFromSubject)
- Both return empty arrays if no subject is found
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
- [ ] I’ve followed MetaMask Contributor Docs and MetaMask Mobile Coding Standards.
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using JSDoc format if applicable
- [ ] I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.
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.