-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
sea.js
executable file
·777 lines (712 loc) · 29.9 KB
/
sea.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
var root;
var Gun;
(function(){
var env;
if(typeof global !== 'undefined'){ env = global }
if(typeof window !== 'undefined'){ env = window }
root = env.window? env.window : global;
try{ env.window && root.localStorage && root.localStorage.clear() }catch(e){}
//try{ indexedDB.deleteDatabase('radatatest') }catch(e){}
if(root.Gun){
root.Gun = root.Gun;
root.Gun.TESTING = true;
} else {
try{ require('fs').unlinkSync('data.json') }catch(e){}
try{ require('../../lib/fsrm')('radatatest') }catch(e){}
root.Gun = require('../../gun');
root.Gun.TESTING = true;
require('../../lib/store');
require('../../lib/rfs');
}
try{ var expect = global.expect = require("../expect") }catch(e){}
if(!root.Gun.SEA){
require('../../sea.js');
}
}(this));
;(function(){
Gun = root.Gun
var SEA = Gun.SEA
if(!SEA){ return }
describe('SEA', function(){
this.timeout(1000 * 9);
var user;
var gun;
var pub;
var prep = async function(d,k, n,s){ return {'#':s,'.':k,':': await SEA.opt.parse(d),'>':Gun.state.is(n, k)} }; // shim for old - prep for signing.
var pack = function(d,cb,k, n,s){ return new Promise(function(res, rej){ SEA.opt.pack(d, function(r){ res(r) }, k,n,s) }) }; // make easier to upgrade test, cb to await
describe('Utility', function(){
it('deleting old SEA tests (may take long time)', function(done){
done(); // Mocha doesn't print test until after its done, so show this first.
});
it('deleted', function(done){
this.timeout(60 * 1000);
if(!Gun.window){ return done() }
indexedDB.deleteDatabase('radatatest').onsuccess = function(e){ done() }
});
/*it('generates aeskey from jwk', function(done) { // DEPRECATED!!!
console.log("WARNING: THIS DOES NOT WORK IN BROWSER!!!! NEEDS FIX");
SEA.opt.aeskey('x','x').then(k => {
//console.log("DATA", k.data);
expect(k.data.toString('base64')).to.be('Xd6JaIf2dUybFb/jpEGuSAbfL96UABMR4IvxEGIuC74=')
done()
})
})*/
it('quickstart', function(done){
SEA.pair(function(pair){
SEA.encrypt('hello self', pair, function(enc){
SEA.sign(enc, pair, function(data){
SEA.verify(data, pair.pub, function(msg){
SEA.decrypt(msg, pair, function(dec){
expect(dec).to.be('hello self');
SEA.work(dec, pair, function(proof){
SEA.work('hello self', pair, function(check){
expect(proof).to.be(check);
SEA.pair(function(alice){
SEA.pair(function(bob){
SEA.secret(bob.epub, alice, function(aes){
SEA.encrypt('shared data', aes, function(enc){
SEA.secret(alice.epub, bob, function(aes){
SEA.decrypt(enc, aes, function(dec){
expect(dec).to.be('shared data');
done();
});});});});});});});});});});});});});
})
it('quickwrong', function(done){
SEA.pair(function(alice){
SEA.pair(function(bob){
SEA.sign('asdf', alice, function(data){
SEA.verify(data, bob.pub, function(msg){
expect(msg).to.be(undefined);
SEA.verify(data.slice(0,20)+data.slice(21), alice.pub, function(msg){
expect(msg).to.be(undefined);
SEA.encrypt('secret', alice, function(enc){
SEA.decrypt(enc, bob, function(dec){
expect(dec).to.be(undefined);
SEA.decrypt(enc.slice(0,20)+enc.slice(21), alice, function(dec){
expect(dec).to.be(undefined);
done();
});});});});});});});});
})
it('types', function(done){
var pair, s, v;
SEA.pair(function(pair){
SEA.sign(null, pair, function(s){
SEA.verify(s, pair, function(v){
expect(null).to.be(v);
SEA.sign(true, pair, function(s){
SEA.verify(s, pair, function(v){
expect(true).to.be(v);
SEA.sign(false, pair, function(s){
SEA.verify(s, pair, function(v){
expect(false).to.be(v);
SEA.sign(0, pair, function(s){
SEA.verify(s, pair, function(v){
expect(0).to.be(v);
SEA.sign(1, pair, function(s){
SEA.verify(s, pair, function(v){
expect(1).to.be(v);
SEA.sign(1.01, pair, function(s){
SEA.verify(s, pair, function(v){
expect(1.01).to.be(v);
SEA.sign('', pair, function(s){
SEA.verify(s, pair, function(v){
expect('').to.be(v);
SEA.sign('a', pair, function(s){
SEA.verify(s, pair, function(v){
expect('a').to.be(v);
SEA.sign([], pair, function(s){
SEA.verify(s, pair, function(v){
expect([]).to.eql(v);
SEA.sign([1], pair, function(s){
SEA.verify(s, pair, function(v){
expect([1]).to.eql(v);
SEA.sign({}, pair, function(s){
SEA.verify(s, pair, function(v){
expect({}).to.eql(v);
SEA.sign({a:1}, pair, function(s){
SEA.verify(s, pair, function(v){
expect({a:1}).to.eql(v);
SEA.sign(JSON.stringify({a:1}), pair, function(s){
SEA.verify(s, pair, function(v){
expect({a:1}).to.eql(v);
done();
});});});});});});});});});});});});});});});});});});});});});});});});});});});
})
it('atypes', function(done){
var pair, s, v;
SEA.pair(function(pair){
SEA.encrypt(null, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(null).to.be(v);
SEA.encrypt(true, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(true).to.be(v);
SEA.encrypt(false, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(false).to.be(v);
SEA.encrypt(0, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(0).to.be(v);
SEA.encrypt(1, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(1).to.be(v);
SEA.encrypt(1.01, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(1.01).to.be(v);
SEA.encrypt('', pair, function(s){
SEA.decrypt(s, pair, function(v){
expect('').to.be(v);
SEA.encrypt('a', pair, function(s){
SEA.decrypt(s, pair, function(v){
expect('a').to.be(v);
SEA.encrypt([], pair, function(s){
SEA.decrypt(s, pair, function(v){
expect([]).to.eql(v);
SEA.encrypt([1], pair, function(s){
SEA.decrypt(s, pair, function(v){
expect([1]).to.eql(v);
SEA.encrypt({}, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect({}).to.eql(v);
SEA.encrypt({a:1}, pair, function(s){
SEA.decrypt(s, pair, function(v){
expect({a:1}).to.eql(v);
SEA.encrypt(JSON.stringify({a:1}), pair, function(s){
SEA.decrypt(s, pair, function(v){
expect({a:1}).to.eql(v);
done();
});});});});});});});});});});});});});});});});});});});});});});});});});});});
})
/*it('DOESNT DECRYPT SCIENTIFIC NOTATION', function(done){
var pair, s, v;
SEA.pair(function(pair){
SEA.encrypt('4e2', pair, function(s){
SEA.decrypt(s, pair, function(v){
expect(400).to.be(v);
done();
});});});
})*/
it('legacy', function(done){ (async function(){
var pw = 'test123';
// https://cdn.jsdelivr.net/npm/gun@0.9.99999/sea.js !
var old = JSON.parse(atob("eyJfIjp7IiMiOiJ+TkJhdDdKeUk0REw1ZDlPMEZNbWVFN0RacVZRZUVPblhKcldycDVUUGlyMC5PckV6WVIwc3h0NHRtV0tiajFQdHRaeW1HUmdyc1FVVDNHaTk1UE9vMUdBIiwiPiI6eyJwdWIiOjEsImFsaWFzIjoxLCJlcHViIjoxLCJhdXRoIjoxfX0sInB1YiI6Ik5CYXQ3SnlJNERMNWQ5TzBGTW1lRTdEWnFWUWVFT25YSnJXcnA1VFBpcjAuT3JFellSMHN4dDR0bVdLYmoxUHR0WnltR1JncnNRVVQzR2k5NVBPbzFHQSIsImFsaWFzIjoiU0VBe1wibVwiOlwiXFxcImJvYlxcXCJcIixcInNcIjpcIt4uXFx1MDAwNCpbcECT/sxe83eYe/M+bmB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xcXFwiOlxcXFxcXFwiVU5Lv+Zko1xcXFxcXFxcdTAwMDOt1ET2JHhcXFxcXFxcXHUwMDE1/1xcXFxcXFwiLFxcXFxcXFwic1xcXFxcXFwiOlxcXFxcXFwiz0VOO9GwaJlcXFxcXFxcIn1cXFwiLFxcXCJzXFxcIjpcXFwiZ0F4TFJpa2dEakIzbXJDNGpucUFRak5NNEZXemF0a1Eyb2xDR2Z5TTc2amg3azNEUzAyRlp1MEV1eWg2RGFITlxcXCJ9XCIsXCJzXCI6XCKze+BcXHUwMDBilPlcXHUwMDA2z1srodVcXHUwMDA0P1xcXCJcXFwib2rndUadtqJcXHUwMDE2bFtf0PSvJNdcXHUwMDE2Y71nnlxcdTAwMWOZXFx1MDAwN1xcdTAwMTlcXHUwMDE36NZcXHUwMDA0Uk7DQK/y/oixrIr1XFx1MDAxZnVcXHUwMDE3oCBhXCJ9In0="));
var okey = {"pub":"NBat7JyI4DL5d9O0FMmeE7DZqVQeEOnXJrWrp5TPir0.OrEzYR0sxt4tmWKbj1PttZymGRgrsQUT3Gi95POo1GA","epub":"GIPf6v_3y_CeJP1kEfKv9jfgt0OfFx4rppJKMpHOL-Q.Nc6tICRZpll10n9WclG8W4-mt1Wfr6rhwsBr7ZQNWnc","priv":"leIA-BOFLECsOOdT_B8B0s1Ii0VHZZGlHz8q_dK-xLs","epriv":"1BTJpYdwSLesrtuB7pYQdsrFHsxKSJ-d9PXt2qp6NyQ"}
var auth = await SEA.verify(old.auth, old.pub);
var proof = await SEA.work(pw, auth.s, null, {encode: 'utf8'});
var dec = await SEA.decrypt(auth.ek, proof, null);
expect(dec.priv).to.be(okey.priv);
expect(dec.epriv).to.be(okey.epriv);
var gun = Gun({super: true}), tmp = old._['#'];
var graph = {};
gun._.graph[tmp] = graph[tmp] = old;
var alias = await SEA.verify(old.alias, false);
expect(alias).to.be('bob');
alias = Gun.state.ify({}, tmp, 1, {'#': tmp}, tmp = '~@'+alias);
graph[tmp] = alias;
gun._.graph[tmp] = alias;//gun.on('test', {$: gun, put: graph});
var use = gun.user();
use.auth('bob', 'test123', function(ack){
expect(ack.err).to.not.be.ok();
done();
});
}())});
it('legacy []', function(done){ (async function(){
var pw = 'test123';
// https://cdn.jsdelivr.net/npm/gun@0.9.99999/sea.js !
var old = JSON.parse(atob("eyJfIjp7IiMiOiJ+VThkS0dySFJhX01sMFZ1YlR5OUZBYTlQS1ZlYlh0eTFjS05zWWxnYjduNC5QeVd5cUVVb0ZpYVduUElOV0Nad0xBbzFobjN1MldPWTU3SzZHZnpsNjhVIiwiPiI6eyJwdWIiOjE1NDY5MDI1MDQ5NzksImFsaWFzIjoxNTQ2OTAyNTA0OTc5LCJlcHViIjoxNTQ2OTAyNTA0OTc5LCJhdXRoIjoxNTQ2OTAyNTA0OTc5fX0sInB1YiI6IlU4ZEtHckhSYV9NbDBWdWJUeTlGQWE5UEtWZWJYdHkxY0tOc1lsZ2I3bjQuUHlXeXFFVW9GaWFXblBJTldDWndMQW8xaG4zdTJXT1k1N0s2R2Z6bDY4VSIsImFsaWFzIjoiU0VBe1wibVwiOltcIn5VOGRLR3JIUmFfTWwwVnViVHk5RkFhOVBLVmViWHR5MWNLTnNZbGdiN240LlB5V3lxRVVvRmlhV25QSU5XQ1p3TEFvMWhuM3UyV09ZNTdLNkdmemw2OFVcIixcImFsaWFzXCIsXCJhbGljZVwiLDE1NDY5MDI1MDQ5NzldLFwic1wiOlwienpuaGtIZjhZdFpZM2lGd3FVd0lJUldMTjhZMmlHbmNkcnVTaStGNDNmU1BLYWpSZlI0VzhXVHM4bElSMDBndGJmTWJxS0NjQkpGN3VNSkdGRC9WV2c9PVwifSIsImVwdWIiOiJTRUF7XCJtXCI6W1wiflU4ZEtHckhSYV9NbDBWdWJUeTlGQWE5UEtWZWJYdHkxY0tOc1lsZ2I3bjQuUHlXeXFFVW9GaWFXblBJTldDWndMQW8xaG4zdTJXT1k1N0s2R2Z6bDY4VVwiLFwiZXB1YlwiLFwiRkRzM1VvNTNFZEp6eFN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2JyaktxSlBnSllrVWE2bk9YdlRmQkFzV1BPVzVnemh4Q2RPVGNFQm5icWlpWXc9PVwifSJ9"));
var okey = {"pub":"U8dKGrHRa_Ml0VubTy9FAa9PKVebXty1cKNsYlgb7n4.PyWyqEUoFiaWnPINWCZwLAo1hn3u2WOY57K6Gfzl68U","epub":"FDs3Uo53EdJzxShpJCiW-DfOCyTKC6Sw1y-OeRqjnYE.qTgra9EBMfhJMuYLVcZz4YbIKFo9zsA0zLq_6tEO0r4","priv":"jMy7WfcldJ4esZEijAj4LTb99smtY_H0yKJLemJl2HI","epriv":"1DszMh-85pGTPLYtRunG-Q-xB78AE4k07PPkbedYYwk"}
var gun = Gun({super: true}), tmp = old._['#'];//Gun.node.soul(old);
var graph = {};
gun._.graph[tmp] = graph[tmp] = old;
var alias = SEA.opt.unpack(await SEA.verify(old.alias, false), 'alias', old);
expect(alias).to.be('alice');
alias = Gun.state.ify({}, tmp, 1, {'#': tmp}, tmp = '~@'+alias);
gun._.graph[tmp] = graph[tmp] = alias;
//gun.on('test', {$: gun, put: graph});
var use = gun.user();
use.auth('alice', 'test123', function(ack){
expect(ack.err).to.not.be.ok();
done();
});
}())})
it('JSON escape', function(done){ (async function(){
var plain = "hello world";
var json = JSON.stringify({hello:'world'});
var n1 = Gun.state.ify({}, 'key', 1, plain, 'soul');
var n2 = Gun.state.ify({}, 'key', 1, json, 'soul');
var tmp = await prep(plain, 'key', n1, 'soul');
expect(tmp[':']).to.be("hello world");
tmp = await prep(json, 'key', n2, 'soul');
expect(tmp[':'].hello).to.be("world");
tmp = SEA.opt.unpack(tmp);
expect(tmp.hello).to.be("world");
done();
}())});
it('double sign', function(done){ (async function(){
var pair = await SEA.pair();
var sig = await SEA.sign('hello world', pair);
var dup = await SEA.sign(sig, pair);
expect(dup).to.be(sig);
var json = JSON.stringify({hello:'world'});
var n1 = Gun.state.ify({}, 'key', 1, json, 'soul');
var sig = await SEA.sign(await prep(json, 'key', n1, 'soul'), pair, null, {raw:1 , check: await pack(json, 'key', n1, 'soul')});
var dup = await SEA.sign(await prep(sig, 'key', n1, 'soul'), pair, null, {raw:1 , check: await pack(sig, 'key', n1, 'soul')});
expect(dup).to.be.eql(sig);
var json = JSON.stringify({hello:'world'});
var n1 = Gun.state.ify({}, 'key', 1, json, 'soul');
var bob = await SEA.pair();
var sig = await SEA.sign(await prep(json, 'key', n1, 'soul'), bob, null, {raw:1 , check: await pack(json, 'key', n1, 'soul')});
var dup = await SEA.sign(await prep(sig, 'key', n1, 'soul'), pair, null, {raw:1 , check: await pack(sig, 'key', n1, 'soul')});
expect(dup).to.not.be.eql(sig);
var json = JSON.stringify({hello:'world'});
var bob = await SEA.pair();
var sig = await SEA.sign(json, bob);
var dup = await SEA.sign(sig, pair);
expect(dup).to.not.be.eql(sig);
done();
}())})
});
describe('User', function(){
var gun = Gun(), gtmp;
it('test', function(done){
var g = Gun();
user = g.user();
var gid;
SEA.pair(function(p){
user.is = user._.sea = p;
gtmp = gid = 'test~'+p.pub;
g.get(gid).put({yo: 'hi'}, async function(ack){
var data = await SEA.opt.parse(g._.graph[gid].yo);
expect(data[':']).to.be('hi');
expect(data['~']).to.be.ok();
g.get(gid).get('yo').once(function(r){
expect(r).to.be('hi');
user.leave();
done();
})
})
})
});
it('is instantiable', function(done){
user.leave();
user = gun.user();
done();
})
it('register users', function(done){
user.create('carl', 'testing123', function(ack){
pub = '~'+ack.pub;
expect(ack.err).to.not.be.ok();
done();
})
});
it('login users', function(done){
user.auth('carl', 'testing123', function(ack){
expect(ack.err).to.not.be.ok();
done()
})
})
it('logout, login via {pub}', function(done){
var pub = user.is.pub;
user.leave();
user.auth({pub:pub}, 'testing123', function(ack){
expect(ack.err).to.not.be.ok();
done();
})
})
it('save data', function(done){
user.get('a').get('b').put(0, function(ack){
expect(ack.err).to.not.be.ok();
done();
});
})
it('read data', function(done){
user.get('a').get('b').once(function(data){
expect(data).to.be(0);
done();
});
})
it('save json', function(done){
user.get('a').get('c').put(JSON.stringify({hello:'world'}), function(ack){
expect(ack.err).to.not.be.ok();
done();
});
})
it('read json', function(done){
user.get('a').get('c').once(function(data){
expect(data).to.be(JSON.stringify({hello:'world'}));
done();
});
})
it('save & read encrypt', function(done){
SEA.encrypt('hi', user._.sea, function(data){
var is = data.slice();
user.get('a').get('d').put(data, function(ack){
expect(ack.err).to.not.be.ok();
setTimeout(function(){
user.get('a').get('d').once(function(data){
expect(data).to.be(is);
done();
});
})
});
})
})
it('refresh login', function(done){
this.timeout(9000);
setTimeout(function(){
gun = Gun();
user = gun.user();
user.auth('carl', 'testing123', function(ack){
expect(ack.err).to.not.be.ok();
done()
})
}, 800);
})
it('gun put JSON', function(done){
gun.get('x').get('y').put(JSON.stringify({hello:'world'}), function(ack){
expect(ack.err).to.not.be.ok();
done();
});
})
it('gun get JSON', function(done){
gun.get('x').get('y').once(function(data){
expect(data).to.be(JSON.stringify({hello:'world'}));
done();
});
})
it('set user ref should be found', function(done){
var gun = Gun();
var user = gun.user();
var msg = {what: 'hello world'};
user.create('zach', 'password');
gun.on('auth', function(){
var ref = user.get('who').get('all').set(msg);
user.get('who').get('said').set(ref);
user.get('who').get('said').map().once(function(data){
//console.log("*****", data);
expect(data.what).to.be.ok();
done();
})
})
});
it('set user ref null override', function foo(done){
this.timeout(9000);
var gun = Gun();
//user.leave();
var user = gun.user();
var msg = {what: 'hello world'};
user.create('xavier', 'password');
gun.on('auth', function(){
//console.log(1);
if(done.a){ return } done.a = 1;
var ref = user.get('who').get('all').set(msg, A);
var stub = user.get('stub').put({});
function A(){
//console.log(2);
user.get('who').put(stub, B);
function B(){
//console.log(3);
var tmp = ref._.has || ref._.soul;
user.get('who').get('all').get(tmp).put({boom: 'ah'}, C);
function C(){
//console.log(4);
user.get('who').get('all').map().once(function(data){
//console.log(5);
expect(data).to.be.ok();
expect(data.what).to.not.be.ok();
done();
});
}
}
};
});
});
it("User's nodes must be signed when on user scope!", function(done) {
/// https://github.com/amark/gun/issues/850
/// https://github.com/amark/gun/issues/616
this.timeout(9000);
var gun = Gun();
var user = gun.user();
user.create('xavier2', 'password2');
gun.on('auth', function(){
user.get("testauthed").get("arumf").set({"this": "is", "an": {"obj2": "again2"}}, function(ack) {
var notsigned = [];
//Gun.obj.map(gun._.graph, function(v,k) {
Object.keys(gun._.graph).forEach(function(k,v){ v = gun._.graph[k];
if (k[0]==='~' || k.indexOf('~', 1)!==-1) { return; } /// ignore '~pubkey' and '~@alias'
notsigned.push(k);
});
expect(notsigned.length).to.be(0); /// all souls must have to be suffixed with the user's pubkey.
done();
});
});
});
describe('predictable souls', function(){
var alice = {
"pub": "sT1s6lOaUgia5Aiy_Qg_Z4ubCCVFDyGVJsi-i0VmKJI.UTmwQrcKxkHfw0lFK2bkVDaYbd4_2T1Gj-MONFMostM",
"priv": "HUmmMsaphGuOsUHAGGg9HHrYOA5FCrsueY6QexE79AE",
"epub": "MIPYx3rdRJbJSvtan0ruwIjMYaB5W7t42MJ4U1Y2jsk.HFNKa-LoIp5MPI-KFXZhvANjhAxL8dzgXWzLVhewtuk",
"epriv": "7X9rN1NxDYi9jtNU7daIA33__KYEIw3bN5amI-Rc5sw"
};
it("user's", function(done){
var gun = Gun();
gun.on('auth', function(){
gun.user().get('z').get('y').get('x').put({c: {b: {a: 1}}}, function(ack){setTimeout(function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub+'/';
//console.log(p, g);
expect(g[p+'z']).to.be.ok();
expect(g[p+'z/y']).to.be.ok();
expect(g[p+'z/y/x']).to.be.ok();
expect(g[p+'z/y/x/c']).to.be.ok();
expect(g[p+'z/y/x/c/b']).to.be.ok();
done();
},200)});
});
gun.user().auth(alice);
});
it('user mix', function(done){
var gun = Gun();
gun.on('auth', async function(){
if(done.a){ return } done.a = 1;
var c = 0, go = function(){ check(++c) }
var ref = gun.user().get('zasdf').put({a: 9}, go);
//ref._.REF = 'ref!';
//console.only.i=1;console.log("=================");
var at = gun.user().get('zfdsa').get('y').get('x').get('c').put(ref, go);
//ref._.DAT = 'dat!';
at.get('foo').get('bar').put('yay', go);
ref.get('foo').get('ah').put(1, go);
function check(){
if(c !== 4){ return }
setTimeout(function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub;
//console.log(g);
expect(Object.keys(g[p]).sort()).to.be.eql(['_', 'zasdf', 'zfdsa'].sort());
expect(Object.keys(g[p+'/zasdf']).sort()).to.be.eql(['_', 'a', 'foo'].sort());
expect(Object.keys(g[p+'/zasdf/foo']).sort()).to.be.eql(['_', 'bar', 'ah'].sort());
expect(Object.keys(g[p+'/zfdsa']).sort()).to.be.eql(['_', 'y'].sort());
expect(Object.keys(g[p+'/zfdsa/y']).sort()).to.be.eql(['_', 'x'].sort());
expect(Object.keys(g[p+'/zfdsa/y/x']).sort()).to.be.eql(['_', 'c'].sort());
expect(g[p+'/zfdsa'].y.indexOf('/zfdsa/y"') > 0).to.be.ok();
expect(g[p+'/zfdsa/y'].x.indexOf('/zfdsa/y/x"') > 0).to.be.ok();
expect(g[p+'/zfdsa/y/x'].c.indexOf('/zasdf"') > 0).to.be.ok();
done();
},100)};
});
gun.user().auth(alice);
});
it('user thread', function(done){
// grr this doesn't properly replicate the issue I saw before
var gun = Gun();
gun.on('auth', async function(){
if(done.a){ return } done.a = 1;
var to = gun.user().get('pchat').get('their.pub');
var enc = await SEA.encrypt('hi', 'secret');
var msg = { msg: enc };
to.get('2020').put(msg, function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub+'/';
//console.log(p, Object.keys(g[p+'pchat/their.pub/2020']||{}).sort());
expect(Object.keys(g[p+'pchat/their.pub/2020']).sort()).to.be.eql(['_', 'msg'].sort());
expect(g[p+'2020']).to.not.be.ok();
done();
});
});
gun.user().auth(alice);
});
});
describe('node', function(){
var u;
if(''+u === typeof process){ return }
console.log("REMEMBER TO RUN mocha test/sea/nodeauth !!!!");
});
});
describe('CERTIFY', function () {
var gun = Gun()
var user = gun.user()
it('Certify: Simple', function(done){(async function(){
var alice = await SEA.pair()
var bob = await SEA.pair()
var dave = await SEA.pair()
var cert = await SEA.certify(bob, {"*": "private"}, alice)
user.leave()
user.auth(bob, () => {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty")
.put(data, () => {
// Bob reads
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty").once(_data=>{
expect(_data).to.be(data)
user.leave()
// everyone reads
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty").once(_data=>{
expect(_data).to.be(data)
user.auth(dave, () => {
// Dave reads
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty").once(_data=>{
expect(_data).to.be(data)
user.leave()
done()
})
})
})
})
}, { opt: { cert } })
})
}())})
it('Certify: Attack', function(done){(async function(){
var alice = await SEA.pair()
var bob = await SEA.pair()
var cert = await SEA.certify(bob, {"*": "private"}, alice);
user.leave()
user.auth(bob, () => {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("wrongway")
.get("asdf")
.get("qwerty")
.put(data, ack => {
expect(ack.err).to.be.ok()
user.leave()
done()
}, { opt: { cert } })
})
}())})
it('Certify: Public inbox', function(done){(async function(){
var alice = await SEA.pair()
var bob = await SEA.pair()
var cert = await SEA.certify('*', [{"*": "test", "+": "*"}, {"*": "inbox", "+": "*"}], alice)
user.leave()
user.auth(bob, () => {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("inbox")
.get(user.is.pub)
.put(data, ack => {
expect(ack.err).to.not.be.ok()
user.leave()
done()
}, { opt: { cert } })
})
}())});
it('Certify: Expiry', function(done){(async function(){
var alice = await SEA.pair()
var bob = await SEA.pair()
var cert = await SEA.certify(bob, {"*": "private"}, alice, null, {
expiry: Gun.state() - 100, // expired 100 milliseconds ago
})
user.leave()
user.auth(bob, () => {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty")
.put(data, ack => {
expect(ack.err).to.be.ok()
user.leave()
done()
}, { opt: { cert } })
})
}())})
it('Certify: Path or Key must contain Certificant Pub', function(done){(async function(){
var alice = await SEA.pair()
var bob = await SEA.pair()
var cert = await SEA.certify(bob, {"*": "private", "+": "*"}, alice)
user.leave()
user.auth(bob, () => {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("private")
.get('wrongway')
.put(data, ack => {
expect(ack.err).to.be.ok()
gun.get("~" + alice.pub)
.get("private")
.get(bob.pub)
.get('today')
.put(data, ack => {
expect(ack.err).to.not.be.ok()
gun.get("~" + alice.pub)
.get("private")
.get(bob.pub)
.get('today')
.once(_data => {
expect(_data).to.be(data)
user.leave();
done()
})
}, { opt: { cert } })
}, { opt: { cert } })
})
}())})
it('Certify: Advanced - Block', function(done){(async function(){
var alice = await SEA.pair()
var dave = await SEA.pair()
var bob = await SEA.pair()
var cert = await SEA.certify(bob, {"*": "private"}, alice, null, {
expiry: Gun.state() + 5000, // expires in 5 seconds
block: 'block' // path to block in Alice's graph
})
// Alice points her block to Dave's graph
await user.auth(alice)
if (user.is) {
await user.get('block').put({'#': '~'+dave.pub+'/block'});
await user.leave()
}
// Dave logins, he adds Bob to his block, which is connected to the certificate that Alice issued for Bob
await user.auth(dave)
if (user.is) {
await user.get('block').get(bob.pub).put(true)
await user.leave()
}
// Bob logins and tries to hack Alice
await user.auth(bob)
if (user.is) {
var data = Gun.state().toString(36)
gun.get("~" + alice.pub)
.get("private")
.get("asdf")
.get("qwerty")
.put(data, ack => {
expect(ack.err).to.be.ok()
user.leave()
done()
}, { opt: { cert } })
}
}())})
});
describe.skip('Frozen', function () {
it('Across spaces', function(done){
var gun = Gun();
var user = gun.user();
user.create('alice/as', 'password');
gun.on('auth', async function(){
user.put({name: "Alice", country: "USA"});
var data = "hello world";
var hash = await SEA.work(data, null, null, {name: "SHA-256"});
gun.get('#users').get(hash).put(data);
console.log(1);
gun.get('#users').map()/*.get('country')*/.on(data => console.log(data));
});
});
});
})
}());