const { Client, GatewayIntentBits } = require('discord.js'); const axios = require('axios'); const express = require('express'); const crypto = require('crypto'); const fs = require('fs'); // Load and save JSON data functions const users = loadUserData(); // Load user data from JSON at the start // Utility functions for reading and writing JSON files function loadUserData() { if (!fs.existsSync('users.json')) { return {}; // If the file doesn't exist, return an empty object } const data = fs.readFileSync('users.json'); return JSON.parse(data); } function saveUserData(users) { fs.writeFileSync('users.json', JSON.stringify(users, null, 2)); } // Set up the Discord bot const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.DirectMessages ] }); client.once('ready', () => { console.log('Bot is online!'); }); // Function to generate a wallet (public/private key pair) function generateWallet() { const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, }); return { publicKey: publicKey.export({ type: 'pkcs1', format: 'pem' }), privateKey: privateKey.export({ type: 'pkcs1', format: 'pem' }), }; } // Bot command to create a wallet client.on('messageCreate', async (message) => { const userId = message.author.id; if (message.content.startsWith('!createwallet')) { if (users[userId]) { return message.reply('You already have a wallet!'); } const wallet = generateWallet(); users[userId] = { publicKey: wallet.publicKey, privateKey: wallet.privateKey, balance: 0 }; saveUserData(users); // Save updated data to JSON message.reply(`Wallet created! Your public address is:\n${wallet.publicKey}`); } if (message.content.startsWith('!balance')) { const balance = users[userId]?.balance || 0; message.reply(`Your current balance is: ${balance} tokens`); } if (message.content === '!ping') { message.reply('Pong'); } }); // Set up the Express API const app = express(); app.use(express.json()); // Function to verify signature function verifySignature(data, signature, publicKey) { const verifier = crypto.createVerify('sha256'); verifier.update(data); return verifier.verify(publicKey, signature, 'hex'); } // Proof-of-Work Function (adds delay by solving computational problem) function proofOfWork(data, difficulty) { let nonce = 0; let hash = ''; const target = '0'.repeat(difficulty); // Adjust difficulty here (number of leading zeros) do { nonce++; hash = crypto.createHash('sha256').update(data + nonce).digest('hex'); } while (!hash.startsWith(target)); return { nonce, hash }; } // API route to get balance app.get('/balance/:userId', (req, res) => { const userId = req.params.userId; const balance = users[userId]?.balance || 0; res.json({ balance }); }); // API route to handle mined tokens with delay and proof-of-work app.post('/mine', (req, res) => { const { userId, tokens, signature } = req.body; const user = users[userId]; if (!user) { return res.status(400).json({ error: 'User not found' }); } const data = `${userId}:${tokens}`; if (!verifySignature(data, signature, user.publicKey)) { return res.status(400).json({ error: 'Invalid signature' }); } // Option 1: Proof-of-Work Mining (adjust difficulty for more delay) console.log('Starting proof-of-work...'); const { nonce, hash } = proofOfWork(data, 4); // Difficulty of 4 (can be increased for more delay) console.log(`Mining complete! Nonce: ${nonce}, Hash: ${hash}`); // Option 2: Simulate mining time with a delay setTimeout(() => { // Add tokens to user's balance after delay user.balance = (user.balance || 0) + tokens; saveUserData(users); // Save updated balance to JSON res.json({ balance: user.balance, nonce, hash }); }, 5000); // Simulate 5 seconds delay }); // Start both the bot and the API const PORT = 45712; app.get("/", (req, res) => { res.sendStatus(200); }); app.listen(PORT, () => { console.log(`API running on port ${PORT}`); client.login('YOUR_BOT_TOKEN'); // Replace with your actual bot token });