DEV Community

Cover image for Creating a Simple Web-Based Ethereum Wallet
codenextgen
codenextgen

Posted on

Creating a Simple Web-Based Ethereum Wallet

In this guide, we'll walk through the process of creating a simple web-based wallet that shows your Ethereum balance. We'll use HTML, JavaScript, and a JSON-RPC provider like Infura or Alchemy to interact with the Ethereum network.

Step 1: Set Up the HTML

Create an index.html file with a simple structure:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Ethereum Wallet</title>
</head>
<body>
<h1>Simple Ethereum Wallet</h1>
<div>
<label for="address">Enter your Ethereum address:</label>
<input type="text" id="address" placeholder="0xYourEthereumAddress">
<button onclick="getBalance()">Get Balance</button>
</div>
<div id="balance"></div>

<span class="nt">&lt;script </span><span class="na">src=</span><span class="s">"wallet.js"</span><span class="nt">&gt;&lt;/script&gt;</span>
Enter fullscreen mode Exit fullscreen mode

</body>
</html>

Enter fullscreen mode Exit fullscreen mode




Step 2: Set Up the JavaScript

Create a wallet.js file to handle the logic:



async function getBalance() {
const address = document.getElementById('address').value;
if (!address) {
alert('Please enter your Ethereum address.');
return;
}

<span class="kd">const</span> <span class="nx">response</span> <span class="o">=</span> <span class="k">await</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID</span><span class="dl">'</span><span class="p">,</span> <span class="p">{</span>
    <span class="na">method</span><span class="p">:</span> <span class="dl">'</span><span class="s1">POST</span><span class="dl">'</span><span class="p">,</span>
    <span class="na">headers</span><span class="p">:</span> <span class="p">{</span>
        <span class="dl">'</span><span class="s1">Content-Type</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">application/json</span><span class="dl">'</span><span class="p">,</span>
    <span class="p">},</span>
    <span class="na">body</span><span class="p">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">({</span>
        <span class="na">jsonrpc</span><span class="p">:</span> <span class="dl">'</span><span class="s1">2.0</span><span class="dl">'</span><span class="p">,</span>
        <span class="na">method</span><span class="p">:</span> <span class="dl">'</span><span class="s1">eth_getBalance</span><span class="dl">'</span><span class="p">,</span>
        <span class="na">params</span><span class="p">:</span> <span class="p">[</span><span class="nx">address</span><span class="p">,</span> <span class="dl">'</span><span class="s1">latest</span><span class="dl">'</span><span class="p">],</span>
        <span class="na">id</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">}),</span>
<span class="p">});</span>

<span class="kd">const</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">response</span><span class="p">.</span><span class="nf">json</span><span class="p">();</span>
<span class="kd">const</span> <span class="nx">balance</span> <span class="o">=</span> <span class="nf">parseInt</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">result</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1</span><span class="nx">e18</span><span class="p">;</span> <span class="c1">// Convert from wei to ETH</span>

<span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="dl">'</span><span class="s1">balance</span><span class="dl">'</span><span class="p">).</span><span class="nx">innerText</span> <span class="o">=</span> <span class="s2">`Balance: </span><span class="p">${</span><span class="nx">balance</span><span class="p">}</span><span class="s2"> ETH`</span><span class="p">;</span>
Enter fullscreen mode Exit fullscreen mode

}

Enter fullscreen mode Exit fullscreen mode




Step 3: Replace YOUR_INFURA_PROJECT_ID

Sign up for an Infura account and create a new project to get your PROJECT_ID. Replace YOUR_INFURA_PROJECT_ID in the JavaScript file with your actual Infura project ID.

Step 4: Run the Application

Open the index.html file in your browser. Enter your Ethereum address and click the "Get Balance" button to see your balance.

Summary

Keccak-256

Keccak-256 is a cryptographic hash function that is part of the SHA-3 family. It is widely used in blockchain technologies, including Ethereum, for hashing data. Keccak-256 produces a 256-bit (32-byte) hash value from an input of any length. This hash function is used for various purposes, such as generating addresses, verifying transactions, and ensuring data integrity.

RPC (Remote Procedure Call)

RPC is a protocol that allows a program to cause a subroutine or procedure to execute in another address space (commonly on another physical machine). It is used to request services from a remote computer on a network without having to understand the network's details.

JSON-RPC

JSON-RPC is a stateless, lightweight remote procedure call protocol. It defines several data structures and the rules around their processing. It is transport-agnostic, meaning it can be used over different transport protocols, such as HTTP. JSON-RPC is commonly used in blockchain technologies to interact with nodes.

Common RPC Methods on Ethereum (ETH) and Solana (SOL)

Ethereum (ETH)

  • eth_getBalance: Returns the balance of the account of a given address.
  • eth_getTransactionCount: Returns the number of transactions sent from an address.
  • eth_sendTransaction: Sends a transaction to the network.
  • eth_call: Executes a new message call immediately without creating a transaction on the blockchain.
  • eth_blockNumber: Returns the number of the most recent block.

Solana (SOL)

  • getBalance: Returns the balance of a given account.
  • getTransaction: Returns information about a transaction by its signature.
  • sendTransaction: Sends a transaction to the network.
  • getSlot: Returns the current slot number.
  • getAccountInfo: Returns information about a specific account.

Creating a Simple Web-Based Wallet

This example demonstrates a simple interaction with the Ethereum blockchain using JSON-RPC. For a production-ready application, you would need to handle more edge cases, security considerations, and possibly use a more robust framework or library.

By following these steps, you can create a basic web-based wallet that interacts with the Ethereum network to display the balance of a given address. This is a foundational step towards building more complex blockchain applications.

Top comments (0)