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

entropy-js does not support react native #249

Open
benschac opened this issue Nov 15, 2023 · 4 comments
Open

entropy-js does not support react native #249

benschac opened this issue Nov 15, 2023 · 4 comments

Comments

@benschac
Copy link

iOS Bundling failed 9071ms
The package at "node_modules/@entropyxyz/entropy-js/dist/index.js" attempted to import the Node standard library module "crypto".
It failed because the native React runtime does not include the Node standard library.

entropy-js is shipping with deps that expect crypto from nodejs standard lib, crashing the application

@benschac
Copy link
Author

link to PR for reference: https://github.com/entropyxyz/entropy-client/pull/129

@benschac benschac changed the title entropy-js throws in react native entropy-js does not support react native Nov 16, 2023
@benschac
Copy link
Author

benschac commented Nov 16, 2023

Going to use this a research / context note for internal teammates / outside contributors / users of entropy.js / brain dump

Problem

entropy-js has a requirement on @polkadot/wasm-crypo-wasm which has a requirement on a builtin node package crypto which isn't supported in react-native / web runtimes.

<img width="200" alt="Screenshot 2023-11-16 at 1 57 35 PM" src="https://github.com/entropyxyz/entropy-js/assets/2502947/4bd2ce83-ab8a-46ee-a0fc-a9cd83b177c7">

It's particularly tricky / hacky to get crypto nodejs package to work.

Possible solutions

rn-nodeify --install installs shims for core node modules, see './shims.js' (https://github.com/tradle/rn-nodeify/blob/master/shims.js) for the current mappings. It recurses down node_modules and modifies all the package.json's in there to add/update the browser and react-native fields. It sounds scary because it is. However, it does work.

rn-nodeify --hack Now that you're scared, I should also mention that there are some package-specific hacks (see './pkg-hacks.js' (https://github.com/tradle/rn-nodeify/blob/master/pkg-hacks.js)), for when the React Native packager choked on something that Webpack and Browserify swallowed.

If you're looking for a saner approach, check out ReactNativify (https://github.com/[philikon](https://github.com/philikon)/ReactNativify). I haven't tested it myself, but I think philikon (https://github.com/philikon) will be happy to help.

  • https://github.com/philikon/ReactNativify - a lot of setup, I don't think folks will have the patience for this kind of thing.
  • Create a web / react-native build of the sdk with built in shims/polyfills so the developer using the sdk and just plug-and-play, and not have to do any crazy setup. We already need to do it for web since node:fs isn't a thing.

Tried solutions

Other research

@benschac
Copy link
Author

benschac commented Dec 5, 2023

I posted this above a couple of weeks ago:

https://hackernoon.com/using-core-node-js-modules-in-react-native-apps-64acd4d07140

I'm suggesting something similar for entropy js client.

I assuming entropy-js is working on web because: https://www.npmjs.com/package/crypto-js

Active development of CryptoJS has been discontinued. This library is no longer maintained.

Nowadays, NodeJS and modern browsers have a native Crypto module. The latest version of CryptoJS already uses the native Crypto module for random number generation, since Math.random() is not crypto-safe. Further development of CryptoJS would result in it only being a wrapper of native Crypto. Therefore, development and maintenance has been discontinued, it is time to go for the native crypto module.

https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

@mixmix
Copy link
Collaborator

mixmix commented Apr 16, 2024

I have some experience in this neighborhood. You're right it can get ugly.

Leads for later:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants