-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
86 lines (64 loc) · 1.99 KB
/
main.js
File metadata and controls
86 lines (64 loc) · 1.99 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
const fs = require("fs");
const Blockchain = require("./blockchain");
const { generateWallet, signMessage } = require("./wallet_utils");
// Create blockchain
const bc = new Blockchain();
// Create wallet
const wallet = generateWallet();
const privateKey = wallet.privateKey;
// Create message
const message = {
user: "Adarsh",
action: "cleaned the road"
};
// Generate signature
const signature = signMessage(privateKey, message);
// Add block
bc.addBlock({
user: "Adarsh",
action: "cleaned the road",
signature: signature
});
// Check blockchain validity
console.log("Blockchain valid:", bc.isChainValid());
// Print 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);
});
// Print balances
console.log("\n--- User Balances ---");
try {
const balances = JSON.parse(fs.readFileSync("balances.json"));
for (const user in balances) {
console.log(`${user}: ${balances[user]} CLEAN-COINs`);
}
} catch {
console.log("⚠️ No balances found.");
}
// -------- DASHBOARD --------
function displayDashboard(blockchain) {
console.log("\n=== 🚀 BLOCKCHAIN DASHBOARD ===");
blockchain.chain.forEach((block) => {
console.log(`\n📦 Block #${block.index}`);
console.log(`⏰ Timestamp : ${block.timestamp}`);
console.log(`📄 Data :`, block.data);
console.log(`🔗 Previous Hash : ${block.previous_hash}`);
console.log(`🧾 Hash : ${block.hash}`);
});
console.log("\n=== 💰 User Balances ===");
try {
const balances = JSON.parse(fs.readFileSync("balances.json"));
for (const user in balances) {
console.log(`👤 ${user}: ${balances[user]} CLEAN-COINs`);
}
} catch {
console.log("⚠️ No balances found.");
}
console.log("\n✅ Blockchain valid:", blockchain.isChainValid());
}
// Show dashboard
displayDashboard(bc);