-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (202 loc) · 5.99 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
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
$(document).ready(async function() {
try {
await window.ethereum.enable();
} catch (e) {
alert("Access Denied.");
}
const documentRegistryContractAddress = "0xd8fa4d3772b1575e7569ef22c4289d32f1d211b2";
const documentRegistryContractABI = [
{
"constant": false,
"inputs": [
{
"internalType": "string",
"name": "hash",
"type": "string"
}
],
"name": "add",
"outputs": [
{
"internalType": "uint256",
"name": "dateAdded",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "index",
"type": "uint256"
}
],
"name": "getDocument",
"outputs": [
{
"internalType": "string",
"name": "hash",
"type": "string"
},
{
"internalType": "uint256",
"name": "dateAdded",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getDocumentsCount",
"outputs": [
{
"internalType": "uint256",
"name": "documentCount",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
const IPFS = window.IpfsApi("localhost", "5001");
const Buffer = IPFS.Buffer;
// === User Interface Handlers Start ===
$("#linkHome").click(function() {
showView("viewHome");
});
$("#linkSubmitDocument").click(function() {
showView("viewSubmitDocument");
});
$("#linkGetDocuments").click(function() {
$("#viewGetDocuments div").remove();
showView("viewGetDocuments");
viewGetDocuments();
});
// Attach AJAX "loading" event listener
$(document).on({
ajaxStart: function() {
$("#loadingBox").show();
},
ajaxStop: function() {
$("#loadingBox").hide();
}
});
function showView(viewName) {
// Hide all views and show the selected view only
$("main > section").hide();
$("#" + viewName).show();
}
function showInfo(message) {
$("#infoBox>p").html(message);
$("#infoBox").show();
$("#infoBox>header").click(function() {
$("#infoBox").hide();
});
}
function showError(errorMsg) {
$("#errorBox>p").html("Error: " + errorMsg);
$("#errorBox").show();
$("#errorBox>header").click(function() {
$("#errorBox").hide();
});
}
$("#documentUploadButton").click(uploadDocument);
// === User Interface Interactions End ===
function uploadDocument() {
if($('#documentForUpload')[0].files.length === 0) {
showError("Please select a file to upload!");
return;
}
let fileReader = new FileReader();
fileReader.onload = function () {
if (typeof web3 === 'undefined') {
showError(
"Please install MetaMask to access the Ethereum Web3 API from your browser."
);
return;
}
let fileBuffer = Buffer.from(fileReader.result);
let contract = web3.eth
.contract(documentRegistryContractABI)
.at(documentRegistryContractAddress);
IPFS.files.add(fileBuffer, (err, result) => {
if (err) {
showError(err);
return;
}
if (result) {
let ipfsHash = result[0].hash;
contract.add(ipfsHash, (error, txHash) => {
if (error) {
showError("Smart contract call failed: " + error);
return;
}
if (txHash) {
showInfo(
`Document ${ipfsHash} successfully added to the registry. Transaction hash: ${txHash}`
);
return;
}
});
}
});
};
fileReader.readAsArrayBuffer($('#documentForUpload')[0].files[0]);
}
function viewGetDocuments() {
if (typeof web3 === 'undefined') {
showError("Please install MetaMash to access the Ethereum Web3 API from your Web browser.");
return;
}
let contract = web3.eth
.contract(documentRegistryContractABI)
.at(documentRegistryContractAddress);
contract.getDocumentsCount((err, res) => {
if (err) {
showError("Smart contract failed: " + err);
return;
}
let documentsCount = res.toNumber();
if (documentsCount > 0) {
let html = $('<div>');
for (let i = 0; i < documentsCount; i++) {
contract.getDocument(i, (err, result) => {
if (err) {
showError("Smart contract call failed: " + err);
return;
}
let ipfsHash = result[0];
let contractPublishDate = result[1];
let div = $('<div>');
let url = "http://localhost:8080/ipfs/" + ipfsHash;
let displayDate = new Date(contractPublishDate * 1000).toLocaleString();
div.append($(`<p>Document published on: ${displayDate}</p>`));
div.append(`<img src="${url}" />`);
html.append(div);
});
}
html.append('</div>');
$('#viewGetDocuments').append(html);
} else {
$('#viewGetDocuments').append('<div>No documents in the document registry!</div>');
}
});
}
});