Search code examples
ethereumweb3js

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?


Solution

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