-
Notifications
You must be signed in to change notification settings - Fork 1
/
EtherProof.js
253 lines (233 loc) · 11.6 KB
/
EtherProof.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
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
var reader = new FileReader();
var file;
var toBeHashed = '';
var EtherProofAPI = [{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"hash","type":"bytes32"}],"name":"addFile","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"getTimestamp","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"checkExistence","outputs":[{"name":"","type":"bool"}],"type":"function"}];
// Need to get a permanent address once deployed on main net
var address = '0xd49bc1cc5a80835b0a70875f9594204d6dd5a0dc';
/* Copy paste the following code into geth to deploy the contract.
Once the contract is deployed (either on main net or your private test net),
copy paste its address into the line above replacing the current address
Web3 deploy:
var etherproofContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"hash","type":"bytes32"}],"name":"addFile","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"getTimestamp","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"hash","type":"bytes32"}],"name":"checkExistence","outputs":[{"name":"","type":"bool"}],"type":"function"}]);
var etherproof = etherproofContract.new(
{
from: web3.eth.accounts[0],
data: '606060405261023a806100126000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063473781451461005a5780636b31827014610086578063d45c44351461009e578063eef66dac146100ca57610058565b005b61007060048080359060200190919050506100f8565b6040518082815260200191505060405180910390f35b61009c600480803590602001909190505061012a565b005b6100b460048080359060200190919050506101a2565b6040518082815260200191505060405180910390f35b6100e060048080359060200190919050506101d4565b60405180821515815260200191505060405180910390f35b60006000600050600083600019168152602001908152602001600020600050600101600050549050610125565b919050565b610133816101d4565b80156101415750620186a034105b1561014b57610002565b60406040519081016040528042815260200143815260200150600060005060008360001916815260200190815260200160002060005060008201518160000160005055602082015181600101600050559050505b50565b600060006000506000836000191681526020019081526020016000206000506000016000505490506101cf565b919050565b600060006000600050600084600019168152602001908152602001600020600050600001600050541415801561022e5750600060006000506000846000191681526020019081526020016000206000506001016000505414155b9050610235565b91905056',
gas: 4700000
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
*/
var EtherProof = web3.eth.contract(EtherProofAPI).at(address);
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({ top: y - 15, left: x - 100 });
} else {
inputFile.offset({ top: -400, left: -400 });
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({ top: y - 15, left: x - 160 });
} else {
inputFile.offset({ top: -400, left: -400 });
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
})
document.getElementById('file').addEventListener('change', load, true);
document.getElementById('file').onchange = function() {
var name = this.value;
name = name.split("\\");
name = name[name.length - 1];
document.getElementById('chosenFile').textContent = name;
document.getElementById('hash').textContent = '';
document.getElementById('existence').textContent = '';
document.getElementById('register').style.display = 'none';
loop();
}
function load() {
file = document.getElementById('file').files[0];
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) {
var result = evt.target.result;
toBeHashed = result;
}
document.getElementById('hash').textContent = 'Keccak-256 hash of file: ' + web3.sha3(toBeHashed);
existence();
}
}
function loop() {
if (!file) {
alert('Please select a file!');
return;
}
reader.readAsBinaryString(file);
}
function existence() {
var hash = document.getElementById('hash').textContent.slice(-66);
var existence = document.getElementById('existence');
var fileExistence = EtherProof.checkExistence(hash, function(error, result) {
if (error) {
console.error(error);
}
});
if (hash.length != 66) {
existence.innerHTML = '<h2> Pick a file and generate a hash first </h2>';
document.getElementById('register').style.display = 'none';
} else if (!fileExistence) {
existence.innerHTML = '<h2> This File Does Not Exist </h2>';
existence.innerHTML += 'Register Your File Now for 100 000 wei';
existence.innerHTML += '<br>';
document.getElementById('register').style.display = 'block';
} else {
document.getElementById('register').style.display = 'none';
existence.innerHTML = '<h2> This File Exists </h2>';
var timestamp = EtherProof.getTimestamp(hash, function(error, result) {
if (error) {
console.error(error);
}
});
var blockNumber = EtherProof.getBlockNumber(hash, function(error, result) {
if (error) {
console.error(error);
}
});
existence.innerHTML += '<p> Timestamp: ' + new Date(timestamp * 1000) + '</p>' +
'<p> Block Number: ' + blockNumber + '</p>';
}
}
function register(form) {
document.getElementById('existence').innerHTML = '';
document.getElementById('register').style.display = 'none';
EtherProof.addFile.sendTransaction(
web3.sha3(toBeHashed),
{
from: web3.eth.accounts[$('#accounts').val()],
value: 100000,
gas: 4700000
}, function (error, result) {
if (error) {
console.log(error);
}
var txhash = result;
var filter = web3.eth.filter('latest');
filter.watch(function (error, result) {
if (error) {
console.log(error);
}
var receipt = web3.eth.getTransactionReceipt(txhash);
if (receipt && receipt.transactionHash == txhash) {
alert("Your transaction has been mined!");
if (localStorage.getItem('recentHashes')) {
}
var r = JSON.parse(localStorage.getItem('recentHashes'));
var length = r.length;
if (length < 5) {
r[length] = web3.sha3(toBeHashed);
} else {
r[5] = web3.sha3(toBeHashed);
for (var i = 0; i < 5; ++i) {
r[i] = r[i + 1];
}
}
localStorage.setItem('recentHashes', JSON.stringify(r));
updateTable();
filter.stopWatching();
}
});
}
);
}
function updateTable() {
var hashTable = document.getElementById('recentHashes');
var numRows = hashTable.rows.length;
for (var i = 1; i < numRows; ++i) {
hashTable.deleteRow(1);
}
var length = JSON.parse(localStorage.getItem('recentHashes')).length;
if (length > 5) {
length = 5;
}
for (var j = 0; j < length; ++j) {
var row = hashTable.insertRow(1);
var newHash = row.insertCell(0);
var newTimestamp = row.insertCell(1);
var newBlockNumber = row.insertCell(2);
newHash.innerHTML = JSON.parse(localStorage.getItem('recentHashes'))[j];
var timestamp = EtherProof.getTimestamp(newHash.innerHTML, function(error, result) {
if (error) {
console.error(error);
}
});
var blockNumber = EtherProof.getBlockNumber(newHash.innerHTML, function(error, result) {
if (error) {
console.error(error);
}
});
newTimestamp.innerHTML = new Date(timestamp * 1000);
newBlockNumber.innerHTML = blockNumber;
}
}
function search(form) {
var hash = form.searchHash.value;
var searchResult = document.getElementById('searchResult');
if (!EtherProof.checkExistence(hash)) {
searchResult.innerHTML = '<h2> This File Does Not Exist </h2>';
} else {
searchResult.innerHTML = '<h2> This File Exists </h2>';
var timestamp = EtherProof.getTimestamp(hash, function(error, result) {
if (error) {
console.error(error);
}
});
var blockNumber = EtherProof.getBlockNumber(hash, function(error, result) {
if (error) {
console.error(error);
}
});
searchResult.innerHTML += '<p> Timestamp: ' + new Date(timestamp * 1000) + '</p>' +
'<p> Block Number: ' + blockNumber + '</p>';
}
}
$(document).ready(function() {
for (var i = 0; i < web3.eth.accounts.length; ++i) {
$('#accounts').append("<option value=\"" + i + "\">" + web3.eth.accounts[i] + "</option>");
}
updateTable();
});