-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
635 lines (615 loc) · 24.1 KB
/
index.html
File metadata and controls
635 lines (615 loc) · 24.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PyChain Wallet</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/forge/1.3.1/forge.min.js"></script>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
/>
<script>
tailwind.config = {
darkMode: "class",
theme: {
extend: {
colors: {
slate: { 850: "#151f32", 950: "#020617" },
},
fontFamily: { mono: ["Consolas", "Monaco", "monospace"] },
},
},
};
</script>
<style>
[v-cloak] {
display: none;
}
.glass-panel {
background: rgba(30, 41, 59, 0.7);
backdrop-filter: blur(10px);
border: 1px solid rgba(51, 65, 85, 0.5);
}
.btn-action {
transition: all 0.2s;
}
.btn-action:active {
transform: scale(0.95);
}
</style>
</head>
<body
class="bg-slate-950 text-slate-300 font-sans text-sm min-h-screen flex items-center justify-center p-4"
>
<div
id="app"
v-cloak
class="w-full max-w-[1000px] h-[600px] flex rounded-2xl overflow-hidden shadow-2xl border border-slate-800 bg-slate-900"
>
<!-- SIDEBAR NAVIGATION -->
<div
class="w-16 md:w-64 bg-slate-950 flex flex-col border-r border-slate-800"
>
<div class="p-6 flex items-center gap-3">
<div
class="w-8 h-8 rounded bg-blue-600 flex items-center justify-center text-white font-bold shadow-lg shadow-blue-500/20"
>
<i class="fa-brands fa-hive"></i>
</div>
<span class="text-lg font-bold text-slate-100 hidden md:block"
>PyChain</span
>
</div>
<nav class="flex-1 px-4 space-y-2 mt-4">
<button
@click="currentView = 'wallet'"
:class="currentView === 'wallet' ? 'bg-blue-600 text-white' : 'text-slate-400 hover:text-slate-200 hover:bg-slate-900'"
class="w-full flex items-center gap-3 px-3 py-3 rounded-xl transition-all"
>
<i class="fa-solid fa-wallet text-lg w-6 text-center"></i>
<span class="font-medium hidden md:block">Wallet</span>
</button>
<button
@click="currentView = 'explorer'"
:class="currentView === 'explorer' ? 'bg-blue-600 text-white' : 'text-slate-400 hover:text-slate-200 hover:bg-slate-900'"
class="w-full flex items-center gap-3 px-3 py-3 rounded-xl transition-all"
>
<i class="fa-solid fa-cube text-lg w-6 text-center"></i>
<span class="font-medium hidden md:block">Explorer</span>
</button>
</nav>
<div class="p-4 border-t border-slate-900">
<div
class="text-[10px] uppercase text-slate-600 font-bold mb-1 hidden md:block"
>
Connection
</div>
<div
class="flex items-center gap-2 bg-slate-900 p-2 rounded border border-slate-800"
>
<div
class="w-2 h-2 rounded-full"
:class="isConnected ? 'bg-emerald-500 animate-pulse' : 'bg-red-500'"
></div>
<input
v-model="nodeUrl"
@blur="fetchData"
class="bg-transparent w-full text-xs font-mono text-slate-400 focus:outline-none"
placeholder="Node URL"
/>
</div>
</div>
</div>
<!-- MAIN CONTENT -->
<div
class="flex-1 flex flex-col relative overflow-hidden bg-[url('https://tailwindcss.com/_next/static/media/hero-dark.9a8f2762.jpg')] bg-cover bg-center"
>
<div class="absolute inset-0 bg-slate-950/80 backdrop-blur-sm"></div>
<!-- ONBOARDING OVERLAY (If no wallet) -->
<div
v-if="!wallet.publicKey"
class="absolute inset-0 z-50 flex items-center justify-center p-6 glass-panel"
>
<div class="max-w-md w-full space-y-6 text-center">
<div
class="w-16 h-16 bg-slate-800 rounded-full flex items-center justify-center mx-auto border border-slate-700"
>
<i class="fa-solid fa-key text-2xl text-slate-400"></i>
</div>
<div>
<h2 class="text-2xl font-bold text-white">Welcome to PyChain</h2>
<p class="text-slate-400 mt-2">
To get started, create a new cryptographic identity or import an
existing one.
</p>
</div>
<div class="grid grid-cols-2 gap-4">
<button
@click="createWallet"
class="bg-blue-600 hover:bg-blue-500 text-white py-3 rounded-xl font-bold transition shadow-lg shadow-blue-500/20"
>
Create New
</button>
<button
@click="showImport = !showImport"
class="bg-slate-800 hover:bg-slate-700 text-white py-3 rounded-xl font-bold transition border border-slate-700"
>
Import Key
</button>
</div>
<div v-if="showImport" class="text-left animate-fade-in-up">
<textarea
v-model="importKeyInput"
class="w-full bg-slate-950 border border-slate-800 rounded-lg p-3 text-xs font-mono text-slate-300 h-24 focus:ring-1 focus:ring-blue-500 focus:outline-none"
placeholder="Paste your Private Key PEM..."
></textarea>
<button
@click="importWallet"
class="w-full mt-2 bg-slate-700 hover:bg-slate-600 text-white py-2 rounded-lg text-xs font-bold"
>
Load Wallet
</button>
</div>
</div>
</div>
<!-- VIEW: WALLET -->
<div
v-if="currentView === 'wallet' && wallet.publicKey"
class="relative z-10 flex-1 flex flex-col p-8 overflow-y-auto"
>
<!-- Balance Card -->
<div
class="w-full bg-gradient-to-br from-blue-900 to-slate-900 rounded-2xl p-8 border border-white/10 shadow-2xl text-center mb-8 relative overflow-hidden group"
>
<div
class="absolute -top-10 -right-10 w-40 h-40 bg-blue-500/20 rounded-full blur-3xl group-hover:bg-blue-500/30 transition duration-700"
></div>
<h3
class="text-slate-400 font-medium text-xs uppercase tracking-widest mb-2"
>
Total Balance
</h3>
<div class="text-5xl font-bold text-white tracking-tight mb-2">
{{ myBalance.toFixed(2) }}
<span class="text-lg text-blue-300">PYC</span>
</div>
<div
class="text-xs text-blue-200/50 font-mono cursor-pointer hover:text-white transition"
@click="copyToClipboard(wallet.publicKey)"
>
{{ truncate(wallet.publicKey, 12) }}
<i class="fa-regular fa-copy ml-1"></i>
</div>
</div>
<!-- Action Buttons -->
<div class="grid grid-cols-3 gap-4 max-w-lg mx-auto w-full mb-8">
<button
@click="modalAction = 'receive'"
class="btn-action flex flex-col items-center gap-2 p-4 rounded-xl bg-slate-800/50 hover:bg-slate-800 border border-slate-700"
>
<div
class="w-10 h-10 rounded-full bg-emerald-500/10 text-emerald-400 flex items-center justify-center text-lg"
>
<i class="fa-solid fa-arrow-down"></i>
</div>
<span class="font-bold text-xs text-slate-300">Receive</span>
</button>
<button
@click="modalAction = 'send'"
class="btn-action flex flex-col items-center gap-2 p-4 rounded-xl bg-slate-800/50 hover:bg-slate-800 border border-slate-700"
>
<div
class="w-10 h-10 rounded-full bg-blue-500/10 text-blue-400 flex items-center justify-center text-lg"
>
<i class="fa-solid fa-paper-plane"></i>
</div>
<span class="font-bold text-xs text-slate-300">Send</span>
</button>
<button
@click="mineBlock"
class="btn-action flex flex-col items-center gap-2 p-4 rounded-xl bg-slate-800/50 hover:bg-slate-800 border border-slate-700"
>
<div
class="w-10 h-10 rounded-full bg-amber-500/10 text-amber-400 flex items-center justify-center text-lg"
>
<i class="fa-solid fa-hammer" :class="{'fa-spin': loading}"></i>
</div>
<span class="font-bold text-xs text-slate-300">Mine (Earn)</span>
</button>
</div>
<!-- Transaction History -->
<div class="glass-panel rounded-xl flex-1 flex flex-col min-h-0">
<div
class="p-4 border-b border-slate-700/50 flex justify-between items-center"
>
<h4 class="font-bold text-slate-200">Recent Activity</h4>
<button
@click="fetchData"
class="text-xs text-slate-500 hover:text-white"
>
<i class="fa-solid fa-rotate-right"></i>
</button>
</div>
<div class="overflow-y-auto p-2 space-y-1">
<div
v-if="myTransactions.length === 0"
class="text-center py-10 text-slate-600"
>
No transactions found
</div>
<div
v-for="tx in myTransactions"
class="flex items-center justify-between p-3 hover:bg-white/5 rounded-lg transition group"
>
<div class="flex items-center gap-3">
<div
class="w-8 h-8 rounded-full flex items-center justify-center"
:class="tx.type === 'in' ? 'bg-emerald-500/20 text-emerald-400' : 'bg-slate-700 text-slate-400'"
>
<i
class="fa-solid"
:class="tx.type === 'in' ? (tx.sender === '0' ? 'fa-hammer' : 'fa-arrow-down') : 'fa-arrow-up'"
></i>
</div>
<div>
<div class="font-bold text-slate-200 text-xs">
{{ tx.label }}
</div>
<div class="text-[10px] text-slate-500 font-mono">
{{ tx.date }}
</div>
</div>
</div>
<div
class="font-mono font-bold"
:class="tx.type === 'in' ? 'text-emerald-400' : 'text-slate-400'"
>
{{ tx.type === 'in' ? '+' : '-' }}{{ tx.amount }}
</div>
</div>
</div>
</div>
</div>
<!-- VIEW: EXPLORER -->
<div
v-if="currentView === 'explorer'"
class="relative z-10 flex-1 flex flex-col p-8 overflow-y-auto"
>
<div class="flex items-center justify-between mb-6">
<h2 class="text-2xl font-bold text-white">Network Explorer</h2>
<div class="flex gap-4 text-xs font-mono text-slate-400">
<div class="glass-panel px-3 py-1 rounded">
Height: <span class="text-white">{{ chain.length }}</span>
</div>
<div class="glass-panel px-3 py-1 rounded">
Difficulty:
<span class="text-amber-500">{{ currentDifficulty }}</span>
</div>
<div class="glass-panel px-3 py-1 rounded">
Mempool: <span class="text-blue-400">{{ pendingTxCount }}</span>
</div>
</div>
</div>
<div class="space-y-4">
<div
v-for="block in reversedChain"
:key="block.index"
class="glass-panel rounded-xl p-4 hover:border-slate-600 transition"
>
<div class="flex justify-between items-start mb-3">
<div class="flex items-center gap-3">
<div
class="bg-slate-800 text-slate-300 font-mono font-bold px-2 py-1 rounded text-xs"
>
#{{ block.index }}
</div>
<div class="text-[10px] text-slate-500 font-mono">
{{ truncate(block.previous_hash, 20) }}
</div>
</div>
<div class="text-[10px] text-slate-400">
{{ formatTime(block.timestamp) }}
</div>
</div>
<!-- Block Txs -->
<div class="bg-slate-950/50 rounded-lg p-2 space-y-1">
<div
v-if="block.transactions.length === 0"
class="text-[10px] text-slate-600 pl-2"
>
Empty Block
</div>
<div
v-for="tx in block.transactions"
class="flex items-center text-[10px] font-mono p-1 border-b border-white/5 last:border-0"
>
<span
v-if="tx.sender === '0'"
class="text-amber-500 w-16 font-bold"
>REWARD</span
>
<span v-else class="text-blue-500 w-16 font-bold"
>TRANSFER</span
>
<span class="text-slate-400 truncate w-24"
>{{ truncate(tx.sender, 6) }}</span
>
<i class="fa-solid fa-arrow-right text-slate-600 mx-2"></i>
<span class="text-slate-300 truncate w-24"
>{{ truncate(tx.recipient, 6) }}</span
>
<span class="ml-auto text-emerald-400 font-bold"
>{{ tx.amount }} PYC</span
>
</div>
</div>
</div>
</div>
</div>
<!-- MODALS -->
<div
v-if="modalAction"
class="absolute inset-0 z-50 bg-slate-950/90 backdrop-blur flex items-center justify-center p-4"
>
<div class="glass-panel w-full max-w-sm rounded-2xl p-6 relative">
<button
@click="modalAction = null"
class="absolute top-4 right-4 text-slate-500 hover:text-white"
>
<i class="fa-solid fa-xmark text-lg"></i>
</button>
<!-- RECEIVE MODAL -->
<div v-if="modalAction === 'receive'" class="text-center space-y-4">
<h3 class="text-lg font-bold text-white">Receive Assets</h3>
<div class="bg-white p-2 rounded-lg inline-block">
<!-- Fake QR for visual -->
<div
class="w-32 h-32 bg-slate-900 flex items-center justify-center text-slate-700 text-[10px]"
>
<i class="fa-solid fa-qrcode text-4xl"></i>
</div>
</div>
<div
class="bg-slate-950 border border-slate-800 p-3 rounded-lg text-[10px] font-mono text-slate-300 break-all text-left"
>
{{ wallet.publicKey }}
</div>
<button
@click="copyToClipboard(wallet.publicKey)"
class="bg-blue-600 text-white w-full py-2 rounded-lg font-bold text-xs hover:bg-blue-500"
>
Copy Address
</button>
</div>
<!-- SEND MODAL -->
<div v-if="modalAction === 'send'" class="space-y-4">
<h3 class="text-lg font-bold text-white">Send Coins</h3>
<div>
<label class="text-[10px] text-slate-500 uppercase font-bold"
>Recipient Public Key</label
>
<input
v-model="txForm.recipient"
class="w-full bg-slate-950 border border-slate-800 rounded-lg p-3 text-xs text-white focus:outline-none focus:border-blue-500 font-mono"
placeholder="Paste address..."
/>
</div>
<div>
<label class="text-[10px] text-slate-500 uppercase font-bold"
>Amount</label
>
<input
type="number"
v-model.number="txForm.amount"
class="w-full bg-slate-950 border border-slate-800 rounded-lg p-3 text-lg font-bold text-white focus:outline-none focus:border-blue-500"
placeholder="0.00"
/>
</div>
<button
@click="sendTransaction"
:disabled="loading"
class="w-full bg-blue-600 text-white py-3 rounded-xl font-bold hover:bg-blue-500 disabled:opacity-50"
>
{{ loading ? 'Signing...' : 'Confirm Transaction' }}
</button>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp } = Vue;
createApp({
data() {
return {
nodeUrl: "http://localhost:50000",
currentView: "wallet",
modalAction: null,
showImport: false,
importKeyInput: "",
isConnected: false,
loading: false,
chain: [],
pendingTxCount: 0,
wallet: { publicKey: "", privateKey: "" },
txForm: { recipient: "", amount: "" },
};
},
computed: {
myBalance() {
if (!this.wallet.publicKey) return 0;
let balance = 0;
this.chain.forEach((block) => {
block.transactions.forEach((tx) => {
if (tx.recipient === this.wallet.publicKey)
balance += tx.amount;
if (tx.sender === this.wallet.publicKey) balance -= tx.amount;
});
});
return balance;
},
reversedChain() {
return [...this.chain].reverse();
},
currentDifficulty() {
return this.chain.length
? this.chain[this.chain.length - 1].difficulty || 4
: 4;
},
myTransactions() {
if (!this.wallet.publicKey) return [];
const txs = [];
// Reverse iterate blocks
for (let i = this.chain.length - 1; i >= 0; i--) {
const block = this.chain[i];
block.transactions.forEach((tx) => {
if (tx.recipient === this.wallet.publicKey) {
txs.push({
type: "in",
amount: tx.amount,
sender: tx.sender,
label: tx.sender === "0" ? "Mining Reward" : "Received",
date: new Date(block.timestamp * 1000).toLocaleTimeString(),
});
}
if (tx.sender === this.wallet.publicKey) {
txs.push({
type: "out",
amount: tx.amount,
sender: tx.sender,
label: "Sent",
date: new Date(block.timestamp * 1000).toLocaleTimeString(),
});
}
});
}
return txs.slice(0, 50); // Show last 50
},
},
mounted() {
// Check LocalStorage
const pk = localStorage.getItem("pychain_pk");
const pub = localStorage.getItem("pychain_pub");
if (pk && pub) {
this.wallet.privateKey = pk;
this.wallet.publicKey = pub;
this.fetchData();
}
},
methods: {
async fetchData() {
try {
const res = await axios.get(`${this.nodeUrl}/chain`);
this.chain = res.data.chain;
this.isConnected = true;
} catch (e) {
this.isConnected = false;
console.error("Node offline");
}
},
async createWallet() {
try {
const res = await axios.get(`${this.nodeUrl}/wallet/new`);
this.saveWallet(res.data.private_key, res.data.public_key);
} catch (e) {
alert("Error connecting to node to generate keys");
}
},
importWallet() {
try {
// Quick validation
if (!this.importKeyInput.includes("BEGIN PRIVATE KEY"))
throw new Error();
// Generate public key from private using forge (to ensure match)
const priv = forge.pki.privateKeyFromPem(this.importKeyInput);
const pub = forge.pki.publicKeyToPem(priv.publicKey);
this.saveWallet(this.importKeyInput, pub);
} catch (e) {
alert("Invalid Private Key PEM format");
}
},
saveWallet(priv, pub) {
this.wallet.privateKey = priv;
this.wallet.publicKey = pub;
localStorage.setItem("pychain_pk", priv);
localStorage.setItem("pychain_pub", pub);
this.showImport = false;
this.fetchData();
},
async mineBlock() {
this.loading = true;
try {
// VITAL: Pass the wallet address so we get the money!
await axios.get(
`${this.nodeUrl}/mine?miner_address=${encodeURIComponent(
this.wallet.publicKey
)}`
);
await this.fetchData();
} catch (e) {
alert("Mining failed");
} finally {
this.loading = false;
}
},
async sendTransaction() {
this.loading = true;
try {
const md = forge.md.sha256.create();
const message = `${this.wallet.publicKey}${this.txForm.recipient}${this.txForm.amount}`;
md.update(message, "utf8");
const key = forge.pki.privateKeyFromPem(this.wallet.privateKey);
const signature = forge.util.bytesToHex(
key.sign(
md,
forge.pss.create({
md: forge.md.sha256.create(),
mgf: forge.mgf.mgf1.create(forge.md.sha256.create()),
saltLength: forge.pss.MAX_SALT_LENGTH,
})
)
);
await axios.post(`${this.nodeUrl}/transactions/new`, {
sender: this.wallet.publicKey,
recipient: this.txForm.recipient,
amount: this.txForm.amount,
signature: signature,
});
this.modalAction = null;
this.txForm.recipient = "";
this.txForm.amount = "";
alert("Transaction Broadcasted!");
// Auto-mine to confirm for UX speed (optional)
await this.mineBlock();
} catch (e) {
alert(
"Transaction Failed: " +
(e.response?.data?.message || e.message)
);
} finally {
this.loading = false;
}
},
copyToClipboard(txt) {
navigator.clipboard.writeText(txt);
alert("Copied!");
},
truncate(str, n) {
if (!str) return "";
if (str === "0") return "SYSTEM";
// Remove PEM headers for clean view
const clean = str.replace(/-----.*-----|\n/g, "");
return clean.substring(0, n) + "...";
},
formatTime(ts) {
return new Date(ts * 1000).toLocaleString();
},
},
}).mount("#app");
</script>
</body>
</html>