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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
262 changes: 100 additions & 162 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6709,6 +6709,101 @@
);
}

/**
* 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
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),

};
}

Check failure on line 6805 in app/scripts/metamask-controller.js

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Delete `⏎`

/**
* A method for creating an ethereum provider that is safely restricted for the requesting subject.
*
Expand Down Expand Up @@ -6855,22 +6950,15 @@
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,
),
Expand All @@ -6882,11 +6970,7 @@
this.permissionController,
origin,
),
requestPermittedChainsPermissionIncrementalForOrigin: (options) =>
this.requestPermittedChainsPermissionIncremental({
...options,
origin,
}),

requestPermissionsForOrigin: (requestedPermissions) =>
this.permissionController.requestPermissions(
{ origin },
Expand All @@ -6911,51 +6995,7 @@
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 @@ -6965,32 +7005,13 @@
});
}
},
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),

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
handleMmiAuthenticate:
Expand Down Expand Up @@ -7320,92 +7341,9 @@
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