LinusU / react-native-get-random-values

A small implementation of `getRandomValues` for React Native
MIT License
350 stars 48 forks source link

Expo 48 getRandomValues undefined #51

Open Willie-Pretorius opened 1 year ago

Willie-Pretorius commented 1 year ago

Please Help I am upgrading an expo application to expo-48. "react-native": "0.71.14", "react-native-get-random-values": "~1.9.0", "expo-crypto": "12.2.1", "expo": "48.0.21"

I have added some console logs to try and find out whats happening and it appears that I am getting ExpoCrypto but getRandomValues is undefined.

function getRandomValues (array) {
  if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray)) {
    throw new TypeMismatchError('Expected an integer array')
  }

  if (array.byteLength > 65536) {
    throw new QuotaExceededError('Can only request a maximum of 65536 bytes')
  }

  // Expo SDK 48+
  if (global.expo){
    console.log("global.expo")
  }
  if (global.expo.modules){
    console.log("global.expo.modules ")
  }
  if (global.expo.modules.ExpoCrypto){
    console.log("global.expo.modules.ExpoCrypto")
  }
  if (global.expo.modules.ExpoCrypto.getRandomValues){
    console.log("global.expo.modules.ExpoCrypto.getRandomValues")
  } else console.log("global.expo.modules.ExpoCrypto.getRandomValues is " + typeof global.expo.modules.ExpoCrypto.getRandomValues)

   // Expo SDK 48+
   if (global.expo && global.expo.modules && global.expo.modules.ExpoCrypto && global.expo.modules.ExpoCrypto.getRandomValues) {
    // ExpoCrypto.getRandomValues doesn't return the array
    global.expo.modules.ExpoCrypto.getRandomValues(array)
    return array
  }

  // Calling getRandomBase64 in debug mode leads to the error
  // "Calling synchronous methods on native modules is not supported in Chrome".
  // So in that specific case we fall back to just using Math.random.
  if (__DEV__) {
    if (typeof global.nativeCallSyncHook === 'undefined') {
      return insecureRandomValues(array)
    }
  }

  base64Decode(getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength))

  return array
}

//Output -------------------------------------------------------------- LOG global.expo LOG global.expo.modules LOG global.expo.modules.ExpoCrypto LOG global.expo.modules.ExpoCrypto.getRandomValues is undefined LOG global.expo LOG global.expo.modules LOG global.expo.modules.ExpoCrypto LOG global.expo.modules.ExpoCrypto.getRandomValues is undefined

-Update

I am just building to expo-go with metro builder. My linking is being done by "expo-modules-autolinking": "~1.1.0 || ~1.2.0",