-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.js
77 lines (58 loc) · 2.64 KB
/
serviceWorker.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
var BlobBuilder = function () {
this.parts = [];
}
BlobBuilder.prototype.append = function (part) {
this.parts.push(part);
this.blob = undefined; // Invalidate the blob
};
BlobBuilder.prototype.getBlob = function () {
if (!this.blob) {
this.blob = new Blob(this.parts, { type: "text/plain" });
}
return this.blob;
};
var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i};
let peerList = [];
let ownerPubKey = "";
let lastTimeStamp = 0;
const timeout = 60 * 60
self.addEventListener('fetch', async function (event) {
var hostname = self.location.origin;
// Ignore requests not for the hosted origin
if (!event.request.url.startsWith(hostname)) {
return;
}
// If we haven't feched the peer list in a while do it
if (lastTimeStamp < + new Date() - timeout) {
//peerList = Object.keys(await (await fetch('http://54.204.137.51:3141/getaddr?json=true')).json());
}
// TODO Fetch contract
// Choose random peer
//let peer = peerList[Math.floor(Math.random() * peerList.length)];
var host = 'localhost'
var filelen = 10;
var proof = {};
var response = new BlobBuilder();
for (let i = 0; i < filelen; i++) {
var r = await fetch(`http://${host}:8080${self.location.pathname}`,
{
method: 'POST',
body: JSON.stringify({
block: i,
proof: JSON.stringify(proof),
}),
})
let block = await r.text()
console.log(block)
response.append(block)
// Generate proof
let indexes = [0 , 1024 * (i+1)]
response.parts.join()
proof = {
indexes: indexes,
hash: sha256(response.parts.join().substring(indexes[0], indexes[1])),
nonce: 1337,
}
}
return response.getBlob()
})