-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.html
More file actions
164 lines (153 loc) · 5.32 KB
/
Block.html
File metadata and controls
164 lines (153 loc) · 5.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Blockchain Example</title>
<script src="https://cdn.jsdelivr.net/npm/crypto-js@4.0.0/crypto-js.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-image: url('https://images8.alphacoders.com/123/thumb-1920-1239296.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 100px;
}
.container {
width: 80%;
margin-top: 20px;
background-color: rgba(255, 255, 255, 0.8); /* Color de fondo para mejorar la legibilidad */
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #ffffff;
display: flex;
align-items: center;
}
h3 {
color: #000000;
text-align: right;
display: flex;
align-items: center;
}
h1 img, h3 img {
margin-left: 10px;
width: 40px;
height: auto;
}
form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
input, button {
padding: 10px;
margin: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #4CAF50;
color: white;
cursor: pointer;
}
.block {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
align-items: center;
position: relative;
}
.block img {
width: 40px;
height: 40px;
position: absolute;
top: 10px;
right: 10px;
}
p {
margin: 5px 0;
}
</style>
</head>
<body>
<h1>
Blockchain Example
</h1>
<div class="container">
<div id="blockchain"></div>
<form id="blockForm">
<input type="text" id="blockData" placeholder="Block Data" required>
<button type="submit">Add Block</button>
</form>
<h3> <img src="https://upload.wikimedia.org/wikipedia/commons/4/46/Bitcoin.svg" alt="BTC Logo">.Palacios</h3>
</div>
<script>
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
calculateHash() {
return CryptoJS.SHA256(this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash).toString();
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock() {
return new Block(0, '01/01/2021', 'Bloque Génesis', '0');
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
this.displayBlockchain();
}
displayBlockchain() {
const blockchainDiv = document.getElementById('blockchain');
blockchainDiv.innerHTML = '';
this.chain.forEach(block => {
const blockDiv = document.createElement('div');
blockDiv.className = 'block';
blockDiv.innerHTML = `
<p>Index: ${block.index}</p>
<p>Timestamp: ${block.timestamp}</p>
<p>Data: ${JSON.stringify(block.data)}</p>
<p>Previous Hash: ${block.previousHash}</p>
<p>Hash: ${block.hash}</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/4/46/Bitcoin.svg" alt="BTC Logo">
`;
blockchainDiv.appendChild(blockDiv);
});
}
}
const myBlockchain = new Blockchain();
myBlockchain.displayBlockchain();
document.getElementById('blockForm').addEventListener('submit', function(event) {
event.preventDefault();
const data = document.getElementById('blockData').value;
myBlockchain.addBlock(new Block(myBlockchain.chain.length, new Date().toLocaleString(), { data: data }));
document.getElementById('blockData').value = '';
});
</script>
</body>
</html>