Skip to content

Conversation

matthiasgeihs
Copy link
Contributor

@matthiasgeihs matthiasgeihs commented Jul 17, 2025

Explanation

New controller for MetaMask Shield.

Functionality:

  • Watch for incoming transactions added to transaction controller.
  • On new transaction, trigger coverage check.
  • Store the coverage check result.
  • Update coverage check data whenever a transaction is re-simulated.

References

Changelog

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

(The last two are not applicable as this is a new package.)

@chaitanyapotti chaitanyapotti added the area-shield Transaction Shield label Jul 18, 2025
Copy link

socket-security bot commented Jul 25, 2025

No dependency changes detected. Learn more about Socket for GitHub.

👍 No dependency changes detected in pull request

@matthiasgeihs matthiasgeihs force-pushed the mg/create-shield-controller branch from 8e91db5 to a6d65ff Compare July 28, 2025 14:03
@matthiasgeihs matthiasgeihs force-pushed the mg/create-shield-controller branch 2 times, most recently from 02bf49f to a9bf413 Compare August 6, 2025 15:34
@matthiasgeihs matthiasgeihs marked this pull request as ready for review August 6, 2025 16:01
@matthiasgeihs matthiasgeihs requested a review from a team as a code owner August 6, 2025 16:01
@matthiasgeihs matthiasgeihs force-pushed the mg/create-shield-controller branch 2 times, most recently from d2a3e58 to 258c03b Compare August 14, 2025 09:39
cursor[bot]

This comment was marked as outdated.

chaitanyapotti
chaitanyapotti previously approved these changes Aug 15, 2025
@matthiasgeihs matthiasgeihs force-pushed the mg/create-shield-controller branch from 6c7f901 to 99a4963 Compare August 15, 2025 09:17
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Looks pretty good! The suggestions I made are mostly about aligning with conventions.

// eslint-disable-next-line no-unmodified-loop-condition
while (!timeoutReached) {
const startTime = Date.now();
const res = await fetch(`${this.#baseUrl}/api/v1/coverage/result`, {
Copy link
Contributor

Choose a reason for hiding this comment

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

This file seems to rely on the global fetch function. We've had problems with this in the past across clients, and we've found it's better to pass in the fetch function so that clients can pass whatever implementation is specific to the platform. Thoughts on having your ShieldRemoteBackend class take a fetch option? (Alternatively, the "data service" pattern I mention in an earlier comment already does this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm ok with passing in an optional fetch function, if this appeared useful in the past.

For context: I've been following the approach of TransactionController here and it seems like it's just relying on the default fetch as well.

I'll address the other minor comments first, and then potentially come back to this one, including your ask to use the "data service" pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you say a bit more about what problems you were previously facing with using fetch? It seems to work fine in the context of TransactionController. Anyhow, I can see that relying on platform-dependent globals can be an issue. Added a fetch parameter.

Copy link
Contributor

@mcmire mcmire Aug 18, 2025

Choose a reason for hiding this comment

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

Can you say a bit more about what problems you were previously facing with using fetch?

I believe that we last encountered issues with fetch in 2023, and at the time, Mobile was using Node 14 in development, and we were using a version of React Native that didn't support Node 16 (and perhaps tools like LavaMoat didn't support Node 16 either). Therefore we couldn't assume that global fetch worked in all platforms. I believe we tried to resolve the differences using cross-fetch, but there were security issues with that package. Simliarly, node-fetch didn't play nice with Mobile and isomorphic-fetch didn't play nice with Snaps. I think we ultimately decided that injecting fetch was the most bulletproof path going forward. (Here is a PR removing node-fetch from a repo and here is a PR removing isomorphic-fetch, with related Slack thread).

However, since then, various parts of our stack has been upgraded (dev version of Node on both platforms, React Native in Mobile, LavaMoat, etc.). In fact I see that Snaps is actively using global fetch here, and of course you point out that TransactionController uses it. So perhaps the advice I gave you is outdated. I'll look into this more tomorrow.

@matthiasgeihs matthiasgeihs requested a review from mcmire August 18, 2025 08:30
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

This looks good to me now!

@matthiasgeihs matthiasgeihs force-pushed the mg/create-shield-controller branch from 6d2343e to 7accf4b Compare August 19, 2025 06:23
@matthiasgeihs matthiasgeihs enabled auto-merge (squash) August 19, 2025 06:23
@matthiasgeihs matthiasgeihs merged commit 5ddc6ba into main Aug 19, 2025
227 checks passed
@matthiasgeihs matthiasgeihs deleted the mg/create-shield-controller branch August 19, 2025 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants