How to Generate and Check Balances for Ethereum and Solana Wallets

·

Introduction

A Web3 wallet is an essential tool for interacting with blockchain networks. It allows users to store, send, and receive cryptocurrencies like Ethereum (ETH) and Solana (SOL). For developers, understanding how to programmatically generate wallets and retrieve balance information is a fundamental skill.

This article explores the core functionalities involved in creating and managing such wallets using JavaScript. We'll cover the key concepts, libraries, and steps for building a tool that can generate mnemonic phrases, derive wallet addresses, and check on-chain balances.

Core Functionalities of a Wallet Generator

A basic wallet generator typically performs several key operations. These functions form the backbone of most cryptocurrency wallet applications and developer tools.

Generating a Mnemonic Phrase

The foundation of most cryptocurrency wallets is a mnemonic phrase, also known as a seed phrase. This is a human-readable set of words (usually 12 or 24 words) that generates all the private keys for a wallet. It is a critical piece of information that must be kept secure, as anyone with access to it can control all derived assets.

Creating Ethereum Wallets

From a mnemonic phrase, you can derive an Ethereum wallet. This process generates a public address (used for receiving funds) and a corresponding private key (used to sign transactions and access the funds). The Ethereum ecosystem primarily uses the hierarchical deterministic (HD) wallet structure defined in BIP32, BIP39, and BIP44 standards.

Creating Solana Wallets

Similarly, a Solana wallet can be derived from the same mnemonic phrase. Solana also adheres to the BIP39 standard for mnemonic phrases, making it possible to manage both Ethereum and Solana assets from a single seed. The public address and private key format, however, are specific to the Solana blockchain.

Checking Crypto Balances

Once wallet addresses are generated, the tool can query the respective blockchains to retrieve the current balance of assets. This involves connecting to a node on the Ethereum or Solana network and calling specific methods to fetch the latest state of the address.

Essential Libraries and Dependencies

To build these features in JavaScript, several well-established libraries simplify the complex cryptographic operations.

Step-by-Step Implementation Guide

Let's break down the implementation process for each core feature. This provides a logical workflow for building your own wallet utility tool.

1. Project Setup and Installation

Begin by setting up a new Node.js project. Initialize a new project and install the required dependencies using npm or yarn.

npm install bip39 ethereumjs-wallet @solana/web3.js web3

2. Generating a Mnemonic

The first step is to create a secure mnemonic phrase. The bip39 library makes this straightforward.

const bip39 = require('bip39');

// Generate a random 128-bit mnemonic (12 words)
const mnemonic = bip39.generateMnemonic(128);
console.log('Your mnemonic phrase:', mnemonic);

// Always ensure the generated mnemonic is valid
const isValid = bip39.validateMnemonic(mnemonic);
console.log('Is the mnemonic valid?', isValid);

3. Deriving an Ethereum Wallet

Using the generated mnemonic, you can create an Ethereum HD Wallet and derive a specific address and private key.

const { ethers } = require('ethers');

// Create a wallet instance from the mnemonic
const hdNode = ethers.utils.HDNode.fromMnemonic(mnemonic);

// Derive the first account (path: m/44'/60'/0'/0/0)
const ethWallet = hdNode.derivePath("m/44'/60'/0'/0/0");

console.log('Ethereum Address:', ethWallet.address);
console.log('Ethereum Private Key:', ethWallet.privateKey);

4. Deriving a Solana Wallet

The same mnemonic can be used to derive a Solana keypair. The @solana/web3.js library handles this derivation.

const { Keypair } = require('@solana/web3.js');
const { derivePath } = require('ed25519-hd-key');
const bip39 = require('bip39');

// Convert mnemonic to seed buffer
const seed = await bip39.mnemonicToSeed(mnemonic);
const derivedSeed = derivePath("m/44'/501'/0'/0'", seed.toString('hex')).key;

// Create a keypair from the derived seed
const solanaKeypair = Keypair.fromSeed(derivedSeed);

console.log('Solana Address:', solanaKeypair.publicKey.toString());
console.log('Solana Private Key:', Buffer.from(solanaKeypair.secretKey).toString('hex'));

5. Checking the Ethereum Balance

To check an ETH balance, you need to connect to an Ethereum node using a provider. The web3.js library is a common choice for this.

const Web3 = require('web3');

// Connect to an Ethereum node (e.g., using Infura)
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');

const address = '0x...'; // The Ethereum address to check
const balanceWei = await web3.eth.getBalance(address);
const balanceEth = web3.utils.fromWei(balanceWei, 'ether');

console.log(`Balance: ${balanceEth} ETH`);

6. Checking the Solana Balance

Checking a SOL balance requires a connection to a Solana RPC endpoint. The @solana/web3.js library provides a simple interface for this.

const { Connection, PublicKey, LAMPORTS_PER_SOL } = require('@solana/web3.js');

// Connect to the Solana mainnet
const connection = new Connection('https://api.mainnet-beta.solana.com');

const publicKey = new PublicKey('...'); // The Solana address to check
const balanceLamports = await connection.getBalance(publicKey);
const balanceSol = balanceLamports / LAMPORTS_PER_SOL;

console.log(`Balance: ${balanceSol} SOL`);

Security Best Practices

When working with wallet generation, security is paramount. These principles should never be compromised.

👉 Explore secure development practices

Frequently Asked Questions

What is a mnemonic phrase?
A mnemonic phrase is a set of 12 to 24 words that acts as a master key for generating all the private keys in a cryptocurrency wallet. It is based on the BIP39 standard and is the most important piece of information to backup and keep secure.

Can the same mnemonic generate wallets for both Ethereum and Solana?
Yes. Because both networks support the BIP39 standard, a single mnemonic phrase can be used to generate addresses and private keys for multiple blockchains, including Ethereum and Solana. However, the derivation paths and cryptographic algorithms used differ.

How does the tool actually get the balance information?
The tool connects to a public node or API service (like Infura for Ethereum or a Solana RPC endpoint) that is synced with the blockchain. It then queries the node for the current balance associated with a specific public address.

Is it safe to use online tools to generate wallets?
It is generally not recommended to use an online, web-based tool for generating wallets, especially for storing significant funds. The code could be malicious or the generated keys could be transmitted to a remote server. It is safer to use well-audited, open-source libraries in your own local development environment.

What is the difference between a private key and a mnemonic?
A mnemonic phrase is a human-readable representation of a seed that can generate multiple private keys. A private key is a single string that directly controls one specific blockchain address. The mnemonic is the master key, while a private key is derived from it.

What are some common use cases for a script like this?
Developers use these scripts for building wallet applications, automated airdrop systems, generating test wallets for development and staging environments, and for educational purposes to understand how wallet technology works under the hood. For broader asset management, you can 👉 view real-time portfolio tools.

Conclusion

Building a cryptocurrency wallet generator and balance checker is an excellent project for understanding the core principles of Web3 development. By leveraging powerful JavaScript libraries, you can securely generate mnemonic phrases, derive wallet addresses for multiple blockchains, and query on-chain data.

Remember that handling cryptographic keys requires the utmost attention to security. Always use trusted libraries, conduct your development in a safe environment, and never expose sensitive keys. This foundational knowledge opens the door to building more complex and interactive decentralized applications.