-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
277 lines (229 loc) · 7.46 KB
/
App.js
File metadata and controls
277 lines (229 loc) · 7.46 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
import React, { useState } from "react";
import CryptoJS from "crypto-js";
import axios from "axios";
import Web3 from "web3";
import CertiStorageABI from "./CertiStorageABI.json";
function App() {
const [file, setFile] = useState(null);
const [hash, setHash] = useState("");
const [cid, setCID] = useState("");
const [web3, setWeb3] = useState(null);
const [contract, setContract] = useState(null);
const [account, setAccount] = useState("");
const [studentName, setStudentName] = useState("");
const [verifyHash, setVerifyHash] = useState("");
const [certificateDetails, setCertificateDetails] = useState(null);
const handleFileChange = (e) => {
const selectedFile = e.target.files[0];
setFile(selectedFile);
};
const generateHash = () => {
if (!file) {
alert("Please select a file first!");
return;
}
const reader = new FileReader();
reader.onload = (e) => {
const fileContent = e.target.result;
const sha256Hash = "0x" + CryptoJS.SHA256(fileContent).toString();
setHash(sha256Hash);
};
reader.readAsText(file);
};
const uploadToIPFS = async () => {
if (!file) {
alert("Please select a file first!");
return;
}
const formData = new FormData();
formData.append("file", file);
const jwt = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mb3JtYXRpb24iOnsiaWQiOiJmMmZhYTAzYy1kNmNmLTQzYjktOWUwMS00NDQ0NTYxNDYwOTUiLCJlbWFpbCI6InZlZGFudHNvbmk4MTlAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsInBpbl9wb2xpY3kiOnsicmVnaW9ucyI6W3siZGVzaXJlZFJlcGxpY2F0aW9uQ291bnQiOjEsImlkIjoiRlJBMSJ9LHsiZGVzaXJlZFJlcGxpY2F0aW9uQ291bnQiOjEsImlkIjoiTllDMSJ9XSwidmVyc2lvbiI6MX0sIm1mYV9lbmFibGVkIjpmYWxzZSwic3RhdHVzIjoiQUNUSVZFIn0sImF1dGhlbnRpY2F0aW9uVHlwZSI6InNjb3BlZEtleSIsInNjb3BlZEtleUtleSI6IjY1NjdlZDUzMzFmYjhiNWVkYjdjIiwic2NvcGVkS2V5U2VjcmV0IjoiMGQ2YzZiNTJlYjIxODFmN2FiZTdmNzI1ODAzNjI4YmIwNTdhNzFlMzA2OTg4MDY2MGVkNWIwYjEzNGQyY2E2ZCIsImV4cCI6MTc4MzYwODUzMH0.BEIPuZ7C3PtE_hJOxnRGI91nomytLeWickVFzeiP1FU";
try {
const res = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", formData, {
maxContentLength: Infinity,
maxBodyLength: Infinity,
headers: {
"Content-Type": "multipart/form-data",
Authorization: jwt,
},
});
setCID(res.data.IpfsHash);
alert("Uploaded to IPFS via Pinata successfully!");
} catch (error) {
console.error("Pinata Upload Error:", error.response ? error.response.data : error);
alert("Upload failed!");
}
};
const connectToBlockchain = async () => {
if (!window.ethereum) {
alert("Please install MetaMask!");
return;
}
try {
const chainId = await window.ethereum.request({ method: 'eth_chainId' });
// 🟠 If not Ganache (0x539 or 1337), then try switching
if (chainId !== '0x539') {
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x539' }],
});
console.log("✅ Switched to Ganache!");
} catch (switchError) {
alert("❌ Cannot switch to Ganache automatically. Please switch manually in MetaMask.");
return;
}
}
const web3Instance = new Web3(window.ethereum);
await window.ethereum.request({ method: "eth_requestAccounts" });
const accounts = await web3Instance.eth.getAccounts();
const contractAddress = "0xD68Fe0b2C15EdE87bB595bdd645c16f29FE8c540"; // your Ganache deployed contract
const certiContract = new web3Instance.eth.Contract(
CertiStorageABI,
contractAddress
);
setWeb3(web3Instance);
setContract(certiContract);
setAccount(accounts[0]);
alert("✅ Connected to Ganache Blockchain successfully!");
} catch (error) {
console.error("❌ Blockchain connection failed", error);
alert("Connection failed!");
}
};
const storeCertificate = async () => {
if (!contract) {
alert("Please connect to blockchain first!");
return;
}
if (!studentName || !hash || !cid) {
alert("Please fill all details and upload file first!");
return;
}
try {
await contract.methods
.storeCertificate(studentName, hash, cid)
.send({ from: account });
alert("Certificate stored successfully on blockchain!");
} catch (error) {
console.error("Transaction failed:", error);
alert("Transaction failed!");
}
};
const checkNetwork = async () => {
if (!window.ethereum) {
alert("MetaMask not detected!");
return false;
}
try {
const chainId = await window.ethereum.request({ method: 'eth_chainId' });
console.log("✅ Detected Chain ID:", chainId);
// Remove all checks temporarily
return true;
} catch (err) {
console.error("Error getting chain ID:", err);
return false;
}
};
const verifyCertificate = async () => {
if (!contract) {
alert("Please connect to blockchain first!");
return;
}
if (!verifyHash) {
alert("Please enter a hash to verify!");
return;
}
try {
const result = await contract.methods.verifyCertificate(verifyHash).call();
console.log(result);
if (result[0]) {
setCertificateDetails({
studentName: result[0],
cid: result[1],
});
} else {
alert("❌ Certificate not found on blockchain.");
setCertificateDetails(null);
}
} catch (error) {
console.error("Verification failed:", error);
alert("Verification failed!");
}
};
return (
<div style={{ padding: "30px" }}>
<h1>Blockchain Certificate Verification</h1>
<h2>1️⃣ Upload Certificate</h2>
<input type="file" onChange={handleFileChange} />
<br /><br />
<button onClick={generateHash}>Generate SHA-256 Hash</button>
<br /><br />
{hash && (
<div>
<h3>Generated Hash:</h3>
<p style={{ wordBreak: "break-all" }}>{hash}</p>
</div>
)}
<br />
<button onClick={uploadToIPFS}>Upload to IPFS</button>
<br /><br />
{cid && (
<div>
<h3>IPFS CID:</h3>
<p>{cid}</p>
<a
href={`https://ipfs.io/ipfs/${cid}`}
target="_blank"
rel="noopener noreferrer"
>
View on IPFS
</a>
</div>
)}
<br />
<hr />
<h2>2️⃣ Blockchain Connection</h2>
<button onClick={connectToBlockchain}>Connect to Ganache Blockchain</button>
<br /><br />
{account && <p><strong>Connected Account:</strong> {account}</p>}
<hr />
<h2>3️⃣ Store Certificate on Blockchain</h2>
<input
type="text"
placeholder="Enter Student Name"
value={studentName}
onChange={(e) => setStudentName(e.target.value)}
/>
<br /><br />
<button onClick={storeCertificate}>Store on Blockchain</button>
<br /><br />
<hr />
<h2>4️⃣ Verify Certificate by Hash</h2>
<input
type="text"
placeholder="Enter SHA-256 Hash"
value={verifyHash}
onChange={(e) => setVerifyHash(e.target.value)}
/>
<br /><br />
<button onClick={verifyCertificate}>Verify Certificate</button>
<br /><br />
{certificateDetails && (
<div>
<h3>✅ Certificate Found:</h3>
<p><strong>Student Name:</strong> {certificateDetails.studentName}</p>
<p><strong>IPFS CID:</strong> {certificateDetails.cid}</p>
<a
href={`https://ipfs.io/ipfs/${certificateDetails.cid}`}
target="_blank"
rel="noopener noreferrer"
>
View Certificate on IPFS
</a>
</div>
)}
</div>
);
}
export default App;