-
Notifications
You must be signed in to change notification settings - Fork 2
/
gunjstrustsharekeyv2.js
674 lines (636 loc) · 28.2 KB
/
gunjstrustsharekeyv2.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
/*
Self contain Sandbox Gun Module:
version: 2.0
Created by: Lightnet
Credit: amark ( https://github.com/amark/gun)
License: MIT
Gun Notes:
* Work in progress
* with sea.js and gun.js are buggy with auth checks.
Information: To create share keys from User or Gun Graph. By creating trust map list and
key by default when encrypt function are call. Users are store in get('trust') map
and you can change the key name in opt config to make it harder.
User trust map store in child of root graph key. The trust store where graph path in user root.
user.get(trust).get(publickey).get(pathnode).put('saltkey')
Gun graph path is independent of created key root is base where graph call encryptput function
but not the root graph node. It base on where the key root child graph location to generate
keys and trust map graph id to identify the keys and value. Simple example is tree node that
each reference with ID that can't be same tree node. Each tree graph node has trust list if
exist to share key who the owner to create it.
Share Key config that support Gun and User that will detect type of root for User and Gun object.
But can't be root graph when key are made will give errors.
If get an error that object can't be used as text or string that root graph has to be json object.
```
var object={// root id Object
node:{// sub child
trust:[], //note is just example reference. `[]` it can't be used as graph. // sub child
value:"key", //sub child
}
}
// example
let trust = get('trust');
trust.get('publickey1').put('secret key');
trust.get('publickey2').put('secret key');
gun.get('object').put({trust:trust,value:"key"}); //json object
```
This is json object format. Example how share key works. Name can be config.
var object="key" //incorrect format
//example
gun.get('object').put('key') ///incorrect format, not json object
//gun.get('foo').get('trust') //not here
//user.get('trust') // since it root as well user own this graph
gun.get('foo').get('something').encryptput('bar'); //create share key and create list
gun.get('foo').get('something').get('trust') //it store users trust list on where encryptput graph.
It the best to Share Key Genarete Salt Key by default to let know anyone to your keys.
Random key used from:
- SEA.random(16).toString();
- Gun.text.random(16);
User / Gun:
- function grantkey (to allow owner user access to key graph value for other users. Check and create salt keys)
- function revokekey (to owner revoke user access to key graph for user. Note it will break salt key if share with other users.)
- Recreate new salt Share Key and reencrypt value.(This break other salt keys are shared.)
- function encryptput put value (allow owner user to encrypt key value when creating and check salt key)
- function decryptvalue return value (allow owner user to decrypt key value)
- function decryptdata (to allow other user to decrypt key value/data from gun or sea but not self)
- `let to = gun.user(public key)`
User and Gun function for encrypt, decrypt and config key graph.
sharekeyvalue:"value"; //default //change to assign random secret key id
sharekeytrust:"trust"; //default
(gun/user).get('any').decryptvalue(cb,{sharekeyvalue:"value",sharekeytrust:"trust",sharekeydebug:false})
(gun/user).get('any').encryptput(data,cb,{sharekeyvalue:"value",sharekeytrust:"trust",sharekeydebug:false})
(gun/user).get('any').(grantkey|revokekey|decryptdata)(to,cb,{sharekeyvalue:"value",sharekeytrust:"trust",sharekeydebug:false})
{sharekeybbase:true} - This is for gun and not user graph.
{sharekeydebug:false} - Debug key, value, and secret sea
Notes:
- Not tested large scale.
- Not work on debug fails and checks. (work in progress)
- Grant/Revoke self share key break share keys.
- User encrypt json format will do fine. Gun encrypt root need to string and not json.
- Need to fix which is user or gun root check for easy access when call function.
- when user is store in root but when get from `let to = gun.user('key')` will not auto detect as user but gun object.
*/
(function() {
var Gun = (typeof window !== "undefined")? window.Gun : require('gun/gun');
Gun.on('opt', function(context) {
//context.opt.sharekeytype="path";// path for user //This will auto check default for gun and user
//context.opt.sharekeytype="graph"; //gun graph sea key will be convert to string and base (bug).
context.opt.sharekeydebug = true;
context.opt.sharekeyvalue = 'value';
context.opt.sharekeytrust = 'trust';
context.opt.sharekeybbase = true; //btoa, atob //base64 ecode and decode
this.to.next(context);
});
/*
//user...grantkey(to);
let user = gun.user();
let to = gun.user(publickey);
user.get('profile').get('alias').grantkey(to);
*/
function grantkey(to, cb, opt){
// added new user to key to share current graph key
console.log("`.grantkey` PROTOTYPE API MAY BE CHANGED OR RENAMED USE!");
cb = cb || function(ctx) { return ctx };
opt = opt || {};
let gun = this, user = gun.back(-1).user(), pair = user._.sea, path = '';
gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
opt.sharekeydebug = opt.sharekeydebug || gun._.root.opt.sharekeydebug;
opt.sharekeyvalue = opt.sharekeyvalue || gun._.root.opt.sharekeyvalue;
opt.sharekeytrust = opt.sharekeytrust || gun._.root.opt.sharekeytrust;
opt.sharekeybbase = opt.sharekeybbase || gun._.root.opt.sharekeybbase;
if (gun._.$ instanceof Gun.User){//check gun node is user object
//console.log("User PASS");
opt.sharekeytype = "path";
}else{
//console.log("Gun PASS");
opt.sharekeytype = "graph";
}
(async function(){
if(opt.sharekeydebug){
console.log("opt.sharekeytype: ",opt.sharekeytype);
}
let enc, sec;
if(opt.sharekeytype == "path"){
sec = await user.get(opt.sharekeytrust).get(pair.pub).get(path).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
sec = await SEA.decrypt(sec, pair);
if(!sec){
//console.log("CREATE SALT KEY")
sec = SEA.random(16).toString();
if(opt.sharekeydebug){
console.log("CREATE SECRET: ",sec);
}
//sec = Gun.text.random(16);
enc = await SEA.encrypt(sec, pair);
user.get(opt.sharekeytrust).get(pair.pub).get(path).put(enc);
}
}
if(opt.sharekeytype == "graph"){
sec = await gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
if(sec !=null){
if(opt.sharekeybbase){
sec = window.atob(sec);
}
sec = JSON.parse(sec);
}
sec = await SEA.decrypt(sec, pair);
if(!sec){
//console.log("CREATE SALT KEY")
sec = SEA.random(16).toString();
if(opt.sharekeydebug){
console.log("CREATE SECRET: ",sec);
}
//sec = Gun.text.random(16);
enc = await SEA.encrypt(sec, pair);
enc = JSON.stringify(enc);//need to be string bug root gun
if(opt.sharekeybbase){
enc = window.btoa(enc);
}
//console.log(enc);
gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).put(enc);
}
}
let pub = await to.get('pub').then();
let epub = await to.get('epub').then();
pub = await pub; epub = await epub;
let dh = await SEA.secret(epub, pair);
enc = await SEA.encrypt(sec, dh);
if(opt.sharekeytype == "path"){
if(opt.sharekeydebug){
console.log("TO sec: ",enc);
}
user.get(opt.sharekeytrust).get(pub).get(path).put(enc, cb);
}
if(opt.sharekeytype == "graph"){
enc = JSON.stringify(enc);
if(opt.sharekeybbase){
enc = window.btoa(enc);
}
//console.log(enc);
if(opt.sharekeydebug){
console.log("TO sec: ",enc);
}
gun.get(opt.sharekeytrust).get(pub).get(gun._.get).put(enc, cb);
}
}());
return gun;
}
/*
//- Recreated new salt key to share.
//- reencrypt key value on new salt
//user...revokekey(to);
let user = gun.user();
let to = gun.user(publickey);
user.get('profile').get('alias').revokekey(to);
*/
function revokekey(to, cb, opt){
// recreated new salt key share current graph key
console.log("`.revokekey` PROTOTYPE API MAY BE CHANGED OR RENAMED USE!");
cb = cb || function(ctx) { return ctx };
opt = opt || {};
let gun = this, user = gun.back(-1).user(), pair = user._.sea, path = '';
gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
//console.log(path);
if(!to){console.log("to User not set!");}
opt.sharekeydebug = opt.sharekeydebug || gun._.root.opt.sharekeydebug;
opt.sharekeyvalue = opt.sharekeyvalue || gun._.root.opt.sharekeyvalue;
opt.sharekeytrust = opt.sharekeytrust || gun._.root.opt.sharekeytrust;
opt.sharekeybbase = opt.sharekeybbase || gun._.root.opt.sharekeybbase;
if (gun._.$ instanceof Gun.User){//check gun node is user object
//console.log("User PASS");
opt.sharekeytype = "path";
}else{
//console.log("Gun PASS");
opt.sharekeytype = "graph";
}
(async function(){
if(opt.sharekeydebug){
console.log("opt.sharekeytype: ",opt.sharekeytype);
}
let alias = await to.get("alias").then();
let pub;
//console.log(alias);
if(!alias){
cb({err:'Error alias not found!'});
return gun;
}
//GET Salt Key
let enc, sec, key, value;
if(opt.sharekeytype == "path"){
sec = await user.get(opt.sharekeytrust).get(pair.pub).get(path).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
sec = await SEA.decrypt(sec, pair);
key = await gun.once().then();
value = await SEA.decrypt(key, sec);
}
if(opt.sharekeytype == "graph"){
sec = await gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
if(sec !=null){
if(opt.sharekeybbase){
sec = window.atob(sec);//decode
}
sec = JSON.parse(sec);
}
sec = await SEA.decrypt(sec, pair);
key = await gun.get(opt.sharekeyvalue).once().then();
if(opt.sharekeybbase){
key = window.atob(key);//decode
}
key = JSON.parse(key);
value = await SEA.decrypt(key, sec);
}
if(opt.sharekeydebug){
console.log("VALUE: ",value);
}
// Create New Salt Key
sec = SEA.random(16).toString();
//sec = Gun.text.random(16);
enc = await SEA.encrypt(sec, pair);
if(opt.sharekeytype == "path"){
user.get(opt.sharekeytrust).get(pair.pub).get(path).put(enc);
pub = await to.get('pub').then();//revoke user
user.get(opt.sharekeytrust).once().map().once(async (data,mkey)=>{//trust users
let uname;
if(opt.sharekeydebug){
uname = await gun.back(-1).user(mkey).get('alias').then();
}
if(pair.pub != mkey){//check self user to be resalt
if(pub == mkey){ //check user to be revoke
//do nothing??? (revoke user)
if(opt.sharekeydebug){
console.log(uname, "FAIL");
}
}else{
let ckey = await user.get(opt.sharekeytrust).get(mkey).get(path).then();
if(opt.sharekeydebug){
console.log(uname, "PASS");
}
if(ckey != "null"){//Check if there user are revoke key if they are null should be ignore.
if(opt.sharekeydebug){
console.log(uname, "CREATE NEW SALT SHARE KEY ");
}
let mto = gun.back(-1).user(mkey);
let mpub = await mto.get('pub').then();
let mepub = await mto.get('epub').then();
let dh = await SEA.secret(mepub, pair);
let menc = await SEA.encrypt(sec, dh);
//NEW SALT KEY
if(opt.sharekeydebug){
console.log("NEW SHARE KEY: ",menc);
}
user.get(opt.sharekeytrust).get(mpub).get(path).put(menc);
}
}
}
});
}
if(opt.sharekeytype == "graph"){
enc = JSON.stringify(enc);//need to be string bug root gun
enc = window.btoa(enc);
gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).put(enc);
pub = await to.get('pub').then();
gun.get(opt.sharekeytrust).map().once(async (data,mkey)=>{//trust users
let uname;
if(opt.sharekeydebug){
uname = await gun.back(-1).user(mkey).get('alias').then();
}
if(pair.pub != mkey){//check self user
if(pub == mkey){ //check user to be revoke
//do not here?(ban user)
if(opt.sharekeydebug){
console.log(uname, "FAIL");
}
}else{
let ckey = await gun.get(opt.sharekeytrust).get(key).get(gun._.get).then();
if(opt.sharekeydebug){
console.log(uname, "PASS");
}
if(ckey != "null"){//Check if there user are revoke key if they are null.
if(opt.sharekeydebug){
console.log(uname, "NEW SHARE KEY!");
}
let mto = gun.back(-1).user(mkey);
let mpub = await mto.get('pub').then();
let mepub = await mto.get('epub').then();
let dh = await SEA.secret(mepub, pair);
//NEW SHARE KEY
let menc = await SEA.encrypt(sec, dh);
menc = JSON.stringify(menc);
if(opt.sharekeybbase){
menc = window.btoa(menc);
}
if(opt.sharekeydebug){
console.log("NEW SHARE KEY: ",menc);
}
gun.get(opt.sharekeytrust).get(mpub).get(gun._.get).put(menc);
}
}
}
})
}
//encrypt Value
let v = await SEA.encrypt(value, sec);
if(opt.sharekeytype == "path"){
gun.put(v, cb);
}
if(opt.sharekeytype == "graph"){
v = JSON.stringify(v);
if(opt.sharekeybbase){
v = window.btoa(v);
}
gun.get(opt.sharekeyvalue).put(v, cb);
}
// REMOVE SHARE KEY
pub = await to.get('pub').then();
if(opt.sharekeytype == "path"){
user.get(opt.sharekeytrust).get(pub).get(path).put("null", cb);//REMOVE SECRET KEY
}
if(opt.sharekeytype == "graph"){
gun.get(opt.sharekeytrust).get(pub).get(gun._.get).put("null", cb);//REMOVE SECRET KEY
}
}());
return gun;
}
/*
//user...encryptput(value);
let user = gun.user();
user.get('profile').get('alias').encryptput("name");
*/
function encryptput(data, cb, opt){
// encrypt key > put value
console.log("`.encryptput` PROTOTYPE API MAY BE CHANGED OR RENAMED USE!");
cb = cb || function(ctx) { return ctx };
opt = opt || {};
let gun = this, user = gun.back(-1).user(), pair = user._.sea, path = '';
let rootgun = this;
gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
opt.sharekeydebug = opt.sharekeydebug || gun._.root.opt.sharekeydebug;
opt.sharekeyvalue = opt.sharekeyvalue || gun._.root.opt.sharekeyvalue;
opt.sharekeytrust = opt.sharekeytrust || gun._.root.opt.sharekeytrust;
opt.sharekeybbase = opt.sharekeybbase || gun._.root.opt.sharekeybbase;
if (gun._.$ instanceof Gun.User){//check gun node is user object
//console.log("User PASS");
opt.sharekeytype = "path";
}else{
//console.log("Gun PASS");
opt.sharekeytype = "graph";
}
//console.log(Gun.is(gun));//check if gun is object match
(async function(){
if(opt.sharekeydebug){
console.log("opt.sharekeytype: ",opt.sharekeytype);
}
let enc, sec;
if(opt.sharekeytype == "path"){
sec = await user.get(opt.sharekeytrust).get(pair.pub).get(path).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
sec = await SEA.decrypt(sec, pair);
if(!sec){
sec = SEA.random(16).toString();
if(opt.sharekeydebug){
console.log("CREATE SECRET: ",sec);
}
//sec = Gun.text.random(16);
enc = await SEA.encrypt(sec, pair);
user.get(opt.sharekeytrust).get(pair.pub).get(path).put(enc);
}
enc = await SEA.encrypt(data, sec);
gun.put(enc, cb);//PUT ENCRYPT DATA
}
if(opt.sharekeytype == "graph"){
sec = await gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
if(sec !=null){
if(opt.sharekeybbase){
sec = window.atob(sec);
}
sec = JSON.parse(sec);
sec = await SEA.decrypt(sec, pair);
}
if(!sec){
sec = SEA.random(16).toString();
if(opt.sharekeydebug){
console.log("CREATE SECRET: ",sec);
}
enc = await SEA.encrypt(sec, pair);
enc = JSON.stringify(enc);//need to be string bug root gun
if(opt.sharekeybbase){
enc = window.btoa(enc);
}
if(opt.sharekeydebug){
console.log("SECRET ENC: ",enc);
}
gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).put(enc);
}
if(opt.sharekeydebug){
console.log("DATA: ",data);
}
enc = await SEA.encrypt(data, sec);
enc = JSON.stringify(enc);
if(opt.sharekeybbase){
enc = window.btoa(enc);
}
//gun put need be child key value
gun.get(opt.sharekeyvalue).put(enc, cb);//PUT ENCRYPT DATA
}
}());
return gun;
}
/*
user...decryptvalue(cb);
let user = gun.user();
user.get('profile').get('alias').decryptvalue(ack=>{
//console.log(ack);
});
*/
function decryptvalue(cb,opt){
//get decrypt key to return value
console.log("`.decryptvalue` PROTOTYPE API MAY BE CHANGED OR RENAMED USE!");
cb = cb || function(ctx) { return ctx };
opt = opt || {};
let gun = this, user = gun.back(-1).user(), pair = user._.sea, path = '';
opt.sharekeydebug = opt.sharekeydebug || gun._.root.opt.sharekeydebug;
opt.sharekeyvalue = opt.sharekeyvalue || gun._.root.opt.sharekeyvalue;
opt.sharekeytrust = opt.sharekeytrust || gun._.root.opt.sharekeytrust;
opt.sharekeybbase = opt.sharekeybbase || gun._.root.opt.sharekeybbase;
gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
if (gun._.$ instanceof Gun.User){//check gun node is user object
//console.log("User PASS");
opt.sharekeytype = "path";
}else{
//console.log("Gun PASS");
opt.sharekeytype = "graph";
}
(async function(){
if(opt.sharekeydebug){
console.log("opt.sharekeytype: ",opt.sharekeytype);
}
let sec, key;
if(opt.sharekeytype == "path"){
sec = await user.get(opt.sharekeytrust).get(pair.pub).get(path).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
if(!sec){
cb(null);
return gun;
}
sec = await SEA.decrypt(sec, pair);
key = await gun.then();
}
if(opt.sharekeytype == "graph"){
sec = await gun.get(opt.sharekeytrust).get(pair.pub).get(gun._.get).then();
if(opt.sharekeydebug){
console.log("SECRET: ",sec);
}
if(!sec){
cb(null);
return gun;
}
if(opt.sharekeybbase){
sec = window.atob(sec);
}
sec = JSON.parse(sec);
sec = await SEA.decrypt(sec, pair);
key = await gun.get(opt.sharekeyvalue).then();//default 'value'
if(opt.sharekeydebug){
console.log("VALUE SEC: ",key);
}
if(key !=null){
if(opt.sharekeybbase){
key = window.atob(key);
}
key = JSON.parse(key);
}
}
let mvalue = await SEA.decrypt(key, sec);
if(opt.sharekeydebug){
console.log("VALUE: ", mvalue);
}
cb(mvalue);
}());
return gun;
}
//working to decrypt data??
/*
//user...decryptdata(to,db);
//gun...decryptdata(to,db);
let user = gun.user();
let to = gun.user(publickey);
to.get("profile").get("alias").decryptdata(to,ack=>{
console.log(ack);
});
*/
function decryptdata(to, cb, opt){
// gun graph to decrypt key to return value
console.log("`.decryptdata` PROTOTYPE API MAY BE DELETED OR CHANGED OR RENAMED USE!");
cb = cb || function(ctx) { return ctx };
opt = opt || {};
let gun = this, user = gun.back(-1).user(), pair = user._.sea, path = '';
//opt.sharekeytype = opt.sharekeytype || gun._.root.opt.sharekeytype;
opt.sharekeydebug = opt.sharekeydebug || gun._.root.opt.sharekeydebug;
opt.sharekeyvalue = opt.sharekeyvalue || gun._.root.opt.sharekeyvalue;
opt.sharekeytrust = opt.sharekeytrust || gun._.root.opt.sharekeytrust;
opt.sharekeybbase = opt.sharekeybbase || gun._.root.opt.sharekeybbase;
if(!to){
cb({err:"User not set!"});
console.log("User graph net set!");
return gun;
}
console.log(opt.sharekeydebug);
if (gun._.$ instanceof Gun.User){//check gun node is user object
//console.log("User PASS");
opt.sharekeytype = opt.sharekeytype || "path";
}else{
//console.log("Gun PASS");
opt.sharekeytype = opt.sharekeytype || "graph";
}
gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
(async function(){
if(opt.sharekeydebug){
console.log("opt.sharekeytype: ",opt.sharekeytype);
}
//KEY SALT
let enc1
if(opt.sharekeytype == "path"){
enc1 = await to.get('trust').get(pair.pub).get(path).then();
if(opt.sharekeydebug){
console.log("SECRET: ",enc1);
}
}
if(opt.sharekeytype == "graph"){
enc1 = await gun.get('trust').get(pair.pub).get(gun._.get).then();
if(opt.sharekeydebug){
console.log("SECRET: ",enc1);
}
if(enc1 !=null){
if(enc1 == "null"){
console.log("Error Null || Denied!");
cb(null);
return gun;
}
if(opt.sharekeybbase){
enc1 = window.atob(enc1);
}
enc1 = JSON.parse(enc1);
}
}
if(!enc1){
console.log("Error Null || Denied!");
cb(null);
return gun;
}
let epub = await to.get('epub').then();
//PAIR SHARE
let mix = await SEA.secret(epub, pair);
//KEY SHARE
let key = await SEA.decrypt(enc1, mix);
if(opt.sharekeydebug){
console.log("key: ",key);
}
//VALUE
let enc2
if(opt.sharekeytype == "path"){
enc2 = await gun.then();
if(opt.sharekeydebug){
console.log("VALUE sec: ",enc2);
}
}
if(opt.sharekeytype == "graph"){
enc2 = await gun.get(opt.sharekeyvalue).then();
if(opt.sharekeydebug){
console.log("VALUE sec: ",enc2);
}
if(opt.sharekeybbase){
enc2 = window.atob(enc2);
}
enc2 = JSON.parse(enc2);
}
let dvalue = await SEA.decrypt(enc2, key);
if(opt.sharekeydebug){
console.log("VALUE: ",dvalue);
}
cb(dvalue);
}());
return gun;
}
//SETUP FUNCTION for GUN
Gun.chain.grantkey = grantkey;
Gun.chain.revokekey = revokekey;
Gun.chain.encryptput = encryptput;
Gun.chain.decryptvalue = decryptvalue;
Gun.chain.decryptdata = decryptdata;
}());