const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const fs = require('fs'); const path = require('path'); const Wallet = require('ethereumjs-wallet').default; const client = require('./client'); const app = express(); app.use(cors()); app.use(bodyParser.json()); const CHAIN_ID = '14006'; // Definisikan Chain ID di sini const WALLET_DB_PATH = path.join(__dirname, 'wallets.json'); // Fungsi untuk membaca database wallet dari file JSON function readWalletsDB() { return new Promise((resolve, reject) => { fs.readFile(WALLET_DB_PATH, 'utf8', (err, data) => { if (err) { return reject(err); } resolve(JSON.parse(data)); }); }); } // Fungsi untuk menulis database wallet ke file JSON function writeWalletsDB(wallets) { return new Promise((resolve, reject) => { fs.writeFile(WALLET_DB_PATH, JSON.stringify(wallets, null, 2), 'utf8', (err) => { if (err) { return reject(err); } resolve(); }); }); } // Fungsi untuk membuat wallet acak function createRandomWallet() { const wallet = Wallet.generate(); return { address: wallet.getAddressString(), privateKey: wallet.getPrivateKeyString(), balance: '0x0' }; } app.post('/blockchain', async (req, res) => { const { method, params, id } = req.body; console.log('Received request:', req.body); if (method === 'GetChainId') { client.GetChainId({}, (error, response) => { if (error) { console.error('Error in GetChainId:', error); res.status(500).json({ jsonrpc: '2.0', error: { code: -32603, message: 'Internal JSON-RPC error', data: error.message }, id: id }); } else { console.log('GetChainId response:', response); res.json({ jsonrpc: '2.0', result: { chainId: response.chainId }, id: id }); } }); } else if (method === 'eth_chainId') { console.log('Handling eth_chainId method'); res.json({ jsonrpc: '2.0', result: `0x${parseInt(CHAIN_ID).toString(16)}`, // Kembalikan Chain ID dalam format heksadesimal id: id }); } else if (method === 'eth_blockNumber') { console.log('Handling eth_blockNumber method'); // Kembalikan block number, sebagai contoh kita akan mengembalikan nilai statis res.json({ jsonrpc: '2.0', result: '0x1', // Kembalikan block number dalam format heksadesimal id: id }); } else if (method === 'net_version') { console.log('Handling net_version method'); res.json({ jsonrpc: '2.0', result: CHAIN_ID, // Kembalikan Chain ID id: id }); } else if (method === 'eth_getBalance') { console.log('Handling eth_getBalance method'); const [address, block] = params; try { const walletsDB = await readWalletsDB(); const wallet = walletsDB.wallets.find(w => w.address.toLowerCase() === address.toLowerCase()); if (wallet) { res.json({ jsonrpc: '2.0', result: wallet.balance, // Kembalikan balance dalam format heksadesimal id: id }); } else { res.status(404).json({ jsonrpc: '2.0', error: { code: -32602, message: 'Address not found' }, id: id }); } } catch (error) { console.error('Error in eth_getBalance:', error); res.status(500).json({ jsonrpc: '2.0', error: { code: -32603, message: 'Internal JSON-RPC error', data: error.message }, id: id }); } } else if (method === 'eth_getBlockByNumber') { console.log('Handling eth_getBlockByNumber method'); // Kembalikan block, sebagai contoh ini akan mengembalikan nilai statis res.json({ jsonrpc: '2.0', result: { number: '0x1', hash: '0x0', parentHash: '0x0', nonce: '0x0', sha3Uncles: '0x0', logsBloom: '0x0', transactionsRoot: '0x0', stateRoot: '0x0', receiptsRoot: '0x0', miner: '0x0', difficulty: '0x0', totalDifficulty: '0x0', extraData: '0x0', size: '0x0', gasLimit: '0x0', gasUsed: '0x0', timestamp: '0x0', transactions: [], uncles: [] }, // Kembalikan block dalam format JSON id: id }); } else if (method === 'addWallets') { console.log('Handling addWallets method'); const [numberOfWallets] = params; try { const walletsDB = await readWalletsDB(); for (let i = 0; i < numberOfWallets; i++) { const newWallet = createRandomWallet(); walletsDB.wallets.push(newWallet); } await writeWalletsDB(walletsDB); res.json({ jsonrpc: '2.0', result: `${numberOfWallets} wallets added successfully`, id: id }); } catch (error) { console.error('Error in addWallets:', error); res.status(500).json({ jsonrpc: '2.0', error: { code: -32603, message: 'Internal JSON-RPC error', data: error.message }, id: id }); } } else if (method === 'addBalance') { console.log('Handling addBalance method'); const [address, amount] = params; try { const walletsDB = await readWalletsDB(); const wallet = walletsDB.wallets.find(w => w.address.toLowerCase() === address.toLowerCase()); if (wallet) { // Konversi saldo dari desimal ke heksadesimal (sebaiknya jika di perlukan untuk pengembangan lebih lanjut gunkan hexadesimal untuk kedua nya) const amountInHex = BigInt(amount).toString(16); wallet.balance = (BigInt(wallet.balance) + BigInt('0x' + amountInHex)).toString(16); await writeWalletsDB(walletsDB); res.json({ jsonrpc: '2.0', result: `Balance added successfully`, id: id }); } else { res.status(404).json({ jsonrpc: '2.0', error: { code: -32602, message: 'Address not found' }, id: id }); } } catch (error) { console.error('Error in addBalance:', error); res.status(500).json({ jsonrpc: '2.0', error: { code: -32603, message: 'Internal JSON-RPC error', data: error.message }, id: id }); } } else if (method === 'eth_gasPrice') { console.log('Handling eth_gasPrice method'); // Implementasi untuk mengembalikan gas price(bebas tetapi lebih baik gunkan konversi gwei)., misalnya: res.json({ jsonrpc: '2.0', result: '0x12a05f200', // Contoh gas price dalam format heksadesimal id: id }); } else if (method === 'eth_estimateGas') { console.log('Handling eth_estimateGas method'); // Implementasi untuk menghitung estimate gas, misalnya: res.json({ jsonrpc: '2.0', result: '0x5208', // Contoh estimate gas dalam format heksadesimal id: id }); } else if (method === 'eth_getTransactionCount') { console.log('Handling eth_getTransactionCount method'); const [address, block] = params; // Implementasi untuk mengembalikan transaction count, misalnya: res.json({ jsonrpc: '2.0', result: '0x1', // Contoh transaction count dalam format heksadesimal id: id }); } }); app.listen(8000, () => { console.log('Server HTTP berjalan di port 8000'); });