4 min read
- NodeJS installed on your system.
- A text editor
- Terminal aka Command Line
What is an Ethereum address?
While signing in to any platform on the internet, you need to authenticate using a combination of credentials. Consider an Ethereum address as your username and a corresponding private key as the password. While your Ethereum address is public and can be shared, the private key must always be kept secret. Using this combination lets you interact with the Ethereum blockchain. An Ethereum address is your identity on the blockchain, and it looks like this “0x6E0d01A76C3Cf4288372a29124A26D4353EE51BE”. Having a valid Ethereum address is required for:
- Receiving/Sending Ethereum currency
- Signing/Sending transactions
- Connecting to decentralized applications
How an Ethereum address is generated:
A random private key of 64 (hex) characters (256 bits / 32 bytes) is generated first. For example:
A 128 (hex) character (64 bytes) public key is then derived from the generated private key using Elliptic Curve Digital Signature Algorithm (ECDSA). For example:
The Keccak-256 hash function is then applied to (128 characters / 64 bytes) the public key to obtain a 64 character (32 bytes) hash string. The last 40 characters / 20 bytes of this string prefixed with 0x become the final Ethereum address. For example:
Note: 0x in coding indicates that the number/string is written in hex.
What is Ethers.js?
Ethers.js is a lightweight alternative to Web3.js, which is the most commonly used Ethereum library today. Ethers.js is considered by some to be more stable and less buggy than other libraries and has extensive documentation. This library is also very friendly to beginners. Ethers.js is very well maintained and is preferred over Web3.js by many new developers.
You can learn more about Ethers.js and How to connect to Ethereum network with Ethers.js here.
Our first step here will be to check if node.js is installed on the system. To do so, copy-paste the following in your terminal/cmd:
$ node -v
If not installed, you can download the LTS version of NodeJS from the official website.
If Node.js is properly installed, let’s add the Ethers.js (version 6) library using npm (Node Package Manager, a part of Node.js).
$ npm i ethers
The most common issue at this step is an internal failure with `node-gyp.` You can follow node-gyp installation instructions here.
Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue.
Another common issue is a stale cache; clear your npm cache by entering the following command:
$ npm cache clean
If Ethers.js is successfully installed, let’s proceed with creating an Ethereum address.
Create a file named address.js, which will be a short script to create a random private key and an Ethereum address from that key, copy-paste the following in your address.js file:
var ethers = require('ethers');
var crypto = require('crypto');
var id = crypto.randomBytes(32).toString('hex');
var privateKey = "0x"+id;
console.log("SAVE BUT DO NOT SHARE THIS:", privateKey);
var wallet = new ethers.Wallet(privateKey);
console.log("Address: " + wallet.address);
Explanation of the code above
Line 1-2: Importing ethers library and crypto module that comes with node.js
Line 4: Generating a random 32 bytes hexadecimal string using the crypto object and storing it in the id variable.
Line 4: Adding ‘0x’ prefix to the string in id and storing the new string in a variable called privateKey.
Line 6: Printing our private key with a warning.
Line 8: Creating a new wallet using the privateKey and storing it in the wallet variable.
Line 9: Printing the address of the newly created wallet with a message “Address:”
Save and run your script to generate a new Ethereum address.
$ node address
If your code executes successfully, the output will look similar to the screenshot below. The first line consists of the private key, and the second line consists of your new Ethereum address.
Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)