From 37edafae4bbf176d68b08f173b17c5ee085e65c8 Mon Sep 17 00:00:00 2001 From: Raj Date: Tue, 2 Dec 2025 14:56:24 +0530 Subject: [PATCH] fix(x-randomvalues): prioritize native RN modules over polyfilled crypto --- packages/x-randomvalues/src/react-native.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/x-randomvalues/src/react-native.ts b/packages/x-randomvalues/src/react-native.ts index 13dba2ac9c..6f319205a2 100644 --- a/packages/x-randomvalues/src/react-native.ts +++ b/packages/x-randomvalues/src/react-native.ts @@ -43,14 +43,18 @@ function getRandomValuesRn (output: Uint8Array): Uint8Array { ); } +// Check for native RN modules first (highest priority) +const hasNativeRNModules = !!NativeModules['ExpoRandom'] || !!(NativeModules as RNExt).RNGetRandomValues; +const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function'; + export const getRandomValues = ( - (typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function') - ? getRandomValuesBrowser - : (typeof xglobal['nativeCallSyncHook'] === 'undefined') - ? () => { + hasNativeRNModules + ? getRandomValuesRn + : hasNativeCrypto + ? getRandomValuesBrowser + : () => { throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.'); } - : getRandomValuesRn ); export const crypto = (