LinusU / react-native-get-random-values

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

ethers does not recognize crypto.getRandomValues polyfilled #37

Closed emclab closed 2 years ago

emclab commented 2 years ago

My React Native 0.68.0 app has an issue with ethersjs 5.6.5 which is not caused by react-native-get-random-values (the module). I just post the issue here to see if someone can point out a solution. ethersjs is using crypto.getRandomValues provided by the module to generate random bytes. Here is the order to import all related modules which worked before:

import 'react-native-get-random-values';  //1.8.0
import "@ethersproject/shims";  //for ethers.js
// Import the ethers library
import { ContractFactory, ethers } from "ethers";  //5.6.5

Here is the code to generate random bytes:

console.log("typeof global.crypto.getrandomvalues : ", typeof(global.crypto.getRandomValues));  //function returned
let a = new ethers.utils.randomBytes(32);  //can the randomBytes(32) be generated with rn get random values alone??
var _privateKey = ethers.utils.hexlify(a);  //convert to hex string 

ethers.utils.randomBytes uses crypto.getRandomValues polyfilled by the module and the code above worked for a long time. However all of suddenly it stopped working and throws error:

[Error: no secure random source avaialble (operation="crypto.getRandomValues", code=UNSUPPORTED_OPERATION, version=random/5.6.0)]

Here is a discussion about how to properly import react-native-get-random-values (once again it was not an issue with react-native-get-random-values). Can someone in this community shed some light on how to solve this issue in ethersjs? The app is just looking for cryptographically secure 32 random bytes which can be used as private key. Many thanks.

emclab commented 2 years ago

Before ethers.utils.randomBytes(32), the type of global.crypto.getRandomBytes is function.

LinusU commented 2 years ago

Since this isn't an issue with this package (as I understand it?) and no one has unfortunately been able to provide more information, I'm going to close this.

Unfortunately I'm not familiar with the internals of ethers, so I won't be able to help here...