I'm trying to use ethers.js inside Expo, but whatever I do it keeps telling me:
WARNING: Missing strong random number source; using weak randomBytes
Is there any way to use a strong random source. I know expo has expo-random, but I see no clear way of making ethers.js use that.
I need to experiment with expo to figure out how to do this, but haven鈥檛 had a chance. I don鈥檛 use Expo myself, but this is a common question.
For most operations you do not need this though, as it is only used for Wallet.createRandom.
thanks for the quick response.
that's actually the thing i need, creating a random wallet! together with signing a transaction.
I've forked ether.js and am trying to get it to work. Will post here on success.
I see. In the meantime, you can implement createRandom easily. :)
function myCreateRandom() {
const randomBytes = ... // Use expo's method to create 16 bytes of random data
const mnemonic = ethers.utils.HDNode.entropyToMnemonic(randomBytes);
return Wallet.fromMnemonic(mnemonic);
}
Which should get you up an running without needing to fork for now. :)
thanks, that worked 馃帀
for the next person trying to get this to work:
import * as Random from 'expo-random';
import 'ethers/dist/shims.js';
import { ethers } from "ethers";
const createWallet = async () => {
const randomBytes = await Random.getRandomBytesAsync(16);
const mnemonic = ethers.utils.HDNode.entropyToMnemonic(randomBytes);
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
return wallet;
}
Thanks! I鈥檒l add that in the docs for Expo. :)
In my case, I have
import * as Random from 'expo-random';
import 'ethers/dist/shims.js';
import { ethers } from 'ethers';
export async function generateMnemonics() {
// return utils.HDNode.entropyToMnemonic(utils.randomBytes(16)).split(' ');
const randomBytes = await Random.getRandomBytesAsync(16);
return utils.HDNode.entropyToMnemonic(randomBytes).split(' ');
}
But I am still getting that warning
yes, i am also still getting the warning.
But I think the unsafe random source is not used since this function takes in random bytes directly from the caller.
Here's a version of TweetNaCl.js for React Native that uses expo-random as the source of randomness https://github.com/rajtatata/react-native-expo-tweet-nacl
Thanks! I鈥檒l add that in the docs for Expo. :)
@ricmoo Did this land anywhere in the v5 docs? Thanks!
I've referenced this loosely here and think the better way to support this is to use the crypto native package it mentions. Does that work in Expo?
I'm going to close this now, but if you'd like anything more added to the docs please continue discussing or re-open the issue.
Thanks! :)
Most helpful comment
thanks, that worked 馃帀
for the next person trying to get this to work: