forked from captain-khuman/hackathonnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.js
359 lines (318 loc) · 12 KB
/
interact.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
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
require('dotenv').config();
const {Web3} = require('web3');
const express = require('express');
const app = express();
const web3 = new Web3(process.env.NODE_URL || "http://127.0.0.1:7545");
const cors = require('cors');
app.use(cors());
// Contract ABIs and Addresses
const AccessControlABI = require('./build/contracts/axess.json').abi;
const DataPrivacyABI = require('./build/contracts/privacy.json').abi;
const accessFinalABI = require('./build/contracts/AccessControl.json').abi;
const accessControlAddress = "0x30Cc93F37eF78CEFE383A8682B613a079d8029B4";
const dataPrivacyAddress = "0x4020F692d599Fa323786D959313888E372CFd022";
const accessFinalAddress = "0x2e3A67b5A26e085b1473F73b68642743920ae6B0";
const accessControl = new web3.eth.Contract(AccessControlABI, accessControlAddress);
const dataPrivacy = new web3.eth.Contract(DataPrivacyABI, dataPrivacyAddress);
const accessFinal = new web3.eth.Contract(accessFinalABI, accessFinalAddress);
let account;
// Initialize Ethereum account
async function getAccounts() {
try {
const accounts = await web3.eth.getAccounts();
account = accounts[0];
console.log(`Connected to account: ${account}`);
} catch (error) {
console.error("Error fetching accounts:", error.message);
}
}
(async () => {
await getAccounts();
})();
// Contract interaction functions
async function addUser(userAddress) {
try {
const result = await accessControl.methods.addUser(userAddress).send({ from: account });
return `User ${userAddress} added.`;
} catch (error) {
throw new Error(`Failed to add user: ${error.message}`);
}
}
async function logAccessAttempt(userAddress) {
try {
const result = await accessControl.methods.logAccessAttempt(userAddress).send({ from: account });
return `Access attempt logged for ${userAddress}.`;
} catch (error) {
throw new Error(`Failed to log access: ${error.message}`);
}
}
async function setDataPrivacy(enabled) {
try {
const result = await dataPrivacy.methods.setDataPrivacy(enabled).send({ from: account });
return `Privacy setting set to ${enabled}.`;
} catch (error) {
throw new Error(`Failed to set privacy: ${error.message}`);
}
}
async function verifyPersonnel(personnelAddress) {
try {
const result = await accessFinal.methods.verifyPersonnel(personnelAddress).send({ from: account });
return `Personnel ${personnelAddress} verified.`;
} catch (error) {
throw new Error(`Failed to verify personnel: ${error.message}`);
}
}
async function revokePersonnel(personnelAddress) {
try {
const result = await accessFinal.methods.revokePersonnel(personnelAddress).send({ from: account });
return `Personnel ${personnelAddress} revoked.`;
} catch (error) {
throw new Error(`Failed to revoke personnel: ${error.message}`);
}
}
async function isVerified(personnelAddress) {
try {
const result = await accessFinal.methods.isVerified(personnelAddress).call();
return `Verification status: ${result}`;
} catch (error) {
throw new Error(`Failed to check verification: ${error.message}`);
}
}
async function getOwner() {
try {
const result = await accessFinal.methods.owner().call();
return `Contract owner: ${result}`;
} catch (error) {
throw new Error(`Failed to get owner: ${error.message}`);
}
}
// HTML content variable
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blockchain Access Control</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
body {
font-family: 'Roboto', sans-serif;
background: url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MXwyMDg2MzZ8MHwxfGFsbHwxfHx8fHx8fHwxNjA3ODk1MTI4&ixlib=rb-1.2.1&q=80&w=1920') no-repeat center center fixed;
background-size: cover;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
background: rgba(255, 255, 255, 0.9);
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0,0,0,0.3);
margin: auto;
backdrop-filter: blur(10px);
}
h2 {
color: #007BFF;
text-align: center;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 20px;
}
input, button {
width: 100%;
padding: 12px;
margin: 5px 0;
border: 1px solid #ced4da;
border-radius: 5px;
transition: border-color 0.3s;
}
input:focus, button:focus {
border-color: #007BFF;
outline: none;
}
button {
background: #007BFF;
color: white;
border: none;
cursor: pointer;
font-weight: bold;
}
button:hover {
background: #0056b3;
}
#response {
margin-top: 20px;
padding: 10px;
border-radius: 5px;
text-align: center;
font-weight: bold;
}
.success {
background: #d4edda;
color: #155724;
}
.error {
background: #f8d7da;
color: #721c24;
}
label {
margin: 10px 0 5px;
display: block;
font-weight: 600;
}
.checkbox-container {
display: flex;
align-items: center;
margin: 10px 0;
}
.checkbox-container input {
width: auto;
margin-right: 10px;
}
.icon {
margin-right: 8px;
}
</style>
</head>
<body>
<div class="container">
<h2>Blockchain Access Control</h2>
<div class="input-group">
<label for="personnelAddress"><i class="fas fa-user-shield icon"></i>Personnel Address</label>
<input type="text" id="personnelAddress" placeholder="Enter Personnel Address">
<button onclick="verify()"><i class="fas fa-check-circle icon"></i>Verify Personnel</button>
<button onclick="revoke()"><i class="fas fa-user-slash icon"></i>Revoke Personnel</button>
<button onclick="checkVerification()"><i class="fas fa-search icon"></i>Check Verification</button>
</div>
<div class="input-group">
<label for="userAddress"><i class="fas fa-user-plus icon"></i>User Address</label>
<input type="text" id="userAddress" placeholder="Enter User Address">
<button onclick="addUser()"><i class="fas fa-user-plus icon"></i>Add User</button>
<button onclick="logAccess()"><i class="fas fa-clipboard-list icon"></i>Log Access</button>
</div>
<div class="checkbox-container">
<input type="checkbox" id="privacySetting">
<label for="privacySetting"><i class="fas fa-lock icon"></i>Enable Privacy</label>
</div>
<button onclick="setPrivacy()"><i class="fas fa-shield-alt icon"></i>Set Privacy</button>
<button onclick="getOwner()"><i class="fas fa-info-circle icon"></i>Get Owner</button>
<div id="response"></div>
</div>
<script>
async function sendRequest(endpoint, data) {
const responseDiv = document.getElementById('response');
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await response.json();
if (!response.ok) throw new Error(result.message);
responseDiv.className = 'success';
responseDiv.textContent = result.message;
} catch (error) {
responseDiv.className = 'error';
responseDiv.textContent = error.message;
}
}
async function verify() {
const address = document.getElementById('personnelAddress').value;
await sendRequest('/verifyPersonnel', { personnelAddress: address });
}
async function revoke() {
const address = document.getElementById('personnelAddress').value;
await sendRequest('/revokePersonnel', { personnelAddress: address });
}
async function checkVerification() {
const address = document.getElementById('personnelAddress').value;
await sendRequest('/isVerified', { personnelAddress: address });
}
async function addUser() {
const address = document.getElementById('userAddress').value;
await sendRequest('/addUser', { userAddress: address });
}
async function logAccess() {
const address = document.getElementById('userAddress').value;
await sendRequest('/logAccessAttempt', { userAddress: address });
}
async function setPrivacy() {
const enabled = document.getElementById('privacySetting').checked;
await sendRequest('/setDataPrivacy', { enabled });
}
async function getOwner() {
await sendRequest('/getOwner', {});
}
</script>
</body>
</html>
`;
// Routes
app.get('/', (req, res) => {
res.send(htmlContent);
});
// API endpoints
app.post('/verifyPersonnel', express.json(), async (req, res) => {
try {
const message = await verifyPersonnel(req.body.personnelAddress);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post('/revokePersonnel', express.json(), async (req, res) => {
try {
const message = await revokePersonnel(req.body.personnelAddress);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/isVerified', express.json(), async (req, res) => {
try {
const message = await isVerified(req.body.personnelAddress);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post('/addUser', express.json(), async (req, res) => {
try {
const message = await addUser(req.body.userAddress);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post('/logAccessAttempt', express.json(), async (req, res) => {
try {
const message = await logAccessAttempt(req.body.userAddress);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post('/setDataPrivacy', express.json(), async (req, res) => {
try {
const message = await setDataPrivacy(req.body.enabled);
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/getOwner', express.json(), async (req, res) => {
try {
const message = await getOwner();
res.json({ message });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Start server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});