-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground.js
416 lines (385 loc) · 11.7 KB
/
background.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
var both;
try{both = browser}catch(e){}
try{both = both || chrome}catch(e){}
both.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
setTimeout(function(){
window.open('./options.html'); // first install
}, 750);
populate();
}else if(details.reason == "update"){
var thisVersion = both.runtime.getManifest().version;
console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
}
});
both.runtime.onMessage.addListener(function(msg, info, ack){ var tmp;
//console.log("background:", msg);
msg._ = msg._ || {};
if(tmp = msg.rpc){
if(!(tmp = both.RPC[tmp] || both.RPC[tmp[0]])){ return } // Error?
tmp(msg, ack, info);
return true;
}
return true;
});
both.runtime.onSuspend.addListener(function() {
console.log("Suspended");
});
both.RPC = {};
var opt, user, gun;
;(async function(){ // login!
//localStorage.clear();sessionStorage.clear();
try{ opt = JSON.parse(localStorage['party-config']) }catch(e){}
opt = opt || {};
opt.gun = opt.gun || {};
opt.gun.localStorage = false;
gun = Gun(opt.gun);
gun.on('auth', function(ack){ console.log('logged in') });
if(opt.pair){
user = gun.user().auth(opt.pair);
} else
if(opt.alias && opt.remember){
user = gun.user().auth(opt.alias, opt.remember);
} else {
opt.pair = await SEA.pair(); // new account every time!
user = gun.user().auth(opt.pair);
opt.gun = opt.gun || {};
try{ localStorage['party-config'] = JSON.stringify(opt) }catch(e){}
}
}());
/* --------------------------- */
both.RPC.ask = function(msg, ack){
if(!msg || 'ask' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
ask("What would you like to say?", async function(_){
if(msg.encrypt){
_.say = await encrypt(_.say);
_.say = await say(_) || _.say;
}
ack(_);
}, {any: 'any', width: 600})
}
/* --------------------------- */
both.RPC.sync = async function(msg, ack, info){try{
if(!msg || 'sync' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
if(!user || !user.is){ return }
var know = msg.put, ref;
if(!know){ return }
var anon = await anonkey();
var data = know; //Gun.text.ify(know);
if(know.fbid){
ref = gun.get(anon).get(low(know.fbid)).get(1).put(data);
}
if(know.alias){
//console.log(low(know.fbid), low(know.alias), ref, data);
tmp = gun.get(anon).get(low(know.alias)).get(1).put(ref||data);
ref = ref || tmp;
}
if(know.name){
tmp = gun.get(anon).get(low(know.name)).get(1).put(ref||data);
}
}catch(e){console.log(e)}}
both.RPC.pub = async function(msg, ack, info){try{
if(!msg || 'pub' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
if(!user || !user.is){ return }
var pub, tmp = msg.put;
if(!tmp){ return }
if(!(tmp = tmp.m)){ return }
if(!(tmp = tmp['#'])){ return }
pub = SEA.opt.pub(tmp);
if(!pub){ return }
var know = msg.know, ref;
if(!know){ return }
if(!know.id){ return }
var anon = await anonkey();
//return;
var ref = gun.get(anon).get(low(know.id)).get(1);
var is = await ref.then();
if(is.fbid != know.id){ return }
if(is.alias === know.alias || is.name === know.name){
ref.get('pub').put(pub);
}
//console.log("CHECK PUB KEY:", is, know);
return;
}catch(e){console.log(e)}}
async function anonkey(){
if(anonkey.tmp){
return anonkey.tmp;
}
var pair = (user._||{}).sea, pub = user.is.pub, enc;
var key = await user.get('trust').get(pub).get('knows').then();
key = await SEA.decrypt(key, pair);
if(!key){
key = SEA.random(24).toString('base64');
enc = await SEA.encrypt(key, pair);
user.get('trust').get(pub).get('knows').put(enc);
}
return anonkey.tmp = key;
}
both.RPC.anonkey = async function(msg, ack){
if(!msg || 'anonkey' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
ack({anon: await anonkey()});
}
async function sha(t){
return await SEA.work(t, null, null, {name: 'SHA-256'})
}
function low(t) {
return ((''+t)||'').toLowerCase().replace(
/([àáâãäå])|([ç])|([èéêë])|([ìíîï])|([ñ])|([òóôõöø])|([ß])|([ùúûü])|([ÿ])|([æ])/g,
function (str, a, c, e, i, n, o, s, u, y, ae) {
if (a) return 'a';
if (c) return 'c';
if (e) return 'e';
if (i) return 'i';
if (n) return 'n';
if (o) return 'o';
if (s) return 's';
if (u) return 'u';
if (y) return 'y';
if (ae) return 'ae';
}
);
}
function get(obj, at){
if(!obj){ return }
if(!at || !at.length){ return obj }
return get(obj[at.shift()], at);
}
/* --------------------------- */
async function encrypt(msg){
if(!opt.custom){
var x = SEA.random(12).toString('base64'), y = SEA.random(12).toString('base64');
var key = await SEA.work(x, y);//, null, {iterations: 500000}) // half million iteration = half second on mac air
var enc = await SEA.encrypt(msg, key, null, {raw: 1});
enc.v = 1;
enc.w = x+'.'+y;
return JSON.stringify(enc);
}
return;
}
async function decrypt(msg){
if(!msg){ return }
var dec = msg.m || msg;
dec = dec[':'] || dec;
if(!opt.custom){
var work = dec.w;
if(work){
work = work.split('.');
var slowSurveillanceToAGrindingHalt = await SEA.work(work[0], work[1]);//, null, {iterations: 500000});
// oh hey, hi! Its you, Mr. Tyranny & Ms. Greed! Thought you'd peep on our code, huh?
// WE DO NOT GIVE YOU PERMISSION TO ACCESS ANY PERSON'S DATA, doing so may cause you to knowingly violate laws, such as making unauthorized copies or caches or other of a person's data, such as copyright infringement in the USA - even if you have non-exclusive rights to the encrypted form of the data, YOU ARE NOT AUTHORIZED to access, attempt to access, or have gained access to the decrypted data, doing so will be considered as trespassing, abuse or misconduct of computer use, and theft or fraud from obtaining value of a person's data.
dec = await SEA.decrypt(dec, slowSurveillanceToAGrindingHalt);
return dec;
}
return msg;
}
return;
}
/* --------------------------- */
async function say(msg){
if(!msg.say){ return }
var id = user._.opt.uuid;
if(!id){ return }
id = id();
if(!id){ return }
var pair = (user._||{}).sea;
var enc = await SEA.encrypt(msg.say, pair);
var node = Gun.state.ify({}, 'what', Gun.state(), enc, id);
var ref = user.get('who').get('all').get(id).put(node); // encrypt in gun ontop of PoW
user.get('who').get('said').get(id).put(Gun.val.link.ify(id));
node = Gun.obj.to(node, {what: msg.say});
var prep = SEA.opt.prep(node.what, 'what', node, id);
var sig = await SEA.sign(prep, pair);
if(!sig){ return }
return (opt.introduce || "Want to join my private party? Install http://party.lol to read my secret post:")+"\n"+sig;
}
say.key = async function(){
var mo = ':'+say.now().slice(0,2).join('.');
if(mo === say.mo && say.tmp){
return say.tmp;
}
var pair = (user._||{}).sea, pub = user.is.pub, enc;
var key = await user.get('trust').get(pub).get(mo).then();
key = await SEA.decrypt(key, pair);
if(!key){
key = SEA.random(12).toString('base64');
enc = await SEA.encrypt(key, pair);
user.get('trust').get(pub).get(mo).put(enc);
}
say.mo = mo;
return say.tmp = key;
}
say.now = function(t){
return new Date(t || Gun.state()).toISOString().split(/[\-t\:\.z]/ig).slice(0,-1);
}
/* --------------------------- */
both.RPC.SEA = function(msg, ack){
if(!msg || !msg.rpc || 'SEA' !== msg.rpc[0]){ return }
var tmp = msg.put || msg.get;
var u, cb = function(data){
if(u === data){
return ack({err: SEA.err || "SEA Error"});
}
ack({put: data});
}
try{
if(msg.put){
SEA[msg.rpc[1]](tmp.data, tmp.pair, cb, tmp.opt);
} else
if(msg.get){
SEA[msg.rpc[1]](cb, tmp.opt);
}
}catch(err){
SEA.err = err;
cb();
}
}
function ask(q, cb, opt){
if(ask.lock){
if(ask.ui && !ask.ui.closed){
cb(false, "Already asking User something else.");
ask.ui.focus();
return;
}
}
opt = (opt === true)? {any: 'any'} : (opt || {});
ask.lock = true;
ask.last = cb;
opt.width = opt.width || 450;
opt.height = opt.height || 400;
var tmp = ask.ui = window.open("popup.html#" + (opt.any || 'ask'), "Answer", "chrome=yes,centerscreen=yes,dialog=yes,dependent=yes,titlebar=no,alwaysRaised=yes,width="+opt.width+",height="+opt.height+",left="+((screen.width/2)-(opt.width/2))+",top="+((screen.height/2)-(opt.height/2))+",");
if(ask.ui){
ask.ui.ASK = q;
ask.ui.focus()
}
setTimeout(function(){
if(!tmp || !tmp.document || !tmp.document.hasFocus()){ // fix vanishing glitch.
ask.lock = false;
ask(q, cb, opt)
}
},100);
}
/* --------------------------- */
both.RPC.say = function(msg, ack){
if(!msg || 'say' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
if(!ask.last){ return }
if(ask.ui){ ask.ui.close() }
ask.lock = ask.ui = false;
ask.last(msg._);
}
/* --------------------------- */
both.RPC.gun = function(msg, ack){
if(!msg || 'gun' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
//gun.on('in', msg.data || msg);
}
/* --------------------------- */
/* --------------------------- */
both.RPC.tie = function(msg, ack){
if(!msg || 'tie' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
if(msg.q && msg.q.length){
msg.q.forEach(function(msg){
gun.on('in', msg);
})
}
//if(tie.q && tie.q.length){
var tmp = tie.q; tie.q = [];
ack({q: tmp});
//}
}
var tie = {q: []};
gun.on('out', function(msg){
this.to.next(msg);
tie.q.push(msg);
})
/* --------------------------- */
both.RPC.toss = async function(msg, ack){
if(!msg || 'toss' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
var u, toss = {what: hold[msg.key]};
if(msg.decrypt){
toss.what = await decrypt(toss.what);
}
ack(toss);
}
var hold = {}, stack = {};
both.RPC.hold = async function(msg, ack){
if(!msg || 'hold' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
var key = msg.key;
hold[key] = msg.hold;
stack[key] = ack;
setTimeout(function(){
delete hold[key];
delete stack[key];
}, 1000 * 60 * 5);
}
both.RPC.height = function(msg, ack){
if(!msg || 'height' !== msg.rpc){ return }
if(!msg._.I){ return } // Error?
var key = msg.key, to = stack[msg.key];
if(!to){ return }
to(msg);
delete hold[key];
delete stack[key];
}
/* --------------------------- */
function populate(){
both.tabs.query({}, function(tabs){
setTimeout(function each(){
var tab = tabs.pop();
if(!tab){ return }
both.tabs.reload(tab.id);
setTimeout(each, 250);
},250);
});
}
/* --------------------------- */
// experiment
;(function(){
var scope = {};
// SEA's official API is callback, with optional Promise support.
// BOTH OF THESE ARE EXPERIMENTAL, NOT OFFICIAL API COMPATIBLE YET!
SEA.I = function(cb, opt){
return new Promise(async function(res, rej){
opt = opt || {};
cb = cb || function(){};
//var yes = confirm("dApp wants to act on your behalf. OK? "+(opt.how||'')+' '+(opt.why||''));
ask("dApp wants to act on your behalf. OK? "+(opt.how||'')+' '+(opt.why||''), async function(ack){
if(!ack.say){
rej(SEA.err = "User says no. " + (ack.why||ack.err||''));
return;
}
var tmp = scope.pair = scope.pair || await gen();
cb(tmp); res(tmp);
});
}, opt);
}
SEA.name = function(cb, opt){
return new Promise(async function(res, rej){
opt = opt || {};
cb = cb || function(){};
//var yes = confirm("dApp wants to know your name. OK? "+(opt.how||'')+' '+(opt.why||''));
ask("dApp wants to know your name. OK? "+(opt.how||'')+' '+(opt.why||''), async function(say){
if(!say.yes){
rej(SEA.err = "User says no." + (why||''));
return;
}
var tmp = '~'+(scope.pair = scope.pair || await gen()).pub; // ~ is SEA pubkey namespace.
cb(tmp); res(tmp);
}, opt);
});
}
async function gen(){
if(user && user._.sea){
return user._.sea;
}
var json = prompt("Import existing pair? (as JSON)");
return scope.pair = json? JSON.parse(json) : await SEA.pair();
}
}());