我们如何在以太坊中使用web3JS生成地址和私钥?
请有人帮忙。
如果您使用的是 web3.js 1.0.0,请参阅 web3.eth.accounts.create
。
如果您使用的是 0.2x.x,我认为 web3.js 不包含执行此操作的方法,但您可以使用 ethereumjs-wallet
.
web3-eth-accounts
是执行帐户相关操作的独立包,请参阅以下内容
代码片段
const Accounts = require('web3-eth-accounts')
const provider = 'wss://mainnet.infura.io/ws/v3/<your-infura-project-key>'
const accounts = new Accounts(provider)
const wallet = accounts.create()
console.log(`Private Key: n${wallet.privateKey}`)
console.log(`Address: n${wallet.address}`)
输出:
Private Key:
0xa81aa482c47342fb...
Address:
0x85fB46c47D8...