简体   繁体   English

通过 ethers.js 创建与特定提供商连接的随机钱包

[英]Create random Wallet with connection to a specific provider via ethers.js

Problem问题

I am trying to create random wallets via the function Ethers.Wallet.createRandom with a connection to a specific network (like Ropsten for example).我正在尝试通过Ethers.Wallet.createRandom函数创建随机钱包,并连接到特定网络(例如Ropsten )。 The docs specify that I can pass an optional options object .文档指定我可以传递一个可选的options对象

What I tried我试过的

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.不幸的是,那没有用。

Question

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.返回的钱包 object ( docs ) 包含私钥和钱包地址。 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.因此,如果私钥0x123生成地址0x456 ,您始终可以使用相同的0x123私钥在所有网络(主网、Ropsten、Rinkeby 等)上从0x456地址签署交易。

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 ).因此,如果您想限制应用程序可以发送交易的网络,您需要验证提供者的getNetwork()返回值 ( 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.添加options参数:在createRandom() 源代码中,它只使用该参数来影响如何生成私钥的伪随机性。 But again, it's not related to the network(s) on which you're going to be using the wallet.但同样,它与您将使用钱包的网络无关。

import { ethers } from 'ethers';

setWallet(ethers.Wallet.createRandom(['21321']));

Options of createRandom may have the properties: createRandom的选项可能具有以下属性:

extraEntropy — additional entropy to stir into the random source extraEntropy — 加入随机源的额外熵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM