Open In App

How to Create an Ethereum Wallet Address From a Private Key?

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An Ethereum Wallet is a type of software application that allows users to interact with their Ethereum Account on the Ethereum Network or the Ethereum Blockchain. Ethereum Wallets are controlled through a password or a private key that allows users to send or receive funds or transactions within the wallet. These private keys are only available to the owner of the wallet due to privacy as anyone who knows the private key can access the wallet and hence also access their funds. Ethereum Accounts are used to create smart contracts, interact with decentralized applications, and are used to run applications smoothly without any fraud, downtime, or third-party interference. There are two types of Ethereum accounts:

  1. Externally Owned Account: Externally Owned Accounts also known as EOA are accounts that don’t have a Smart Contract code. EOA’s have a private key, which means that the user has control over access to funds or contracts. Only Externally Created Accounts can initiate transactions. You can make an ETH transfer or payment by signing transactions with a private key.
  2. Contract Account: Contract Accounts have Smart Contract Codes which Externally Owned Accounts don’t have. Contract Accounts don’t have their own private key and are controlled by their smart contract code. These accounts can only be activated by sending Ethereum (ETH) into them. These accounts cannot initiate transactions because they don’t have a private key.

There are many types of Wallets like MetaMask, Atomic Wallet, Electrum Wallet, MyEtherWallet, CoinBase, etc.

What is the Ethereum Wallet Address?

Ethereum Wallet Address is a distinct alphanumeric crypto identifier that contains 42 hexadecimal characters that start with 0x and is followed by a series of 40 random characters which can send transactions and has a balance in it. 

  • For example, 0xdB055877e6c13b6A6B25aBcAA29B393777dD0a73 is a type of Ethereum address. 
  • The unique address is required to receive funds from another cryptocurrency network and to access it, you should have your own private key. 
  • In order to access your private key, you must have your own wallet such as MetaMask, CoinBase, or TrezorOne to name a few. 

What is a Private Key?

A private key is a secure and secret number that helps users to secure their digital money and also allows them to make transactions and generate the receiving addresses. 

  • It is an integral part of the Ethereum Network as this secret number acts as a secure password that protects the user from unauthorized access to their funds. 
  • A private key provides a very high level of security as it can take years and years to crack a private key. 
  • The private key is only known to the user and is used to carry out any cryptocurrency transaction from their account. 
  • Private keys can derive public keys and hence public keys can derive Ethereum addresses but the same cannot be said for vice-versa as it is not possible to derive public keys from Ethereum addresses and private keys from public keys.

Why do we Need a Private Key?

There are several reasons why a private key is required:

  • Private keys are secure codes and are not meant to be given to anyone but users need a method by which they can allow other users to find their assets and vice-versa. 
  • As a result, we use Ethereum addresses that are derived from private keys which we can share with other users to know our cryptocurrency assets and balance. 
  • To create an Ethereum address, we need a public key which is derived from a private key. 

Prerequisites

1. Node.js: Follow the installation steps for Node.js.

2. Microsoft Visual Studio Code: Follow the installation steps for Visual Studio Code. After installing the visual studio code, follow the steps below to install the CodeRunner extension.

  • Now open Visual Studio Code and Go to Extensions (Ctrl + Shift + X).
Open Extensions in Visual Studio Code

 

  • Search CodeRunner and install it.
Search CodeRunner

 

Hence, the prerequisites that we needed are successfully installed and now we are ready to create our Ethereum Wallet Address from a private key.

There are 2 methods to create an Ethereum wallet address from a private key:

  1. Using Elliptic Curve Digital Signature Algorithm (ECDSA).
  2. By installing Node.js Packages.

Method 1: Using Elliptic Curve Digital Signature Algorithm (ECDSA).

An Elliptic curve is a curve that is defined by the equation y2 = x3 + ax + b. Elliptic curve cryptography is a form of public key cryptography which is based on the algebraic structure of elliptic curves over infinite fields. There are many such curves for different types of cryptocurrencies that are widely used in the world. Ethereum uses the secp256k1 curve. This is the special feature of ECDSA that we can derive the public key from the private key. We need to apply ECDSA to our private key to get the 64-byte integer in which the first 32 integers represent X and the other 32 integers represent Y on the elliptic curve.  
Below are the steps to create an Ethereum wallet address from a private key:

Step 1: Convert the private key to hexadecimal format. 

The first step is to convert our private key into a hexadecimal format.  Let’s consider a private key to understand why a private key is converted to hexadecimal format:

Private Key: 99609413211026217191848487601459650900609461714685306780198554267270848908445

A private key can be selected randomly in the range of values ranging from 1 to 2^256-1. The computer understands the binary language that is 0 and 1, and our private key is in a decimal format so there is a need to represent the private key in terms of bits and bytes. Decimal numbers have a base of 10 whereas binary numbers have a base of 2. For example, 78 is in decimal format. Let’s convert 78 into binary by the following steps:

Divide (78)10 successively by 2 until the quotient is 0:

78/2 = 39, remainder = 0
39/2 = 19, remainder = 1
19/2 = 9, remainder = 1
9/2 = 4, remainder = 1
4/2 = 2, remainder = 0
2/2 = 1, remainder = 0
1/2 = 0, remainder = 1

Read from the bottom (MSB) to top (LSB) and hence we get 1001110 which is 78 in binary format.

Now, if we convert the private key into a binary format it will become very large. So instead of converting this private key into binary, computers generally convert this into a hexadecimal format which has a base of 16. 
Hexadecimal numbers are represented by only 16 symbols and use ABCDEF to represent 10 to 15. The private key in hexadecimal format can be written as:

Private key in decimal format: 99609413211026217191848487601459650900609461714685306780198554267270848908445

Private key in hexadecimal format: DC38EE117CAE37750EB1ECC5CFD3DE8E85963B481B93E732C5D0CB66EE6B0C9D

To convert the private key (hexadecimal format) to Ethereum wallet address Javascript will be used for the implementation. 

Step 2: Install packages for implementation.

A. Create a node project: Open the terminal in Visual Studio Code and type the following command in the terminal to create a node project:

npm init –y

B. Install ethereumjs-wallet: Type the below command to install the ethereumjs-wallet package, which is a library that allows you to interact with an Ethereum wallet.

npm install –save ethereumjs-wallet

C. Install Keccak-256 and SHA-3 package: To install the Keccak-256 and SHA-3 packages type the below command in the terminal:

npm i keccak

npm install js-sha3

Ethereum uses Keccak-256 cryptographic hash function in a consensus engine known as Ethash. It is a version of Secure Hash Algorithm Version 3 or SHA-3. 

  • It can be used for transparency, scam-less authentication, encryption, and pseudo-random number generation. 
  • After getting the public key, the Keccak-256 hash function is applied and Ethereum Address is obtained. 
  • In order to derive the address from the public key, apply the Keccak-256 to the key and then take the last 20 bytes of the address. 
  • Keccak-256 returns a 32-byte array. 

Step 3: Implementation.

Below is the implementation of the above approach:

Javascript




const EC = require('elliptic').ec;
const BN = require('bn.js')
// Apply the Keccak-256 hash function
const keccak256 = require('js-sha3').keccak256;
 
// Elliptic Curve used by Ethereum that is secp526k1
const ec = new EC('secp256k1');
 
const SK = new BN('DC38EE117CAE37750EB1ECC5CFD3DE8E85963B481B93E732C5D0CB66EE6B0C9D', 16);
 
// Elliptic Curve Generator Point
const G = ec.g;
// Public Point Elliptic Curve Multiplication
const pp = G.mul(SK);
 
// The first 32 bits representing point x
const x = pp.getX().toBuffer();
 
// The last 32 bits representing point y
const y = pp.getY().toBuffer();
 
// Concatenation of the 64 bit of Elliptic Curve Multiplication
const bt = Buffer.concat([x,y])
 
// Here we are doing keccak256 hashing of our ECDSA public key
const kc = keccak256(bt)
 
// Here we will be doing two things
// First we will get the hex format of our Keccak-256 hash
// Secondly we will obtain the right most 160 bits
 
const ethereumAddress = Buffer.from((kc, 'hex').slice(-20).toString('hex'))
console.log('Address is', ethereumAddress);


Output:

Address is c5ed5d9b9c957be2baa01c16310aa4d1f8bc8e6f

Step 4: After getting the output, just add 0x at the start of the address and your Ethereum Wallet Address is ready.

0xc5ed5d9b9c957be2baa01c16310aa4d1f8bc8e6f

Explanation:

  • The code starts by creating a new EC object.
  • This is an elliptic curve that secp256k1 uses.
  • The EC object is then used to create a BN object called SK, which has 16 bytes of entropy.
  • The code then creates a G object that is initialized with ec curve’s generator point (G=ec.g).
  • The code then multiplies this private key by G, which is ec’s multiplication function for Elliptic Curve Groups (ECG).
  • Then x and y are created from pp using getX() and getY().
  • Then, it gets x and y from G’s mul function which returns two value on the curve: X and Y
  • Then bt is created as an array of bytes that contains x and y and are then concatenated into one buffer using Buffer.concat().
  • Now, keccak256 is used to hash this buffer to get kc as its result.
  • Finally, ethereumAddress is created by taking kc and converting it into hexadecimal string format before returning it to display in the console window.
  • The code above will then concatenate the hex string representation of the keccak256 hash to a buffer and return it as an Ethereum address.
  • The code is a simple example of how to create an Ethereum Address.

Method 2: By Installing Node.js packages.

There is also another method of deriving the Ethereum address from a private key. Also, this method is simpler than the previous one.

Step 1: Installing the packages.

A. Type the following code in the terminal. It will create a node project in Visual Studio Code.

npm init –y

B. After this, type the following code in your terminal. The following code installs the ethereumjs-wallet package, which is a library that allows you to interact with an Ethereum wallet.

npm install –save ethereumjs-wallet

C. Open Visual Studio Code and type the following code in your terminal. The package will create a new public and private key pair.

npm install ethereum-private-key-to-public-key

D. After installing the above package, type the following code in your terminal. The package will be used to convert a public key into an Ethereum address.

npm install ethereum-public-key-to-address

Step 2: Implementation.

  1. Import the privateKeyToPublicKey module.
  2. Apply console.log in order to get the output.

Below is the implementation of the above approach to convert the private key to the public key:

Javascript




const privateKeyToPublicKey = require('ethereum-private-key-to-public-key')
console.log(privateKeyToPublicKey(Buffer.from(
'4f3edf983ac986a65a342ce7c78d9ac076d3b113bce9c46f30d7d25171b32b1d',
'hex')).toString('hex'))


Output:

04c1573f1528638ae14cbe04a74e6583c5562d59214223762c1a11121e24619cbc09d27a7a1cb989dd801cc028dd8225f8e2d2fd57d852b5bf697112f69b6229d1

After getting the public key, import the publicKeyToAddress module and again apply console.log in order to get the address.

Javascript




const publicKeyToAddress = require('ethereum-public-key-to-address')
console.log(publicKeyToAddress(Buffer.from(
'04c1573f1528638ae14cbe04a74e6583c5562d59214223762c1a11121e24619cbc09d27a7a1cb989dd801cc028dd8225f8e2d2fd57d852b5bf697112f69b6229d1',
'hex')))


Output:

0xAf3CD5c36B97E9c28c263dC4639c6d7d53303A13

Explanation:

  • The first code starts by requiring the privateKeyToPublicKey module.
  • The code then uses that module to convert a private key into its corresponding public key.
  • The first line of the program is console.log(privateKeyToPublicKey(Buffer.from(‘4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d’,’hex’)).toString(‘hex’)) which converts the private key to public key.
  • Step 3 starts by requiring the publicKeyToAddress function.
  •  This function takes a public key and returns an address for it.
  • The code then prints out the result of calling this function with the hex string 04c1573f1528638ae14cbe04a74e6583c5562d59214223762c1a11121e24619cbc09d27a7a1cb989dd801cc028dd8225f8e2d2fd57d852b5bf697112f69b6229d1
  • The code generates an address for the public key of 04c1573f1528638ae14cbe04a74e6583c5562d59214223762c1a11121e24619cbc09d27a7a1cb989dd801cc028dd8225f8e2d2fd57d852b5bf697112f69b6229d1
  •  The code is a method that takes in a private key and returns the address of the corresponding Ethereum account.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads