massalabs / massa-web3

Web3 libraries for Massa
MIT License
29 stars 75 forks source link

Trying to use in React Native #632

Closed raulddan closed 1 day ago

raulddan commented 1 month ago

Im trying to get the wallet address from SecretKey (using React Native) and receive this error:

"crypto.node.createHash is not a function (it is undefined) "

Is there a solution to make it compatible with React Native ? Thanks

gicu-adasanu commented 1 month ago

I debugged at library level and found the following problem, in the @noble library used by massa-web3, the crypto object can have 2 states, node or web. In my case it is node, but it is empty, and in the sha512 method when I try to get the public key from the private one I get this error.

sha512: async (...messages) => { console.log(crypto) const message = concatBytes(...messages); if (crypto.web) { const buffer = await crypto.web.subtle.digest('SHA-512', message.buffer); return new Uint8Array(buffer); } else if (crypto.node) { return Uint8Array.from(crypto.node.createHash('sha512').update(message).digest()); } else { throw new Error("The environment doesn't have sha512 function"); } },

peterjah commented 3 weeks ago

Hello, can you provide a repo to reproduce the issue ? We internally use massa-web3 both in node and and browser (with vite/react).

@raulddan if you have the error crypto.node.createHash is not a function (it is undefined) its mean crypto.web is falsy, meaning the context of browser execution is not detected. Maybe you miss some react plugins to make it work. I thinkg this thread can help: https://stackoverflow.com/questions/29836434/requiring-unknown-module-crypto-in-react-native-environment

gicu-adasanu commented 3 weeks ago

Hello @peterjah, I tried with rn-nodeify. Without success. I published the repository with a simple project, react-native and expo. In the commit is the version without rn-nodeify. I added you too. https://github.com/gicu-adasanu/massa-wallet.git

peterjah commented 3 weeks ago

Hello @peterjah, I tried with rn-nodeify. Without success. I published the repository with a simple project, react-native and expo. In the commit is the version without rn-nodeify. I added you too. https://github.com/gicu-adasanu/massa-wallet.git

ok thx. i cloned your repo, but how to reproduce the error ? i am runing npm run start and i only see a json and no errors

peterjah commented 3 weeks ago

OK its working with npm run web. I just installed some dependencies: npx expo install react-native-web react-dom @expo/metro-runtime and it seems to work as expected: image

gicu-adasanu commented 3 weeks ago

@peterjah it runs through expo, npx expo start. I'll compile the project and run it on my phone to see if it works. on the web I know it works.

gicu-adasanu commented 3 weeks ago

npx expo install react-native-web react-dom @expo/metro-runtime

I tried to install these dependencies, without success :(

peterjah commented 3 weeks ago

I see... I don't know about Expo, and at this point, it is hard to assume that your issue comes from massa-web3 itself, maybe you should try to get some help from Expo community?