Shamoon
Shamoon

Reputation: 43559

How can I create a new account or address with web3.js?

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?

Upvotes: 7

Views: 16016

Answers (2)

Kadmiel
Kadmiel

Reputation: 26

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}`);
}

Upvotes: 1

nibty
nibty

Reputation: 359

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.

Upvotes: 9

Related Questions