MetaMask / snaps

Extend the functionality of MetaMask using Snaps
https://metamask.io/snaps/
Other
724 stars 557 forks source link

Using `fetch` in a Snap #224

Closed anudit closed 2 years ago

anudit commented 2 years ago

Hey, I'm trying to use fetch in a snap and can't seem to get it to work,

I can see that there is some work on Permissions and Endowments here https://github.com/MetaMask/snaps-skunkworks/pull/154 & https://github.com/MetaMask/snaps-skunkworks/blob/main/packages/controllers/src/permissions/README.md that should help with using fetch but I couldn't find docs on how to get it to work with a Snap.

Trying extra libraries I run into the following errors,

With "node-fetch": "^3.2.0"

Build error: Can't walk dependency graph: ENOENT: no such file or directory, lstat 'D:\Users\anudit\Documents\Github\omnidsnap\node:http'
    required by D:\Users\anudit\Documents\Github\omnidsnap\node_modules\node-fetch\src\index.js
error Command failed with exit code 1.

Running into this with "@web-std/fetch@3.0.3"

Build error: Can't walk dependency graph: Cannot find module 'stream/web' from 'D:\Users\anudit\Documents\Github\omnidsnap\node_modules\@web-std\stream\src\stream.cjs'
    required by D:\Users\anudit\Documents\Github\omnidsnap\node_modules\@web-std\stream\src\stream.cjs

Running into this with "isomorphic-unfetch@3.1.0"

Build success: 'src\index.js' bundled as 'dist\bundle.js'!

events.js:377
      throw er; // Unhandled 'error' event
      ^
ReferenceError [Error]: self is not defined
  at Object.1.unfetch (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:4:18)
  at o (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:1:265)
  at eval (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:1:316)
  at Object.3.isomorphic-unfetch (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:61:15)
  at o (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:1:265)
  at r (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:1:431)
  at Proxy.eval (eval at <anonymous> (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10)), <anonymous>:1:460)
  at Object.eval (eval at makeEvaluateFactory (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:1401:10), <anonymous>:8:16)
  at safeEvaluate (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:2196:14)       
  at compartmentEvaluate (D:\Users\anudit\Documents\Github\omnidsnap\node_modules\ses\dist\ses.cjs:2330:10)
Emitted 'error' event on process instance at:

Snap Codebase : https://github.com/anudit/omnidsnap

rekmarks commented 2 years ago

Hello @anudit, thank you for creating this issue! To use fetch, your snap manifest needs to have the permission endowment:network-access in initialPermissions in snap.manifest.json. When the snap is executed by MetaMask, fetch will be available to use, so you don't need to use fetch polyfills like node-fetch, isomorphic-fetch etc. as production dependencies.

Let me know if that helps, or if you have any other issues! The self issue in particular may be unrelated, and if so, you will have to manually fix it in a post-processing step as e.g. the Filecoin snap authors did here. In the near future snaps-cli will be able to do such postprocessing for you, but for now it's manual!

anudit commented 2 years ago

Hey @rekmarks, that works perfectly. Thanks!

PS: Any ideas on https://github.com/MetaMask/metamask-extension/issues/12444 ?

rekmarks commented 2 years ago

Great! I replied on that other issue you asked about.

I'll close this since it appears to be resolved. Thanks again!