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

fix: Check Null for RNRandomBytes #53

Closed
wants to merge 1 commit into from
Closed

fix: Check Null for RNRandomBytes #53

wants to merge 1 commit into from

Conversation

0ndt
Copy link

@0ndt 0ndt commented Jun 3, 2022

lssues: #40

Expo projects that use the react-native-crypto library have problems with react-native-randombytes.

Uncaught Error:

null is not an object (evaluating 'RNRandomBytes.seed')
simulator_screenshot_AF257038-13CE-4691-A4DF-BDD3D8541EAC

means that the RNrandomBytes value is uninitialized. So make sure this value is initialized properly first.

Check Null for RNRandomBytes:

I used if to check if RandomBytes is initialized. This solves the problem with Expo react-native mentioned in issue #40

Testing:

Have been tested on Android simulator, iOS simulator, and Android devices, iOS devices.
_react-native: 0.64
Expo: 44
react-native-crypto: 2.2

@0ndt 0ndt closed this Jun 3, 2022
@0ndt 0ndt reopened this Jun 3, 2022
Comment on lines +15 to 22
if (RNRandomBytes) {
if (RNRandomBytes.seed) {
let seedBuffer = toBuffer(RNRandomBytes.seed)
addEntropy(seedBuffer)
} else {
seedSJCL()
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be more like this?

Suggested change
if (RNRandomBytes) {
if (RNRandomBytes.seed) {
let seedBuffer = toBuffer(RNRandomBytes.seed)
addEntropy(seedBuffer)
} else {
seedSJCL()
}
}
if (RNRandomBytes && RNRandomBytes.seed) {
let seedBuffer = toBuffer(RNRandomBytes.seed)
addEntropy(seedBuffer)
} else {
seedSJCL()
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to make the code look clear, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about clarity, but about the fact that if RNRandomBytes evaluates to false then nothing gets called and in the original code, there's a call to seedSJCL() on the else branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite sure that seedSJCL() will continue to have the lssues #40 , so it is necessary to use the way I used it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're probably right.. I haven't spent time through the library code, I just landed on this PR as a fix to my current blocker.

@0ndt
Copy link
Author

0ndt commented Jun 30, 2022

We have solved the problem by patch-package. This is a truly helpful dependency.

@normanzb
Copy link

normanzb commented Aug 4, 2022

the root question is why RNRandomBytes is not available. it should be there as it is implemented as a native module.
what we found out is when this lib is used as transitive depedency, react-native's cli doesn't do autolinking.
So you need to make sure react-native-randombytes is in your dependency list in package.json file, to be discovered by react native's autolinking. Once that's done you then do again pod install and rebuild the app.

I think whichever the lib that depends on this lib, they need to put this lib into their peerDependency instead.

ref:
react-native-community/cli#1347

@0ndt 0ndt closed this Jan 16, 2023
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

Successfully merging this pull request may close these issues.

3 participants