-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
354 lines (306 loc) · 12.5 KB
/
Copy pathclient.js
File metadata and controls
354 lines (306 loc) · 12.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const readline = require('readline');
const fs = require('fs');
const { Wallet } = require('./wallet');
const { Blockchain, Transaction } = require('./blockchain');
const { Miner } = require('./miner');
const { P2PNode } = require('./p2p');
const http = require('http');
class VBlockchainClient {
constructor() {
this.wallet = null;
this.blockchain = null;
this.miner = null;
this.p2pNode = null;
this.serverAddress = 'http://localhost:3000';
this.miningInterval = null;
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
async initialize() {
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ V-BLOCKCHAIN CLIENT v1.0 ║');
console.log('║ Proof of Vote Consensus Network ║');
console.log('╚════════════════════════════════════════════════════════════╝\n');
const choice = await this.question('Do you have an existing wallet? (yes/no): ');
if (choice.toLowerCase() === 'yes') {
await this.loadWallet();
} else {
await this.createNewWallet();
}
if (this.wallet) {
this.blockchain = new Blockchain(this.wallet.address);
this.p2pNode = new P2PNode(
this.wallet.address,
4000 + Math.floor(Math.random() * 1000),
this.serverAddress
);
this.p2pNode.setBlockchain(this.blockchain);
this.p2pNode.start();
this.miner = new Miner(this.wallet.address, this.blockchain, this.p2pNode);
await this.registerOnServer();
await this.mainMenu();
}
}
async createNewWallet() {
const username = await this.question('Enter a username: ');
this.wallet = new Wallet(username);
this.wallet.generateKeys();
this.wallet.saveToFile();
console.log('\n✓ Wallet created successfully!');
await this.wallet.displayWalletInfo();
const generateQR = await this.question('Generate QR code? (yes/no): ');
if (generateQR.toLowerCase() === 'yes') {
const qrPath = await this.wallet.generateQRCode();
console.log(`✓ QR Code saved: ${qrPath}`);
}
}
async loadWallet() {
const username = await this.question('Enter username: ');
const walletFile = `wallet_${username}.json`;
if (!fs.existsSync(walletFile)) {
console.log('✗ Wallet not found');
return;
}
this.wallet = Wallet.loadFromFile(walletFile);
console.log('✓ Wallet loaded successfully');
await this.wallet.displayWalletInfo();
}
async registerOnServer() {
console.log('\n[Network] Registering on server...');
const nodeId = this.wallet.address;
const port = this.p2pNode.port;
try {
const response = await this.makeRequest('POST', '/register-node', {
nodeId,
host: 'localhost',
port,
publicKey: this.wallet.publicKey.substring(0, 200)
});
console.log('✓ Registered on network');
console.log(`✓ Found ${response.peers.length} peers in network`);
const registerAddr = await this.question('\nRegister address on server? (yes/no): ');
if (registerAddr.toLowerCase() === 'yes') {
await this.registerAddressOnServer();
}
} catch (error) {
console.log('⚠ Could not register on server:', error.message);
}
}
async registerAddressOnServer() {
try {
await this.makeRequest('POST', '/register-address', {
address: this.wallet.address,
username: this.wallet.username,
publicKey: this.wallet.publicKey.substring(0, 200)
});
console.log('✓ Address registered with 50 V welcome bonus!');
} catch (error) {
console.log('✗ Failed to register address:', error.message);
}
}
async mainMenu() {
let running = true;
while (running) {
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ MAIN MENU ║');
console.log('╚════════════════════════════════════════════════════════════╝');
console.log('1. View Wallet Info');
console.log('2. Send Transaction');
console.log('3. View Transaction History');
console.log('4. Start Mining');
console.log('5. Stop Mining');
console.log('6. Cast Vote on Block');
console.log('7. View Blockchain');
console.log('8. Display QR Code');
console.log('9. Network Stats');
console.log('0. Exit');
console.log('════════════════════════════════════════════════════════════');
const choice = await this.question('Select option: ');
switch (choice) {
case '1':
await this.wallet.displayWalletInfo();
const balance = this.blockchain.getBalance(this.wallet.address);
console.log(`Balance in blockchain: ${balance} V`);
break;
case '2':
await this.sendTransaction();
break;
case '3':
this.viewTransactionHistory();
break;
case '4':
this.startMining();
break;
case '5':
this.miner.stopMining();
break;
case '6':
await this.castVote();
break;
case '7':
this.viewBlockchain();
break;
case '8':
await this.displayQRCode();
break;
case '9':
await this.viewNetworkStats();
break;
case '0':
running = false;
console.log('\nShutting down...');
break;
default:
console.log('✗ Invalid option');
}
}
this.cleanup();
}
async sendTransaction() {
const receiver = await this.question('Receiver address: ');
const amountStr = await this.question('Amount (V): ');
const amount = parseFloat(amountStr);
if (isNaN(amount) || amount <= 0) {
console.log('✗ Invalid amount');
return;
}
const balance = this.blockchain.getBalance(this.wallet.address);
if (balance < amount) {
console.log(`✗ Insufficient balance. Current: ${balance} V`);
return;
}
const transaction = new Transaction(
this.wallet.address,
receiver,
amount
);
transaction.sign(this.wallet.privateKey);
transaction.publicKey = this.wallet.publicKey;
if (this.blockchain.addTransaction(transaction)) {
console.log('✓ Transaction added to pending queue');
this.p2pNode.broadcastNewTransaction(transaction);
} else {
console.log('✗ Transaction invalid');
}
}
viewTransactionHistory() {
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ TRANSACTION HISTORY ║');
console.log('╚════════════════════════════════════════════════════════════╝');
let count = 0;
for (const block of this.blockchain.chain) {
for (const tx of block.transactions) {
if (tx.sender === this.wallet.address || tx.receiver === this.wallet.address) {
console.log(`\nTx #${count++}:`);
console.log(` From: ${tx.sender}`);
console.log(` To: ${tx.receiver}`);
console.log(` Amount: ${tx.amount} V`);
console.log(` Time: ${new Date(tx.timestamp).toLocaleString()}`);
}
}
}
if (count === 0) {
console.log('No transactions found');
}
}
startMining() {
const miningInterval = this.miner.startMining((block) => {
console.log(`\n✓ NEW BLOCK MINED!`);
console.log(` Index: ${block.index}`);
console.log(` Hash: ${block.calculateHash()}`);
console.log(` Transactions: ${block.transactions.length}`);
});
this.miningInterval = miningInterval;
}
async castVote() {
const choice = await this.question('Vote YES or NO (yes/no): ');
const voteValue = choice.toLowerCase() === 'yes';
const blockIndex = this.blockchain.chain.length - 1;
const result = this.miner.castVote(blockIndex, voteValue);
console.log(`Vote recorded. Approval: ${result.percentage.toFixed(2)}%`);
console.log(`Threshold: ${this.blockchain.voteThreshold}%`);
}
viewBlockchain() {
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ BLOCKCHAIN ║');
console.log('╚════════════════════════════════════════════════════════════╝');
for (let i = 0; i < this.blockchain.chain.length; i++) {
const block = this.blockchain.chain[i];
console.log(`\nBlock #${block.index}:`);
console.log(` Hash: ${block.calculateHash()}`);
console.log(` Previous: ${block.previousHash}`);
console.log(` Miner: ${block.miner}`);
console.log(` Transactions: ${block.transactions.length}`);
console.log(` Timestamp: ${new Date(block.timestamp).toLocaleString()}`);
console.log(` Nonce: ${block.nonce}`);
const votePercentage = block.getVotePercentage();
console.log(` Votes: ${Object.keys(block.votes).length} (${votePercentage.toFixed(2)}% approved)`);
}
console.log(`\nChain valid: ${this.blockchain.isChainValid()}`);
}
async displayQRCode() {
const qrPath = await this.wallet.generateQRCode();
console.log(`✓ QR Code generated: ${qrPath}`);
console.log(`✓ Your address: ${this.wallet.address}`);
console.log(`✓ Share this QR code or address to receive V tokens`);
}
async viewNetworkStats() {
try {
const stats = await this.makeRequest('GET', '/network-stats');
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ NETWORK STATISTICS ║');
console.log('╚════════════════════════════════════════════════════════════╝');
console.log(`Total Nodes: ${stats.totalNodes}`);
console.log(`Active Nodes: ${stats.activeNodes}`);
console.log(`Registered Addresses: ${stats.totalAddresses}`);
console.log(`Total V in Circulation: ${stats.totalV} V`);
} catch (error) {
console.log('✗ Could not fetch network stats:', error.message);
}
}
makeRequest(method, path, data = null) {
return new Promise((resolve, reject) => {
const url = new URL(this.serverAddress + path);
const options = {
hostname: url.hostname,
port: url.port,
path: url.pathname + url.search,
method: method,
headers: {
'Content-Type': 'application/json'
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(body));
} catch (e) {
resolve(body);
}
});
});
req.on('error', reject);
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
question(prompt) {
return new Promise(resolve => {
this.rl.question(prompt, resolve);
});
}
cleanup() {
this.miner.stopMining();
this.p2pNode.close();
this.rl.close();
process.exit(0);
}
}
const client = new VBlockchainClient();
client.initialize().catch(console.error);