EthStorage Web3.js Integration Guide
Web3.js is the original and most widely-used JavaScript library for interacting with the EthStorage blockchain. This guide covers everything from basic setup to advanced patterns using BlockEden.xyz's EthStorage infrastructure.
Overview
Web3.js provides a complete JavaScript API for EthStorage, including:
- Account Management: Create, import, and manage EthStorage accounts
- Smart Contract Interaction: Deploy and call smart contracts
- Transaction Handling: Send, sign, and monitor transactions
- Event Monitoring: Subscribe to blockchain events
- Utility Functions: Format conversions, hashing, and validation
Installation & Setup
Installation
# Using npm
npm install web3
# Using yarn
yarn add web3
# Using pnpm
pnpm add web3
Basic Setup
import { Web3 } from 'web3';
// Initialize with BlockEden.xyz endpoint
const web3 = new Web3('https://api.blockeden.xyz/ethstorage/${accessKey}');
// Verify connection
async function testConnection() {
try {
const blockNumber = await web3.eth.getBlockNumber();
console.log('Current block number:', blockNumber);
console.log('Web3.js version:', web3.version);
} catch (error) {
console.error('Connection failed:', error);
}
}
testConnection();
Environment Configuration
// config/web3.js
import { Web3 } from 'web3';
const networks = {
mainnet: 'https://ethereum-mainnet.blockeden.xyz',
sepolia: 'https://ethereum-sepolia.blockeden.xyz',
polygon: 'https://polygon-mainnet.blockeden.xyz',
arbitrum: 'https://arbitrum-mainnet.blockeden.xyz'
};
export function createWeb3Instance(network = 'mainnet', apiKey) {
const rpcUrl = `${networks[network]}/${apiKey}`;
return new Web3(rpcUrl);
}
// Usage
const web3 = createWeb3Instance('mainnet', process.env.BLOCKEDEN_API_KEY);
Account Management
Creating Accounts
// Create a new account
const account = web3.eth.accounts.create();
console.log('Address:', account.address);
console.log('Private Key:', account.privateKey);
// Create multiple accounts
const accounts = web3.eth.accounts.create(5); // Creates 5 accounts
console.log('Generated accounts:', accounts);
// Create from entropy
const entropy = web3.utils.randomHex(32);
const accountFromEntropy = web3.eth.accounts.create(entropy);
Account Recovery
// Recover account from private key
const privateKey = '0x1234567890abcdef...';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Recover from mnemonic (requires additional library)
import { generateMnemonic, mnemonicToSeedSync } from 'bip39';
import { hdkey } from 'ethereumjs-wallet';
function recoverFromMnemonic(mnemonic, index = 0) {
const seed = mnemonicToSeedSync(mnemonic);
const hdwallet = hdkey.fromMasterSeed(seed);
const path = `m/44'/60'/0'/0/${index}`;
const wallet = hdwallet.derivePath(path).getWallet();
return web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex'));
}
// Usage
const mnemonic = 'your twelve word mnemonic phrase here...';
const account = recoverFromMnemonic(mnemonic, 0);
Wallet Management
class EthStorageWallet {
constructor(web3Instance) {
this.web3 = web3Instance;
this.accounts = new Map();
}
// Add account to wallet
addAccount(privateKey, password) {
const account = this.web3.eth.accounts.privateKeyToAccount(privateKey);
const encrypted = this.web3.eth.accounts.encrypt(privateKey, password);
this.accounts.set(account.address.toLowerCase(), {
account,
encrypted
});
return account.address;
}
// Get account by address
getAccount(address) {
return this.accounts.get(address.toLowerCase())?.account;
}
// Sign transaction
async signTransaction(txObject, fromAddress, password) {
const accountData = this.accounts.get(fromAddress.toLowerCase());
if (!accountData) {
throw new Error('Account not found in wallet');
}
const decrypted = this.web3.eth.accounts.decrypt(accountData.encrypted, password);
return await this.web3.eth.accounts.signTransaction(txObject, decrypted.privateKey);
}
// Get all addresses
getAddresses() {
return Array.from(this.accounts.keys());
}
// Remove account
removeAccount(address) {
return this.accounts.delete(address.toLowerCase());
}
}
// Usage
const wallet = new EthStorageWallet(web3);
const address = wallet.addAccount('0x1234...', 'password123');
console.log('Added account:', address);
Reading Blockchain Data
Account Information
// Get account balance
async function getAccountInfo(address) {
try {
// Balance in wei
const balanceWei = await web3.eth.getBalance(address);
// Convert to ether
const balanceEth = web3.utils.fromWei(balanceWei, 'ether');
// Transaction count (nonce)
const nonce = await web3.eth.getTransactionCount(address);
// Check if it's a contract
const code = await web3.eth.getCode(address);
const isContract = code !== '0x';
return {
address,
balance: {
wei: balanceWei,
eth: balanceEth
},
nonce,
isContract
};
} catch (error) {
console.error('Error getting account info:', error);
throw error;
}
}
// Usage
const info = await getAccountInfo('0x742D5Cc6bF2442E8C7c74c7b4Be6AB9d6f10f5B4');
console.log('Account info:', info);
Block Information
// Get latest block
async function getLatestBlock() {
const block = await web3.eth.getBlock('latest', true); // true = include transactions
return {
number: block.number,
hash: block.hash,
timestamp: new Date(Number(block.timestamp) * 1000),
gasUsed: block.gasUsed,
gasLimit: block.gasLimit,
transactionCount: block.transactions.length,
miner: block.miner
};
}
// Get block by number
async function getBlockByNumber(blockNumber) {
const block = await web3.eth.getBlock(blockNumber, false); // false = only tx hashes
return block;
}
// Get block range
async function getBlockRange(startBlock, endBlock) {
const blocks = [];
for (let i = startBlock; i <= endBlock; i++) {
const block = await web3.eth.getBlock(i, false);
blocks.push(block);
}
return blocks;
}