Skip to content

refactor: dedupe common middleware hooks between Eip1193 and CAIP engines #32904

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

Merged
merged 5 commits into from
Jun 20, 2025
Merged
Changes from all 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
261 changes: 99 additions & 162 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6546,6 +6546,100 @@ export default class MetamaskController extends EventEmitter {
);
}

/**
* Creates middleware hooks that are shared between the Eip1193 and Multichain engines.
*
* @private
* @param {string} origin - The connection's origin string.
* @returns {object} The shared hooks.
*/
setupCommonMiddlewareHooks(origin) {
Copy link
Contributor

Choose a reason for hiding this comment

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

thoughts on regrouping these by type and bringing back the comments describing each group? like // network configuration-related

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call! lmk if I got it right 🙏

return {
// Miscellaneous
addSubjectMetadata:
this.subjectMetadataController.addSubjectMetadata.bind(
this.subjectMetadataController,
),
getProviderState: this.getProviderState.bind(this),
handleWatchAssetRequest: this.handleWatchAssetRequest.bind(this),
requestUserApproval:
this.approvalController.addAndShowApprovalRequest.bind(
this.approvalController,
),
getCaveat: ({ target, caveatType }) => {
try {
return this.permissionController.getCaveat(
origin,
target,
caveatType,
);
} catch (e) {
if (e instanceof PermissionDoesNotExistError) {
// suppress expected error in case that the origin
// does not have the target permission yet
} else {
throw e;
}
}

return undefined;
},
requestPermittedChainsPermissionIncrementalForOrigin: (options) =>
this.requestPermittedChainsPermissionIncremental({
...options,
origin,
}),

// Network configuration-related
addNetwork: this.networkController.addNetwork.bind(
this.networkController,
),
updateNetwork: this.networkController.updateNetwork.bind(
this.networkController,
),
setActiveNetwork: async (networkClientId) => {
await this.networkController.setActiveNetwork(networkClientId);
// if the origin has the CAIP-25 permission
// we set per dapp network selection state
if (
this.permissionController.hasPermission(
origin,
Caip25EndowmentPermissionName,
)
) {
this.selectedNetworkController.setNetworkClientIdForDomain(
origin,
networkClientId,
);
}
},
getNetworkConfigurationByChainId:
this.networkController.getNetworkConfigurationByChainId.bind(
this.networkController,
),
getCurrentChainIdForDomain: (domain) => {
const networkClientId =
this.selectedNetworkController.getNetworkClientIdForDomain(domain);
const { chainId } =
this.networkController.getNetworkConfigurationByNetworkClientId(
networkClientId,
);
return chainId;
},

// Web3 shim-related
getWeb3ShimUsageState: this.alertController.getWeb3ShimUsageState.bind(
this.alertController,
),
setWeb3ShimUsageRecorded:
this.alertController.setWeb3ShimUsageRecorded.bind(
this.alertController,
),
rejectApprovalRequestsForOrigin: () =>
this.rejectOriginPendingApprovals(origin),
};
}

/**
* A method for creating an ethereum provider that is safely restricted for the requesting subject.
*
Expand Down Expand Up @@ -6699,25 +6793,18 @@ export default class MetamaskController extends EventEmitter {
engine.push(
createEip1193MethodMiddleware({
subjectType,
...this.setupCommonMiddlewareHooks(origin),

// Miscellaneous
addSubjectMetadata:
this.subjectMetadataController.addSubjectMetadata.bind(
this.subjectMetadataController,
),
metamaskState: this.getState(),
getProviderState: this.getProviderState.bind(this),
getUnlockPromise: this.appStateController.getUnlockPromise.bind(
this.appStateController,
),
handleWatchAssetRequest: this.handleWatchAssetRequest.bind(this),
requestUserApproval:
this.approvalController.addAndShowApprovalRequest.bind(
this.approvalController,
),

sendMetrics: this.metaMetricsController.trackEvent.bind(
this.metaMetricsController,
),

// Permission-related
getAccounts: this.getPermittedAccounts.bind(this, origin),
getCaip25PermissionFromLegacyPermissionsForOrigin:
Expand All @@ -6726,11 +6813,7 @@ export default class MetamaskController extends EventEmitter {
this.permissionController,
origin,
),
requestPermittedChainsPermissionIncrementalForOrigin: (options) =>
this.requestPermittedChainsPermissionIncremental({
...options,
origin,
}),

requestPermissionsForOrigin: (requestedPermissions) =>
this.permissionController.requestPermissions(
{ origin },
Expand All @@ -6755,51 +6838,7 @@ export default class MetamaskController extends EventEmitter {
console.log(e);
}
},
getCaveat: ({ target, caveatType }) => {
try {
return this.permissionController.getCaveat(
origin,
target,
caveatType,
);
} catch (e) {
if (e instanceof PermissionDoesNotExistError) {
// suppress expected error in case that the origin
// does not have the target permission yet
} else {
throw e;
}
}

return undefined;
},
// network configuration-related
setActiveNetwork: async (networkClientId) => {
await this.networkController.setActiveNetwork(networkClientId);
// if the origin has the CAIP-25 permission
// we set per dapp network selection state
if (
this.permissionController.hasPermission(
origin,
Caip25EndowmentPermissionName,
)
) {
this.selectedNetworkController.setNetworkClientIdForDomain(
origin,
networkClientId,
);
}
},
addNetwork: this.networkController.addNetwork.bind(
this.networkController,
),
updateNetwork: this.networkController.updateNetwork.bind(
this.networkController,
),
getNetworkConfigurationByChainId:
this.networkController.getNetworkConfigurationByChainId.bind(
this.networkController,
),
setTokenNetworkFilter: (chainId) => {
const { tokenNetworkFilter } =
this.preferencesController.getPreferences();
Expand All @@ -6812,32 +6851,13 @@ export default class MetamaskController extends EventEmitter {
setEnabledNetworks: (chainIds) => {
this.networkOrderController.setEnabledNetworks(chainIds);
},
getCurrentChainIdForDomain: (domain) => {
const networkClientId =
this.selectedNetworkController.getNetworkClientIdForDomain(domain);
const { chainId } =
this.networkController.getNetworkConfigurationByNetworkClientId(
networkClientId,
);
return chainId;
},

// Web3 shim-related
getWeb3ShimUsageState: this.alertController.getWeb3ShimUsageState.bind(
this.alertController,
),
setWeb3ShimUsageRecorded:
this.alertController.setWeb3ShimUsageRecorded.bind(
this.alertController,
),
updateCaveat: this.permissionController.updateCaveat.bind(
this.permissionController,
origin,
),
hasApprovalRequestsForOrigin: () =>
this.approvalController.has({ origin }),
rejectApprovalRequestsForOrigin: () =>
this.rejectOriginPendingApprovals(origin),
}),
);

Expand Down Expand Up @@ -7157,92 +7177,9 @@ export default class MetamaskController extends EventEmitter {
engine.push(
createMultichainMethodMiddleware({
subjectType,

// Miscellaneous
addSubjectMetadata:
this.subjectMetadataController.addSubjectMetadata.bind(
this.subjectMetadataController,
),
getProviderState: this.getProviderState.bind(this),
handleWatchAssetRequest: this.handleWatchAssetRequest.bind(this),
requestUserApproval:
this.approvalController.addAndShowApprovalRequest.bind(
this.approvalController,
),
getCaveat: ({ target, caveatType }) => {
try {
return this.permissionController.getCaveat(
origin,
target,
caveatType,
);
} catch (e) {
if (e instanceof PermissionDoesNotExistError) {
// suppress expected error in case that the origin
// does not have the target permission yet
} else {
throw e;
}
}

return undefined;
},
addNetwork: this.networkController.addNetwork.bind(
this.networkController,
),
updateNetwork: this.networkController.updateNetwork.bind(
this.networkController,
),
setActiveNetwork: async (networkClientId) => {
await this.networkController.setActiveNetwork(networkClientId);
// if the origin has the CAIP-25 permission
// we set per dapp network selection state
if (
this.permissionController.hasPermission(
origin,
Caip25EndowmentPermissionName,
)
) {
this.selectedNetworkController.setNetworkClientIdForDomain(
origin,
networkClientId,
);
}
},
getNetworkConfigurationByChainId:
this.networkController.getNetworkConfigurationByChainId.bind(
this.networkController,
),
getCurrentChainIdForDomain: (domain) => {
const networkClientId =
this.selectedNetworkController.getNetworkClientIdForDomain(domain);
const { chainId } =
this.networkController.getNetworkConfigurationByNetworkClientId(
networkClientId,
);
return chainId;
},

// Web3 shim-related
getWeb3ShimUsageState: this.alertController.getWeb3ShimUsageState.bind(
this.alertController,
),
setWeb3ShimUsageRecorded:
this.alertController.setWeb3ShimUsageRecorded.bind(
this.alertController,
),

requestPermittedChainsPermissionIncrementalForOrigin: (options) =>
this.requestPermittedChainsPermissionIncremental({
...options,
origin,
}),

rejectApprovalRequestsForOrigin: () =>
this.rejectOriginPendingApprovals(origin),
...this.setupCommonMiddlewareHooks(origin),
}),
);

engine.push(this.metamaskMiddleware);

try {
Expand Down
Loading