menu

How to Generate Ethereum Private key and Address

If you are using Node.js, you can use the package called “ethereumjs-wallet” to generate Ethereum private keys and addresses. This is official package provided and maintained by the Ethereum JavaScript community.

To generate Ethereum addresses, follow these steps:

Step 1: Create Node project

To create a new Node project, create a new folder and initialize the project using the following command.

npm init --y

Step 2: Install Node dependency

Install the dependency using the following command.

npm install --save ethereumjs-wallet

Step 3: Generate Addresses

Here is a code to generate 1000 Ethereum Addresses.

const ethWallet = require('ethereumjs-wallet');
for(let index=0; index < 1000; index++) {
    let addressData = ethWallet.generate();
    console.log(`Private key = , ${addressData.getPrivateKeyString()}`);
    console.log(`Address = , ${addressData.getAddressString()}`);
}

It’s as simple as this.

Generate Ethereum Address

However, this is not a production approach. You don’t wanna generate a private key on a random basis. If you want to generate addresses from the raw private key you can use this method.

    let addressData = ethWallet.fromPrivateKey('Your private key');
    console.log(`Private key = , ${addressData.getPrivateKeyString()}`);
    console.log(`Address = , ${addressData.getAddressString()}`);

If you want to generate using the extended private key or xprv which is the most used approach, you can use this code:

    let addressData = ethWallet.fromExtendedPrivateKey('Your xprv key');
    console.log(`Private key = , ${addressData.getPrivateKeyString()}`);
    console.log(`Address = , ${addressData.getAddressString()}`);

Check out the complete documentation for more detailed information.