我正在尝试与 进行交互geth
,并且我需要创建一个新的存款地址(并能够控制它)。我如何使用 web3.js 来实现这个?
小智 9
您可以使用该Web3.eth.accounts.create()
功能。它将返回一个您可以控制的帐户对象。
https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html
例子:
var Web3 = require("web3");
var web3 = new Web3('http://localhost:8545'); // your geth
var account = web3.eth.accounts.create();
Run Code Online (Sandbox Code Playgroud)
您也可以使用该web3.eth.personal.newAccount()
功能。
http://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#newaccount
查看此博客文章以帮助确定哪一个适合您。 https://medium.com/@andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb
注意:此答案适用于 Web3.js 1.0。
小智 1
您可以通过以下方式创建新地址web3.js
:
const Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed.binance.org/');
const privateKey = 'private key';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
console.log(`Account: ${account.address}`);
for (let i = 0; i < 3; i++) {
const newAccount = web3.eth.accounts.create();
console.log(`Address ${i + 1}: ${newAccount.address}`);
}
Run Code Online (Sandbox Code Playgroud)