-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
152 lines (128 loc) · 4.64 KB
/
app.js
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
function mineBlock() {
{
fetch('http://localhost:5000/mine_block')
.then(response => response.json())
.then(data => {
displayMinedBlock(data);
});
}
}
// Function to check the validity of the blockchain
function checkValidityChain() {
console.log('Checking Validity...');
fetch('http://localhost:5000/is_valid')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
displayValidity(data);
})
.catch(error => {
console.error('Error:', error);
});
}
// Function to get the blockchain data
function getChain() {
fetch('http://localhost:5000/get_chain')
.then(response => response.json())
.then(data => {
displayChain(data.chain);
})
.catch(error => {
console.error('Error:', error);
});
}
// Function to reset the blockchain
function resetChain() {
fetch('http://localhost:5000/reset_chain', {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log(data.message);
})
.catch(error => {
console.error('Error:', error);
});
}
function displayMinedBlock(data) {
const blockchainDiv = document.getElementById('blockchain');
blockchainDiv.innerHTML = ''; // Clear the blockchain container
const blockDiv = document.createElement('div');
blockDiv.classList.add('block');
const blockTitle = document.createElement('p');
blockTitle.classList.add('block-title');
blockTitle.textContent = `Block #${data.index}`;
blockDiv.appendChild(blockTitle);
const blockContent = document.createElement('div');
blockContent.classList.add('block-content');
blockContent.innerHTML = `
<p><strong>Timestamp:</strong> ${data.timestamp}</p>
<p><strong>Proof:</strong> ${data.proof}</p>
<p><strong>Previous Hash:</strong> ${data.previous_hash}</p>
`;
blockDiv.appendChild(blockContent);
blockchainDiv.appendChild(blockDiv);
}
function displayValidity(data) {
console.log('Displaying Validity:', data.message);
const responseSpan = document.getElementById('response');
responseSpan.textContent = '';
const blockchainDiv = document.getElementById('blockchain');
blockchainDiv.innerHTML = '';
const validityDiv = document.createElement('div');
validityDiv.classList.add('block');
const validityTitle = document.createElement('p');
validityTitle.classList.add('block-title');
validityTitle.textContent = 'Blockchain Validity';
validityDiv.appendChild(validityTitle);
const validityContent = document.createElement('div');
validityContent.classList.add('block-content');
validityContent.innerHTML = `
<p>${data.message}</p>
`;
validityDiv.appendChild(validityContent);
blockchainDiv.appendChild(validityDiv);
}
// Function to display the blockchain in card format
function displayChain(chain) {
const blockchainDiv = document.getElementById('blockchain');
blockchainDiv.innerHTML = '<h2>Blockchain :</h2><br>';
// Iterate through the chain and display each block
chain.forEach(block => {
const blockDiv = document.createElement('div');
blockDiv.classList.add('block');
const blockTitle = document.createElement('p');
blockTitle.classList.add('block-title');
blockTitle.textContent = `Block #${block.index}`;
blockDiv.appendChild(blockTitle);
const blockContent = document.createElement('div');
blockContent.classList.add('block-content');
blockContent.innerHTML = `
<p><strong>Timestamp:</strong> ${block.timestamp}</p>
<p><strong>Proof:</strong> ${block.proof}</p>
<p><strong>Previous Hash:</strong> ${block.previous_hash}</p>
`;
blockDiv.appendChild(blockContent);
blockchainDiv.appendChild(blockDiv);
});
}
// Function to reset the blockchain and display a message
function resetBlockchain() {
fetch('http://localhost:5000/reset_chain', {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log(data.message);
const blockchainDiv = document.getElementById('blockchain');
blockchainDiv.innerHTML = '';
getChain();
})
.catch(error => {
console.error('Error:', error);
});
}