mvayngrib / react-native-randombytes

randomBytes for react-native
MIT License
83 stars 95 forks source link

null is not an object (evaluating RNRandomBytes.seed) #40

Open JUDOKICK opened 4 years ago

JUDOKICK commented 4 years ago

I am currently unable to use react-native-crypto because randombytes throws this null error. I am using an ejected Expo, SDK35, and all steps followed from react-native-crypto I have linked correctly, used rn-nodeify and imported the shim file and still it is not working.

I have tried possibly all combinations of steps and procedures for installations and none of them seem to work for the latest expo/react-native sdks.

It has to do with this section of code

let sjcl = require('sjcl') `let RNRandomBytes = require('react-native').NativeModules.RNRandomBytes

function noop () {}

function toBuffer (nativeStr) { return new Buffer(nativeStr, 'base64') }

function init () { if (RNRandomBytes.seed) { let seedBuffer = toBuffer(RNRandomBytes.seed) addEntropy(seedBuffer) } else { seedSJCL() } } The error is line 15if (RNRandomBytes.seed)` meaning that it never gets initialized somehow

LuisAcerv commented 4 years ago

Did you found a solution for this?

JUDOKICK commented 4 years ago

No unfortunately. I ended up just using the native crypto libraries in Android to get it to work. Very frustrating issue though.

Yasir-Shah commented 4 years ago

@JUDOKICK I'm having the same issue, please help is there any workaround? I spent 5days on it but that issue is still there how can I fix this problem? What did you in your case? how did you implement a native crypto library in android?

JUDOKICK commented 4 years ago

Using Java and linking the two with an ejected version of my app. There is no fix for this. React-Native won't support new NodeJs libraries. This used to work on older versions. But now all the libraries that end up needing the NodeJs libraries use newer versions. Unless you feel like digging through your dependencies and locking each specific version just to get this to work. I highly suggest ejecting and using the native java/swift libraries.

Yasir-Shah commented 4 years ago

ok, thanks may I know how can I use native libraries? Can you share a code example like you did it would be really helpful for me? Please help

JUDOKICK commented 4 years ago

Unfortunately, you're going to have to look on your own for examples on how to do that as it's different for every implementation.

On Fri, 1 May 2020, 11:56 Yasir Shah, notifications@github.com wrote:

ok, thanks may I know how can I use native libraries? Please help

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mvayngrib/react-native-randombytes/issues/40#issuecomment-622343810, or unsubscribe https://github.com/notifications/unsubscribe-auth/AELX7GYLUNWOBWD3A753LBLRPKTFXANCNFSM4KSRSW2A .

starpl commented 3 years ago

I found a temporary solution to this problem well maybe not temporary but its kind of hack

To add missing native module you need to manually add specific files from this repo to your project

For example, if you need this to work on Android, you have to copy two files in android/src/main/java/com/bitgo/randombytes/ You need to use those two files like if you wanted to implement your own native modules; you can find guide how to connect everything right here

And after that, in debug mode I was able to see that native module is not null and everything works fine

jxiu0129 commented 3 years ago

In my case, I had react-native-randombytes installed. Maybe you did the react-native link or react-native link react-native-randombytes as me. I opened podfile and it did link for us. However, if you opened pod.lock and looked for it, it did not auto install for us. I went to ios folder and pod install, it finally works. this issue wasted me like 5 days.

ebma commented 3 years ago

I think expo is the problem here. I tried setting it up with this boilerplate where you can specify if you want to use expo for your project or not. Without expo it is working fine and with expo it fails with this error.

Just wanted to leave this comment in case it helps anyone pin down the issue.

adamsr123 commented 2 years ago

This was a problem for me while working with expo SDK and I needed rn-nodeify:

  1. Run postinstall rn-nodeify.
  2. Removed the postinstall and react-native-randombytes
  3. expo install react-native-randombytes

I think that by running the 3rd command with expo and not with rn-nodeify it linked the needed dependencies.

Update: It created another problem, so didn't fix it. I think metro config fixed the problem.

EL-MehdiLoukach commented 2 years ago

I have an expo application, I added the import statement to my code and suddenly faced this error ? is there anyway to make it work with expo ?

alexanderbkl commented 2 years ago

I fixed it by opening the file explorer and explicitly searched for all the files inside my project that contain "RNRandomBytes.seed" text in them and then, inside all of the file indexes found, I just replace the init() function with this code:

function init () {
  if (RNRandomBytes) {
    if (RNRandomBytes.seed) {
      let seedBuffer = toBuffer(RNRandomBytes.seed)
      addEntropy(seedBuffer)
    } else {
      seedSJCL()
    }
  }
}
mohammadsadeghforoughi commented 2 years ago

I still have this issue with expo

StuartFlutter commented 2 years ago

3. expo install react-native-randombytes

Can you elaborate on exactly how this fixed your issue please? I have the same error and tried this hacky approach only to have the app fail in a similar fashion - "o.default.randomBytes is not a function"

StuartFlutter commented 2 years ago

3. expo install react-native-randombytes

Does your app not need RNRandomBytes? Do you do any crypto operations in your app?

I am struggling to get this fixed.

andresmejia3 commented 2 years ago

In my case, I had react-native-randombytes installed. Maybe you did the react-native link or react-native link react-native-randombytes as me. I opened podfile and it did link for us. However, if you opened pod.lock and looked for it, it did not auto install for us. I went to ios folder and pod install, it finally works. this issue wasted me like 5 days.

You are a god among men