Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snaps): Add support for custom network per Snap #26389

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const getPermissionSpecifications = ({
permissionType: PermissionType.Endowment,
targetName: PermissionNames.permittedChains,
allowedCaveats: [CaveatTypes.restrictNetworkSwitching],
subjectTypes: [SubjectType.Website],
subjectTypes: [SubjectType.Website, SubjectType.Snap],

factory: (permissionOptions, requestData) => {
if (!requestData.approvedChainIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const addEthereumChain = {
getCurrentChainIdForDomain: true,
getCaveat: true,
requestPermittedChainsPermission: true,
grantPermittedChainsPermission: true,
getChainPermissionsFeatureFlag: true,
},
};
Expand All @@ -44,6 +45,7 @@ async function addEthereumChainHandler(
getCurrentChainIdForDomain,
getCaveat,
requestPermittedChainsPermission,
grantPermittedChainsPermission,
getChainPermissionsFeatureFlag,
},
) {
Expand Down Expand Up @@ -162,6 +164,7 @@ async function addEthereumChainHandler(
requestUserApproval,
getCaveat,
requestPermittedChainsPermission,
grantPermittedChainsPermission,
endApprovalFlow,
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { errorCodes, ethErrors } from 'eth-rpc-errors';
import { ApprovalType } from '@metamask/controller-utils';
import { isSnapId } from '@metamask/snaps-utils';

import {
BUILT_IN_INFURA_NETWORKS,
Expand Down Expand Up @@ -200,6 +201,7 @@ export async function switchChain(
requestUserApproval,
getCaveat,
requestPermittedChainsPermission,
grantPermittedChainsPermission,
},
) {
try {
Expand All @@ -214,7 +216,11 @@ export async function switchChain(
permissionedChainIds === undefined ||
!permissionedChainIds.includes(chainId)
) {
await requestPermittedChainsPermission([chainId]);
if (isSnapId(origin)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this functionality?

await grantPermittedChainsPermission([chainId]);
} else {
await requestPermittedChainsPermission([chainId]);
}
}
} else {
await requestUserApproval({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const switchEthereumChain = {
getCurrentChainIdForDomain: true,
requestUserApproval: true,
getChainPermissionsFeatureFlag: true,
grantPermittedChainsPermission: true,
},
};

Expand All @@ -35,6 +36,7 @@ async function switchEthereumChainHandler(
getCurrentChainIdForDomain,
requestUserApproval,
getChainPermissionsFeatureFlag,
grantPermittedChainsPermission,
},
) {
let chainId;
Expand Down Expand Up @@ -91,6 +93,7 @@ async function switchEthereumChainHandler(
requestUserApproval,
getCaveat,
requestPermittedChainsPermission,
grantPermittedChainsPermission,
},
);
}
26 changes: 24 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ export default class MetamaskController extends EventEmitter {
trackMetaMetricsEvent: (...args) =>
this.metaMetricsController.trackEvent(...args),
});

this.networkController.initializeProvider();
this.provider =
this.networkController.getProviderAndBlockTracker().provider;
Expand Down Expand Up @@ -5502,6 +5503,25 @@ export default class MetamaskController extends EventEmitter {
},
},
),
grantPermittedChainsPermission: (chainIds) => {
this.permissionController.grantPermissionsIncremental({
subject: {
origin,
},
requestData: {
approvedChainIds: chainIds,
},
approvedPermissions: {
[PermissionNames.permittedChains]: {
caveats: [
CaveatFactories[CaveatTypes.restrictNetworkSwitching](
chainIds,
),
],
},
Comment on lines +5511 to +5521
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit strange that we have to specify the chain IDs twice 🤔 Seems like the permission specification strictly uses requestData

},
});
},
requestPermissionsForOrigin:
this.permissionController.requestPermissions.bind(
this.permissionController,
Expand Down Expand Up @@ -5550,9 +5570,11 @@ export default class MetamaskController extends EventEmitter {
),
setActiveNetwork: async (networkClientId) => {
await this.networkController.setActiveNetwork(networkClientId);
// if the origin has the eth_accounts permission
// we set per dapp network selection state

// If the origin has the `eth_accounts` permission, or if it's a Snap,
// we set per origin network selection state.
if (
subjectType === SubjectType.Snap ||
this.permissionController.hasPermission(
origin,
PermissionNames.eth_accounts,
Expand Down
Loading
Loading