I am trying to create random wallets via the function Ethers.Wallet.createRandom
with a connection to a specific network (like Ropsten for example). The docs specify that I can pass an optional options
object.
I tried passing a connection to the network like this:
const networkProvider = await new Ethers.getDefaultProvider(process.env.NETWORK);
const wallet = Ethers.Wallet.createRandom({ provider: networkProvider });
Unfortunately, that did not work.
Is there an option to pass the network when creating the wallet or has this to be done later?
The returned Wallet object (docs) contains the private key and the address of the wallet. However, the address generating algorithm is universal across all networks. So if a private key 0x123
generates an address 0x456
, you can always use the same 0x123
private key to sign transactions from 0x456
address on all networks - the mainnet, Ropsten, Rinkeby, etc.
The network on which you broadcast the transaction depends on what network is your provider connected to. So if your want to limit on what networks the app can send transactions, you need to validate the provider's getNetwork()
returned value (docs).
Ad the options
param: In the createRandom()
source code, it only uses the param to affect the pseudo-randomness of how the private key is generated. But again, it's not related to the network(s) on which you're going to be using the wallet.