Skip to content

fix(x-randomvalues): prioritize native RN modules over polyfilled crypto#2013

Merged
rajk93 merged 2 commits intomasterfrom
rk/fix-rn-randomvalues
Dec 9, 2025
Merged

fix(x-randomvalues): prioritize native RN modules over polyfilled crypto#2013
rajk93 merged 2 commits intomasterfrom
rk/fix-rn-randomvalues

Conversation

@rajk93
Copy link
Copy Markdown
Contributor

@rajk93 rajk93 commented Dec 2, 2025

Problem

In React Native environments, getRandomValues() was incorrectly using the browser implementation instead of native modules (RNGetRandomValues or ExpoRandom), even when these native modules were installed and available.

Ref - polkadot-js/wasm#598

Root Cause

The detection logic in src/react-native.ts checked for the presence of xglobal.crypto first. When libraries like react-native-get-random-values polyfill the global crypto object, this check would pass and use the browser implementation, never checking if native modules were available for React Native.

Solution

Inverted the priority order of checks:

Before:

  1. Check if xglobal.crypto exists -> use browser implementation
  2. Check if in RN environment -> use native modules

After:

  1. Check if native modules exist (RNGetRandomValues or ExpoRandom) -> use native implementation
  2. Fallback to xglobal.crypto if available -> use browser implementation
  3. Throw error if nothing is available

@rajk93 rajk93 marked this pull request as ready for review December 2, 2025 11:27
Copy link
Copy Markdown
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

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

I am a bit on the fence about this since React Native has historically always had issues with pjs.

IMO support for RN should be a a bottom of the barrel priority.

@rajk93 rajk93 merged commit 440b146 into master Dec 9, 2025
4 checks passed
@rajk93 rajk93 deleted the rk/fix-rn-randomvalues branch December 9, 2025 07:31
@polkadot-js-bot
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Dec 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants