-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (141 loc) · 5.75 KB
/
index.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
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
var account;
web3.eth.getAccounts().then((f) => {
account = f[0];
})
abi = JSON.parse('[{"constant":true,"inputs":[{"name":"studentID","type":"uint64"},{"name":"password","type":"bytes16"}],"name":"fetchRecord","outputs":[{"name":"","type":"bytes16"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"authID","type":"bytes32"}],"name":"validAuthority","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"authID","type":"bytes32"},{"name":"studentID","type":"uint64"},{"name":"record","type":"bytes16"},{"name":"password","type":"bytes16"}],"name":"updateRecord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"studentID","type":"uint64"},{"name":"oldPassword","type":"bytes16"},{"name":"newPassword","type":"bytes16"}],"name":"updatePassword","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"studentID","type":"uint64"},{"name":"password","type":"bytes16"}],"name":"passwordAuth","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"authIDs","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')
contract = new web3.eth.Contract(abi);
contract.options.address = "0x76F5Ae639C39746949bb34A4092385146A854830";
// update this contract address with your contract address
AWS.config.credentials.get(function(err) {
if (err) alert(err);
console.log(AWS.config.credentials);
});
var bucketName = 'csce678-project'; // Enter your bucket name
var bucket = new AWS.S3({
params: {
Bucket: bucketName
}
});
var pdfDataURL;
var pdfHash;
function updateRecord() {
student = $("#student").val();
auth = $("#auth").val();
plain_password = $("#password").val();
password = md5($("#password").val());
if(typeof pdfHash === 'undefined' || pdfHash === "" || student === ""){
if(student === ""){
alert("Enter Student ID.");
} else if(typeof pdfHash === 'undefined' || pdfHash === ""){
alert("Upload a file.");
} else {
alert("Enter Student ID, \n Upload a file.");
}
} else if (auth === ""){
alert("Authentication ID cannot be empty for update operation!");
} else if (plain_password === ""){
alert("Password cannot be empty!");
} else {
contract.methods.updateRecord(web3.utils.asciiToHex(auth), student, "0x"+pdfHash, "0x"+password).send({from: account}).then((f) => {
alert("Record has been updated.")
var keyName = student + '.txt';
content = pdfDataURL;
var params = {
Key: keyName,
Body: content,
ACL: 'public-read'
};
bucket.putObject(params, function(err, data) {
if (err) {
results.innerHTML = 'ERROR: ' + err;
} else {
console.log(data);
}
});
}).catch((error) => {
console.log('Error occurred!', error);
alert("Error! Check your Authentication ID.");
});
}
}
function fetchRecord() {
student = $("#student").val();
password = md5($("#password").val());
contract.methods.fetchRecord(student, "0x"+password).call().then((pdfHashFetched) => {
bucket.getObject({
Key: student+'.txt'
}, function(err, data) {
if (err) {
alert("The file has been tampered.");
} else {
var pdfDataURL = new TextDecoder("utf-8").decode(data.Body);
if("0x"+md5(pdfDataURL) == pdfHashFetched) {
var pdfAsArray = convertDataURIToBinary(pdfDataURL);
var pdfjsLib = window['pdfjs-dist/build/pdf'];
downloadFile(new Blob([pdfAsArray]), student+".pdf");
} else {
alert("The file has been tampered.");
}
}
});
}).catch((error) => {
console.log('Error occurred!', error);
alert("Check your Student ID and password!");
});
}
function updatePassword() {
student = $("#studentID").val();
oldPassword = md5($("#oldPassword").val());
newPassword = md5($("#newPassword").val());
if(student === "" || $("#oldPassword").val() === "" || $("#newPassword").val() === "")
{
alert("Student ID, old password and new password cannot be empty!");
}
else
{
contract.methods.updatePassword(student, "0x"+oldPassword, "0x"+newPassword).send({from: account}).then((f) => {
alert("Password has been updated.")
}).catch((error) => {
console.log('Error occurred!', error);
alert("Check your Student ID and old password!");
});
}
}
function uploadFile(){
var file = document.getElementById("record_file").files[0];
getAsText(file);
}
function getAsText(readFile) {
var reader = new FileReader();
reader.readAsDataURL(readFile);
reader.onload = loaded;
}
function loaded(evt) {
pdfDataURL = evt.target.result;
pdfHash = md5(pdfDataURL);
}
function convertDataURIToBinary(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(var i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
const downloadFile = (blob, fileName) => {
const link = document.createElement('a');
// create a blobURI pointing to our Blob
link.href = URL.createObjectURL(blob);
link.download = fileName;
// some browser needs the anchor to be in the doc
document.body.append(link);
link.click();
link.remove();
// in case the Blob uses a lot of memory
window.addEventListener('focus', e=>URL.revokeObjectURL(link.href), {once:true});
};