gun-eth 1.5.3 → 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3467 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('gun/sea.js');
6
+ var ethers = require('ethers');
7
+
8
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
9
+ function getDefaultExportFromCjs (x) {
10
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
11
+ }
12
+
13
+ function commonjsRequire(path) {
14
+ throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
15
+ }
16
+
17
+ var gun$1 = {exports: {}};
18
+
19
+ gun$1.exports;
20
+
21
+ (function (module) {
22
+ (function(){
23
+
24
+ /* UNBUILD */
25
+ function USE(arg, req){
26
+ return req? commonjsRequire(arg) : arg.slice? USE[R(arg)] : function(mod, path){
27
+ arg(mod = {exports: {}});
28
+ USE[R(path)] = mod.exports;
29
+ }
30
+ function R(p){
31
+ return p.split('/').slice(-1).toString().replace('.js','');
32
+ }
33
+ }
34
+ { var MODULE = module; }
35
+ USE(function(module){
36
+ // Shim for generic javascript utilities.
37
+ String.random = function(l, c){
38
+ var s = '';
39
+ l = l || 24; // you are not going to make a 0 length random number, so no need to check type
40
+ c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
41
+ while(l-- > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); }
42
+ return s;
43
+ };
44
+ String.match = function(t, o){ var tmp, u;
45
+ if('string' !== typeof t){ return false }
46
+ if('string' == typeof o){ o = {'=': o}; }
47
+ o = o || {};
48
+ tmp = (o['='] || o['*'] || o['>'] || o['<']);
49
+ if(t === tmp){ return true }
50
+ if(u !== o['=']){ return false }
51
+ tmp = (o['*'] || o['>']);
52
+ if(t.slice(0, (tmp||'').length) === tmp){ return true }
53
+ if(u !== o['*']){ return false }
54
+ if(u !== o['>'] && u !== o['<']){
55
+ return (t >= o['>'] && t <= o['<'])? true : false;
56
+ }
57
+ if(u !== o['>'] && t >= o['>']){ return true }
58
+ if(u !== o['<'] && t <= o['<']){ return true }
59
+ return false;
60
+ };
61
+ String.hash = function(s, c){ // via SO
62
+ if(typeof s !== 'string'){ return }
63
+ c = c || 0; // CPU schedule hashing by
64
+ if(!s.length){ return c }
65
+ for(var i=0,l=s.length,n; i<l; ++i){
66
+ n = s.charCodeAt(i);
67
+ c = ((c<<5)-c)+n;
68
+ c |= 0;
69
+ }
70
+ return c;
71
+ };
72
+ var has = Object.prototype.hasOwnProperty;
73
+ Object.plain = function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false };
74
+ Object.empty = function(o, n){
75
+ for(var k in o){ if(has.call(o, k) && (!n || -1==n.indexOf(k))){ return false } }
76
+ return true;
77
+ };
78
+ Object.keys = Object.keys || function(o){
79
+ var l = [];
80
+ for(var k in o){ if(has.call(o, k)){ l.push(k); } }
81
+ return l;
82
+ }
83
+ ;(function(){
84
+ var u, sT = setTimeout, l = 0, c = 0
85
+ , sI = (typeof setImmediate !== ''+u && setImmediate) || (function(c,f){
86
+ if(typeof MessageChannel == ''+u){ return sT }
87
+ (c = new MessageChannel()).port1.onmessage = function(e){ ''==e.data && f(); };
88
+ return function(q){ f=q;c.port2.postMessage(''); }
89
+ }()), check = sT.check = sT.check || (typeof performance !== ''+u && performance)
90
+ || {now: function(){ return +new Date }};
91
+ sT.hold = sT.hold || 9; // half a frame benchmarks faster than < 1ms?
92
+ sT.poll = sT.poll || function(f){
93
+ if((sT.hold >= (check.now() - l)) && c++ < 3333){ f(); return }
94
+ sI(function(){ l = check.now(); f(); },c=0);
95
+ };
96
+ }());
97
+ (function(){ // Too many polls block, this "threads" them in turns over a single thread in time.
98
+ var sT = setTimeout, t = sT.turn = sT.turn || function(f){ 1 == s.push(f) && p(T); }
99
+ , s = t.s = [], p = sT.poll, i = 0, f, T = function(){
100
+ if(f = s[i++]){ f(); }
101
+ if(i == s.length || 99 == i){
102
+ s = t.s = s.slice(i);
103
+ i = 0;
104
+ }
105
+ if(s.length){ p(T); }
106
+ };
107
+ }());
108
+ (function(){
109
+ var u, sT = setTimeout, T = sT.turn;
110
+ (sT.each = sT.each || function(l,f,e,S){ S = S || 9; (function t(s,L,r){
111
+ if(L = (s = (l||[]).splice(0,S)).length){
112
+ for(var i = 0; i < L; i++){
113
+ if(u !== (r = f(s[i]))){ break }
114
+ }
115
+ if(u === r){ T(t); return }
116
+ } e && e(r);
117
+ }());})();
118
+ }());
119
+ })(USE, './shim');
120
+ USE(function(module){
121
+ // On event emitter generic javascript utility.
122
+ module.exports = function onto(tag, arg, as){
123
+ if(!tag){ return {to: onto} }
124
+ var u, f = 'function' == typeof arg, tag = (this.tag || (this.tag = {}))[tag] || f && (
125
+ this.tag[tag] = {tag: tag, to: onto._ = { next: function(arg){ var tmp;
126
+ if(tmp = this.to){ tmp.next(arg); }
127
+ }}});
128
+ if(f){
129
+ var be = {
130
+ off: onto.off ||
131
+ (onto.off = function(){
132
+ if(this.next === onto._.next){ return !0 }
133
+ if(this === this.the.last){
134
+ this.the.last = this.back;
135
+ }
136
+ this.to.back = this.back;
137
+ this.next = onto._.next;
138
+ this.back.to = this.to;
139
+ if(this.the.last === this.the){
140
+ delete this.on.tag[this.the.tag];
141
+ }
142
+ }),
143
+ to: onto._,
144
+ next: arg,
145
+ the: tag,
146
+ on: this,
147
+ as: as,
148
+ };
149
+ (be.back = tag.last || tag).to = be;
150
+ return tag.last = be;
151
+ }
152
+ if((tag = tag.to) && u !== arg){ tag.next(arg); }
153
+ return tag;
154
+ };
155
+ })(USE, './onto');
156
+ USE(function(module){
157
+ // Valid values are a subset of JSON: null, binary, number (!Infinity), text,
158
+ // or a soul relation. Arrays need special algorithms to handle concurrency,
159
+ // so they are not supported directly. Use an extension that supports them if
160
+ // needed but research their problems first.
161
+ module.exports = function (v) {
162
+ // "deletes", nulling out keys.
163
+ return v === null ||
164
+ "string" === typeof v ||
165
+ "boolean" === typeof v ||
166
+ // we want +/- Infinity to be, but JSON does not support it, sad face.
167
+ // can you guess what v === v checks for? ;)
168
+ ("number" === typeof v && v != Infinity && v != -Infinity && v === v) ||
169
+ (!!v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
170
+ };
171
+ })(USE, './valid');
172
+ USE(function(module){
173
+ USE('./shim');
174
+ function State(){
175
+ var t = +new Date;
176
+ if(last < t){
177
+ return N = 0, last = t + State.drift;
178
+ }
179
+ return last = t + ((N += 1) / D) + State.drift;
180
+ }
181
+ State.drift = 0;
182
+ var NI = -Infinity, N = 0, D = 999, last = NI, u; // WARNING! In the future, on machines that are D times faster than 2016AD machines, you will want to increase D by another several orders of magnitude so the processing speed never out paces the decimal resolution (increasing an integer effects the state accuracy).
183
+ State.is = function(n, k, o){ // convenience function to get the state on a key on a node and return it.
184
+ var tmp = (k && n && n._ && n._['>']) || o;
185
+ if(!tmp){ return }
186
+ return ('number' == typeof (tmp = tmp[k]))? tmp : NI;
187
+ };
188
+ State.ify = function(n, k, s, v, soul){ // put a key's state on a node.
189
+ (n = n || {})._ = n._ || {}; // safety check or init.
190
+ if(soul){ n._['#'] = soul; } // set a soul if specified.
191
+ var tmp = n._['>'] || (n._['>'] = {}); // grab the states data.
192
+ if(u !== k && k !== '_'){
193
+ if('number' == typeof s){ tmp[k] = s; } // add the valid state.
194
+ if(u !== v){ n[k] = v; } // Note: Not its job to check for valid values!
195
+ }
196
+ return n;
197
+ };
198
+ module.exports = State;
199
+ })(USE, './state');
200
+ USE(function(module){
201
+ USE('./shim');
202
+ function Dup(opt){
203
+ var dup = {s:{}}, s = dup.s;
204
+ opt = opt || {max: 999, age: 1000 * 9};//*/ 1000 * 9 * 3};
205
+ dup.check = function(id){
206
+ if(!s[id]){ return false }
207
+ return dt(id);
208
+ };
209
+ var dt = dup.track = function(id){
210
+ var it = s[id] || (s[id] = {});
211
+ it.was = dup.now = +new Date;
212
+ if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9); }
213
+ if(dt.ed){ dt.ed(id); }
214
+ return it;
215
+ };
216
+ dup.drop = function(age){
217
+ dup.to = null;
218
+ dup.now = +new Date;
219
+ var l = Object.keys(s);
220
+ console.STAT && console.STAT(dup.now, +new Date - dup.now, 'dup drop keys'); // prev ~20% CPU 7% RAM 300MB // now ~25% CPU 7% RAM 500MB
221
+ setTimeout.each(l, function(id){ var it = s[id]; // TODO: .keys( is slow?
222
+ if(it && (age || opt.age) > (dup.now - it.was)){ return }
223
+ delete s[id];
224
+ },0,99);
225
+ };
226
+ return dup;
227
+ }
228
+ module.exports = Dup;
229
+ })(USE, './dup');
230
+ USE(function(module){
231
+ // request / response module, for asking and acking messages.
232
+ USE('./onto'); // depends upon onto!
233
+ module.exports = function ask(cb, as){
234
+ if(!this.on){ return }
235
+ var lack = (this.opt||{}).lack || 9000;
236
+ if(!('function' == typeof cb)){
237
+ if(!cb){ return }
238
+ var id = cb['#'] || cb, tmp = (this.tag||'')[id];
239
+ if(!tmp){ return }
240
+ if(as){
241
+ tmp = this.on(id, as);
242
+ clearTimeout(tmp.err);
243
+ tmp.err = setTimeout(function(){ tmp.off(); }, lack);
244
+ }
245
+ return true;
246
+ }
247
+ var id = (as && as['#']) || random(9);
248
+ if(!cb){ return id }
249
+ var to = this.on(id, cb, as);
250
+ to.err = to.err || setTimeout(function(){ to.off();
251
+ to.next({err: "Error: No ACK yet.", lack: true});
252
+ }, lack);
253
+ return id;
254
+ };
255
+ var random = String.random || function(){ return Math.random().toString(36).slice(2) };
256
+ })(USE, './ask');
257
+ USE(function(module){
258
+
259
+ function Gun(o){
260
+ if(o instanceof Gun){ return (this._ = {$: this}).$ }
261
+ if(!(this instanceof Gun)){ return new Gun(o) }
262
+ return Gun.create(this._ = {$: this, opt: o});
263
+ }
264
+
265
+ Gun.is = function($){ return ($ instanceof Gun) || ($ && $._ && ($ === $._.$)) || false };
266
+
267
+ Gun.version = 0.2020;
268
+
269
+ Gun.chain = Gun.prototype;
270
+ Gun.chain.toJSON = function(){};
271
+
272
+ USE('./shim');
273
+ Gun.valid = USE('./valid');
274
+ Gun.state = USE('./state');
275
+ Gun.on = USE('./onto');
276
+ Gun.dup = USE('./dup');
277
+ Gun.ask = USE('./ask');
278
+ (function(){
279
+ Gun.create = function(at){
280
+ at.root = at.root || at;
281
+ at.graph = at.graph || {};
282
+ at.on = at.on || Gun.on;
283
+ at.ask = at.ask || Gun.ask;
284
+ at.dup = at.dup || Gun.dup();
285
+ var gun = at.$.opt(at.opt);
286
+ if(!at.once){
287
+ at.on('in', universe, at);
288
+ at.on('out', universe, at);
289
+ at.on('put', map, at);
290
+ Gun.on('create', at);
291
+ at.on('create', at);
292
+ }
293
+ at.once = 1;
294
+ return gun;
295
+ };
296
+ function universe(msg){
297
+ // TODO: BUG! msg.out = null being set!
298
+ //if(!F){ var eve = this; setTimeout(function(){ universe.call(eve, msg,1) },Math.random() * 100);return; } // ADD F TO PARAMS!
299
+ if(!msg){ return }
300
+ if(msg.out === universe){ this.to.next(msg); return }
301
+ var eve = this, as = eve.as, at = as.at || as, gun = at.$, dup = at.dup, tmp, DBG = msg.DBG;
302
+ (tmp = msg['#']) || (tmp = msg['#'] = text_rand(9));
303
+ if(dup.check(tmp)){ return } dup.track(tmp);
304
+ tmp = msg._; msg._ = ('function' == typeof tmp)? tmp : function(){};
305
+ (msg.$ && (msg.$ === (msg.$._||'').$)) || (msg.$ = gun);
306
+ if(msg['@'] && !msg.put){ ack(msg); }
307
+ if(!at.ask(msg['@'], msg)){ // is this machine listening for an ack?
308
+ DBG && (DBG.u = +new Date);
309
+ if(msg.put){ put(msg); return } else
310
+ if(msg.get){ Gun.on.get(msg, gun); }
311
+ }
312
+ DBG && (DBG.uc = +new Date);
313
+ eve.to.next(msg);
314
+ DBG && (DBG.ua = +new Date);
315
+ if(msg.nts || msg.NTS){ return } // TODO: This shouldn't be in core, but fast way to prevent NTS spread. Delete this line after all peers have upgraded to newer versions.
316
+ msg.out = universe; at.on('out', msg);
317
+ DBG && (DBG.ue = +new Date);
318
+ }
319
+ function put(msg){
320
+ if(!msg){ return }
321
+ var ctx = msg._||'', root = ctx.root = ((ctx.$ = msg.$||'')._||'').root;
322
+ if(msg['@'] && ctx.faith && !ctx.miss){ // TODO: AXE may split/route based on 'put' what should we do here? Detect @ in AXE? I think we don't have to worry, as DAM will route it on @.
323
+ msg.out = universe;
324
+ root.on('out', msg);
325
+ return;
326
+ }
327
+ ctx.latch = root.hatch; ctx.match = root.hatch = [];
328
+ var put = msg.put;
329
+ var DBG = ctx.DBG = msg.DBG, S = +new Date; CT = CT || S;
330
+ if(put['#'] && put['.']){ /*root && root.on('put', msg);*/ return } // TODO: BUG! This needs to call HAM instead.
331
+ DBG && (DBG.p = S);
332
+ ctx['#'] = msg['#'];
333
+ ctx.msg = msg;
334
+ ctx.all = 0;
335
+ ctx.stun = 1;
336
+ var nl = Object.keys(put);//.sort(); // TODO: This is unbounded operation, large graphs will be slower. Write our own CPU scheduled sort? Or somehow do it in below? Keys itself is not O(1) either, create ES5 shim over ?weak map? or custom which is constant.
337
+ console.STAT && console.STAT(S, ((DBG||ctx).pk = +new Date) - S, 'put sort');
338
+ var ni = 0, nj, kl, soul, node, states, err, tmp;
339
+ (function pop(o){
340
+ if(nj != ni){ nj = ni;
341
+ if(!(soul = nl[ni])){
342
+ console.STAT && console.STAT(S, ((DBG||ctx).pd = +new Date) - S, 'put');
343
+ fire(ctx);
344
+ return;
345
+ }
346
+ if(!(node = put[soul])){ err = ERR+cut(soul)+"no node."; } else
347
+ if(!(tmp = node._)){ err = ERR+cut(soul)+"no meta."; } else
348
+ if(soul !== tmp['#']){ err = ERR+cut(soul)+"soul not same."; } else
349
+ if(!(states = tmp['>'])){ err = ERR+cut(soul)+"no state."; }
350
+ kl = Object.keys(node||{}); // TODO: .keys( is slow
351
+ }
352
+ if(err){
353
+ msg.err = ctx.err = err; // invalid data should error and stun the message.
354
+ fire(ctx);
355
+ //console.log("handle error!", err) // handle!
356
+ return;
357
+ }
358
+ var i = 0, key; o = o || 0;
359
+ while(o++ < 9 && (key = kl[i++])){
360
+ if('_' === key){ continue }
361
+ var val = node[key], state = states[key];
362
+ if(u === state){ err = ERR+cut(key)+"on"+cut(soul)+"no state."; break }
363
+ if(!valid(val)){ err = ERR+cut(key)+"on"+cut(soul)+"bad "+(typeof val)+cut(val); break }
364
+ //ctx.all++; //ctx.ack[soul+key] = '';
365
+ ham(val, key, soul, state, msg);
366
+ ++C; // courtesy count;
367
+ }
368
+ if((kl = kl.slice(i)).length){ turn(pop); return }
369
+ ++ni; kl = null; pop(o);
370
+ }());
371
+ } Gun.on.put = put;
372
+ // TODO: MARK!!! clock below, reconnect sync, SEA certify wire merge, User.auth taking multiple times, // msg put, put, say ack, hear loop...
373
+ // WASIS BUG! local peer not ack. .off other people: .open
374
+ function ham(val, key, soul, state, msg){
375
+ var ctx = msg._||'', root = ctx.root, graph = root.graph, tmp;
376
+ var vertex = graph[soul] || empty, was = state_is(vertex, key, 1), known = vertex[key];
377
+
378
+ var DBG = ctx.DBG; if(tmp = console.STAT){ if(!graph[soul] || !known){ tmp.has = (tmp.has || 0) + 1; } }
379
+
380
+ var now = State();
381
+ if(state > now){
382
+ setTimeout(function(){ ham(val, key, soul, state, msg); }, (tmp = state - now) > MD? MD : tmp); // Max Defer 32bit. :(
383
+ console.STAT && console.STAT(((DBG||ctx).Hf = +new Date), tmp, 'future');
384
+ return;
385
+ }
386
+ if(state < was){ /*old;*/ { return } } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future. // for AXE this would reduce rebroadcast, but GUN does it on message forwarding. // TURNS OUT CACHE MISS WAS NOT NEEDED FOR NEW CHAINS ANYMORE!!! DANGER DANGER DANGER, ALWAYS RETURN! (or am I missing something?)
387
+ if(!ctx.faith){ // TODO: BUG? Can this be used for cache miss as well? // Yes this was a bug, need to check cache miss for RAD tests, but should we care about the faith check now? Probably not.
388
+ if(state === was && (val === known || L(val) <= L(known))){ /*console.log("same");*/ /*same;*/ if(!ctx.miss){ return } } // same
389
+ }
390
+ ctx.stun++; // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
391
+ var aid = msg['#']+ctx.all++, id = {toString: function(){ return aid }, _: ctx}; id.toJSON = id.toString; // this *trick* makes it compatible between old & new versions.
392
+ root.dup.track(id)['#'] = msg['#']; // fixes new OK acks for RPC like RTC.
393
+ DBG && (DBG.ph = DBG.ph || +new Date);
394
+ root.on('put', {'#': id, '@': msg['@'], put: {'#': soul, '.': key, ':': val, '>': state}, ok: msg.ok, _: ctx});
395
+ }
396
+ function map(msg){
397
+ var DBG; if(DBG = (msg._||'').DBG){ DBG.pa = +new Date; DBG.pm = DBG.pm || +new Date;}
398
+ var eve = this, root = eve.as, graph = root.graph, ctx = msg._, put = msg.put, soul = put['#'], key = put['.'], val = put[':'], state = put['>']; msg['#']; var tmp;
399
+ if((tmp = ctx.msg) && (tmp = tmp.put) && (tmp = tmp[soul])){ state_ify(tmp, key, state, val, soul); } // necessary! or else out messages do not get SEA transforms.
400
+ //var bytes = ((graph[soul]||'')[key]||'').length||1;
401
+ graph[soul] = state_ify(graph[soul], key, state, val, soul);
402
+ if(tmp = (root.next||'')[soul]){
403
+ //tmp.bytes = (tmp.bytes||0) + ((val||'').length||1) - bytes;
404
+ //if(tmp.bytes > 2**13){ Gun.log.once('byte-limit', "Note: In the future, GUN peers will enforce a ~4KB query limit. Please see https://gun.eco/docs/Page") }
405
+ tmp.on('in', msg);
406
+ }
407
+ fire(ctx);
408
+ eve.to.next(msg);
409
+ }
410
+ function fire(ctx, msg){ var root;
411
+ if(ctx.stop){ return }
412
+ if(!ctx.err && 0 < --ctx.stun){ return } // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
413
+ ctx.stop = 1;
414
+ if(!(root = ctx.root)){ return }
415
+ var tmp = ctx.match; tmp.end = 1;
416
+ if(tmp === root.hatch){ if(!(tmp = ctx.latch) || tmp.end){ delete root.hatch; } else { root.hatch = tmp; } }
417
+ ctx.hatch && ctx.hatch(); // TODO: rename/rework how put & this interact.
418
+ setTimeout.each(ctx.match, function(cb){cb && cb();});
419
+ if(!(msg = ctx.msg) || ctx.err || msg.err){ return }
420
+ msg.out = universe;
421
+ ctx.root.on('out', msg);
422
+
423
+ CF(); // courtesy check;
424
+ }
425
+ function ack(msg){ // aggregate ACKs.
426
+ var id = msg['@'] || '', ctx;
427
+ if(!(ctx = id._)){
428
+ var dup = (dup = msg.$) && (dup = dup._) && (dup = dup.root) && (dup = dup.dup);
429
+ if(!(dup = dup.check(id))){ return }
430
+ msg['@'] = dup['#'] || msg['@']; // This doesn't do anything anymore, backtrack it to something else?
431
+ return;
432
+ }
433
+ ctx.acks = (ctx.acks||0) + 1;
434
+ if(ctx.err = msg.err){
435
+ msg['@'] = ctx['#'];
436
+ fire(ctx); // TODO: BUG? How it skips/stops propagation of msg if any 1 item is error, this would assume a whole batch/resync has same malicious intent.
437
+ }
438
+ ctx.ok = msg.ok || ctx.ok;
439
+ if(!ctx.stop && !ctx.crack){ ctx.crack = ctx.match && ctx.match.push(function(){back(ctx);}); } // handle synchronous acks. NOTE: If a storage peer ACKs synchronously then the PUT loop has not even counted up how many items need to be processed, so ctx.STOP flags this and adds only 1 callback to the end of the PUT loop.
440
+ back(ctx);
441
+ }
442
+ function back(ctx){
443
+ if(!ctx || !ctx.root){ return }
444
+ if(ctx.stun || ctx.acks !== ctx.all){ return }
445
+ ctx.root.on('in', {'@': ctx['#'], err: ctx.err, ok: ctx.err? u : ctx.ok || {'':1}});
446
+ }
447
+
448
+ var ERR = "Error: Invalid graph!";
449
+ var cut = function(s){ return " '"+(''+s).slice(0,9)+"...' " };
450
+ var L = JSON.stringify, MD = 2147483647, State = Gun.state;
451
+ var C = 0, CT, CF = function(){if(C>999 && (C/-(CT - (CT = +new Date))>1)){Gun.window && console.log("Warning: You're syncing 1K+ records a second, faster than DOM can update - consider limiting query.");CF=function(){C=0;};}};
452
+
453
+ }());
454
+ (function(){
455
+ Gun.on.get = function(msg, gun){
456
+ var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'];
457
+ var next = root.next || (root.next = {}), at = next[soul];
458
+
459
+ // TODO: Azarattum bug, what is in graph is not same as what is in next. Fix!
460
+
461
+ // queue concurrent GETs?
462
+ // TODO: consider tagging original message into dup for DAM.
463
+ // TODO: ^ above? In chat app, 12 messages resulted in same peer asking for `#user.pub` 12 times. (same with #user GET too, yipes!) // DAM note: This also resulted in 12 replies from 1 peer which all had same ##hash but none of them deduped because each get was different.
464
+ // TODO: Moving quick hacks fixing these things to axe for now.
465
+ // TODO: a lot of GET #foo then GET #foo."" happening, why?
466
+ // TODO: DAM's ## hash check, on same get ACK, producing multiple replies still, maybe JSON vs YSON?
467
+ // TMP note for now: viMZq1slG was chat LEX query #.
468
+ /*if(gun !== (tmp = msg.$) && (tmp = (tmp||'')._)){
469
+ if(tmp.Q){ tmp.Q[msg['#']] = ''; return } // chain does not need to ask for it again.
470
+ tmp.Q = {};
471
+ }*/
472
+ /*if(u === has){
473
+ if(at.Q){
474
+ //at.Q[msg['#']] = '';
475
+ //return;
476
+ }
477
+ at.Q = {};
478
+ }*/
479
+ var ctx = msg._||{}, DBG = ctx.DBG = msg.DBG;
480
+ DBG && (DBG.g = +new Date);
481
+ //console.log("GET:", get, node, has, at);
482
+ //if(!node && !at){ return root.on('get', msg) }
483
+ //if(has && node){ // replace 2 below lines to continue dev?
484
+ if(!node){ return root.on('get', msg) }
485
+ if(has){
486
+ if('string' != typeof has || u === node[has]){
487
+ if(!((at||'').next||'')[has]){ root.on('get', msg); return }
488
+ }
489
+ node = state_ify({}, has, state_is(node, has), node[has], soul);
490
+ // If we have a key in-memory, do we really need to fetch?
491
+ // Maybe... in case the in-memory key we have is a local write
492
+ // we still need to trigger a pull/merge from peers.
493
+ }
494
+ //Gun.window? Gun.obj.copy(node) : node; // HNPERF: If !browser bump Performance? Is this too dangerous to reference root graph? Copy / shallow copy too expensive for big nodes. Gun.obj.to(node); // 1 layer deep copy // Gun.obj.copy(node); // too slow on big nodes
495
+ node && ack(msg, node);
496
+ root.on('get', msg); // send GET to storage adapters.
497
+ };
498
+ function ack(msg, node){
499
+ var S = +new Date, ctx = msg._||{}, DBG = ctx.DBG = msg.DBG;
500
+ var to = msg['#'], id = text_rand(9), keys = Object.keys(node||'').sort(), soul = ((node||'')._||'')['#']; keys.length; var root = msg.$._.root, F = (node === root.graph[soul]);
501
+ console.STAT && console.STAT(S, ((DBG||ctx).gk = +new Date) - S, 'got keys');
502
+ // PERF: Consider commenting this out to force disk-only reads for perf testing? // TODO: .keys( is slow
503
+ node && (function go(){
504
+ S = +new Date;
505
+ var i = 0, k, put = {}, tmp;
506
+ while(i < 9 && (k = keys[i++])){
507
+ state_ify(put, k, state_is(node, k), node[k], soul);
508
+ }
509
+ keys = keys.slice(i);
510
+ (tmp = {})[soul] = put; put = tmp;
511
+ var faith; if(F){ faith = function(){}; faith.ram = faith.faith = true; } // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
512
+ tmp = keys.length;
513
+ console.STAT && console.STAT(S, -(S - (S = +new Date)), 'got copied some');
514
+ DBG && (DBG.ga = +new Date);
515
+ root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG, FOO: 1});
516
+ console.STAT && console.STAT(S, +new Date - S, 'got in');
517
+ if(!tmp){ return }
518
+ setTimeout.turn(go);
519
+ }());
520
+ if(!node){ root.on('in', {'@': msg['#']}); } // TODO: I don't think I like this, the default lS adapter uses this but "not found" is a sensitive issue, so should probably be handled more carefully/individually.
521
+ } Gun.on.get.ack = ack;
522
+ }());
523
+ (function(){
524
+ Gun.chain.opt = function(opt){
525
+ opt = opt || {};
526
+ var gun = this, at = gun._, tmp = opt.peers || opt;
527
+ if(!Object.plain(opt)){ opt = {}; }
528
+ if(!Object.plain(at.opt)){ at.opt = opt; }
529
+ if('string' == typeof tmp){ tmp = [tmp]; }
530
+ if(!Object.plain(at.opt.peers)){ at.opt.peers = {};}
531
+ if(tmp instanceof Array){
532
+ opt.peers = {};
533
+ tmp.forEach(function(url){
534
+ var p = {}; p.id = p.url = url;
535
+ opt.peers[url] = at.opt.peers[url] = at.opt.peers[url] || p;
536
+ });
537
+ }
538
+ obj_each(opt, function each(k){ var v = this[k];
539
+ if((this && this.hasOwnProperty(k)) || 'string' == typeof v || Object.empty(v)){ this[k] = v; return }
540
+ if(v && v.constructor !== Object && !(v instanceof Array)){ return }
541
+ obj_each(v, each);
542
+ });
543
+ at.opt.from = opt;
544
+ Gun.on('opt', at);
545
+ at.opt.uuid = at.opt.uuid || function uuid(l){ return Gun.state().toString(36).replace('.','') + String.random(l||12) };
546
+ return gun;
547
+ };
548
+ }());
549
+
550
+ var obj_each = function(o,f){ Object.keys(o).forEach(f,o); }, text_rand = String.random, turn = setTimeout.turn, valid = Gun.valid, state_is = Gun.state.is, state_ify = Gun.state.ify, u, empty = {}, C;
551
+
552
+ Gun.log = function(){ return (!Gun.log.off && C.log.apply(C, arguments)), [].slice.call(arguments).join(' ') };
553
+ Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) };
554
+
555
+ if(typeof window !== "undefined"){ (window.GUN = window.Gun = Gun).window = window; }
556
+ try{ if(typeof MODULE !== "undefined"){ MODULE.exports = Gun; } }catch(e){}
557
+ module.exports = Gun;
558
+
559
+ (Gun.window||{}).console = (Gun.window||{}).console || {log: function(){}};
560
+ (C = console).only = function(i, s){ return (C.only.i && i === C.only.i && C.only.i++) && (C.log.apply(C, arguments) || s) };
561
+ Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!");
562
+ })(USE, './root');
563
+ USE(function(module){
564
+ var Gun = USE('./root');
565
+ Gun.chain.back = function(n, opt){ var tmp;
566
+ n = n || 1;
567
+ if(-1 === n || Infinity === n){
568
+ return this._.root.$;
569
+ } else
570
+ if(1 === n){
571
+ return (this._.back || this._).$;
572
+ }
573
+ var gun = this, at = gun._;
574
+ if(typeof n === 'string'){
575
+ n = n.split('.');
576
+ }
577
+ if(n instanceof Array){
578
+ var i = 0, l = n.length, tmp = at;
579
+ for(i; i < l; i++){
580
+ tmp = (tmp||empty)[n[i]];
581
+ }
582
+ if(u !== tmp){
583
+ return opt? gun : tmp;
584
+ } else
585
+ if((tmp = at.back)){
586
+ return tmp.$.back(n, opt);
587
+ }
588
+ return;
589
+ }
590
+ if('function' == typeof n){
591
+ var yes, tmp = {back: at};
592
+ while((tmp = tmp.back)
593
+ && u === (yes = n(tmp, opt))){}
594
+ return yes;
595
+ }
596
+ if('number' == typeof n){
597
+ return (at.back || at).$.back(n - 1);
598
+ }
599
+ return this;
600
+ };
601
+ var empty = {}, u;
602
+ })(USE, './back');
603
+ USE(function(module){
604
+ // WARNING: GUN is very simple, but the JavaScript chaining API around GUN
605
+ // is complicated and was extremely hard to build. If you port GUN to another
606
+ // language, consider implementing an easier API to build.
607
+ var Gun = USE('./root');
608
+ Gun.chain.chain = function(sub){
609
+ var gun = this, at = gun._, chain = new (sub || gun).constructor(gun), cat = chain._, root;
610
+ cat.root = root = at.root;
611
+ cat.id = ++root.once;
612
+ cat.back = gun._;
613
+ cat.on = Gun.on;
614
+ cat.on('in', Gun.on.in, cat); // For 'in' if I add my own listeners to each then I MUST do it before in gets called. If I listen globally for all incoming data instead though, regardless of individual listeners, I can transform the data there and then as well.
615
+ cat.on('out', Gun.on.out, cat); // However for output, there isn't really the global option. I must listen by adding my own listener individually BEFORE this one is ever called.
616
+ return chain;
617
+ };
618
+
619
+ function output(msg){
620
+ var get, at = this.as, back = at.back, root = at.root, tmp;
621
+ if(!msg.$){ msg.$ = at.$; }
622
+ this.to.next(msg);
623
+ if(at.err){ at.on('in', {put: at.put = u, $: at.$}); return }
624
+ if(get = msg.get){
625
+ /*if(u !== at.put){
626
+ at.on('in', at);
627
+ return;
628
+ }*/
629
+ if(root.pass){ root.pass[at.id] = at; } // will this make for buggy behavior elsewhere?
630
+ if(at.lex){ Object.keys(at.lex).forEach(function(k){ tmp[k] = at.lex[k]; }, tmp = msg.get = msg.get || {}); }
631
+ if(get['#'] || at.soul){
632
+ get['#'] = get['#'] || at.soul;
633
+ //root.graph[get['#']] = root.graph[get['#']] || {_:{'#':get['#'],'>':{}}};
634
+ msg['#'] || (msg['#'] = text_rand(9)); // A3120 ?
635
+ back = (root.$.get(get['#'])._);
636
+ if(!(get = get['.'])){ // soul
637
+ tmp = back.ask && back.ask['']; // check if we have already asked for the full node
638
+ (back.ask || (back.ask = {}))[''] = back; // add a flag that we are now.
639
+ if(u !== back.put){ // if we already have data,
640
+ back.on('in', back); // send what is cached down the chain
641
+ if(tmp){ return } // and don't ask for it again.
642
+ }
643
+ msg.$ = back.$;
644
+ } else
645
+ if(obj_has(back.put, get)){ // TODO: support #LEX !
646
+ tmp = back.ask && back.ask[get];
647
+ (back.ask || (back.ask = {}))[get] = back.$.get(get)._;
648
+ back.on('in', {get: get, put: {'#': back.soul, '.': get, ':': back.put[get], '>': state_is(root.graph[back.soul], get)}});
649
+ if(tmp){ return }
650
+ }
651
+ /*put = (back.$.get(get)._);
652
+ if(!(tmp = put.ack)){ put.ack = -1 }
653
+ back.on('in', {
654
+ $: back.$,
655
+ put: Gun.state.ify({}, get, Gun.state(back.put, get), back.put[get]),
656
+ get: back.get
657
+ });
658
+ if(tmp){ return }
659
+ } else
660
+ if('string' != typeof get){
661
+ var put = {}, meta = (back.put||{})._;
662
+ Gun.obj.map(back.put, function(v,k){
663
+ if(!Gun.text.match(k, get)){ return }
664
+ put[k] = v;
665
+ })
666
+ if(!Gun.obj.empty(put)){
667
+ put._ = meta;
668
+ back.on('in', {$: back.$, put: put, get: back.get})
669
+ }
670
+ if(tmp = at.lex){
671
+ tmp = (tmp._) || (tmp._ = function(){});
672
+ if(back.ack < tmp.ask){ tmp.ask = back.ack }
673
+ if(tmp.ask){ return }
674
+ tmp.ask = 1;
675
+ }
676
+ }
677
+ */
678
+ root.ask(ack, msg); // A3120 ?
679
+ return root.on('in', msg);
680
+ }
681
+ //if(root.now){ root.now[at.id] = root.now[at.id] || true; at.pass = {} }
682
+ if(get['.']){
683
+ if(at.get){
684
+ msg = {get: {'.': at.get}, $: at.$};
685
+ (back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
686
+ return back.on('out', msg);
687
+ }
688
+ msg = {get: at.lex? msg.get : {}, $: at.$};
689
+ return back.on('out', msg);
690
+ }
691
+ (at.ask || (at.ask = {}))[''] = at; //at.ack = at.ack || -1;
692
+ if(at.get){
693
+ get['.'] = at.get;
694
+ (back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
695
+ return back.on('out', msg);
696
+ }
697
+ }
698
+ return back.on('out', msg);
699
+ } Gun.on.out = output;
700
+
701
+ function input(msg, cat){ cat = cat || this.as; // TODO: V8 may not be able to optimize functions with different parameter calls, so try to do benchmark to see if there is any actual difference.
702
+ var root = cat.root, gun = msg.$ || (msg.$ = cat.$), at = (gun||'')._ || empty, tmp = msg.put||'', soul = tmp['#'], key = tmp['.'], change = (u !== tmp['='])? tmp['='] : tmp[':'], state = tmp['>'] || -Infinity, sat; // eve = event, at = data at, cat = chain at, sat = sub at (children chains).
703
+ if(u !== msg.put && (u === tmp['#'] || u === tmp['.'] || (u === tmp[':'] && u === tmp['=']) || u === tmp['>'])){ // convert from old format
704
+ if(!valid(tmp)){
705
+ if(!(soul = ((tmp||'')._||'')['#'])){ console.log("chain not yet supported for", tmp, '...', msg, cat); return; }
706
+ gun = cat.root.$.get(soul);
707
+ return setTimeout.each(Object.keys(tmp).sort(), function(k){ // TODO: .keys( is slow // BUG? ?Some re-in logic may depend on this being sync?
708
+ if('_' == k || u === (state = state_is(tmp, k))){ return }
709
+ cat.on('in', {$: gun, put: {'#': soul, '.': k, '=': tmp[k], '>': state}, VIA: msg});
710
+ });
711
+ }
712
+ cat.on('in', {$: at.back.$, put: {'#': soul = at.back.soul, '.': key = at.has || at.get, '=': tmp, '>': state_is(at.back.put, key)}, via: msg}); // TODO: This could be buggy! It assumes/approxes data, other stuff could have corrupted it.
713
+ return;
714
+ }
715
+ if((msg.seen||'')[cat.id]){ return } (msg.seen || (msg.seen = function(){}))[cat.id] = cat; // help stop some infinite loops
716
+
717
+ if(cat !== at){ // don't worry about this when first understanding the code, it handles changing contexts on a message. A soul chain will never have a different context.
718
+ Object.keys(msg).forEach(function(k){ tmp[k] = msg[k]; }, tmp = {}); // make copy of message
719
+ tmp.get = cat.get || tmp.get;
720
+ if(!cat.soul && !cat.has){ // if we do not recognize the chain type
721
+ tmp.$$$ = tmp.$$$ || cat.$; // make a reference to wherever it came from.
722
+ } else
723
+ if(at.soul){ // a has (property) chain will have a different context sometimes if it is linked (to a soul chain). Anything that is not a soul or has chain, will always have different contexts.
724
+ tmp.$ = cat.$;
725
+ tmp.$$ = tmp.$$ || at.$;
726
+ }
727
+ msg = tmp; // use the message with the new context instead;
728
+ }
729
+ unlink(msg, cat);
730
+
731
+ if(((cat.soul/* && (cat.ask||'')['']*/) || msg.$$) && state >= state_is(root.graph[soul], key)){ // The root has an in-memory cache of the graph, but if our peer has asked for the data then we want a per deduplicated chain copy of the data that might have local edits on it.
732
+ (tmp = root.$.get(soul)._).put = state_ify(tmp.put, key, state, change, soul);
733
+ }
734
+ if(!at.soul /*&& (at.ask||'')['']*/ && state >= state_is(root.graph[soul], key) && (sat = (root.$.get(soul)._.next||'')[key])){ // Same as above here, but for other types of chains. // TODO: Improve perf by preventing echoes recaching.
735
+ sat.put = change; // update cache
736
+ if('string' == typeof (tmp = valid(change))){
737
+ sat.put = root.$.get(tmp)._.put || change; // share same cache as what we're linked to.
738
+ }
739
+ }
740
+
741
+ this.to && this.to.next(msg); // 1st API job is to call all chain listeners.
742
+ // TODO: Make input more reusable by only doing these (some?) calls if we are a chain we recognize? This means each input listener would be responsible for when listeners need to be called, which makes sense, as they might want to filter.
743
+ cat.any && setTimeout.each(Object.keys(cat.any), function(any){ (any = cat.any[any]) && any(msg); },0,99); // 1st API job is to call all chain listeners. // TODO: .keys( is slow // BUG: Some re-in logic may depend on this being sync.
744
+ cat.echo && setTimeout.each(Object.keys(cat.echo), function(lat){ (lat = cat.echo[lat]) && lat.on('in', msg); },0,99); // & linked at chains // TODO: .keys( is slow // BUG: Some re-in logic may depend on this being sync.
745
+
746
+ if(((msg.$$||'')._||at).soul){ // comments are linear, but this line of code is non-linear, so if I were to comment what it does, you'd have to read 42 other comments first... but you can't read any of those comments until you first read this comment. What!? // shouldn't this match link's check?
747
+ // is there cases where it is a $$ that we do NOT want to do the following?
748
+ if((sat = cat.next) && (sat = sat[key])){ // TODO: possible trick? Maybe have `ionmap` code set a sat? // TODO: Maybe we should do `cat.ask` instead? I guess does not matter.
749
+ tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k]; });
750
+ tmp.$ = (msg.$$||msg.$).get(tmp.get = key); delete tmp.$$; delete tmp.$$$;
751
+ sat.on('in', tmp);
752
+ }
753
+ }
754
+
755
+ link(msg, cat);
756
+ } Gun.on.in = input;
757
+
758
+ function link(msg, cat){ cat = cat || this.as || msg.$._;
759
+ if(msg.$$ && this !== Gun.on){ return } // $$ means we came from a link, so we are at the wrong level, thus ignore it unless overruled manually by being called directly.
760
+ if(!msg.put || cat.soul){ return } // But you cannot overrule being linked to nothing, or trying to link a soul chain - that must never happen.
761
+ var put = msg.put||'', link = put['=']||put[':'], tmp;
762
+ var root = cat.root, tat = root.$.get(put['#']).get(put['.'])._;
763
+ if('string' != typeof (link = valid(link))){
764
+ if(this === Gun.on){ (tat.echo || (tat.echo = {}))[cat.id] = cat; } // allow some chain to explicitly force linking to simple data.
765
+ return; // by default do not link to data that is not a link.
766
+ }
767
+ if((tat.echo || (tat.echo = {}))[cat.id] // we've already linked ourselves so we do not need to do it again. Except... (annoying implementation details)
768
+ && !(root.pass||'')[cat.id]){ return } // if a new event listener was added, we need to make a pass through for it. The pass will be on the chain, not always the chain passed down.
769
+ if(tmp = root.pass){ if(tmp[link+cat.id]){ return } tmp[link+cat.id] = 1; } // But the above edge case may "pass through" on a circular graph causing infinite passes, so we hackily add a temporary check for that.
770
+
771
+ (tat.echo||(tat.echo={}))[cat.id] = cat; // set ourself up for the echo! // TODO: BUG? Echo to self no longer causes problems? Confirm.
772
+
773
+ if(cat.has){ cat.link = link; }
774
+ var sat = root.$.get(tat.link = link)._; // grab what we're linking to.
775
+ (sat.echo || (sat.echo = {}))[tat.id] = tat; // link it.
776
+ var tmp = cat.ask||''; // ask the chain for what needs to be loaded next!
777
+ if(tmp[''] || cat.lex){ // we might need to load the whole thing // TODO: cat.lex probably has edge case bugs to it, need more test coverage.
778
+ sat.on('out', {get: {'#': link}});
779
+ }
780
+ setTimeout.each(Object.keys(tmp), function(get, sat){ // if sub chains are asking for data. // TODO: .keys( is slow // BUG? ?Some re-in logic may depend on this being sync?
781
+ if(!get || !(sat = tmp[get])){ return }
782
+ sat.on('out', {get: {'#': link, '.': get}}); // go get it.
783
+ },0,99);
784
+ } Gun.on.link = link;
785
+
786
+ function unlink(msg, cat){ // ugh, so much code for seemingly edge case behavior.
787
+ var put = msg.put||'', change = (u !== put['='])? put['='] : put[':'], root = cat.root, link, tmp;
788
+ if(u === change){ // 1st edge case: If we have a brand new database, no data will be found.
789
+ // TODO: BUG! because emptying cache could be async from below, make sure we are not emptying a newer cache. So maybe pass an Async ID to check against?
790
+ // TODO: BUG! What if this is a map? // Warning! Clearing things out needs to be robust against sync/async ops, or else you'll see `map val get put` test catastrophically fail because map attempts to link when parent graph is streamed before child value gets set. Need to differentiate between lack acks and force clearing.
791
+ if(cat.soul && u !== cat.put){ return } // data may not be found on a soul, but if a soul already has data, then nothing can clear the soul as a whole.
792
+ //if(!cat.has){ return }
793
+ tmp = (msg.$$||msg.$||'')._||'';
794
+ if(msg['@'] && (u !== tmp.put || u !== cat.put)){ return } // a "not found" from other peers should not clear out data if we have already found it.
795
+ //if(cat.has && u === cat.put && !(root.pass||'')[cat.id]){ return } // if we are already unlinked, do not call again, unless edge case. // TODO: BUG! This line should be deleted for "unlink deeply nested".
796
+ if(link = cat.link || msg.linked){
797
+ delete (root.$.get(link)._.echo||'')[cat.id];
798
+ }
799
+ if(cat.has){ // TODO: Empty out links, maps, echos, acks/asks, etc.?
800
+ cat.link = null;
801
+ }
802
+ cat.put = u; // empty out the cache if, for example, alice's car's color no longer exists (relative to alice) if alice no longer has a car.
803
+ // TODO: BUG! For maps, proxy this so the individual sub is triggered, not all subs.
804
+ setTimeout.each(Object.keys(cat.next||''), function(get, sat){ // empty out all sub chains. // TODO: .keys( is slow // BUG? ?Some re-in logic may depend on this being sync? // TODO: BUG? This will trigger deeper put first, does put logic depend on nested order? // TODO: BUG! For map, this needs to be the isolated child, not all of them.
805
+ if(!(sat = cat.next[get])){ return }
806
+ //if(cat.has && u === sat.put && !(root.pass||'')[sat.id]){ return } // if we are already unlinked, do not call again, unless edge case. // TODO: BUG! This line should be deleted for "unlink deeply nested".
807
+ if(link){ delete (root.$.get(link).get(get)._.echo||'')[sat.id]; }
808
+ sat.on('in', {get: get, put: u, $: sat.$}); // TODO: BUG? Add recursive seen check?
809
+ },0,99);
810
+ return;
811
+ }
812
+ if(cat.soul){ return } // a soul cannot unlink itself.
813
+ if(msg.$$){ return } // a linked chain does not do the unlinking, the sub chain does. // TODO: BUG? Will this cancel maps?
814
+ link = valid(change); // need to unlink anytime we are not the same link, though only do this once per unlink (and not on init).
815
+ tmp = msg.$._||'';
816
+ if(link === tmp.link || (cat.has && !tmp.link)){
817
+ if((root.pass||'')[cat.id] && 'string' !== typeof link); else {
818
+ return;
819
+ }
820
+ }
821
+ delete (tmp.echo||'')[cat.id];
822
+ unlink({get: cat.get, put: u, $: msg.$, linked: msg.linked = msg.linked || tmp.link}, cat); // unlink our sub chains.
823
+ } Gun.on.unlink = unlink;
824
+
825
+ function ack(msg, ev){
826
+ //if(!msg['%'] && (this||'').off){ this.off() } // do NOT memory leak, turn off listeners! Now handled by .ask itself
827
+ // manhattan:
828
+ var as = this.as, at = as.$._; at.root; var get = as.get||'', tmp = (msg.put||'')[get['#']]||'';
829
+ if(!msg.put || ('string' == typeof get['.'] && u === tmp[get['.']])){
830
+ if(u !== at.put){ return }
831
+ if(!at.soul && !at.has){ return } // TODO: BUG? For now, only core-chains will handle not-founds, because bugs creep in if non-core chains are used as $ but we can revisit this later for more powerful extensions.
832
+ at.ack = (at.ack || 0) + 1;
833
+ at.on('in', {
834
+ get: at.get,
835
+ put: at.put = u,
836
+ $: at.$,
837
+ '@': msg['@']
838
+ });
839
+ /*(tmp = at.Q) && setTimeout.each(Object.keys(tmp), function(id){ // TODO: Temporary testing, not integrated or being used, probably delete.
840
+ Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] }, tmp = {}); tmp['@'] = id; // copy message
841
+ root.on('in', tmp);
842
+ }); delete at.Q;*/
843
+ return;
844
+ }
845
+ (msg._||{}).miss = 1;
846
+ Gun.on.put(msg);
847
+ return; // eom
848
+ }
849
+
850
+ var empty = {}, u, text_rand = String.random, valid = Gun.valid, obj_has = function(o, k){ return o && Object.prototype.hasOwnProperty.call(o, k) }, state = Gun.state, state_is = state.is, state_ify = state.ify;
851
+ })(USE, './chain');
852
+ USE(function(module){
853
+ var Gun = USE('./root');
854
+ Gun.chain.get = function(key, cb, as){
855
+ var gun, tmp;
856
+ if(typeof key === 'string'){
857
+ if(key.length == 0) {
858
+ (gun = this.chain())._.err = {err: Gun.log('0 length key!', key)};
859
+ if(cb){ cb.call(gun, gun._.err); }
860
+ return gun;
861
+ }
862
+ var back = this, cat = back._;
863
+ var next = cat.next || empty;
864
+ if(!(gun = next[key])){
865
+ gun = key && cache(key, back);
866
+ }
867
+ gun = gun && gun.$;
868
+ } else
869
+ if('function' == typeof key){
870
+ if(true === cb){ return soul(this, key, cb, as), this }
871
+ gun = this;
872
+ var cat = gun._, opt = cb || {}, root = cat.root, id;
873
+ opt.at = cat;
874
+ opt.ok = key;
875
+ var wait = {}; // can we assign this to the at instead, like in once?
876
+ //var path = []; cat.$.back(at => { at.get && path.push(at.get.slice(0,9))}); path = path.reverse().join('.');
877
+ function any(msg, eve, f){
878
+ if(any.stun){ return }
879
+ if((tmp = root.pass) && !tmp[id]){ return }
880
+ var at = msg.$._, sat = (msg.$$||'')._, data = (sat||at).put, odd = (!at.has && !at.soul), test = {}, tmp;
881
+ if(odd || u === data){ // handles non-core
882
+ data = (u === ((tmp = msg.put)||'')['='])? (u === (tmp||'')[':'])? tmp : tmp[':'] : tmp['='];
883
+ }
884
+ if(('string' == typeof (tmp = Gun.valid(data)))){
885
+ data = (u === (tmp = root.$.get(tmp)._.put))? opt.not? u : data : tmp;
886
+ }
887
+ if(opt.not && u === data){ return }
888
+ if(u === opt.stun){
889
+ if((tmp = root.stun) && tmp.on){
890
+ cat.$.back(function(a){ // our chain stunned?
891
+ tmp.on(''+a.id, test = {});
892
+ if((test.run || 0) < any.id){ return test } // if there is an earlier stun on gapless parents/self.
893
+ });
894
+ !test.run && tmp.on(''+at.id, test = {}); // this node stunned?
895
+ !test.run && sat && tmp.on(''+sat.id, test = {}); // linked node stunned?
896
+ if(any.id > test.run){
897
+ if(!test.stun || test.stun.end){
898
+ test.stun = tmp.on('stun');
899
+ test.stun = test.stun && test.stun.last;
900
+ }
901
+ if(test.stun && !test.stun.end){
902
+ //if(odd && u === data){ return }
903
+ //if(u === msg.put){ return } // "not found" acks will be found if there is stun, so ignore these.
904
+ (test.stun.add || (test.stun.add = {}))[id] = function(){ any(msg,eve,1); }; // add ourself to the stun callback list that is called at end of the write.
905
+ return;
906
+ }
907
+ }
908
+ }
909
+ if(/*odd &&*/ u === data){ f = 0; } // if data not found, keep waiting/trying.
910
+ /*if(f && u === data){
911
+ cat.on('out', opt.out);
912
+ return;
913
+ }*/
914
+ if((tmp = root.hatch) && !tmp.end && u === opt.hatch && !f){ // quick hack! // What's going on here? Because data is streamed, we get things one by one, but a lot of developers would rather get a callback after each batch instead, so this does that by creating a wait list per chain id that is then called at the end of the batch by the hatch code in the root put listener.
915
+ if(wait[at.$._.id]){ return } wait[at.$._.id] = 1;
916
+ tmp.push(function(){any(msg,eve,1);});
917
+ return;
918
+ } wait = {}; // end quick hack.
919
+ }
920
+ // call:
921
+ if(root.pass){ if(root.pass[id+at.id]){ return } root.pass[id+at.id] = 1; }
922
+ if(opt.on){ opt.ok.call(at.$, data, at.get, msg, eve || any); return } // TODO: Also consider breaking `this` since a lot of people do `=>` these days and `.call(` has slower performance.
923
+ if(opt.v2020){ opt.ok(msg, eve || any); return }
924
+ Object.keys(msg).forEach(function(k){ tmp[k] = msg[k]; }, tmp = {}); msg = tmp; msg.put = data; // 2019 COMPATIBILITY! TODO: GET RID OF THIS!
925
+ opt.ok.call(opt.as, msg, eve || any); // is this the right
926
+ } any.at = cat;
927
+ //(cat.any||(cat.any=function(msg){ setTimeout.each(Object.keys(cat.any||''), function(act){ (act = cat.any[act]) && act(msg) },0,99) }))[id = String.random(7)] = any; // maybe switch to this in future?
928
+ (cat.any||(cat.any={}))[id = String.random(7)] = any;
929
+ any.off = function(){ any.stun = 1; if(!cat.any){ return } delete cat.any[id]; };
930
+ any.rid = rid; // logic from old version, can we clean it up now?
931
+ any.id = opt.run || ++root.once; // used in callback to check if we are earlier than a write. // will this ever cause an integer overflow?
932
+ tmp = root.pass; (root.pass = {})[id] = 1; // Explanation: test trade-offs want to prevent recursion so we add/remove pass flag as it gets fulfilled to not repeat, however map map needs many pass flags - how do we reconcile?
933
+ opt.out = opt.out || {get: {}};
934
+ cat.on('out', opt.out);
935
+ root.pass = tmp;
936
+ return gun;
937
+ } else
938
+ if('number' == typeof key){
939
+ return this.get(''+key, cb, as);
940
+ } else
941
+ if('string' == typeof (tmp = valid(key))){
942
+ return this.get(tmp, cb, as);
943
+ } else
944
+ if(tmp = this.get.next){
945
+ gun = tmp(this, key);
946
+ }
947
+ if(!gun){
948
+ (gun = this.chain())._.err = {err: Gun.log('Invalid get request!', key)}; // CLEAN UP
949
+ if(cb){ cb.call(gun, gun._.err); }
950
+ return gun;
951
+ }
952
+ if(cb && 'function' == typeof cb){
953
+ gun.get(cb, as);
954
+ }
955
+ return gun;
956
+ };
957
+ function cache(key, back){
958
+ var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
959
+ if(!next){ next = cat.next = {}; }
960
+ next[at.get = key] = at;
961
+ if(back === cat.root.$){
962
+ at.soul = key;
963
+ //at.put = {};
964
+ } else
965
+ if(cat.soul || cat.has){
966
+ at.has = key;
967
+ //if(obj_has(cat.put, key)){
968
+ //at.put = cat.put[key];
969
+ //}
970
+ }
971
+ return at;
972
+ }
973
+ function soul(gun, cb, opt, as){
974
+ var cat = gun._, acks = 0, tmp;
975
+ if(tmp = cat.soul || cat.link){ return cb(tmp, as, cat) }
976
+ if(cat.jam){ return cat.jam.push([cb, as]) }
977
+ cat.jam = [[cb,as]];
978
+ gun.get(function go(msg, eve){
979
+ if(u === msg.put && !cat.root.opt.super && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks <= tmp){ // TODO: super should not be in core code, bring AXE up into core instead to fix? // TODO: .keys( is slow
980
+ return;
981
+ }
982
+ eve.rid(msg);
983
+ var at = ((at = msg.$) && at._) || {}, i = 0, as;
984
+ tmp = cat.jam; delete cat.jam; // tmp = cat.jam.splice(0, 100);
985
+ //if(tmp.length){ process.nextTick(function(){ go(msg, eve) }) }
986
+ while(as = tmp[i++]){ //Gun.obj.map(tmp, function(as, cb){
987
+ var cb = as[0]; as = as[1];
988
+ cb && cb(at.link || at.soul || Gun.valid(msg.put) || ((msg.put||{})._||{})['#'], as, msg, eve);
989
+ } //);
990
+ }, {out: {get: {'.':true}}});
991
+ return gun;
992
+ }
993
+ function rid(at){
994
+ var cat = this.at || this.on;
995
+ if(!at || cat.soul || cat.has){ return this.off() }
996
+ if(!(at = (at = (at = at.$ || at)._ || at).id)){ return }
997
+ cat.map; var seen;
998
+ //if(!map || !(tmp = map[at]) || !(tmp = tmp.at)){ return }
999
+ if((seen = this.seen || (this.seen = {}))[at]){ return true }
1000
+ seen[at] = true;
1001
+ return;
1002
+ }
1003
+ var empty = {}, valid = Gun.valid, u;
1004
+ })(USE, './get');
1005
+ USE(function(module){
1006
+ var Gun = USE('./root');
1007
+ Gun.chain.put = function(data, cb, as){ // I rewrote it :)
1008
+ var gun = this, at = gun._, root = at.root;
1009
+ as = as || {};
1010
+ as.root = at.root;
1011
+ as.run || (as.run = root.once);
1012
+ stun(as, at.id); // set a flag for reads to check if this chain is writing.
1013
+ as.ack = as.ack || cb;
1014
+ as.via = as.via || gun;
1015
+ as.data = as.data || data;
1016
+ as.soul || (as.soul = at.soul || ('string' == typeof cb && cb));
1017
+ var s = as.state = as.state || Gun.state();
1018
+ if('function' == typeof data){ data(function(d){ as.data = d; gun.put(u,u,as); }); return gun }
1019
+ if(!as.soul){ return get(as), gun }
1020
+ as.$ = root.$.get(as.soul); // TODO: This may not allow user chaining and similar?
1021
+ as.todo = [{it: as.data, ref: as.$}];
1022
+ as.turn = as.turn || turn;
1023
+ as.ran = as.ran || ran;
1024
+ //var path = []; as.via.back(at => { at.get && path.push(at.get.slice(0,9)) }); path = path.reverse().join('.');
1025
+ // TODO: Perf! We only need to stun chains that are being modified, not necessarily written to.
1026
+ (function walk(){
1027
+ var to = as.todo, at = to.pop(), d = at.it; at.ref && at.ref._.id; var v, k, cat, tmp, g;
1028
+ stun(as, at.ref);
1029
+ if(tmp = at.todo){
1030
+ k = tmp.pop(); d = d[k];
1031
+ if(tmp.length){ to.push(at); }
1032
+ }
1033
+ k && (to.path || (to.path = [])).push(k);
1034
+ if(!(v = valid(d)) && !(g = Gun.is(d))){
1035
+ if(!Object.plain(d)){ ran.err(as, "Invalid data: "+ check(d) +" at " + (as.via.back(function(at){at.get && tmp.push(at.get);}, tmp = []) || tmp.join('.'))+'.'+(to.path||[]).join('.')); return }
1036
+ var seen = as.seen || (as.seen = []), i = seen.length;
1037
+ while(i--){ if(d === (tmp = seen[i]).it){ v = d = tmp.link; break } }
1038
+ }
1039
+ if(k && v){ at.node = state_ify(at.node, k, s, d); } // handle soul later.
1040
+ else {
1041
+ if(!as.seen){ ran.err(as, "Data at root of graph must be a node (an object)."); return }
1042
+ as.seen.push(cat = {it: d, link: {}, todo: g? [] : Object.keys(d).sort().reverse(), path: (to.path||[]).slice(), up: at}); // Any perf reasons to CPU schedule this .keys( ?
1043
+ at.node = state_ify(at.node, k, s, cat.link);
1044
+ !g && cat.todo.length && to.push(cat);
1045
+ // ---------------
1046
+ var id = as.seen.length;
1047
+ (as.wait || (as.wait = {}))[id] = '';
1048
+ tmp = (cat.ref = (g? d : k? at.ref.get(k) : at.ref))._;
1049
+ (tmp = (d && (d._||'')['#']) || tmp.soul || tmp.link)? resolve({soul: tmp}) : cat.ref.get(resolve, {run: as.run, /*hatch: 0,*/ v2020:1, out:{get:{'.':' '}}}); // TODO: BUG! This should be resolve ONLY soul to prevent full data from being loaded. // Fixed now?
1050
+ //setTimeout(function(){ if(F){ return } console.log("I HAVE NOT BEEN CALLED!", path, id, cat.ref._.id, k) }, 9000); var F; // MAKE SURE TO ADD F = 1 below!
1051
+ function resolve(msg, eve){
1052
+ var end = cat.link['#'];
1053
+ if(eve){ eve.off(); eve.rid(msg); } // TODO: Too early! Check all peers ack not found.
1054
+ // TODO: BUG maybe? Make sure this does not pick up a link change wipe, that it uses the changign link instead.
1055
+ var soul = end || msg.soul || (tmp = (msg.$$||msg.$)._||'').soul || tmp.link || ((tmp = tmp.put||'')._||'')['#'] || tmp['#'] || (((tmp = msg.put||'') && msg.$$)? tmp['#'] : (tmp['=']||tmp[':']||'')['#']);
1056
+ !end && stun(as, msg.$);
1057
+ if(!soul && !at.link['#']){ // check soul link above us
1058
+ (at.wait || (at.wait = [])).push(function(){ resolve(msg, eve); }); // wait
1059
+ return;
1060
+ }
1061
+ if(!soul){
1062
+ soul = [];
1063
+ (msg.$$||msg.$).back(function(at){
1064
+ if(tmp = at.soul || at.link){ return soul.push(tmp) }
1065
+ soul.push(at.get);
1066
+ });
1067
+ soul = soul.reverse().join('/');
1068
+ }
1069
+ cat.link['#'] = soul;
1070
+ !g && (((as.graph || (as.graph = {}))[soul] = (cat.node || (cat.node = {_:{}})))._['#'] = soul);
1071
+ delete as.wait[id];
1072
+ cat.wait && setTimeout.each(cat.wait, function(cb){ cb && cb(); });
1073
+ as.ran(as);
1074
+ } // ---------------
1075
+ }
1076
+ if(!to.length){ return as.ran(as) }
1077
+ as.turn(walk);
1078
+ }());
1079
+ return gun;
1080
+ };
1081
+
1082
+ function stun(as, id){
1083
+ if(!id){ return } id = (id._||'').id||id;
1084
+ var run = as.root.stun || (as.root.stun = {on: Gun.on}), test = {}, tmp;
1085
+ as.stun || (as.stun = run.on('stun', function(){ }));
1086
+ if(tmp = run.on(''+id)){ tmp.the.last.next(test); }
1087
+ if(test.run >= as.run){ return }
1088
+ run.on(''+id, function(test){
1089
+ if(as.stun.end){
1090
+ this.off();
1091
+ this.to.next(test);
1092
+ return;
1093
+ }
1094
+ test.run = test.run || as.run;
1095
+ test.stun = test.stun || as.stun; return;
1096
+ });
1097
+ }
1098
+
1099
+ function ran(as){
1100
+ if(as.err){ ran.end(as.stun, as.root); return } // move log handle here.
1101
+ if(as.todo.length || as.end || !Object.empty(as.wait)){ return } as.end = 1;
1102
+ //(as.retry = function(){ as.acks = 0;
1103
+ var cat = (as.$.back(-1)._), root = cat.root, ask = cat.ask(function(ack){
1104
+ root.on('ack', ack);
1105
+ if(ack.err && !ack.lack){ Gun.log(ack); }
1106
+ if(++acks > (as.acks || 0)){ this.off(); } // Adjustable ACKs! Only 1 by default.
1107
+ if(!as.ack){ return }
1108
+ as.ack(ack, this);
1109
+ }, as.opt), acks = 0, stun = as.stun, tmp;
1110
+ (tmp = function(){ // this is not official yet, but quick solution to hack in for now.
1111
+ if(!stun){ return }
1112
+ ran.end(stun, root);
1113
+ setTimeout.each(Object.keys(stun = stun.add||''), function(cb){ if(cb = stun[cb]){cb();} }); // resume the stunned reads // Any perf reasons to CPU schedule this .keys( ?
1114
+ }).hatch = tmp; // this is not official yet ^
1115
+ //console.log(1, "PUT", as.run, as.graph);
1116
+ if(as.ack && !as.ok){ as.ok = as.acks || 9; } // TODO: In future! Remove this! This is just old API support.
1117
+ (as.via._).on('out', {put: as.out = as.graph, ok: as.ok && {'@': as.ok+1}, opt: as.opt, '#': ask, _: tmp});
1118
+ //})();
1119
+ } ran.end = function(stun,root){
1120
+ stun.end = noop; // like with the earlier id, cheaper to make this flag a function so below callbacks do not have to do an extra type check.
1121
+ if(stun.the.to === stun && stun === stun.the.last){ delete root.stun; }
1122
+ stun.off();
1123
+ }; ran.err = function(as, err){
1124
+ (as.ack||noop).call(as, as.out = { err: as.err = Gun.log(err) });
1125
+ as.ran(as);
1126
+ };
1127
+
1128
+ function get(as){
1129
+ var at = as.via._, tmp;
1130
+ as.via = as.via.back(function(at){
1131
+ if(at.soul || !at.get){ return at.$ }
1132
+ tmp = as.data; (as.data = {})[at.get] = tmp;
1133
+ });
1134
+ if(!as.via || !as.via._.soul){
1135
+ as.via = at.root.$.get(((as.data||'')._||'')['#'] || at.$.back('opt.uuid')());
1136
+ }
1137
+ as.via.put(as.data, as.ack, as);
1138
+
1139
+
1140
+ return;
1141
+ }
1142
+ function check(d, tmp){ return ((d && (tmp = d.constructor) && tmp.name) || typeof d) }
1143
+
1144
+ var u, noop = function(){}, turn = setTimeout.turn, valid = Gun.valid, state_ify = Gun.state.ify;
1145
+ })(USE, './put');
1146
+ USE(function(module){
1147
+ var Gun = USE('./root');
1148
+ USE('./chain');
1149
+ USE('./back');
1150
+ USE('./put');
1151
+ USE('./get');
1152
+ module.exports = Gun;
1153
+ })(USE, './index');
1154
+ USE(function(module){
1155
+ var Gun = USE('./index');
1156
+ Gun.chain.on = function(tag, arg, eas, as){ // don't rewrite!
1157
+ var gun = this, cat = gun._; cat.root; var act;
1158
+ if(typeof tag === 'string'){
1159
+ if(!arg){ return cat.on(tag) }
1160
+ act = cat.on(tag, arg, eas || cat, as);
1161
+ if(eas && eas.$){
1162
+ (eas.subs || (eas.subs = [])).push(act);
1163
+ }
1164
+ return gun;
1165
+ }
1166
+ var opt = arg;
1167
+ (opt = (true === opt)? {change: true} : opt || {}).not = 1; opt.on = 1;
1168
+ gun.get(tag, opt);
1169
+ /*gun.get(function on(data,key,msg,eve){ var $ = this;
1170
+ if(tmp = root.hatch){ // quick hack!
1171
+ if(wait[$._.id]){ return } wait[$._.id] = 1;
1172
+ tmp.push(function(){on.call($, data,key,msg,eve)});
1173
+ return;
1174
+ }; wait = {}; // end quick hack.
1175
+ tag.call($, data,key,msg,eve);
1176
+ }, opt); // TODO: PERF! Event listener leak!!!?*/
1177
+ /*
1178
+ function one(msg, eve){
1179
+ if(one.stun){ return }
1180
+ var at = msg.$._, data = at.put, tmp;
1181
+ if(tmp = at.link){ data = root.$.get(tmp)._.put }
1182
+ if(opt.not===u && u === data){ return }
1183
+ if(opt.stun===u && (tmp = root.stun) && (tmp = tmp[at.id] || tmp[at.back.id]) && !tmp.end){ // Remember! If you port this into `.get(cb` make sure you allow stun:0 skip option for `.put(`.
1184
+ tmp[id] = function(){one(msg,eve)};
1185
+ return;
1186
+ }
1187
+ //tmp = one.wait || (one.wait = {}); console.log(tmp[at.id] === ''); if(tmp[at.id] !== ''){ tmp[at.id] = tmp[at.id] || setTimeout(function(){tmp[at.id]='';one(msg,eve)},1); return } delete tmp[at.id];
1188
+ // call:
1189
+ if(opt.as){
1190
+ opt.ok.call(opt.as, msg, eve || one);
1191
+ } else {
1192
+ opt.ok.call(at.$, data, msg.get || at.get, msg, eve || one);
1193
+ }
1194
+ };
1195
+ one.at = cat;
1196
+ (cat.act||(cat.act={}))[id = String.random(7)] = one;
1197
+ one.off = function(){ one.stun = 1; if(!cat.act){ return } delete cat.act[id] }
1198
+ cat.on('out', {get: {}});*/
1199
+ return gun;
1200
+ };
1201
+ // Rules:
1202
+ // 1. If cached, should be fast, but not read while write.
1203
+ // 2. Should not retrigger other listeners, should get triggered even if nothing found.
1204
+ // 3. If the same callback passed to many different once chains, each should resolve - an unsubscribe from the same callback should not effect the state of the other resolving chains, if you do want to cancel them all early you should mutate the callback itself with a flag & check for it at top of callback
1205
+ Gun.chain.once = function(cb, opt){ opt = opt || {}; // avoid rewriting
1206
+ if(!cb){ return none(this) }
1207
+ var gun = this, cat = gun._, root = cat.root; cat.put; var id = String.random(7), tmp;
1208
+ gun.get(function(data,key,msg,eve){
1209
+ var $ = this, at = $._, one = (at.one||(at.one={}));
1210
+ if(eve.stun){ return } if('' === one[id]){ return }
1211
+ if(true === (tmp = Gun.valid(data))){ once(); return }
1212
+ if('string' == typeof tmp){ return } // TODO: BUG? Will this always load?
1213
+ clearTimeout((cat.one||'')[id]); // clear "not found" since they only get set on cat.
1214
+ clearTimeout(one[id]); one[id] = setTimeout(once, opt.wait||99); // TODO: Bug? This doesn't handle plural chains.
1215
+ function once(f){
1216
+ if(!at.has && !at.soul){ at = {put: data, get: key}; } // handles non-core messages.
1217
+ if(u === (tmp = at.put)){ tmp = ((msg.$$||'')._||'').put; }
1218
+ if('string' == typeof Gun.valid(tmp)){
1219
+ tmp = root.$.get(tmp)._.put;
1220
+ if(tmp === u && !f){
1221
+ one[id] = setTimeout(function(){ once(1); }, opt.wait||99); // TODO: Quick fix. Maybe use ack count for more predictable control?
1222
+ return
1223
+ }
1224
+ }
1225
+ //console.log("AND VANISHED", data);
1226
+ if(eve.stun){ return } if('' === one[id]){ return } one[id] = '';
1227
+ if(cat.soul || cat.has){ eve.off(); } // TODO: Plural chains? // else { ?.off() } // better than one check?
1228
+ cb.call($, tmp, at.get);
1229
+ clearTimeout(one[id]); // clear "not found" since they only get set on cat. // TODO: This was hackily added, is it necessary or important? Probably not, in future try removing this. Was added just as a safety for the `&& !f` check.
1230
+ } }, {on: 1});
1231
+ return gun;
1232
+ };
1233
+ function none(gun,opt,chain){
1234
+ Gun.log.once("valonce", "Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
1235
+ (chain = gun.chain())._.nix = gun.once(function(data, key){ chain._.on('in', this._); });
1236
+ chain._.lex = gun._.lex; // TODO: Better approach in future? This is quick for now.
1237
+ return chain;
1238
+ }
1239
+
1240
+ Gun.chain.off = function(){
1241
+ // make off more aggressive. Warning, it might backfire!
1242
+ var gun = this, at = gun._, tmp;
1243
+ var cat = at.back;
1244
+ if(!cat){ return }
1245
+ at.ack = 0; // so can resubscribe.
1246
+ if(tmp = cat.next){
1247
+ if(tmp[at.get]){
1248
+ delete tmp[at.get];
1249
+ }
1250
+ }
1251
+ // TODO: delete cat.one[map.id]?
1252
+ if (tmp = cat.any) {
1253
+ delete cat.any;
1254
+ cat.any = {};
1255
+ }
1256
+ if(tmp = cat.ask){
1257
+ delete tmp[at.get];
1258
+ }
1259
+ if(tmp = cat.put){
1260
+ delete tmp[at.get];
1261
+ }
1262
+ if(tmp = at.soul){
1263
+ delete cat.root.graph[tmp];
1264
+ }
1265
+ if(tmp = at.map){
1266
+ Object.keys(tmp).forEach(function(i,at){ at = tmp[i]; //obj_map(tmp, function(at){
1267
+ if(at.link){
1268
+ cat.root.$.get(at.link).off();
1269
+ }
1270
+ });
1271
+ }
1272
+ if(tmp = at.next){
1273
+ Object.keys(tmp).forEach(function(i,neat){ neat = tmp[i]; //obj_map(tmp, function(neat){
1274
+ neat.$.off();
1275
+ });
1276
+ }
1277
+ at.on('off', {});
1278
+ return gun;
1279
+ };
1280
+ var u;
1281
+ })(USE, './on');
1282
+ USE(function(module){
1283
+ var Gun = USE('./index'), next = Gun.chain.get.next;
1284
+ Gun.chain.get.next = function(gun, lex){ var tmp;
1285
+ if(!Object.plain(lex)){ return (next||noop)(gun, lex) }
1286
+ if(tmp = ((tmp = lex['#'])||'')['='] || tmp){ return gun.get(tmp) }
1287
+ (tmp = gun.chain()._).lex = lex; // LEX!
1288
+ gun.on('in', function(eve){
1289
+ if(String.match(eve.get|| (eve.put||'')['.'], lex['.'] || lex['#'] || lex)){
1290
+ tmp.on('in', eve);
1291
+ }
1292
+ this.to.next(eve);
1293
+ });
1294
+ return tmp.$;
1295
+ };
1296
+ Gun.chain.map = function(cb, opt, t){
1297
+ var gun = this, cat = gun._, lex, chain;
1298
+ if(Object.plain(cb)){ lex = cb['.']? cb : {'.': cb}; cb = u; }
1299
+ if(!cb){
1300
+ if(chain = cat.each){ return chain }
1301
+ (cat.each = chain = gun.chain())._.lex = lex || chain._.lex || cat.lex;
1302
+ chain._.nix = gun.back('nix');
1303
+ gun.on('in', map, chain._);
1304
+ return chain;
1305
+ }
1306
+ Gun.log.once("mapfn", "Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
1307
+ chain = gun.chain();
1308
+ gun.map().on(function(data, key, msg, eve){
1309
+ var next = (cb||noop).call(this, data, key, msg, eve);
1310
+ if(u === next){ return }
1311
+ if(data === next){ return chain._.on('in', msg) }
1312
+ if(Gun.is(next)){ return chain._.on('in', next._) }
1313
+ var tmp = {}; Object.keys(msg.put).forEach(function(k){ tmp[k] = msg.put[k]; }, tmp); tmp['='] = next;
1314
+ chain._.on('in', {get: key, put: tmp});
1315
+ });
1316
+ return chain;
1317
+ };
1318
+ function map(msg){ this.to.next(msg);
1319
+ var cat = this.as, gun = msg.$, at = gun._, put = msg.put, tmp;
1320
+ if(!at.soul && !msg.$$){ return } // this line took hundreds of tries to figure out. It only works if core checks to filter out above chains during link tho. This says "only bother to map on a node" for this layer of the chain. If something is not a node, map should not work.
1321
+ if((tmp = cat.lex) && !String.match(msg.get|| (put||'')['.'], tmp['.'] || tmp['#'] || tmp)){ return }
1322
+ Gun.on.link(msg, cat);
1323
+ }
1324
+ var noop = function(){}, u;
1325
+ })(USE, './map');
1326
+ USE(function(module){
1327
+ var Gun = USE('./index');
1328
+ Gun.chain.set = function(item, cb, opt){
1329
+ var gun = this, root = gun.back(-1), soul, tmp;
1330
+ cb = cb || function(){};
1331
+ opt = opt || {}; opt.item = opt.item || item;
1332
+ if(soul = ((item||'')._||'')['#']){ (item = {})['#'] = soul; } // check if node, make link.
1333
+ if('string' == typeof (tmp = Gun.valid(item))){ return gun.get(soul = tmp).put(item, cb, opt) } // check if link
1334
+ if(!Gun.is(item)){
1335
+ if(Object.plain(item)){
1336
+ item = root.get(soul = gun.back('opt.uuid')()).put(item);
1337
+ }
1338
+ return gun.get(soul || root.back('opt.uuid')(7)).put(item, cb, opt);
1339
+ }
1340
+ gun.put(function(go){
1341
+ item.get(function(soul, o, msg){ // TODO: BUG! We no longer have this option? & go error not handled?
1342
+ if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}) }
1343
+ (tmp = {})[soul] = {'#': soul}; go(tmp);
1344
+ },true);
1345
+ });
1346
+ return item;
1347
+ };
1348
+ })(USE, './set');
1349
+ USE(function(module){
1350
+ USE('./shim');
1351
+
1352
+ var noop = function(){};
1353
+ var parse = JSON.parseAsync || function(t,cb,r){ var u, d = +new Date; try{ cb(u, JSON.parse(t,r), json.sucks(+new Date - d)); }catch(e){ cb(e); } };
1354
+ var json = JSON.stringifyAsync || function(v,cb,r,s){ var u, d = +new Date; try{ cb(u, JSON.stringify(v,r,s), json.sucks(+new Date - d)); }catch(e){ cb(e); } };
1355
+ json.sucks = function(d){ if(d > 99){ console.log("Warning: JSON blocking CPU detected. Add `gun/lib/yson.js` to fix."); json.sucks = noop; } };
1356
+
1357
+ function Mesh(root){
1358
+ var mesh = function(){};
1359
+ var opt = root.opt || {};
1360
+ opt.log = opt.log || console.log;
1361
+ opt.gap = opt.gap || opt.wait || 0;
1362
+ opt.max = opt.max || (opt.memory? (opt.memory * 999 * 999) : 300000000) * 0.3;
1363
+ opt.pack = opt.pack || (opt.max * 0.01 * 0.01);
1364
+ opt.puff = opt.puff || 9; // IDEA: do a start/end benchmark, divide ops/result.
1365
+ var puff = setTimeout.turn || setTimeout;
1366
+
1367
+ var dup = root.dup, dup_check = dup.check, dup_track = dup.track;
1368
+
1369
+ var hear = mesh.hear = function(raw, peer){
1370
+ if(!raw){ return }
1371
+ if(opt.max <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
1372
+ if(mesh === this){
1373
+ /*if('string' == typeof raw){ try{
1374
+ var stat = console.STAT || {};
1375
+ //console.log('HEAR:', peer.id, (raw||'').slice(0,250), ((raw||'').length / 1024 / 1024).toFixed(4));
1376
+
1377
+ //console.log(setTimeout.turn.s.length, 'stacks', parseFloat((-(LT - (LT = +new Date))/1000).toFixed(3)), 'sec', parseFloat(((LT-ST)/1000 / 60).toFixed(1)), 'up', stat.peers||0, 'peers', stat.has||0, 'has', stat.memhused||0, stat.memused||0, stat.memax||0, 'heap mem max');
1378
+ }catch(e){ console.log('DBG err', e) }}*/
1379
+ hear.d += raw.length||0 ; ++hear.c; } // STATS!
1380
+ var S = peer.SH = +new Date;
1381
+ var tmp = raw[0], msg;
1382
+ //raw && raw.slice && console.log("hear:", ((peer.wire||'').headers||'').origin, raw.length, raw.slice && raw.slice(0,50)); //tc-iamunique-tc-package-ds1
1383
+ if('[' === tmp){
1384
+ parse(raw, function(err, msg){
1385
+ if(err || !msg){ return mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
1386
+ console.STAT && console.STAT(+new Date, msg.length, '# on hear batch');
1387
+ var P = opt.puff;
1388
+ (function go(){
1389
+ var S = +new Date;
1390
+ var i = 0, m; while(i < P && (m = msg[i++])){ mesh.hear(m, peer); }
1391
+ msg = msg.slice(i); // slicing after is faster than shifting during.
1392
+ console.STAT && console.STAT(S, +new Date - S, 'hear loop');
1393
+ flush(peer); // force send all synchronously batched acks.
1394
+ if(!msg.length){ return }
1395
+ puff(go, 0);
1396
+ }());
1397
+ });
1398
+ raw = ''; //
1399
+ return;
1400
+ }
1401
+ if('{' === tmp || ((raw['#'] || Object.plain(raw)) && (msg = raw))){
1402
+ if(msg){ return hear.one(msg, peer, S) }
1403
+ parse(raw, function(err, msg){
1404
+ if(err || !msg){ return mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
1405
+ hear.one(msg, peer, S);
1406
+ });
1407
+ return;
1408
+ }
1409
+ };
1410
+ hear.one = function(msg, peer, S){ // S here is temporary! Undo.
1411
+ var id, hash, tmp, ash, DBG;
1412
+ if(msg.DBG){ msg.DBG = DBG = {DBG: msg.DBG}; }
1413
+ DBG && (DBG.h = S);
1414
+ DBG && (DBG.hp = +new Date);
1415
+ if(!(id = msg['#'])){ id = msg['#'] = String.random(9); }
1416
+ if(tmp = dup_check(id)){ return }
1417
+ // DAM logic:
1418
+ if(!(hash = msg['##']) && false && u !== msg.put); // disable hashing for now // TODO: impose warning/penalty instead (?)
1419
+ if(hash && (tmp = msg['@'] || (msg.get && id)) && dup.check(ash = tmp+hash)){ return } // Imagine A <-> B <=> (C & D), C & D reply with same ACK but have different IDs, B can use hash to dedup. Or if a GET has a hash already, we shouldn't ACK if same.
1420
+ (msg._ = function(){}).via = mesh.leap = peer;
1421
+ if((tmp = msg['><']) && 'string' == typeof tmp){ tmp.slice(0,99).split(',').forEach(function(k){ this[k] = 1; }, (msg._).yo = {}); } // Peers already sent to, do not resend.
1422
+ // DAM ^
1423
+ if(tmp = msg.dam){
1424
+ if(tmp = mesh.hear[tmp]){
1425
+ tmp(msg, peer, root);
1426
+ }
1427
+ dup_track(id);
1428
+ return;
1429
+ }
1430
+ if(tmp = msg.ok){ msg._.near = tmp['/']; }
1431
+ var S = +new Date;
1432
+ DBG && (DBG.is = S); peer.SI = id;
1433
+ dup_track.ed = function(d){
1434
+ if(id !== d){ return }
1435
+ dup_track.ed = 0;
1436
+ if(!(d = dup.s[id])){ return }
1437
+ d.via = peer;
1438
+ if(msg.get){ d.it = msg; }
1439
+ };
1440
+ root.on('in', mesh.last = msg);
1441
+ DBG && (DBG.hd = +new Date);
1442
+ console.STAT && console.STAT(S, +new Date - S, msg.get? 'msg get' : msg.put? 'msg put' : 'msg');
1443
+ dup_track(id); // in case 'in' does not call track.
1444
+ if(ash){ dup_track(ash); } //dup.track(tmp+hash, true).it = it(msg);
1445
+ mesh.leap = mesh.last = null; // warning! mesh.leap could be buggy.
1446
+ };
1447
+ hear.c = hear.d = 0;
1448
+ (function(){
1449
+ var SMIA = 0;
1450
+ var loop;
1451
+ mesh.hash = function(msg, peer){ var h, s, t;
1452
+ var S = +new Date;
1453
+ json(msg.put, function hash(err, text){
1454
+ var ss = (s || (s = t = text||'')).slice(0, 32768); // 1024 * 32
1455
+ h = String.hash(ss, h); s = s.slice(32768);
1456
+ if(s){ puff(hash, 0); return }
1457
+ console.STAT && console.STAT(S, +new Date - S, 'say json+hash');
1458
+ msg._.$put = t;
1459
+ msg['##'] = h;
1460
+ mesh.say(msg, peer);
1461
+ delete msg._.$put;
1462
+ }, sort);
1463
+ };
1464
+ function sort(k, v){ var tmp;
1465
+ if(!(v instanceof Object)){ return v }
1466
+ Object.keys(v).sort().forEach(sorta, {to: tmp = {}, on: v});
1467
+ return tmp;
1468
+ } function sorta(k){ this.to[k] = this.on[k]; }
1469
+
1470
+ mesh.say = function(msg, peer){ var tmp;
1471
+ if((tmp = this) && (tmp = tmp.to) && tmp.next){ tmp.next(msg); } // compatible with middleware adapters.
1472
+ if(!msg){ return false }
1473
+ var id, hash, raw, ack = msg['@'];
1474
+ //if(opt.super && (!ack || !msg.put)){ return } // TODO: MANHATTAN STUB //OBVIOUSLY BUG! But squelch relay. // :( get only is 100%+ CPU usage :(
1475
+ var meta = msg._||(msg._=function(){});
1476
+ var DBG = msg.DBG, S = +new Date; meta.y = meta.y || S; if(!peer){ DBG && (DBG.y = S); }
1477
+ if(!(id = msg['#'])){ id = msg['#'] = String.random(9); }
1478
+ !loop && dup_track(id);//.it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more! // always track, maybe move this to the 'after' logic if we split function.
1479
+ //if(msg.put && (msg.err || (dup.s[id]||'').err)){ return false } // TODO: in theory we should not be able to stun a message, but for now going to check if it can help network performance preventing invalid data to relay.
1480
+ if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ mesh.hash(msg, peer); return } // TODO: Should broadcasts be hashed?
1481
+ if(!peer && ack){ peer = ((tmp = dup.s[ack]) && (tmp.via || ((tmp = tmp.it) && (tmp = tmp._) && tmp.via))) || ((tmp = mesh.last) && ack === tmp['#'] && mesh.leap); } // warning! mesh.leap could be buggy! mesh last check reduces this. // TODO: CLEAN UP THIS LINE NOW? `.it` should be reliable.
1482
+ if(!peer && ack){ // still no peer, then ack daisy chain 'tunnel' got lost.
1483
+ if(dup.s[ack]){ return } // in dups but no peer hints that this was ack to ourself, ignore.
1484
+ console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to'); // TODO: Delete this now. Dropping lost ACKs is protocol fine now.
1485
+ return false;
1486
+ } // TODO: Temporary? If ack via trace has been lost, acks will go to all peers, which trashes browser bandwidth. Not relaying the ack will force sender to ask for ack again. Note, this is technically wrong for mesh behavior.
1487
+ if(ack && !msg.put && !hash && ((dup.s[ack]||'').it||'')['##']){ return false } // If we're saying 'not found' but a relay had data, do not bother sending our not found. // Is this correct, return false? // NOTE: ADD PANIC TEST FOR THIS!
1488
+ if(!peer && mesh.way){ return mesh.way(msg) }
1489
+ DBG && (DBG.yh = +new Date);
1490
+ if(!(raw = meta.raw)){ mesh.raw(msg, peer); return }
1491
+ DBG && (DBG.yr = +new Date);
1492
+ if(!peer || !peer.id){
1493
+ if(!Object.plain(peer || opt.peers)){ return false }
1494
+ var S = +new Date;
1495
+ opt.puff; var ps = opt.peers, pl = Object.keys(peer || opt.peers || {}); // TODO: .keys( is slow
1496
+ console.STAT && console.STAT(S, +new Date - S, 'peer keys');
1497
+ (function go(){
1498
+ var S = +new Date;
1499
+ //Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
1500
+ loop = 1; var wr = meta.raw; meta.raw = raw; // quick perf hack
1501
+ var i = 0, p; while(i < 9 && (p = (pl||'')[i++])){
1502
+ if(!(p = ps[p] || (peer||'')[p])){ continue }
1503
+ mesh.say(msg, p);
1504
+ }
1505
+ meta.raw = wr; loop = 0;
1506
+ pl = pl.slice(i); // slicing after is faster than shifting during.
1507
+ console.STAT && console.STAT(S, +new Date - S, 'say loop');
1508
+ if(!pl.length){ return }
1509
+ puff(go, 0);
1510
+ ack && dup_track(ack); // keep for later
1511
+ }());
1512
+ return;
1513
+ }
1514
+ // TODO: PERF: consider splitting function here, so say loops do less work.
1515
+ if(!peer.wire && mesh.wire){ mesh.wire(peer); }
1516
+ if(id === peer.last){ return } peer.last = id; // was it just sent?
1517
+ if(peer === meta.via){ return false } // don't send back to self.
1518
+ if((tmp = meta.yo) && (tmp[peer.url] || tmp[peer.pid] || tmp[peer.id]) /*&& !o*/){ return false }
1519
+ console.STAT && console.STAT(S, ((DBG||meta).yp = +new Date) - (meta.y || S), 'say prep');
1520
+ !loop && ack && dup_track(ack); // streaming long responses needs to keep alive the ack.
1521
+ if(peer.batch){
1522
+ peer.tail = (tmp = peer.tail || 0) + raw.length;
1523
+ if(peer.tail <= opt.pack){
1524
+ peer.batch += (tmp?',':'')+raw;
1525
+ return;
1526
+ }
1527
+ flush(peer);
1528
+ }
1529
+ peer.batch = '['; // Prevents double JSON!
1530
+ var ST = +new Date;
1531
+ setTimeout(function(){
1532
+ console.STAT && console.STAT(ST, +new Date - ST, '0ms TO');
1533
+ flush(peer);
1534
+ }, opt.gap); // TODO: queuing/batching might be bad for low-latency video game performance! Allow opt out?
1535
+ send(raw, peer);
1536
+ console.STAT && (ack === peer.SI) && console.STAT(S, +new Date - peer.SH, 'say ack');
1537
+ };
1538
+ mesh.say.c = mesh.say.d = 0;
1539
+ // TODO: this caused a out-of-memory crash!
1540
+ mesh.raw = function(msg, peer){ // TODO: Clean this up / delete it / move logic out!
1541
+ if(!msg){ return '' }
1542
+ var meta = (msg._) || {}, put, tmp;
1543
+ if(tmp = meta.raw){ return tmp }
1544
+ if('string' == typeof msg){ return msg }
1545
+ var hash = msg['##'], ack = msg['@'];
1546
+ if(hash && ack){
1547
+ if(!meta.via && dup_check(ack+hash)){ return false } // for our own out messages, memory & storage may ack the same thing, so dedup that. Tho if via another peer, we already tracked it upon hearing, so this will always trigger false positives, so don't do that!
1548
+ if(tmp = (dup.s[ack]||'').it){
1549
+ if(hash === tmp['##']){ return false } // if ask has a matching hash, acking is optional.
1550
+ if(!tmp['##']){ tmp['##'] = hash; } // if none, add our hash to ask so anyone we relay to can dedup. // NOTE: May only check against 1st ack chunk, 2nd+ won't know and still stream back to relaying peers which may then dedup. Any way to fix this wasted bandwidth? I guess force rate limiting breaking change, that asking peer has to ask for next lexical chunk.
1551
+ }
1552
+ }
1553
+ if(!msg.dam && !msg['@']){
1554
+ var i = 0, to = []; tmp = opt.peers;
1555
+ for(var k in tmp){ var p = tmp[k]; // TODO: Make it up peers instead!
1556
+ to.push(p.url || p.pid || p.id);
1557
+ if(++i > 6){ break }
1558
+ }
1559
+ if(i > 1){ msg['><'] = to.join(); } // TODO: BUG! This gets set regardless of peers sent to! Detect?
1560
+ }
1561
+ if(msg.put && (tmp = msg.ok)){ msg.ok = {'@':(tmp['@']||1)-1, '/': (tmp['/']==msg._.near)? mesh.near : tmp['/']}; }
1562
+ if(put = meta.$put){
1563
+ tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k]; });
1564
+ tmp.put = ':])([:';
1565
+ json(tmp, function(err, raw){
1566
+ if(err){ return } // TODO: Handle!!
1567
+ var S = +new Date;
1568
+ tmp = raw.indexOf('"put":":])([:"');
1569
+ res(u, raw = raw.slice(0, tmp+6) + put + raw.slice(tmp + 14));
1570
+ console.STAT && console.STAT(S, +new Date - S, 'say slice');
1571
+ });
1572
+ return;
1573
+ }
1574
+ json(msg, res);
1575
+ function res(err, raw){
1576
+ if(err){ return } // TODO: Handle!!
1577
+ meta.raw = raw; //if(meta && (raw||'').length < (999 * 99)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory.
1578
+ mesh.say(msg, peer);
1579
+ }
1580
+ };
1581
+ }());
1582
+
1583
+ function flush(peer){
1584
+ var tmp = peer.batch, t = 'string' == typeof tmp;
1585
+ if(t){ tmp += ']'; }// TODO: Prevent double JSON!
1586
+ peer.batch = peer.tail = null;
1587
+ if(!tmp){ return }
1588
+ if(t? 3 > tmp.length : !tmp.length){ return } // TODO: ^
1589
+ if(!t){try{tmp = (1 === tmp.length? tmp[0] : JSON.stringify(tmp));
1590
+ }catch(e){return opt.log('DAM JSON stringify error', e)}}
1591
+ if(!tmp){ return }
1592
+ send(tmp, peer);
1593
+ }
1594
+ // for now - find better place later.
1595
+ function send(raw, peer){ try{
1596
+ var wire = peer.wire;
1597
+ if(peer.say){
1598
+ peer.say(raw);
1599
+ } else
1600
+ if(wire.send){
1601
+ wire.send(raw);
1602
+ }
1603
+ mesh.say.d += raw.length||0; ++mesh.say.c; // STATS!
1604
+ }catch(e){
1605
+ (peer.queue = peer.queue || []).push(raw);
1606
+ }}
1607
+
1608
+ mesh.near = 0;
1609
+ mesh.hi = function(peer){
1610
+ var wire = peer.wire, tmp;
1611
+ if(!wire){ mesh.wire((peer.length && {url: peer, id: peer}) || peer); return }
1612
+ if(peer.id){
1613
+ opt.peers[peer.url || peer.id] = peer;
1614
+ } else {
1615
+ tmp = peer.id = peer.id || peer.url || String.random(9);
1616
+ mesh.say({dam: '?', pid: root.opt.pid}, opt.peers[tmp] = peer);
1617
+ delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self
1618
+ }
1619
+ if(!peer.met){
1620
+ mesh.near++;
1621
+ peer.met = +(new Date);
1622
+ root.on('hi', peer);
1623
+ }
1624
+ // @rogowski I need this here by default for now to fix go1dfish's bug
1625
+ tmp = peer.queue; peer.queue = [];
1626
+ setTimeout.each(tmp||[],function(msg){
1627
+ send(msg, peer);
1628
+ },0,9);
1629
+ //Type.obj.native && Type.obj.native(); // dirty place to check if other JS polluted.
1630
+ };
1631
+ mesh.bye = function(peer){
1632
+ peer.met && --mesh.near;
1633
+ delete peer.met;
1634
+ root.on('bye', peer);
1635
+ var tmp = +(new Date); tmp = (tmp - (peer.met||tmp));
1636
+ mesh.bye.time = ((mesh.bye.time || tmp) + tmp) / 2;
1637
+ };
1638
+ mesh.hear['!'] = function(msg, peer){ opt.log('Error:', msg.err); };
1639
+ mesh.hear['?'] = function(msg, peer){
1640
+ if(msg.pid){
1641
+ if(!peer.pid){ peer.pid = msg.pid; }
1642
+ if(msg['@']){ return }
1643
+ }
1644
+ mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
1645
+ delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self
1646
+ };
1647
+ mesh.hear['mob'] = function(msg, peer){ // NOTE: AXE will overload this with better logic.
1648
+ if(!msg.peers){ return }
1649
+ var peers = Object.keys(msg.peers), one = peers[(Math.random()*peers.length) >> 0];
1650
+ if(!one){ return }
1651
+ mesh.bye(peer);
1652
+ mesh.hi(one);
1653
+ };
1654
+
1655
+ root.on('create', function(root){
1656
+ root.opt.pid = root.opt.pid || String.random(9);
1657
+ this.to.next(root);
1658
+ root.on('out', mesh.say);
1659
+ });
1660
+
1661
+ root.on('bye', function(peer, tmp){
1662
+ peer = opt.peers[peer.id || peer] || peer;
1663
+ this.to.next(peer);
1664
+ peer.bye? peer.bye() : (tmp = peer.wire) && tmp.close && tmp.close();
1665
+ delete opt.peers[peer.id];
1666
+ peer.wire = null;
1667
+ });
1668
+ root.on('bye', function(peer, tmp){ this.to.next(peer);
1669
+ if(tmp = console.STAT){ tmp.peers = mesh.near; }
1670
+ if(!(tmp = peer.url)){ return } setTimeout(function(){ },opt.lack || 9000);
1671
+ });
1672
+ root.on('hi', function(peer, tmp){ this.to.next(peer);
1673
+ if(tmp = console.STAT){ tmp.peers = mesh.near; }
1674
+ if(opt.super){ return } // temporary (?) until we have better fix/solution?
1675
+ var souls = Object.keys(root.next||''); // TODO: .keys( is slow
1676
+ if(souls.length > 9999 && !console.SUBS){ console.log(console.SUBS = "Warning: You have more than 10K live GETs, which might use more bandwidth than your screen can show - consider `.off()`."); }
1677
+ setTimeout.each(souls, function(soul){ var node = root.next[soul];
1678
+ if(opt.super || (node.ask||'')['']){ mesh.say({get: {'#': soul}}, peer); return }
1679
+ setTimeout.each(Object.keys(node.ask||''), function(key){ if(!key){ return }
1680
+ // is the lack of ## a !onion hint?
1681
+ mesh.say({'##': String.hash((root.graph[soul]||'')[key]), get: {'#': soul, '.': key}}, peer);
1682
+ // TODO: Switch this so Book could route?
1683
+ });
1684
+ });
1685
+ });
1686
+
1687
+ return mesh;
1688
+ }
1689
+ var u;
1690
+
1691
+ try{ module.exports = Mesh; }catch(e){}
1692
+
1693
+ })(USE, './mesh');
1694
+ USE(function(module){
1695
+ var Gun = USE('./index');
1696
+ Gun.Mesh = USE('./mesh');
1697
+
1698
+ // TODO: resync upon reconnect online/offline
1699
+ //window.ononline = window.onoffline = function(){ console.log('online?', navigator.onLine) }
1700
+
1701
+ Gun.on('opt', function(root){
1702
+ this.to.next(root);
1703
+ if(root.once){ return }
1704
+ var opt = root.opt;
1705
+ if(false === opt.WebSocket){ return }
1706
+
1707
+ var env = Gun.window || {};
1708
+ var websocket = opt.WebSocket || env.WebSocket || env.webkitWebSocket || env.mozWebSocket;
1709
+ if(!websocket){ return }
1710
+ opt.WebSocket = websocket;
1711
+
1712
+ var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
1713
+
1714
+ mesh.wire || opt.wire;
1715
+ mesh.wire = opt.wire = open;
1716
+ function open(peer){ try{
1717
+ if(!peer || !peer.url){ return wire && wire(peer) }
1718
+ var url = peer.url.replace(/^http/, 'ws');
1719
+ var wire = peer.wire = new opt.WebSocket(url);
1720
+ wire.onclose = function(){
1721
+ reconnect(peer);
1722
+ opt.mesh.bye(peer);
1723
+ };
1724
+ wire.onerror = function(err){
1725
+ reconnect(peer);
1726
+ };
1727
+ wire.onopen = function(){
1728
+ opt.mesh.hi(peer);
1729
+ };
1730
+ wire.onmessage = function(msg){
1731
+ if(!msg){ return }
1732
+ opt.mesh.hear(msg.data || msg, peer);
1733
+ };
1734
+ return wire;
1735
+ }catch(e){ opt.mesh.bye(peer); }}
1736
+
1737
+ setTimeout(function(){ !opt.super && root.on('out', {dam:'hi'}); },1); // it can take a while to open a socket, so maybe no longer lazy load for perf reasons?
1738
+
1739
+ var wait = 2 * 999;
1740
+ function reconnect(peer){
1741
+ clearTimeout(peer.defer);
1742
+ if(!opt.peers[peer.url]){ return }
1743
+ if(doc && peer.retry <= 0){ return }
1744
+ peer.retry = (peer.retry || opt.retry+1 || 60) - ((-peer.tried + (peer.tried = +new Date) < wait*4)?1:0);
1745
+ peer.defer = setTimeout(function to(){
1746
+ if(doc && doc.hidden){ return setTimeout(to,wait) }
1747
+ open(peer);
1748
+ }, wait);
1749
+ }
1750
+ var doc = (''+u !== typeof document) && document;
1751
+ });
1752
+ var u;
1753
+ })(USE, './websocket');
1754
+ USE(function(module){
1755
+ if(typeof Gun === 'undefined'){ return }
1756
+
1757
+ var noop = function(){}, store;
1758
+ try{store = (Gun.window||noop).localStorage;}catch(e){}
1759
+ if(!store){
1760
+ Gun.log("Warning: No localStorage exists to persist data to!");
1761
+ store = {setItem: function(k,v){this[k]=v;}, removeItem: function(k){delete this[k];}, getItem: function(k){return this[k]}};
1762
+ }
1763
+ var json = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)); }catch(e){ cb(e); } };
1764
+
1765
+ Gun.on('create', function lg(root){
1766
+ this.to.next(root);
1767
+ var opt = root.opt; root.graph; var acks = [], disk, to, size, stop;
1768
+ if(false === opt.localStorage){ return }
1769
+ opt.prefix = opt.file || 'gun/';
1770
+ try{ disk = lg[opt.prefix] = lg[opt.prefix] || JSON.parse(size = store.getItem(opt.prefix)) || {}; // TODO: Perf! This will block, should we care, since limited to 5MB anyways?
1771
+ }catch(e){ disk = lg[opt.prefix] = {}; }
1772
+ size = (size||'').length;
1773
+
1774
+ root.on('get', function(msg){
1775
+ this.to.next(msg);
1776
+ var lex = msg.get, soul, data, tmp, u;
1777
+ if(!lex || !(soul = lex['#'])){ return }
1778
+ data = disk[soul] || u;
1779
+ if(data && (tmp = lex['.']) && !Object.plain(tmp)){ // pluck!
1780
+ data = Gun.state.ify({}, tmp, Gun.state.is(data, tmp), data[tmp], soul);
1781
+ }
1782
+ //if(data){ (tmp = {})[soul] = data } // back into a graph.
1783
+ //setTimeout(function(){
1784
+ Gun.on.get.ack(msg, data); //root.on('in', {'@': msg['#'], put: tmp, lS:1});// || root.$});
1785
+ //}, Math.random() * 10); // FOR TESTING PURPOSES!
1786
+ });
1787
+
1788
+ root.on('put', function(msg){
1789
+ this.to.next(msg); // remember to call next middleware adapter
1790
+ var put = msg.put, soul = put['#'], key = put['.'], id = msg['#'], ok = msg.ok||''; // pull data off wire envelope
1791
+ disk[soul] = Gun.state.ify(disk[soul], key, put['>'], put[':'], soul); // merge into disk object
1792
+ if(stop && size > (4999880)){ root.on('in', {'@': id, err: "localStorage max!"}); return; }
1793
+ //if(!msg['@']){ acks.push(id) } // then ack any non-ack write. // TODO: use batch id.
1794
+ if(!msg['@'] && (!msg._.via || Math.random() < (ok['@'] / ok['/']))){ acks.push(id); } // then ack any non-ack write. // TODO: use batch id.
1795
+ if(to){ return }
1796
+ to = setTimeout(flush, 9+(size / 333)); // 0.1MB = 0.3s, 5MB = 15s
1797
+ });
1798
+ function flush(){
1799
+ if(!acks.length && ((setTimeout.turn||'').s||'').length){ setTimeout(flush,99); return; } // defer if "busy" && no saves.
1800
+ var ack = acks; clearTimeout(to); to = false; acks = [];
1801
+ json(disk, function(err, tmp){
1802
+ try{!err && store.setItem(opt.prefix, tmp);
1803
+ }catch(e){ err = stop = e || "localStorage failure"; }
1804
+ if(err){
1805
+ Gun.log(err + " Consider using GUN's IndexedDB plugin for RAD for more storage space, https://gun.eco/docs/RAD#install");
1806
+ root.on('localStorage:error', {err: err, get: opt.prefix, put: disk});
1807
+ }
1808
+ size = tmp.length;
1809
+
1810
+ //if(!err && !Object.empty(opt.peers)){ return } // only ack if there are no peers. // Switch this to probabilistic mode
1811
+ setTimeout.each(ack, function(id){
1812
+ root.on('in', {'@': id, err: err, ok: 0}); // localStorage isn't reliable, so make its `ok` code be a low number.
1813
+ },0,99);
1814
+ });
1815
+ }
1816
+
1817
+ });
1818
+ })(USE, './localStorage');
1819
+
1820
+ }());
1821
+ (function(){
1822
+ var u;
1823
+ if(''+u == typeof Gun){ return }
1824
+ var DEP = function(n){ console.warn("Warning! Deprecated internal utility will break in next version:", n); };
1825
+ // Generic javascript utilities.
1826
+ var Type = Gun;
1827
+ //Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }}
1828
+ Type.fn = Type.fn || {is: function(fn){ DEP('fn'); return (!!fn && 'function' == typeof fn) }};
1829
+ Type.bi = Type.bi || {is: function(b){ DEP('bi');return (b instanceof Boolean || typeof b == 'boolean') }};
1830
+ Type.num = Type.num || {is: function(n){ DEP('num'); return !list_is(n) && ((n - parseFloat(n) + 1) >= 0 || Infinity === n || -Infinity === n) }};
1831
+ Type.text = Type.text || {is: function(t){ DEP('text'); return (typeof t == 'string') }};
1832
+ Type.text.ify = Type.text.ify || function(t){ DEP('text.ify');
1833
+ if(Type.text.is(t)){ return t }
1834
+ if(typeof JSON !== "undefined"){ return JSON.stringify(t) }
1835
+ return (t && t.toString)? t.toString() : t;
1836
+ };
1837
+ Type.text.random = Type.text.random || function(l, c){ DEP('text.random');
1838
+ var s = '';
1839
+ l = l || 24; // you are not going to make a 0 length random number, so no need to check type
1840
+ c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
1841
+ while(l > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); l--; }
1842
+ return s;
1843
+ };
1844
+ Type.text.match = Type.text.match || function(t, o){ var tmp, u; DEP('text.match');
1845
+ if('string' !== typeof t){ return false }
1846
+ if('string' == typeof o){ o = {'=': o}; }
1847
+ o = o || {};
1848
+ tmp = (o['='] || o['*'] || o['>'] || o['<']);
1849
+ if(t === tmp){ return true }
1850
+ if(u !== o['=']){ return false }
1851
+ tmp = (o['*'] || o['>'] || o['<']);
1852
+ if(t.slice(0, (tmp||'').length) === tmp){ return true }
1853
+ if(u !== o['*']){ return false }
1854
+ if(u !== o['>'] && u !== o['<']){
1855
+ return (t >= o['>'] && t <= o['<'])? true : false;
1856
+ }
1857
+ if(u !== o['>'] && t >= o['>']){ return true }
1858
+ if(u !== o['<'] && t <= o['<']){ return true }
1859
+ return false;
1860
+ };
1861
+ Type.text.hash = Type.text.hash || function(s, c){ // via SO
1862
+ DEP('text.hash');
1863
+ if(typeof s !== 'string'){ return }
1864
+ c = c || 0;
1865
+ if(!s.length){ return c }
1866
+ for(var i=0,l=s.length,n; i<l; ++i){
1867
+ n = s.charCodeAt(i);
1868
+ c = ((c<<5)-c)+n;
1869
+ c |= 0;
1870
+ }
1871
+ return c;
1872
+ };
1873
+ Type.list = Type.list || {is: function(l){ DEP('list'); return (l instanceof Array) }};
1874
+ Type.list.slit = Type.list.slit || Array.prototype.slice;
1875
+ Type.list.sort = Type.list.sort || function(k){ // creates a new sort function based off some key
1876
+ DEP('list.sort');
1877
+ return function(A,B){
1878
+ if(!A || !B){ return 0 } A = A[k]; B = B[k];
1879
+ if(A < B){ return -1 }else if(A > B){ return 1 }
1880
+ else { return 0 }
1881
+ }
1882
+ };
1883
+ Type.list.map = Type.list.map || function(l, c, _){ DEP('list.map'); return obj_map(l, c, _) };
1884
+ Type.list.index = 1; // change this to 0 if you want non-logical, non-mathematical, non-matrix, non-convenient array notation
1885
+ Type.obj = Type.boj || {is: function(o){ DEP('obj'); return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }};
1886
+ Type.obj.put = Type.obj.put || function(o, k, v){ DEP('obj.put'); return (o||{})[k] = v, o };
1887
+ Type.obj.has = Type.obj.has || function(o, k){ DEP('obj.has'); return o && Object.prototype.hasOwnProperty.call(o, k) };
1888
+ Type.obj.del = Type.obj.del || function(o, k){ DEP('obj.del');
1889
+ if(!o){ return }
1890
+ o[k] = null;
1891
+ delete o[k];
1892
+ return o;
1893
+ };
1894
+ Type.obj.as = Type.obj.as || function(o, k, v, u){ DEP('obj.as'); return o[k] = o[k] || (u === v? {} : v) };
1895
+ Type.obj.ify = Type.obj.ify || function(o){ DEP('obj.ify');
1896
+ if(obj_is(o)){ return o }
1897
+ try{o = JSON.parse(o);
1898
+ }catch(e){o={};} return o;
1899
+ }
1900
+ ;(function(){ var u;
1901
+ function map(v,k){
1902
+ if(obj_has(this,k) && u !== this[k]){ return }
1903
+ this[k] = v;
1904
+ }
1905
+ Type.obj.to = Type.obj.to || function(from, to){ DEP('obj.to');
1906
+ to = to || {};
1907
+ obj_map(from, map, to);
1908
+ return to;
1909
+ };
1910
+ }());
1911
+ Type.obj.copy = Type.obj.copy || function(o){ DEP('obj.copy'); // because http://web.archive.org/web/20140328224025/http://jsperf.com/cloning-an-object/2
1912
+ return !o? o : JSON.parse(JSON.stringify(o)); // is shockingly faster than anything else, and our data has to be a subset of JSON anyways!
1913
+ }
1914
+ ;(function(){
1915
+ function empty(v,i){ var n = this.n, u;
1916
+ if(n && (i === n || (obj_is(n) && obj_has(n, i)))){ return }
1917
+ if(u !== i){ return true }
1918
+ }
1919
+ Type.obj.empty = Type.obj.empty || function(o, n){ DEP('obj.empty');
1920
+ if(!o){ return true }
1921
+ return obj_map(o,empty,{n:n})? false : true;
1922
+ };
1923
+ }());
1924
+ (function(){
1925
+ function t(k,v){
1926
+ if(2 === arguments.length){
1927
+ t.r = t.r || {};
1928
+ t.r[k] = v;
1929
+ return;
1930
+ } t.r = t.r || [];
1931
+ t.r.push(k);
1932
+ } var keys = Object.keys, map;
1933
+ Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k);}) };
1934
+ Type.obj.map = map = Type.obj.map || function(l, c, _){ DEP('obj.map');
1935
+ var u, i = 0, x, r, ll, lle, f = 'function' == typeof c;
1936
+ t.r = u;
1937
+ if(keys && obj_is(l)){
1938
+ ll = keys(l); lle = true;
1939
+ }
1940
+ _ = _ || {};
1941
+ if(list_is(l) || ll){
1942
+ x = (ll || l).length;
1943
+ for(;i < x; i++){
1944
+ var ii = (i + Type.list.index);
1945
+ if(f){
1946
+ r = lle? c.call(_, l[ll[i]], ll[i], t) : c.call(_, l[i], ii, t);
1947
+ if(r !== u){ return r }
1948
+ } else {
1949
+ //if(Type.test.is(c,l[i])){ return ii } // should implement deep equality testing!
1950
+ if(c === l[lle? ll[i] : i]){ return ll? ll[i] : ii } // use this for now
1951
+ }
1952
+ }
1953
+ } else {
1954
+ for(i in l){
1955
+ if(f){
1956
+ if(obj_has(l,i)){
1957
+ r = _? c.call(_, l[i], i, t) : c(l[i], i, t);
1958
+ if(r !== u){ return r }
1959
+ }
1960
+ } else {
1961
+ //if(a.test.is(c,l[i])){ return i } // should implement deep equality testing!
1962
+ if(c === l[i]){ return i } // use this for now
1963
+ }
1964
+ }
1965
+ }
1966
+ return f? t.r : Type.list.index? 0 : -1;
1967
+ };
1968
+ }());
1969
+ Type.time = Type.time || {};
1970
+ Type.time.is = Type.time.is || function(t){ DEP('time'); return t? t instanceof Date : (+new Date().getTime()) };
1971
+
1972
+ var fn_is = Type.fn.is;
1973
+ var list_is = Type.list.is;
1974
+ var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
1975
+
1976
+ var Val = {};
1977
+ Val.is = function(v){ DEP('val.is'); // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first.
1978
+ if(v === u){ return false }
1979
+ if(v === null){ return true } // "deletes", nulling out keys.
1980
+ if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face.
1981
+ if(text_is(v) // by "text" we mean strings.
1982
+ || bi_is(v) // by "binary" we mean boolean.
1983
+ || num_is(v)){ // by "number" we mean integers or decimals.
1984
+ return true; // simple values are valid.
1985
+ }
1986
+ return Val.link.is(v) || false; // is the value a soul relation? Then it is valid and return it. If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types.
1987
+ };
1988
+ Val.link = Val.rel = {_: '#'};
1989
+ (function(){
1990
+ Val.link.is = function(v){ DEP('val.link.is'); // this defines whether an object is a soul relation or not, they look like this: {'#': 'UUID'}
1991
+ if(v && v[rel_] && !v._ && obj_is(v)){ // must be an object.
1992
+ var o = {};
1993
+ obj_map(v, map, o);
1994
+ if(o.id){ // a valid id was found.
1995
+ return o.id; // yay! Return it.
1996
+ }
1997
+ }
1998
+ return false; // the value was not a valid soul relation.
1999
+ };
2000
+ function map(s, k){ var o = this; // map over the object...
2001
+ if(o.id){ return o.id = false } // if ID is already defined AND we're still looping through the object, it is considered invalid.
2002
+ if(k == rel_ && text_is(s)){ // the key should be '#' and have a text value.
2003
+ o.id = s; // we found the soul!
2004
+ } else {
2005
+ return o.id = false; // if there exists anything else on the object that isn't the soul, then it is considered invalid.
2006
+ }
2007
+ }
2008
+ }());
2009
+ Val.link.ify = function(t){ DEP('val.link.ify'); return obj_put({}, rel_, t) }; // convert a soul into a relation and return it.
2010
+ Type.obj.has._ = '.';
2011
+ var rel_ = Val.link._, u;
2012
+ var bi_is = Type.bi.is;
2013
+ var num_is = Type.num.is;
2014
+ var text_is = Type.text.is;
2015
+ var obj = Type.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
2016
+
2017
+ Type.val = Type.val || Val;
2018
+
2019
+ var Node = {_: '_'};
2020
+ Node.soul = function(n, o){ DEP('node.soul'); return (n && n._ && n._[o || soul_]) }; // convenience function to check to see if there is a soul on a node and return it.
2021
+ Node.soul.ify = function(n, o){ DEP('node.soul.ify'); // put a soul on an object.
2022
+ o = (typeof o === 'string')? {soul: o} : o || {};
2023
+ n = n || {}; // make sure it exists.
2024
+ n._ = n._ || {}; // make sure meta exists.
2025
+ n._[soul_] = o.soul || n._[soul_] || text_random(); // put the soul on it.
2026
+ return n;
2027
+ };
2028
+ Node.soul._ = Val.link._;
2029
+ (function(){
2030
+ Node.is = function(n, cb, as){ DEP('node.is'); var s; // checks to see if an object is a valid node.
2031
+ if(!obj_is(n)){ return false } // must be an object.
2032
+ if(s = Node.soul(n)){ // must have a soul on it.
2033
+ return !obj_map(n, map, {as:as,cb:cb,s:s,n:n});
2034
+ }
2035
+ return false; // nope! This was not a valid node.
2036
+ };
2037
+ function map(v, k){ // we invert this because the way we check for this is via a negation.
2038
+ if(k === Node._){ return } // skip over the metadata.
2039
+ if(!Val.is(v)){ return true } // it is true that this is an invalid node.
2040
+ if(this.cb){ this.cb.call(this.as, v, k, this.n, this.s); } // optionally callback each key/value.
2041
+ }
2042
+ }());
2043
+ (function(){
2044
+ Node.ify = function(obj, o, as){ DEP('node.ify'); // returns a node from a shallow object.
2045
+ if(!o){ o = {}; }
2046
+ else if(typeof o === 'string'){ o = {soul: o}; }
2047
+ else if('function' == typeof o){ o = {map: o}; }
2048
+ if(o.map){ o.node = o.map.call(as, obj, u, o.node || {}); }
2049
+ if(o.node = Node.soul.ify(o.node || {}, o)){
2050
+ obj_map(obj, map, {o:o,as:as});
2051
+ }
2052
+ return o.node; // This will only be a valid node if the object wasn't already deep!
2053
+ };
2054
+ function map(v, k){ var o = this.o, tmp, u; // iterate over each key/value.
2055
+ if(o.map){
2056
+ tmp = o.map.call(this.as, v, ''+k, o.node);
2057
+ if(u === tmp){
2058
+ obj_del(o.node, k);
2059
+ } else
2060
+ if(o.node){ o.node[k] = tmp; }
2061
+ return;
2062
+ }
2063
+ if(Val.is(v)){
2064
+ o.node[k] = v;
2065
+ }
2066
+ }
2067
+ }());
2068
+ var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_map = obj.map;
2069
+ var text = Type.text, text_random = text.random;
2070
+ var soul_ = Node.soul._;
2071
+ var u;
2072
+ Type.node = Type.node || Node;
2073
+
2074
+ var State = Type.state;
2075
+ State.lex = function(){ DEP('state.lex'); return State().toString(36).replace('.','') };
2076
+ State.to = function(from, k, to){ DEP('state.to');
2077
+ var val = (from||{})[k];
2078
+ if(obj_is(val)){
2079
+ val = obj_copy(val);
2080
+ }
2081
+ return State.ify(to, k, State.is(from, k), val, Node.soul(from));
2082
+ }
2083
+ ;(function(){
2084
+ State.map = function(cb, s, as){ DEP('state.map'); var u; // for use with Node.ify
2085
+ var o = obj_is(o = cb || s)? o : null;
2086
+ cb = fn_is(cb = cb || s)? cb : null;
2087
+ if(o && !cb){
2088
+ s = num_is(s)? s : State();
2089
+ o[N_] = o[N_] || {};
2090
+ obj_map(o, map, {o:o,s:s});
2091
+ return o;
2092
+ }
2093
+ as = as || obj_is(s)? s : u;
2094
+ s = num_is(s)? s : State();
2095
+ return function(v, k, o, opt){
2096
+ if(!cb){
2097
+ map.call({o: o, s: s}, v,k);
2098
+ return v;
2099
+ }
2100
+ cb.call(as || this || {}, v, k, o, opt);
2101
+ if(obj_has(o,k) && u === o[k]){ return }
2102
+ map.call({o: o, s: s}, v,k);
2103
+ }
2104
+ };
2105
+ function map(v,k){
2106
+ if(N_ === k){ return }
2107
+ State.ify(this.o, k, this.s) ;
2108
+ }
2109
+ }());
2110
+ var obj = Type.obj; obj.as; var obj_has = obj.has, obj_is = obj.is, obj_map = obj.map, obj_copy = obj.copy;
2111
+ var num = Type.num, num_is = num.is;
2112
+ var fn = Type.fn, fn_is = fn.is;
2113
+ var N_ = Node._, u;
2114
+
2115
+ var Graph = {};
2116
+ (function(){
2117
+ Graph.is = function(g, cb, fn, as){ DEP('graph.is'); // checks to see if an object is a valid graph.
2118
+ if(!g || !obj_is(g) || obj_empty(g)){ return false } // must be an object.
2119
+ return !obj_map(g, map, {cb:cb,fn:fn,as:as}); // makes sure it wasn't an empty object.
2120
+ };
2121
+ function map(n, s){ // we invert this because the way'? we check for this is via a negation.
2122
+ if(!n || s !== Node.soul(n) || !Node.is(n, this.fn, this.as)){ return true } // it is true that this is an invalid graph.
2123
+ if(!this.cb){ return }
2124
+ nf.n = n; nf.as = this.as; // sequential race conditions aren't races.
2125
+ this.cb.call(nf.as, n, s, nf);
2126
+ }
2127
+ function nf(fn){ // optional callback for each node.
2128
+ if(fn){ Node.is(nf.n, fn, nf.as); } // where we then have an optional callback for each key/value.
2129
+ }
2130
+ }());
2131
+ (function(){
2132
+ Graph.ify = function(obj, env, as){ DEP('graph.ify');
2133
+ var at = {path: [], obj: obj};
2134
+ if(!env){
2135
+ env = {};
2136
+ } else
2137
+ if(typeof env === 'string'){
2138
+ env = {soul: env};
2139
+ } else
2140
+ if('function' == typeof env){
2141
+ env.map = env;
2142
+ }
2143
+ if(typeof as === 'string'){
2144
+ env.soul = env.soul || as;
2145
+ as = u;
2146
+ }
2147
+ if(env.soul){
2148
+ at.link = Val.link.ify(env.soul);
2149
+ }
2150
+ env.shell = (as||{}).shell;
2151
+ env.graph = env.graph || {};
2152
+ env.seen = env.seen || [];
2153
+ env.as = env.as || as;
2154
+ node(env, at);
2155
+ env.root = at.node;
2156
+ return env.graph;
2157
+ };
2158
+ function node(env, at){ var tmp;
2159
+ if(tmp = seen(env, at)){ return tmp }
2160
+ at.env = env;
2161
+ at.soul = soul;
2162
+ if(Node.ify(at.obj, map, at)){
2163
+ at.link = at.link || Val.link.ify(Node.soul(at.node));
2164
+ if(at.obj !== env.shell){
2165
+ env.graph[Val.link.is(at.link)] = at.node;
2166
+ }
2167
+ }
2168
+ return at;
2169
+ }
2170
+ function map(v,k,n){
2171
+ var at = this, env = at.env, is, tmp;
2172
+ if(Node._ === k && obj_has(v,Val.link._)){
2173
+ return n._; // TODO: Bug?
2174
+ }
2175
+ if(!(is = valid(v,k,n, at,env))){ return }
2176
+ if(!k){
2177
+ at.node = at.node || n || {};
2178
+ if(obj_has(v, Node._) && Node.soul(v)){ // ? for safety ?
2179
+ at.node._ = obj_copy(v._);
2180
+ }
2181
+ at.node = Node.soul.ify(at.node, Val.link.is(at.link));
2182
+ at.link = at.link || Val.link.ify(Node.soul(at.node));
2183
+ }
2184
+ if(tmp = env.map){
2185
+ tmp.call(env.as || {}, v,k,n, at);
2186
+ if(obj_has(n,k)){
2187
+ v = n[k];
2188
+ if(u === v){
2189
+ obj_del(n, k);
2190
+ return;
2191
+ }
2192
+ if(!(is = valid(v,k,n, at,env))){ return }
2193
+ }
2194
+ }
2195
+ if(!k){ return at.node }
2196
+ if(true === is){
2197
+ return v;
2198
+ }
2199
+ tmp = node(env, {obj: v, path: at.path.concat(k)});
2200
+ if(!tmp.node){ return }
2201
+ return tmp.link; //{'#': Node.soul(tmp.node)};
2202
+ }
2203
+ function soul(id){ var at = this;
2204
+ var prev = Val.link.is(at.link), graph = at.env.graph;
2205
+ at.link = at.link || Val.link.ify(id);
2206
+ at.link[Val.link._] = id;
2207
+ if(at.node && at.node[Node._]){
2208
+ at.node[Node._][Val.link._] = id;
2209
+ }
2210
+ if(obj_has(graph, prev)){
2211
+ graph[id] = graph[prev];
2212
+ obj_del(graph, prev);
2213
+ }
2214
+ }
2215
+ function valid(v,k,n, at,env){ var tmp;
2216
+ if(Val.is(v)){ return true }
2217
+ if(obj_is(v)){ return 1 }
2218
+ if(tmp = env.invalid){
2219
+ v = tmp.call(env.as || {}, v,k,n);
2220
+ return valid(v,k,n, at,env);
2221
+ }
2222
+ env.err = "Invalid value at '" + at.path.concat(k).join('.') + "'!";
2223
+ if(Type.list.is(v)){ env.err += " Use `.set(item)` instead of an Array."; }
2224
+ }
2225
+ function seen(env, at){
2226
+ var arr = env.seen, i = arr.length, has;
2227
+ while(i--){ has = arr[i];
2228
+ if(at.obj === has.obj){ return has }
2229
+ }
2230
+ arr.push(at);
2231
+ }
2232
+ }());
2233
+ Graph.node = function(node){ DEP('graph.node');
2234
+ var soul = Node.soul(node);
2235
+ if(!soul){ return }
2236
+ return obj_put({}, soul, node);
2237
+ }
2238
+ ;(function(){
2239
+ Graph.to = function(graph, root, opt){ DEP('graph.to');
2240
+ if(!graph){ return }
2241
+ var obj = {};
2242
+ opt = opt || {seen: {}};
2243
+ obj_map(graph[root], map, {obj:obj, graph: graph, opt: opt});
2244
+ return obj;
2245
+ };
2246
+ function map(v,k){ var tmp, obj;
2247
+ if(Node._ === k){
2248
+ if(obj_empty(v, Val.link._)){
2249
+ return;
2250
+ }
2251
+ this.obj[k] = obj_copy(v);
2252
+ return;
2253
+ }
2254
+ if(!(tmp = Val.link.is(v))){
2255
+ this.obj[k] = v;
2256
+ return;
2257
+ }
2258
+ if(obj = this.opt.seen[tmp]){
2259
+ this.obj[k] = obj;
2260
+ return;
2261
+ }
2262
+ this.obj[k] = this.opt.seen[tmp] = Graph.to(this.graph, tmp, this.opt);
2263
+ }
2264
+ }());
2265
+ var fn_is = Type.fn.is;
2266
+ var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_has = obj.has, obj_empty = obj.empty, obj_put = obj.put, obj_map = obj.map, obj_copy = obj.copy;
2267
+ var u;
2268
+ Type.graph = Type.graph || Graph;
2269
+ }());
2270
+ } (gun$1));
2271
+
2272
+ var gunExports = gun$1.exports;
2273
+ var Gun$1 = /*@__PURE__*/getDefaultExportFromCjs(gunExports);
2274
+
2275
+ let contractAddresses$1 = {
2276
+ PROOF_OF_INTEGRITY_ADDRESS: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
2277
+ STEALTH_ANNOUNCER_ADDRESS: "0x5FbDB2315678afecb367f032d93F642f64180aa3"
2278
+ };
2279
+ if (typeof window === 'undefined') {
2280
+ const {
2281
+ fileURLToPath
2282
+ } = require('url');
2283
+ const {
2284
+ dirname
2285
+ } = require('path');
2286
+ const {
2287
+ readFileSync
2288
+ } = require('fs');
2289
+ const {
2290
+ join
2291
+ } = require('path');
2292
+ try {
2293
+ const __filename = fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('gun-eth.cjs', document.baseURI).href)));
2294
+ const __dirname = dirname(__filename);
2295
+ const rawdata = readFileSync(join(__dirname, 'contract-address.json'), 'utf8');
2296
+ contractAddresses$1 = JSON.parse(rawdata);
2297
+ console.log("Loaded contract addresses:", contractAddresses$1);
2298
+ } catch (error) {
2299
+ console.warn("Warning: contract-address.json not found or invalid");
2300
+ }
2301
+ }
2302
+ const LOCAL_CONFIG = {
2303
+ CHAIN_ID: 1337,
2304
+ PROOF_OF_INTEGRITY_ADDRESS: contractAddresses$1.PROOF_OF_INTEGRITY_ADDRESS,
2305
+ STEALTH_ANNOUNCER_ADDRESS: contractAddresses$1.STEALTH_ANNOUNCER_ADDRESS,
2306
+ RPC_URL: "http://127.0.0.1:8545",
2307
+ GUN_PEER: "http://localhost:8765/gun"
2308
+ };
2309
+
2310
+ // Indirizzi di produzione per diverse chain
2311
+ const CHAIN_CONFIG = {
2312
+ optimismSepolia: {
2313
+ STEALTH_ANNOUNCER_ADDRESS: "0xD0F2e9DA59d2DFECFdE67CcF17300BB6093A72f8",
2314
+ PROOF_OF_INTEGRITY_ADDRESS: "0x...",
2315
+ RPC_URL: "https://sepolia.optimism.io",
2316
+ CHAIN_ID: 11155420
2317
+ },
2318
+ arbitrumSepolia: {
2319
+ STEALTH_ANNOUNCER_ADDRESS: "0x...",
2320
+ PROOF_OF_INTEGRITY_ADDRESS: "0x...",
2321
+ RPC_URL: "https://sepolia-rollup.arbitrum.io/rpc",
2322
+ CHAIN_ID: 421614
2323
+ },
2324
+ localhost: {
2325
+ RPC_URL: "http://127.0.0.1:8545",
2326
+ CHAIN_ID: 1337
2327
+ }
2328
+ };
2329
+
2330
+ // Esporta gli indirizzi dei contratti
2331
+ const PROOF_OF_INTEGRITY_ADDRESS = CHAIN_CONFIG.optimismSepolia.PROOF_OF_INTEGRITY_ADDRESS;
2332
+ const STEALTH_ANNOUNCER_ADDRESS = CHAIN_CONFIG.optimismSepolia.STEALTH_ANNOUNCER_ADDRESS;
2333
+
2334
+ // Funzione per ottenere gli indirizzi corretti
2335
+ function getAddressesForChain(chainName) {
2336
+ let config;
2337
+
2338
+ // Se è localhost, prova a caricare gli indirizzi locali
2339
+ if (chainName === 'localhost') {
2340
+ try {
2341
+ // Carica gli indirizzi dal file generato dal deploy locale
2342
+ const localAddresses = require('../config/contract-address.json');
2343
+ config = {
2344
+ ...CHAIN_CONFIG.localhost,
2345
+ ...localAddresses
2346
+ };
2347
+ console.log("Using local addresses:", config);
2348
+ return config;
2349
+ } catch (err) {
2350
+ console.warn('No local addresses found');
2351
+ throw new Error('No local addresses found. Did you run local deployment?');
2352
+ }
2353
+ }
2354
+
2355
+ // Altrimenti usa gli indirizzi di produzione
2356
+ config = CHAIN_CONFIG[chainName];
2357
+ if (!config) {
2358
+ throw new Error(`Chain ${chainName} not supported. Supported chains: ${Object.keys(CHAIN_CONFIG).join(', ')}`);
2359
+ }
2360
+ return config;
2361
+ }
2362
+ const STEALTH_ANNOUNCER_ABI = [{
2363
+ "inputs": [{
2364
+ "internalType": "address",
2365
+ "name": "_devAddress",
2366
+ "type": "address"
2367
+ }],
2368
+ "stateMutability": "nonpayable",
2369
+ "type": "constructor"
2370
+ }, {
2371
+ "anonymous": false,
2372
+ "inputs": [{
2373
+ "internalType": "string",
2374
+ "name": "senderPublicKey",
2375
+ "type": "string"
2376
+ }, {
2377
+ "internalType": "string",
2378
+ "name": "spendingPublicKey",
2379
+ "type": "string"
2380
+ }, {
2381
+ "internalType": "address",
2382
+ "name": "stealthAddress",
2383
+ "type": "address"
2384
+ }, {
2385
+ "internalType": "uint256",
2386
+ "name": "timestamp",
2387
+ "type": "uint256"
2388
+ }],
2389
+ "name": "StealthPaymentAnnounced",
2390
+ "type": "event"
2391
+ }, {
2392
+ "anonymous": false,
2393
+ "inputs": [{
2394
+ "internalType": "address",
2395
+ "name": "newAddress",
2396
+ "type": "address"
2397
+ }],
2398
+ "name": "DevAddressUpdated",
2399
+ "type": "event"
2400
+ }, {
2401
+ "anonymous": false,
2402
+ "inputs": [{
2403
+ "internalType": "uint256",
2404
+ "name": "newFee",
2405
+ "type": "uint256"
2406
+ }],
2407
+ "name": "DevFeeUpdated",
2408
+ "type": "event"
2409
+ }, {
2410
+ "inputs": [{
2411
+ "internalType": "string",
2412
+ "name": "senderPublicKey",
2413
+ "type": "string"
2414
+ }, {
2415
+ "internalType": "string",
2416
+ "name": "spendingPublicKey",
2417
+ "type": "string"
2418
+ }, {
2419
+ "internalType": "address",
2420
+ "name": "stealthAddress",
2421
+ "type": "address"
2422
+ }],
2423
+ "name": "announcePayment",
2424
+ "outputs": [],
2425
+ "stateMutability": "payable",
2426
+ "type": "function"
2427
+ }, {
2428
+ "inputs": [],
2429
+ "name": "devAddress",
2430
+ "outputs": [{
2431
+ "internalType": "address",
2432
+ "name": "",
2433
+ "type": "address"
2434
+ }],
2435
+ "stateMutability": "view",
2436
+ "type": "function"
2437
+ }, {
2438
+ "inputs": [],
2439
+ "name": "devFee",
2440
+ "outputs": [{
2441
+ "internalType": "uint256",
2442
+ "name": "",
2443
+ "type": "uint256"
2444
+ }],
2445
+ "stateMutability": "view",
2446
+ "type": "function"
2447
+ }, {
2448
+ "inputs": [],
2449
+ "name": "getAnnouncementsCount",
2450
+ "outputs": [{
2451
+ "internalType": "uint256",
2452
+ "name": "",
2453
+ "type": "uint256"
2454
+ }],
2455
+ "stateMutability": "view",
2456
+ "type": "function"
2457
+ }, {
2458
+ "inputs": [{
2459
+ "internalType": "uint256",
2460
+ "name": "fromIndex",
2461
+ "type": "uint256"
2462
+ }, {
2463
+ "internalType": "uint256",
2464
+ "name": "toIndex",
2465
+ "type": "uint256"
2466
+ }],
2467
+ "name": "getAnnouncementsInRange",
2468
+ "outputs": [{
2469
+ "components": [{
2470
+ "internalType": "string",
2471
+ "name": "senderPublicKey",
2472
+ "type": "string"
2473
+ }, {
2474
+ "internalType": "string",
2475
+ "name": "spendingPublicKey",
2476
+ "type": "string"
2477
+ }, {
2478
+ "internalType": "address",
2479
+ "name": "stealthAddress",
2480
+ "type": "address"
2481
+ }, {
2482
+ "internalType": "uint256",
2483
+ "name": "timestamp",
2484
+ "type": "uint256"
2485
+ }],
2486
+ "internalType": "struct StealthAnnouncer.StealthAnnouncement[]",
2487
+ "name": "",
2488
+ "type": "tuple[]"
2489
+ }],
2490
+ "stateMutability": "view",
2491
+ "type": "function"
2492
+ }, {
2493
+ "inputs": [{
2494
+ "internalType": "uint256",
2495
+ "name": "_newFee",
2496
+ "type": "uint256"
2497
+ }],
2498
+ "name": "updateDevFee",
2499
+ "outputs": [],
2500
+ "stateMutability": "nonpayable",
2501
+ "type": "function"
2502
+ }, {
2503
+ "inputs": [{
2504
+ "internalType": "address",
2505
+ "name": "_newAddress",
2506
+ "type": "address"
2507
+ }],
2508
+ "name": "updateDevAddress",
2509
+ "outputs": [],
2510
+ "stateMutability": "nonpayable",
2511
+ "type": "function"
2512
+ }, {
2513
+ "inputs": [],
2514
+ "name": "withdrawStuckETH",
2515
+ "outputs": [],
2516
+ "stateMutability": "nonpayable",
2517
+ "type": "function"
2518
+ }];
2519
+ const PROOF_OF_INTEGRITY_ABI = [{
2520
+ "inputs": [{
2521
+ "internalType": "bytes[]",
2522
+ "name": "nodeIds",
2523
+ "type": "bytes[]"
2524
+ }],
2525
+ "stateMutability": "nonpayable",
2526
+ "type": "function"
2527
+ }, {
2528
+ "anonymous": false,
2529
+ "inputs": [{
2530
+ "indexed": true,
2531
+ "internalType": "bytes",
2532
+ "name": "nodeId",
2533
+ "type": "bytes"
2534
+ }, {
2535
+ "indexed": false,
2536
+ "internalType": "bytes32",
2537
+ "name": "contentHash",
2538
+ "type": "bytes32"
2539
+ }, {
2540
+ "indexed": false,
2541
+ "internalType": "address",
2542
+ "name": "updater",
2543
+ "type": "address"
2544
+ }],
2545
+ "name": "DataUpdated",
2546
+ "type": "event"
2547
+ }, {
2548
+ "inputs": [{
2549
+ "internalType": "bytes",
2550
+ "name": "nodeId",
2551
+ "type": "bytes"
2552
+ }],
2553
+ "name": "getLatestRecord",
2554
+ "outputs": [{
2555
+ "internalType": "bytes32",
2556
+ "name": "",
2557
+ "type": "bytes32"
2558
+ }, {
2559
+ "internalType": "uint256",
2560
+ "name": "",
2561
+ "type": "uint256"
2562
+ }, {
2563
+ "internalType": "address",
2564
+ "name": "",
2565
+ "type": "address"
2566
+ }],
2567
+ "stateMutability": "view",
2568
+ "type": "function"
2569
+ }, {
2570
+ "inputs": [{
2571
+ "internalType": "bytes",
2572
+ "name": "nodeId",
2573
+ "type": "bytes"
2574
+ }, {
2575
+ "internalType": "bytes32",
2576
+ "name": "contentHash",
2577
+ "type": "bytes32"
2578
+ }],
2579
+ "name": "updateData",
2580
+ "outputs": [],
2581
+ "stateMutability": "nonpayable",
2582
+ "type": "function"
2583
+ }, {
2584
+ "inputs": [{
2585
+ "internalType": "bytes",
2586
+ "name": "nodeId",
2587
+ "type": "bytes"
2588
+ }, {
2589
+ "internalType": "bytes32",
2590
+ "name": "contentHash",
2591
+ "type": "bytes32"
2592
+ }],
2593
+ "name": "verifyData",
2594
+ "outputs": [{
2595
+ "internalType": "bool",
2596
+ "name": "",
2597
+ "type": "bool"
2598
+ }, {
2599
+ "internalType": "uint256",
2600
+ "name": "",
2601
+ "type": "uint256"
2602
+ }, {
2603
+ "internalType": "address",
2604
+ "name": "",
2605
+ "type": "address"
2606
+ }],
2607
+ "stateMutability": "view",
2608
+ "type": "function"
2609
+ }];
2610
+
2611
+ let PROOF_CONTRACT_ADDRESS;
2612
+ let rpcUrl = "";
2613
+ let privateKey = "";
2614
+ const MESSAGE_TO_SIGN = "Access GunDB with Ethereum";
2615
+ let contractAddresses = {
2616
+ PROOF_OF_INTEGRITY_ADDRESS,
2617
+ STEALTH_ANNOUNCER_ADDRESS
2618
+ };
2619
+
2620
+ // Inizializzazione asincrona spostata in una funzione
2621
+ async function initializeTextEncoder() {
2622
+ if (typeof window === 'undefined') {
2623
+ const util = await import('util');
2624
+ global.TextEncoder = util.TextEncoder;
2625
+ global.TextDecoder = util.TextDecoder;
2626
+ }
2627
+ }
2628
+
2629
+ // Inizializzazione contratti per Node.js
2630
+ function initializeContracts() {
2631
+ if (typeof window === 'undefined') {
2632
+ try {
2633
+ const {
2634
+ fileURLToPath
2635
+ } = require('url');
2636
+ const {
2637
+ dirname
2638
+ } = require('path');
2639
+ const {
2640
+ readFileSync
2641
+ } = require('fs');
2642
+ const path = require('path');
2643
+ const __filename = fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('gun-eth.cjs', document.baseURI).href)));
2644
+ const __dirname = dirname(__filename);
2645
+ const rawdata = readFileSync(path.join(__dirname, 'contract-address.json'), 'utf8');
2646
+ contractAddresses = JSON.parse(rawdata);
2647
+ console.log('Loaded contract addresses:', contractAddresses);
2648
+ } catch (err) {
2649
+ console.warn('Warning: contract-address.json not found or invalid');
2650
+ }
2651
+ }
2652
+ }
2653
+
2654
+ // Funzione di inizializzazione che può essere chiamata se necessario
2655
+ async function initialize() {
2656
+ await initializeTextEncoder();
2657
+ initializeContracts();
2658
+ }
2659
+
2660
+ // =============================================
2661
+ // UTILITY FUNCTIONS
2662
+ // =============================================
2663
+ /**
2664
+ * Generates a random node ID for GunDB
2665
+ * @returns {string} A random hexadecimal string
2666
+ */
2667
+ function generateRandomId() {
2668
+ return ethers.ethers.hexlify(ethers.ethers.randomBytes(32)).slice(2);
2669
+ }
2670
+
2671
+ /**
2672
+ * Generates a password from a signature.
2673
+ * @param {string} signature - The signature to derive the password from.
2674
+ * @returns {string|null} The generated password or null if generation fails.
2675
+ */
2676
+ function generatePassword(signature) {
2677
+ try {
2678
+ const hexSignature = ethers.ethers.hexlify(signature);
2679
+ const hash = ethers.ethers.keccak256(hexSignature);
2680
+ console.log("Generated password:", hash);
2681
+ return hash;
2682
+ } catch (error) {
2683
+ console.error("Error generating password:", error);
2684
+ return null;
2685
+ }
2686
+ }
2687
+
2688
+ /**
2689
+ * Converts a Gun private key to an Ethereum account.
2690
+ * @param {string} gunPrivateKey - The Gun private key in base64url format.
2691
+ * @returns {Object} An object containing the Ethereum account and public key.
2692
+ */
2693
+ function gunToEthAccount(gunPrivateKey) {
2694
+ // Function to convert base64url to hex
2695
+ const base64UrlToHex = base64url => {
2696
+ const padding = "=".repeat((4 - base64url.length % 4) % 4);
2697
+ const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/") + padding;
2698
+ const binary = atob(base64);
2699
+ return Array.from(binary, char => char.charCodeAt(0).toString(16).padStart(2, "0")).join("");
2700
+ };
2701
+
2702
+ // Convert Gun private key to hex format
2703
+ const hexPrivateKey = "0x" + base64UrlToHex(gunPrivateKey);
2704
+
2705
+ // Create an Ethereum wallet from the private key
2706
+ const wallet = new ethers.ethers.Wallet(hexPrivateKey);
2707
+
2708
+ // Get the public address (public key)
2709
+ const publicKey = wallet.address;
2710
+ return {
2711
+ account: wallet,
2712
+ publicKey: publicKey,
2713
+ privateKey: hexPrivateKey
2714
+ };
2715
+ }
2716
+
2717
+ /**
2718
+ * Gets an Ethereum signer based on current configuration
2719
+ * @returns {Promise<ethers.Signer>} The configured signer
2720
+ * @throws {Error} If no valid provider is found
2721
+ */
2722
+ const getSigner = async () => {
2723
+ if (rpcUrl && privateKey) {
2724
+ // Standalone mode with local provider
2725
+ const provider = new ethers.ethers.JsonRpcProvider(rpcUrl, {
2726
+ chainId: LOCAL_CONFIG.CHAIN_ID,
2727
+ name: "localhost"
2728
+ });
2729
+ return new ethers.ethers.Wallet(privateKey, provider);
2730
+ } else if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") {
2731
+ // Browser mode
2732
+ await window.ethereum.request({
2733
+ method: "eth_requestAccounts"
2734
+ });
2735
+ const provider = new ethers.ethers.BrowserProvider(window.ethereum);
2736
+ return provider.getSigner();
2737
+ } else {
2738
+ throw new Error("No valid Ethereum provider found");
2739
+ }
2740
+ };
2741
+
2742
+ /**
2743
+ * Utility function to generate stealth address
2744
+ * @param {string} sharedSecret - The shared secret
2745
+ * @param {string} spendingPublicKey - The spending public key
2746
+ * @returns {Object} The stealth address and private key
2747
+ */
2748
+ function deriveStealthAddress(sharedSecret, spendingPublicKey) {
2749
+ try {
2750
+ // Convert shared secret to bytes
2751
+ const sharedSecretBytes = Buffer.from(sharedSecret, 'base64');
2752
+
2753
+ // Generate stealth private key using shared secret and spending public key
2754
+ const stealthPrivateKey = ethers.ethers.keccak256(ethers.ethers.concat([sharedSecretBytes, ethers.ethers.getBytes(spendingPublicKey)]));
2755
+
2756
+ // Create stealth wallet
2757
+ const stealthWallet = new ethers.ethers.Wallet(stealthPrivateKey);
2758
+ console.log("Debug deriveStealthAddress:", {
2759
+ sharedSecretHex: ethers.ethers.hexlify(sharedSecretBytes),
2760
+ spendingPublicKey,
2761
+ stealthPrivateKey,
2762
+ stealthAddress: stealthWallet.address
2763
+ });
2764
+ return {
2765
+ stealthPrivateKey,
2766
+ stealthAddress: stealthWallet.address,
2767
+ wallet: stealthWallet
2768
+ };
2769
+ } catch (error) {
2770
+ console.error("Error in deriveStealthAddress:", error);
2771
+ throw error;
2772
+ }
2773
+ }
2774
+
2775
+ // =============================================
2776
+ // BASIC GUN-ETH CHAIN METHODS
2777
+ // =============================================
2778
+
2779
+ // Set the message to sign
2780
+ Gun$1.chain.MESSAGE_TO_SIGN = MESSAGE_TO_SIGN;
2781
+
2782
+ /**
2783
+ * Sets standalone configuration for Gun.
2784
+ * @param {string} newRpcUrl - The new RPC URL.
2785
+ * @param {string} newPrivateKey - The new private key.
2786
+ * @returns {Gun} The Gun instance for chaining.
2787
+ */
2788
+ Gun$1.chain.setSigner = function (newRpcUrl, newPrivateKey) {
2789
+ rpcUrl = newRpcUrl;
2790
+ privateKey = newPrivateKey;
2791
+ console.log("Standalone configuration set");
2792
+ return this;
2793
+ };
2794
+ Gun$1.chain.getSigner = getSigner;
2795
+
2796
+ /**
2797
+ * Verifies an Ethereum signature.
2798
+ * @param {string} message - The original message that was signed.
2799
+ * @param {string} signature - The signature to verify.
2800
+ * @returns {Promise<string|null>} The recovered address or null if verification fails.
2801
+ */
2802
+ Gun$1.chain.verifySignature = async function (message, signature) {
2803
+ try {
2804
+ const recoveredAddress = ethers.ethers.verifyMessage(message, signature);
2805
+ return recoveredAddress;
2806
+ } catch (error) {
2807
+ console.error("Error verifying signature:", error);
2808
+ return null;
2809
+ }
2810
+ };
2811
+
2812
+ /**
2813
+ * Generates a password from a signature.
2814
+ * @param {string} signature - The signature to derive the password from.
2815
+ * @returns {string|null} The generated password or null if generation fails.
2816
+ */
2817
+ Gun$1.chain.generatePassword = function (signature) {
2818
+ return generatePassword(signature);
2819
+ };
2820
+
2821
+ /**
2822
+ * Creates an Ethereum signature for a given message.
2823
+ * @param {string} message - The message to sign.
2824
+ * @returns {Promise<string|null>} The signature or null if signing fails.
2825
+ */
2826
+ Gun$1.chain.createSignature = async function (message) {
2827
+ try {
2828
+ // Check if message matches MESSAGE_TO_SIGN
2829
+ if (message !== MESSAGE_TO_SIGN) {
2830
+ throw new Error("Invalid message, valid message is: " + MESSAGE_TO_SIGN);
2831
+ }
2832
+ const signer = await getSigner();
2833
+ const signature = await signer.signMessage(message);
2834
+ console.log("Signature created:", signature);
2835
+ return signature;
2836
+ } catch (error) {
2837
+ console.error("Error creating signature:", error);
2838
+ return null;
2839
+ }
2840
+ };
2841
+
2842
+ // =============================================
2843
+ // KEY PAIR MANAGEMENT
2844
+ // =============================================
2845
+ /**
2846
+ * Creates and stores an encrypted key pair for a given address.
2847
+ * @param {string} address - The Ethereum address to associate with the key pair.
2848
+ * @param {string} signature - The signature to use for encryption.
2849
+ * @returns {Promise<void>}
2850
+ */
2851
+ Gun$1.chain.createAndStoreEncryptedPair = async function (address, signature) {
2852
+ try {
2853
+ const gun = this;
2854
+ const pair = await SEA.pair();
2855
+ const v_pair = await SEA.pair();
2856
+ const s_pair = await SEA.pair();
2857
+ const password = generatePassword(signature);
2858
+
2859
+ // Save original SEA pairs
2860
+ const encryptedPair = await SEA.encrypt(JSON.stringify(pair), password);
2861
+ const encryptedV_pair = await SEA.encrypt(JSON.stringify(v_pair), password);
2862
+ const encryptedS_pair = await SEA.encrypt(JSON.stringify(s_pair), password);
2863
+
2864
+ // Convert only to get Ethereum addresses
2865
+ const viewingAccount = gunToEthAccount(v_pair.priv);
2866
+ const spendingAccount = gunToEthAccount(s_pair.priv);
2867
+ gun.get("gun-eth").get("users").get(address).put({
2868
+ pair: encryptedPair,
2869
+ v_pair: encryptedV_pair,
2870
+ s_pair: encryptedS_pair,
2871
+ publicKeys: {
2872
+ viewingPublicKey: v_pair.epub,
2873
+ // Use SEA encryption public key
2874
+ viewingPublicKey: v_pair.epub,
2875
+ // Use SEA encryption public key
2876
+ spendingPublicKey: spendingAccount.publicKey,
2877
+ // Use Ethereum address
2878
+ ethViewingAddress: viewingAccount.publicKey // Also save Ethereum address
2879
+ }
2880
+ });
2881
+ console.log("Encrypted pairs and public keys stored for:", address);
2882
+ } catch (error) {
2883
+ console.error("Error creating and storing encrypted pair:", error);
2884
+ throw error;
2885
+ }
2886
+ };
2887
+
2888
+ /**
2889
+ * Retrieves and decrypts a stored key pair for a given address.
2890
+ * @param {string} address - The Ethereum address associated with the key pair.
2891
+ * @param {string} signature - The signature to use for decryption.
2892
+ * @returns {Promise<Object|null>} The decrypted key pair or null if retrieval fails.
2893
+ */
2894
+ Gun$1.chain.getAndDecryptPair = async function (address, signature) {
2895
+ try {
2896
+ const gun = this;
2897
+ const encryptedData = await gun.get("gun-eth").get("users").get(address).get("pair").then();
2898
+ if (!encryptedData) {
2899
+ throw new Error("No encrypted data found for this address");
2900
+ }
2901
+ const password = generatePassword(signature);
2902
+ const decryptedPair = await SEA.decrypt(encryptedData, password);
2903
+ console.log(decryptedPair);
2904
+ return decryptedPair;
2905
+ } catch (error) {
2906
+ console.error("Error retrieving and decrypting pair:", error);
2907
+ return null;
2908
+ }
2909
+ };
2910
+
2911
+ // =============================================
2912
+ // PROOF OF INTEGRITY
2913
+ // =============================================
2914
+ /**
2915
+ * Proof of Integrity
2916
+ * @param {string} chain - The blockchain to use (e.g., "optimismSepolia").
2917
+ * @param {string} nodeId - The ID of the node to verify or write.
2918
+ * @param {Object} data - The data to write (if writing).
2919
+ * @param {Function} callback - Callback function to handle the result.
2920
+ * @returns {Gun} The Gun instance for chaining.
2921
+ */
2922
+ Gun$1.chain.proof = function (chain, nodeId, data, callback) {
2923
+ console.log("Proof plugin called with:", {
2924
+ chain,
2925
+ nodeId,
2926
+ data
2927
+ });
2928
+ if (typeof callback !== "function") {
2929
+ console.error("Callback must be a function");
2930
+ return this;
2931
+ }
2932
+ try {
2933
+ // Usa getAddressesForChain per ottenere la configurazione corretta
2934
+ const chainConfig = getAddressesForChain(chain);
2935
+ console.log(`Using ${chain} configuration:`, chainConfig);
2936
+
2937
+ // Usa gli indirizzi dalla configurazione
2938
+ const contract = new ethers.ethers.Contract(chainConfig.PROOF_OF_INTEGRITY_ADDRESS, PROOF_OF_INTEGRITY_ABI, signer);
2939
+
2940
+ // Funzione per verificare on-chain
2941
+ const verifyOnChain = async (nodeId, contentHash) => {
2942
+ console.log("Verifying on chain:", {
2943
+ nodeId,
2944
+ contentHash
2945
+ });
2946
+ const signer = await getSigner();
2947
+ const contract = new ethers.ethers.Contract(PROOF_CONTRACT_ADDRESS, PROOF_OF_INTEGRITY_ABI, signer);
2948
+ const [isValid, timestamp, updater] = await contract.verifyData(ethers.ethers.toUtf8Bytes(nodeId), contentHash);
2949
+ console.log("Verification result:", {
2950
+ isValid,
2951
+ timestamp,
2952
+ updater
2953
+ });
2954
+ return {
2955
+ isValid,
2956
+ timestamp,
2957
+ updater
2958
+ };
2959
+ };
2960
+
2961
+ // Funzione per scrivere on-chain
2962
+ const writeOnChain = async (nodeId, contentHash) => {
2963
+ console.log("Writing on chain:", {
2964
+ nodeId,
2965
+ contentHash
2966
+ });
2967
+ const signer = await getSigner();
2968
+ const contract = new ethers.ethers.Contract(PROOF_CONTRACT_ADDRESS, PROOF_OF_INTEGRITY_ABI, signer);
2969
+ const tx = await contract.updateData(ethers.ethers.toUtf8Bytes(nodeId), contentHash);
2970
+ console.log("Transaction sent:", tx.hash);
2971
+ const receipt = await tx.wait();
2972
+ console.log("Transaction confirmed:", receipt);
2973
+ return tx;
2974
+ };
2975
+
2976
+ // Funzione per ottenere l'ultimo record
2977
+ const getLatestRecord = async nodeId => {
2978
+ const signer = await getSigner();
2979
+ const contract = new ethers.ethers.Contract(PROOF_CONTRACT_ADDRESS, PROOF_OF_INTEGRITY_ABI, signer);
2980
+ const [contentHash, timestamp, updater] = await contract.getLatestRecord(ethers.ethers.toUtf8Bytes(nodeId));
2981
+ console.log("Latest record from blockchain:", {
2982
+ nodeId,
2983
+ contentHash,
2984
+ timestamp,
2985
+ updater
2986
+ });
2987
+ return {
2988
+ contentHash,
2989
+ timestamp,
2990
+ updater
2991
+ };
2992
+ };
2993
+ if (nodeId && !data) {
2994
+ // Case 1: User passes only node
2995
+ gun.get(nodeId).once(async existingData => {
2996
+ if (!existingData) {
2997
+ if (callback) callback({
2998
+ err: "Node not found in GunDB"
2999
+ });
3000
+ return;
3001
+ }
3002
+ console.log("existingData", existingData);
3003
+
3004
+ // Use stored contentHash instead of recalculating
3005
+ const contentHash = existingData._contentHash;
3006
+ console.log("contentHash", contentHash);
3007
+ if (!contentHash) {
3008
+ if (callback) callback({
3009
+ err: "No content hash found for this node"
3010
+ });
3011
+ return;
3012
+ }
3013
+ try {
3014
+ const {
3015
+ isValid,
3016
+ timestamp,
3017
+ updater
3018
+ } = await verifyOnChain(nodeId, contentHash);
3019
+ const latestRecord = await getLatestRecord(nodeId);
3020
+ if (isValid) {
3021
+ if (callback) callback({
3022
+ ok: true,
3023
+ message: "Data verified on blockchain",
3024
+ timestamp,
3025
+ updater,
3026
+ latestRecord
3027
+ });
3028
+ } else {
3029
+ if (callback) callback({
3030
+ ok: false,
3031
+ message: "Data not verified on blockchain",
3032
+ latestRecord
3033
+ });
3034
+ }
3035
+ } catch (error) {
3036
+ if (callback) callback({
3037
+ err: error.message
3038
+ });
3039
+ }
3040
+ });
3041
+ } else if (data && !nodeId) {
3042
+ // Case 2: User passes only text (data)
3043
+ const newNodeId = generateRandomId();
3044
+ const dataString = JSON.stringify(data);
3045
+ const contentHash = ethers.ethers.keccak256(ethers.ethers.toUtf8Bytes(dataString));
3046
+ gun.get(newNodeId).put({
3047
+ ...data,
3048
+ _contentHash: contentHash
3049
+ }, async ack => {
3050
+ console.log("ack", ack);
3051
+ if (ack.err) {
3052
+ if (callback) callback({
3053
+ err: "Error saving data to GunDB"
3054
+ });
3055
+ return;
3056
+ }
3057
+ try {
3058
+ const tx = await writeOnChain(newNodeId, contentHash);
3059
+ if (callback) callback({
3060
+ ok: true,
3061
+ message: "Data written to GunDB and blockchain",
3062
+ nodeId: newNodeId,
3063
+ txHash: tx.hash
3064
+ });
3065
+ } catch (error) {
3066
+ if (callback) callback({
3067
+ err: error.message
3068
+ });
3069
+ }
3070
+ });
3071
+ } else {
3072
+ if (callback) callback({
3073
+ err: "Invalid input. Provide either nodeId or data, not both."
3074
+ });
3075
+ }
3076
+ return gun;
3077
+ } catch (error) {
3078
+ callback({
3079
+ err: error.message
3080
+ });
3081
+ return this;
3082
+ }
3083
+ };
3084
+
3085
+ // =============================================
3086
+ // STEALTH ADDRESS CORE FUNCTIONS
3087
+ // =============================================
3088
+ /**
3089
+ * Converts a Gun private key to an Ethereum account.
3090
+ * @param {string} gunPrivateKey - The Gun private key in base64url format.
3091
+ * @returns {Object} An object containing the Ethereum account and public key.
3092
+ */
3093
+ Gun$1.chain.gunToEthAccount = function (gunPrivateKey) {
3094
+ return gunToEthAccount(gunPrivateKey);
3095
+ };
3096
+
3097
+ /**
3098
+ * Generate a stealth key and related key pairs
3099
+ * @param {string} recipientAddress - The recipient's Ethereum address
3100
+ * @param {string} signature - The sender's signature to access their keys
3101
+ * @returns {Promise<Object>} Object containing stealth addresses and keys
3102
+ */
3103
+ Gun$1.chain.generateStealthAddress = async function (recipientAddress, signature) {
3104
+ try {
3105
+ const gun = this;
3106
+
3107
+ // Get recipient's public keys
3108
+ const recipientData = await gun.get("gun-eth").get("users").get(recipientAddress).get("publicKeys").then();
3109
+ if (!recipientData || !recipientData.viewingPublicKey || !recipientData.spendingPublicKey) {
3110
+ throw new Error("Recipient's public keys not found");
3111
+ }
3112
+
3113
+ // Get sender's keys
3114
+ const senderAddress = await this.verifySignature(MESSAGE_TO_SIGN, signature);
3115
+ const password = generatePassword(signature);
3116
+ const senderData = await gun.get("gun-eth").get("users").get(senderAddress).then();
3117
+ if (!senderData || !senderData.s_pair) {
3118
+ throw new Error("Sender's keys not found");
3119
+ }
3120
+
3121
+ // Decrypt sender's spending pair
3122
+ let spendingKeyPair;
3123
+ try {
3124
+ const decryptedData = await SEA.decrypt(senderData.s_pair, password);
3125
+ spendingKeyPair = typeof decryptedData === 'string' ? JSON.parse(decryptedData) : decryptedData;
3126
+ } catch (error) {
3127
+ console.error("Error decrypting spending pair:", error);
3128
+ throw new Error("Unable to decrypt spending pair");
3129
+ }
3130
+
3131
+ // Generate shared secret using SEA ECDH with encryption public key
3132
+ const sharedSecret = await SEA.secret(recipientData.viewingPublicKey, spendingKeyPair);
3133
+ if (!sharedSecret) {
3134
+ throw new Error("Unable to generate shared secret");
3135
+ }
3136
+ console.log("Generate shared secret:", sharedSecret);
3137
+ const {
3138
+ stealthAddress
3139
+ } = deriveStealthAddress(sharedSecret, recipientData.spendingPublicKey);
3140
+ return {
3141
+ stealthAddress,
3142
+ senderPublicKey: spendingKeyPair.epub,
3143
+ // Use encryption public key
3144
+ spendingPublicKey: recipientData.spendingPublicKey
3145
+ };
3146
+ } catch (error) {
3147
+ console.error("Error generating stealth address:", error);
3148
+ throw error;
3149
+ }
3150
+ };
3151
+
3152
+ /**
3153
+ * Publish public keys needed to receive stealth payments
3154
+ * @param {string} signature - The signature to authenticate the user
3155
+ * @returns {Promise<void>}
3156
+ */
3157
+ Gun$1.chain.publishStealthKeys = async function (signature) {
3158
+ try {
3159
+ const gun = this;
3160
+ const address = await this.verifySignature(MESSAGE_TO_SIGN, signature);
3161
+ const password = generatePassword(signature);
3162
+
3163
+ // Get encrypted key pairs
3164
+ const encryptedData = await gun.get("gun-eth").get("users").get(address).then();
3165
+ if (!encryptedData || !encryptedData.v_pair || !encryptedData.s_pair) {
3166
+ throw new Error("Keys not found");
3167
+ }
3168
+
3169
+ // Decrypt viewing and spending pairs
3170
+ const viewingKeyPair = JSON.parse(await SEA.decrypt(encryptedData.v_pair, password));
3171
+ const spendingKeyPair = JSON.parse(await SEA.decrypt(encryptedData.s_pair, password));
3172
+ const viewingAccount = gunToEthAccount(viewingKeyPair.priv);
3173
+ const spendingAccount = gunToEthAccount(spendingKeyPair.priv);
3174
+
3175
+ // Publish only public keys
3176
+ gun.get("gun-eth").get("users").get(address).get("publicKeys").put({
3177
+ viewingPublicKey: viewingAccount.publicKey,
3178
+ spendingPublicKey: spendingAccount.publicKey
3179
+ });
3180
+ console.log("Stealth public keys published successfully");
3181
+ } catch (error) {
3182
+ console.error("Error publishing stealth keys:", error);
3183
+ throw error;
3184
+ }
3185
+ };
3186
+
3187
+ // =============================================
3188
+ // STEALTH PAYMENT FUNCTIONS
3189
+ // =============================================
3190
+ /**
3191
+ * Recover funds from a stealth address
3192
+ * @param {string} stealthAddress - The stealth address to recover funds from
3193
+ * @param {string} senderPublicKey - The sender's public key used to generate the address
3194
+ * @param {string} signature - The signature to decrypt private keys
3195
+ * @returns {Promise<Object>} Object containing wallet to access funds
3196
+ */
3197
+ Gun$1.chain.recoverStealthFunds = async function (stealthAddress, senderPublicKey, signature, spendingPublicKey) {
3198
+ try {
3199
+ const gun = this;
3200
+ const password = generatePassword(signature);
3201
+
3202
+ // Get own key pairs
3203
+ const myAddress = await this.verifySignature(MESSAGE_TO_SIGN, signature);
3204
+ const encryptedData = await gun.get("gun-eth").get("users").get(myAddress).then();
3205
+ if (!encryptedData || !encryptedData.v_pair || !encryptedData.s_pair) {
3206
+ throw new Error("Keys not found");
3207
+ }
3208
+
3209
+ // Decrypt viewing and spending pairs
3210
+ let viewingKeyPair;
3211
+ try {
3212
+ const decryptedViewingData = await SEA.decrypt(encryptedData.v_pair, password);
3213
+ viewingKeyPair = typeof decryptedViewingData === 'string' ? JSON.parse(decryptedViewingData) : decryptedViewingData;
3214
+ } catch (error) {
3215
+ console.error("Error decrypting keys:", error);
3216
+ throw new Error("Unable to decrypt keys");
3217
+ }
3218
+
3219
+ // Generate shared secret using SEA ECDH
3220
+ const sharedSecret = await SEA.secret(senderPublicKey, viewingKeyPair);
3221
+ if (!sharedSecret) {
3222
+ throw new Error("Unable to generate shared secret");
3223
+ }
3224
+ console.log("Recover shared secret:", sharedSecret);
3225
+ const {
3226
+ wallet,
3227
+ stealthAddress: recoveredAddress
3228
+ } = deriveStealthAddress(sharedSecret, spendingPublicKey);
3229
+
3230
+ // Verify address matches
3231
+ if (recoveredAddress.toLowerCase() !== stealthAddress.toLowerCase()) {
3232
+ console.error("Mismatch:", {
3233
+ recovered: recoveredAddress,
3234
+ expected: stealthAddress,
3235
+ sharedSecret
3236
+ });
3237
+ throw new Error("Recovered stealth address does not match");
3238
+ }
3239
+ return {
3240
+ wallet,
3241
+ address: recoveredAddress
3242
+ };
3243
+ } catch (error) {
3244
+ console.error("Error recovering stealth funds:", error);
3245
+ throw error;
3246
+ }
3247
+ };
3248
+
3249
+ /**
3250
+ * Announce a stealth payment
3251
+ * @param {string} stealthAddress - The generated stealth address
3252
+ * @param {string} senderPublicKey - The sender's public key
3253
+ * @param {string} spendingPublicKey - The spending public key
3254
+ * @param {string} signature - The sender's signature
3255
+ * @returns {Promise<void>}
3256
+ */
3257
+ Gun$1.chain.announceStealthPayment = async function (stealthAddress, senderPublicKey, spendingPublicKey, signature) {
3258
+ let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
3259
+ onChain: false,
3260
+ chain: 'optimismSepolia'
3261
+ };
3262
+ try {
3263
+ const gun = this;
3264
+ const senderAddress = await this.verifySignature(MESSAGE_TO_SIGN, signature);
3265
+ if (options.onChain) {
3266
+ // On-chain announcement
3267
+ const signer = await getSigner();
3268
+ const chainConfig = getAddressesForChain(options.chain);
3269
+ const contractAddress = chainConfig.STEALTH_ANNOUNCER_ADDRESS;
3270
+ console.log("Using contract address:", contractAddress);
3271
+ const contract = new ethers.ethers.Contract(contractAddress, STEALTH_ANNOUNCER_ABI, signer);
3272
+
3273
+ // Get dev fee from contract
3274
+ const devFee = await contract.devFee();
3275
+ console.log("Dev fee:", devFee.toString());
3276
+
3277
+ // Call contract
3278
+ const tx = await contract.announcePayment(senderPublicKey, spendingPublicKey, stealthAddress, {
3279
+ value: devFee
3280
+ });
3281
+ console.log("Transaction sent:", tx.hash);
3282
+ const receipt = await tx.wait();
3283
+ console.log("Transaction confirmed:", receipt.hash);
3284
+ console.log("Stealth payment announced on-chain (dev fee paid)");
3285
+ } else {
3286
+ // Off-chain announcement (GunDB)
3287
+ gun.get("gun-eth").get("stealth-payments").set({
3288
+ stealthAddress,
3289
+ senderAddress,
3290
+ senderPublicKey,
3291
+ spendingPublicKey,
3292
+ timestamp: Date.now()
3293
+ });
3294
+ console.log("Stealth payment announced off-chain");
3295
+ }
3296
+ } catch (error) {
3297
+ console.error("Error announcing stealth payment:", error);
3298
+ console.error("Error details:", error.stack);
3299
+ throw error;
3300
+ }
3301
+ };
3302
+
3303
+ /**
3304
+ * Get all stealth payments for an address
3305
+ * @param {string} signature - The signature to authenticate the user
3306
+ * @returns {Promise<Array>} List of stealth payments
3307
+ */
3308
+ Gun$1.chain.getStealthPayments = async function (signature) {
3309
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
3310
+ source: 'both',
3311
+ chain: 'optimismSepolia'
3312
+ };
3313
+ try {
3314
+ const payments = [];
3315
+ if (options.source === 'onChain' || options.source === 'both') {
3316
+ const signer = await getSigner();
3317
+ const chainConfig = getAddressesForChain(options.chain);
3318
+ const contractAddress = chainConfig.STEALTH_ANNOUNCER_ADDRESS;
3319
+ const contract = new ethers.ethers.Contract(contractAddress, STEALTH_ANNOUNCER_ABI, signer);
3320
+ try {
3321
+ // Get total number of announcements
3322
+ const totalAnnouncements = await contract.getAnnouncementsCount();
3323
+ const totalCount = Number(totalAnnouncements.toString());
3324
+ console.log("Total on-chain announcements:", totalCount);
3325
+ if (totalCount > 0) {
3326
+ // Get announcements in batches of 100
3327
+ const batchSize = 100;
3328
+ const lastIndex = totalCount - 1;
3329
+ for (let i = 0; i <= lastIndex; i += batchSize) {
3330
+ const toIndex = Math.min(i + batchSize - 1, lastIndex);
3331
+ const batch = await contract.getAnnouncementsInRange(i, toIndex);
3332
+
3333
+ // For each announcement, try to decrypt
3334
+ for (const announcement of batch) {
3335
+ try {
3336
+ // Verify announcement is valid
3337
+ if (!announcement || !announcement.stealthAddress || !announcement.senderPublicKey || !announcement.spendingPublicKey) {
3338
+ console.log("Invalid announcement:", announcement);
3339
+ continue;
3340
+ }
3341
+
3342
+ // Try to recover funds to verify if announcement is for us
3343
+ const recoveredWallet = await this.recoverStealthFunds(announcement.stealthAddress, announcement.senderPublicKey, signature, announcement.spendingPublicKey);
3344
+
3345
+ // If no errors thrown, announcement is for us
3346
+ payments.push({
3347
+ stealthAddress: announcement.stealthAddress,
3348
+ senderPublicKey: announcement.senderPublicKey,
3349
+ spendingPublicKey: announcement.spendingPublicKey,
3350
+ timestamp: Number(announcement.timestamp),
3351
+ source: 'onChain',
3352
+ wallet: recoveredWallet
3353
+ });
3354
+ } catch (e) {
3355
+ // Not for us, continue
3356
+ console.log(`Announcement not for us: ${announcement.stealthAddress}`);
3357
+ continue;
3358
+ }
3359
+ }
3360
+ }
3361
+ }
3362
+ } catch (error) {
3363
+ console.error("Error retrieving on-chain announcements:", error);
3364
+ }
3365
+ }
3366
+ if (options.source === 'offChain' || options.source === 'both') {
3367
+ // Get off-chain payments
3368
+ const gun = this;
3369
+ const offChainPayments = await new Promise(resolve => {
3370
+ const p = [];
3371
+ gun.get("gun-eth").get("stealth-payments").get(recipientAddress).map().once((payment, id) => {
3372
+ if (payment !== null && payment !== void 0 && payment.stealthAddress) {
3373
+ p.push({
3374
+ ...payment,
3375
+ id,
3376
+ source: 'offChain'
3377
+ });
3378
+ }
3379
+ });
3380
+ setTimeout(() => resolve(p), 2000);
3381
+ });
3382
+ payments.push(...offChainPayments);
3383
+ }
3384
+ console.log(`Found ${payments.length} stealth payments`);
3385
+ return payments;
3386
+ } catch (error) {
3387
+ console.error("Error retrieving stealth payments:", error);
3388
+ throw error;
3389
+ }
3390
+ };
3391
+
3392
+ /**
3393
+ * Clean up old stealth payments
3394
+ * @param {string} recipientAddress - The recipient's address
3395
+ * @returns {Promise<void>}
3396
+ */
3397
+ Gun$1.chain.cleanStealthPayments = async function (recipientAddress) {
3398
+ try {
3399
+ const gun = this;
3400
+ const payments = await gun.get("gun-eth").get("stealth-payments").get(recipientAddress).map().once().then();
3401
+
3402
+ // Remove empty or invalid nodes
3403
+ if (payments) {
3404
+ Object.keys(payments).forEach(async key => {
3405
+ const payment = payments[key];
3406
+ if (!payment || !payment.stealthAddress || !payment.senderPublicKey || !payment.spendingPublicKey) {
3407
+ await gun.get("gun-eth").get("stealth-payments").get(recipientAddress).get(key).put(null);
3408
+ }
3409
+ });
3410
+ }
3411
+ } catch (error) {
3412
+ console.error("Error cleaning stealth payments:", error);
3413
+ }
3414
+ };
3415
+
3416
+ // =============================================
3417
+ // EXPORTS
3418
+ // =============================================
3419
+
3420
+ // Modifica la classe GunEth e la sua esportazione
3421
+ class GunEth {
3422
+ static async init() {
3423
+ await initialize();
3424
+ return this;
3425
+ }
3426
+
3427
+ // Static utility methods
3428
+ static generateRandomId = (() => generateRandomId)();
3429
+ static generatePassword = (() => generatePassword)();
3430
+ static gunToEthAccount = (() => gunToEthAccount)();
3431
+ static getSigner = (() => getSigner)();
3432
+ static deriveStealthAddress = (() => deriveStealthAddress)();
3433
+
3434
+ // Chain methods
3435
+ static chainMethods = (() => ({
3436
+ setSigner: Gun$1.chain.setSigner,
3437
+ getSigner: Gun$1.chain.getSigner,
3438
+ verifySignature: Gun$1.chain.verifySignature,
3439
+ generatePassword: Gun$1.chain.generatePassword,
3440
+ createSignature: Gun$1.chain.createSignature,
3441
+ createAndStoreEncryptedPair: Gun$1.chain.createAndStoreEncryptedPair,
3442
+ getAndDecryptPair: Gun$1.chain.getAndDecryptPair,
3443
+ proof: Gun$1.chain.proof,
3444
+ gunToEthAccount: Gun$1.chain.gunToEthAccount,
3445
+ generateStealthAddress: Gun$1.chain.generateStealthAddress,
3446
+ publishStealthKeys: Gun$1.chain.publishStealthKeys,
3447
+ recoverStealthFunds: Gun$1.chain.recoverStealthFunds,
3448
+ announceStealthPayment: Gun$1.chain.announceStealthPayment,
3449
+ getStealthPayments: Gun$1.chain.getStealthPayments,
3450
+ cleanStealthPayments: Gun$1.chain.cleanStealthPayments
3451
+ }))();
3452
+
3453
+ // Constants
3454
+ static MESSAGE_TO_SIGN = (() => MESSAGE_TO_SIGN)();
3455
+ static PROOF_CONTRACT_ADDRESS = (() => PROOF_CONTRACT_ADDRESS)();
3456
+ static LOCAL_CONFIG = (() => LOCAL_CONFIG)();
3457
+ }
3458
+
3459
+ exports.GunEth = GunEth;
3460
+ exports.MESSAGE_TO_SIGN = MESSAGE_TO_SIGN;
3461
+ exports.default = GunEth;
3462
+ exports.deriveStealthAddress = deriveStealthAddress;
3463
+ exports.generatePassword = generatePassword;
3464
+ exports.generateRandomId = generateRandomId;
3465
+ exports.getSigner = getSigner;
3466
+ exports.gunToEthAccount = gunToEthAccount;
3467
+ exports.initialize = initialize;