-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
149 lines (104 loc) · 3.32 KB
/
cli.js
File metadata and controls
149 lines (104 loc) · 3.32 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require("fs");
const readline = require("readline");
const Blockchain = require("./blockchain");
const { generateWallet, signMessage } = require("./wallet_utils");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const bc = new Blockchain();
function menu() {
console.log("\n--- CLEAN-CHAIN CLI ---");
console.log("1. Create Wallet");
console.log("2. Add Social Impact Activity (Signed)");
console.log("3. View Blockchain");
console.log("4. Check Blockchain Validity");
console.log("5. View User Balances");
console.log("6. Exit");
rl.question("Enter your choice: ", handleChoice);
}
function handleChoice(choice) {
if (choice === "1") {
rl.question("Enter your username: ", (username) => {
const { privateKey, publicKey } = generateWallet();
let wallets = {};
if (fs.existsSync("wallets.json")) {
wallets = JSON.parse(fs.readFileSync("wallets.json"));
}
if (wallets[username]) {
console.log("⚠️ Username already exists!");
} else {
wallets[username] = {
public: publicKey,
private: privateKey
};
fs.writeFileSync(
"wallets.json",
JSON.stringify(wallets, null, 4)
);
console.log(`✅ Wallet created for ${username}`);
console.log(`🔑 Private Key: ${privateKey}`);
console.log(`🔓 Public Key: ${publicKey}`);
}
menu();
});
} else if (choice === "2") {
rl.question("Enter your username: ", (user) => {
if (!fs.existsSync("wallets.json")) {
console.log("⚠️ No wallets found. Create one first.");
return menu();
}
const wallets = JSON.parse(fs.readFileSync("wallets.json"));
if (!wallets[user]) {
console.log("⚠️ Username not found.");
return menu();
}
rl.question("Describe your good action: ", (action) => {
const privKey = wallets[user].private;
const message = {
user: user,
action: action
};
const signature = signMessage(privKey, message);
bc.addBlock({
user: user,
action: action,
signature: signature
});
console.log("✅ Block added successfully and signed!");
menu();
});
});
} else if (choice === "3") {
console.log("\n--- Blockchain ---");
bc.chain.forEach((block) => {
console.log(`\nBlock #${block.index}`);
console.log("Timestamp:", block.timestamp);
console.log("Data:", block.data);
console.log("Previous Hash:", block.previous_hash);
console.log("Hash:", block.hash);
});
menu();
} else if (choice === "4") {
console.log("\nBlockchain Validity:", bc.isChainValid());
menu();
} else if (choice === "5") {
if (fs.existsSync("balances.json")) {
const balances = JSON.parse(fs.readFileSync("balances.json"));
console.log("\n--- User Balances ---");
for (let user in balances) {
console.log(`${user}: ${balances[user]} CLEAN-COINs`);
}
} else {
console.log("⚠️ No balance data found yet.");
}
menu();
} else if (choice === "6") {
console.log("👋 Exiting...");
rl.close();
} else {
console.log("❌ Invalid choice. Try again.");
menu();
}
}
menu();