7

I'm trying to interact with geth, and I need to create a new deposit address (and be able to control it). How can I implement this with web3.js?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • Cross-linking with https://ethereum.stackexchange.com/questions/47940/how-can-i-create-a-new-account-or-address-with-web3-js, which is probably a better place for this question. – user94559 May 08 '18 at 17:40

2 Answers2

9

You can use the Web3.eth.accounts.create() function. It will return an account object which you'll be able to control. https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html

Example:

var Web3 = require("web3");

var web3 = new Web3('http://localhost:8545'); // your geth
var account = web3.eth.accounts.create();

You can also use the web3.eth.personal.newAccount() function. http://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#newaccount

Check this blog post to help determine which is right for you. https://medium.com/@andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb

Note: This answer is for Web3.js 1.0.

Pang
  • 9,564
  • 146
  • 81
  • 122
nibty
  • 359
  • 2
  • 6
1

This is how you can create a new address with 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}`);
}
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Kadmiel
  • 26
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – alea Apr 29 '23 at 17:48