tks-rebuild/miner.js
2025-01-08 13:39:45 +05:30

52 lines
1.5 KiB
JavaScript

const axios = require('axios');
const crypto = require('crypto');
const fs = require('fs');
// Load user wallet from JSON (private key)
const wallet = loadWallet();
function loadWallet() {
if (!fs.existsSync('wallet.json')) {
console.error('Wallet not found! Please create one using the Discord bot.');
process.exit(1);
}
return JSON.parse(fs.readFileSync('wallet.json'));
}
// Generate a random amount of tokens to "mine"
function mineTokens() {
return Math.floor(Math.random() * 10) + 1; // Mine between 1 to 10 tokens
}
// Sign data with user's private key
function signData(data, privateKey) {
const signer = crypto.createSign('sha256');
signer.update(data);
return signer.sign(privateKey, 'hex');
}
// Perform the mining operation
async function performMining() {
const tokens = mineTokens();
const userId = wallet.userId;
const data = `${userId}:${tokens}`;
// Sign the mining data with private key
const signature = signData(data, wallet.privateKey);
// Send mined tokens to the API
try {
const response = await axios.post('http://localhost:45712/mine', {
userId,
tokens,
signature
});
console.log(`Mining successful! Balance: ${response.data.balance}`);
} catch (error) {
console.error('Mining failed:', error.response?.data || error.message);
}
}
// Start mining every 30 seconds
setInterval(performMining, 30000); // Mine every 30 seconds