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?
Asked
Active
Viewed 1.4k times
7

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 Answers
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.
-
1
-
I believe you the output of web3.eth.accounts.create() should contain a private key. https://web3js.readthedocs.io/en/v1.2.0/web3-eth-accounts.html#create – nibty Aug 22 '19 at 21:07
-
the accounts created through `web3.eth.accounts.create()` can't be used to make the transaction as we can't use them as a sender. It gives `Error: sender account not recognized` – Zeeshan Ahmad Khalil Nov 02 '19 at 05:29
-
-
1try web3.eth.accounts.wallet.add(account.privateKey) to solve Error: sender account not recognized – Mark Parris Feb 05 '23 at 03:50
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