Skip to content

Commit

Permalink
feat: Editing flow (#26635)
Browse files Browse the repository at this point in the history
This PR is to add editing flow while switching networks via dapp
This PR also includes the connections flow which includes editing


## **Related issues**

Fixes: 
1. MetaMask/MetaMask-planning#2683
2. MetaMask/MetaMask-planning#2684
3. MetaMask/MetaMask-planning#2697
4. MetaMask/MetaMask-planning#2698
5. MetaMask/MetaMask-planning#2663

## **Manual testing steps**

1. run the extension with CHAIN_PERMISSIONS flag
2. Connect to a dapp 
3. Switch network via metamask
6. toast should show up
7. Then click on edit permissions, it should route to new permissions
screen
8. We have edit networks and edit accounts modal there

## **Screenshots/Recordings**


### **Before**

NA
### **After**



https://github.com/user-attachments/assets/a9dbaecf-c96b-485c-b639-f50cbe30317c


https://github.com/user-attachments/assets/8638bb1f-3739-4a2b-84ae-dca24f8abe6d



## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
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.

---------

Co-authored-by: Alex <adonesky@gmail.com>
Co-authored-by: Jiexi Luan <jiexiluan@gmail.com>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent 39b9985 commit 7b4eb5b
Show file tree
Hide file tree
Showing 60 changed files with 3,832 additions and 313 deletions.
53 changes: 53 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 62 additions & 9 deletions app/scripts/controllers/permissions/background-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import {
CaveatTypes,
RestrictedMethods,
} from '../../../../shared/constants/permissions';
import { CaveatFactories } from './specifications';
import { CaveatFactories, PermissionNames } from './specifications';

export function getPermissionBackgroundApiMethods(permissionController) {
const addMoreAccounts = (origin, accountOrAccounts) => {
const accounts = Array.isArray(accountOrAccounts)
? accountOrAccounts
: [accountOrAccounts];
const addMoreAccounts = (origin, accounts) => {
const caveat = CaveatFactories.restrictReturnedAccounts(accounts);

permissionController.grantPermissionsIncremental({
Expand All @@ -20,11 +17,21 @@ export function getPermissionBackgroundApiMethods(permissionController) {
});
};

return {
addPermittedAccount: (origin, account) => addMoreAccounts(origin, account),
const addMoreChains = (origin, chainIds) => {
const caveat = CaveatFactories.restrictNetworkSwitching(chainIds);

permissionController.grantPermissionsIncremental({
subject: { origin },
approvedPermissions: {
[PermissionNames.permittedChains]: { caveats: [caveat] },
},
});
};

// To add more than one account when already connected to the dapp
addMorePermittedAccounts: (origin, accounts) =>
return {
addPermittedAccount: (origin, account) =>
addMoreAccounts(origin, [account]),
addPermittedAccounts: (origin, accounts) =>
addMoreAccounts(origin, accounts),

removePermittedAccount: (origin, account) => {
Expand Down Expand Up @@ -57,6 +64,52 @@ export function getPermissionBackgroundApiMethods(permissionController) {
}
},

addPermittedChain: (origin, chainId) => addMoreChains(origin, [chainId]),
addPermittedChains: (origin, chainIds) => addMoreChains(origin, chainIds),

removePermittedChain: (origin, chainId) => {
const { value: existingChains } = permissionController.getCaveat(
origin,
PermissionNames.permittedChains,
CaveatTypes.restrictNetworkSwitching,
);

const remainingChains = existingChains.filter(
(existingChain) => existingChain !== chainId,
);

if (remainingChains.length === existingChains.length) {
return;
}

if (remainingChains.length === 0) {
permissionController.revokePermission(
origin,
PermissionNames.permittedChains,
);
} else {
permissionController.updateCaveat(
origin,
PermissionNames.permittedChains,
CaveatTypes.restrictNetworkSwitching,
remainingChains,
);
}
},

requestAccountsAndChainPermissionsWithId: async (origin) => {
const id = nanoid();
permissionController.requestPermissions(
{ origin },
{
[PermissionNames.eth_accounts]: {},
[PermissionNames.permittedChains]: {},
},
{ id },
);
return id;
},

requestAccountsPermissionWithId: async (origin) => {
const id = nanoid();
permissionController.requestPermissions(
Expand Down
Loading

0 comments on commit 7b4eb5b

Please sign in to comment.