-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.py
56 lines (49 loc) · 2.22 KB
/
verifier.py
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
import streamlit as st
import os
import hashlib
from utils.cert_utils import extract_certificate
from utils.streamlit_utils import view_certificate
from connection import contract
from utils.streamlit_utils import displayPDF, hide_icons, hide_sidebar, remove_whitespaces
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
hide_icons()
hide_sidebar()
remove_whitespaces()
options = ("Verify Certificate using PDF", "View/Verify Certificate using Certificate ID")
selected = st.selectbox("", options, label_visibility="hidden")
if selected == options[0]:
uploaded_file = st.file_uploader("Upload the PDF version of the certificate")
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
with open("certificate.pdf", "wb") as file:
file.write(bytes_data)
try:
(uid, candidate_name, course_name, org_name) = extract_certificate("certificate.pdf")
displayPDF("certificate.pdf")
os.remove("certificate.pdf")
# Calculating hash
data_to_hash = f"{uid}{candidate_name}{course_name}{org_name}".encode('utf-8')
certificate_id = hashlib.sha256(data_to_hash).hexdigest()
# Smart Contract Call
result = contract.functions.isVerified(certificate_id).call()
if result:
st.success("Certificated validated successfully!")
else:
st.error("Invalid Certificate! Certificate might be tampered")
except Exception as e:
st.error("Invalid Certificate! Certificate might be tampered")
elif selected == options[1]:
form = st.form("Validate-Certificate")
certificate_id = form.text_input("Enter the Certificate ID")
submit = form.form_submit_button("Validate")
if submit:
try:
view_certificate(certificate_id)
# Smart Contract Call
result = contract.functions.isVerified(certificate_id).call()
if result:
st.success("Certificated validated successfully!")
else:
st.error("Invalid Certificate ID!")
except Exception as e:
st.error("Invalid Certificate ID!")