-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoneyManager.js
More file actions
89 lines (77 loc) · 2.42 KB
/
moneyManager.js
File metadata and controls
89 lines (77 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
import envManager from './utils/envManager.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
envManager.loadEnv();
const WALLET_FILE = path.join(__dirname, '.wallet.enc');
const SECRET_KEY = process.env.WALLET_SECRET;
if (!SECRET_KEY) {
console.error(chalk.red('FATAL: Missing WALLET_SECRET in .env file'));
process.exit(1);
}
class MoneyManager {
constructor() {
this.balance = 0;
this.loadWallet();
}
encrypt(text) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc',
crypto.scryptSync(SECRET_KEY, 'salt', 32),
iv
);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted;
}
// i just know some of yall gonna mess with this. //
decrypt(text) {
const [ivHex, encrypted] = text.split(':');
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc',
crypto.scryptSync(SECRET_KEY, 'salt', 32),
iv
);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
loadWallet() {
try {
if (fs.existsSync(WALLET_FILE)) {
const encrypted = fs.readFileSync(WALLET_FILE, 'utf8');
this.balance = parseInt(this.decrypt(encrypted)) || 100;
} else {
this.balance = 100;
this.saveWallet();
}
} catch (error) {
console.error(chalk.red('Wallet corrupted! Resetting to 100.'));
this.balance = 100;
this.saveWallet();
}
}
saveWallet() {
fs.writeFileSync(WALLET_FILE, this.encrypt(this.balance.toString()), 'utf8');
}
getBalance() {
return this.balance;
}
add(amount) {
this.balance += amount;
this.saveWallet();
return this.balance;
}
deduct(amount) {
if (this.balance >= amount) {
this.balance -= amount;
this.saveWallet();
return true;
}
return false;
}
}
export default new MoneyManager(); // **Singleton instance //