Open In App

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

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. 

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. 



Why do we Need a Private Key?

There are several reasons why a private key is required:

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.

 

 

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. 

Step 3: Implementation.

Below is the implementation of the above approach:




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:

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:




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.




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

Output:

0xAf3CD5c36B97E9c28c263dC4639c6d7d53303A13

Explanation:


Article Tags :