entropyxyz / sdk

Official JavaScript SDK for Entropy blockchain.
GNU Affero General Public License v3.0
7 stars 0 forks source link

`entropy-js` does not support react native #249

Open benschac opened 8 months ago

benschac commented 8 months ago
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 commented 8 months ago

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

benschac commented 8 months ago

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.

\Screenshot 2023-11-16 at 1 57 35 PM](https://github.com/entropyxyz/entropy-js/assets/2502947/4bd2ce83-ab8a-46ee-a0fc-a9cd83b177c7%22%3E)

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.

Tried solutions

Other research

benschac commented 7 months ago

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 commented 3 months ago

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

Leads for later: