@leofcoin/peernet 0.11.6 → 0.11.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import '@vandeurenglenn/debug';
1
2
  import LeofcoinStorage from '@leofcoin/storage';
2
3
  import protons from 'protons';
3
4
  import bs32 from '@vandeurenglenn/base32';
@@ -264,6 +265,7 @@ class Peer {
264
265
  #senderMap = new Map()
265
266
  #iceCompleteTimer
266
267
  #channel
268
+ #peerId
267
269
 
268
270
  get connection() {
269
271
  return this.#connection
@@ -290,12 +292,17 @@ constructor(options = {}) {
290
292
  down: 0
291
293
  };
292
294
 
293
- this.channelName = options.channelName || Buffer.from(Math.random().toString(36).slice(-12)).toString('hex');
294
- console.log(this.channelName);
295
+ this.channelName = options.channelName;
296
+
297
+ this.#peerId = options.peerId;
295
298
  this.options = options;
296
299
  this.#init();
297
300
  }
298
301
 
302
+ get peerId() {
303
+ return this.#peerId
304
+ }
305
+
299
306
  set socketClient(value) {
300
307
  // this.socketClient?.pubsub.unsubscribe('signal', this._in)
301
308
  this._socketClient = value;
@@ -307,28 +314,29 @@ constructor(options = {}) {
307
314
  }
308
315
 
309
316
  send(message) {
310
- this.bw.up += message.length;
317
+ this.bw.up += message.length || message.byteLength;
311
318
  this.channel.send(message);
312
319
  }
313
320
 
314
321
  request(data) {
315
322
  return new Promise((resolve, reject) => {
316
323
  const id = Math.random().toString(36).slice(-12);
324
+ // TODO: get rid of JSON
317
325
  data = new TextEncoder().encode(JSON.stringify({id, data}));
318
326
  const _onData = message => {
319
327
  message = JSON.parse(new TextDecoder().decode(message.data));
320
328
  if (message.id === id) {
321
329
  resolve(message.data);
322
- pubsub.unsubscribe('peer:data', _onData);
330
+ pubsub.unsubscribe(`peer:data`, _onData);
323
331
  }
324
332
  };
325
333
 
326
- pubsub.subscribe('peer:data', _onData);
334
+ pubsub.subscribe(`peer:data`, _onData);
327
335
 
328
336
  // cleanup subscriptions
329
- setTimeout(() => {
330
- pubsub.unsubscribe('peer:data', _onData);
331
- }, 5000);
337
+ // setTimeout(() => {
338
+ // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
339
+ // }, 5000);
332
340
 
333
341
  this.send(data);
334
342
  });
@@ -357,12 +365,13 @@ constructor(options = {}) {
357
365
  this.#connected = true;
358
366
  pubsub.publish('peer:connected', this);
359
367
  };
360
- message.channel.onclose = () => console.log('close');
368
+ message.channel.onclose = () => this.close.bind(this);
369
+
361
370
  message.channel.onmessage = (message) => {
362
- if (message.to) {
363
- if (message.to === this.id) pubsub.publish('peer:data', message);
364
- } else pubsub.publish('peer:data', message);
365
- this.bw.down += message.length;
371
+ pubsub.publish('peer:data', message);
372
+ debug(`incoming message from ${this.id}`);
373
+ debug(message);
374
+ this.bw.down += message.length || message.byteLength;
366
375
  };
367
376
  this.channel = message.channel;
368
377
  };
@@ -377,10 +386,10 @@ constructor(options = {}) {
377
386
  this.channel.onclose = () => this.close.bind(this);
378
387
 
379
388
  this.channel.onmessage = (message) => {
380
- if (message.to) {
381
- if (message.to === this.id) pubsub.publish('peer:data', message);
382
- } else pubsub.publish('peer:data', message);
383
- this.bw.down += message.length;
389
+ pubsub.publish('peer:data', message);
390
+ debug(`incoming message from ${this.peerId}`);
391
+ debug(message);
392
+ this.bw.down += message.length || message.byteLength;
384
393
  };
385
394
 
386
395
  const offer = await this.#connection.createOffer();
@@ -410,6 +419,8 @@ constructor(options = {}) {
410
419
  // if (data.videocall) return this._startStream(true, false); // start video and audio stream
411
420
  // if (data.call) return this._startStream(true, true); // start audio stream
412
421
  if (message.candidate) {
422
+ debug(`incoming candidate ${this.channelName}`);
423
+ debug(message.candidate.candidate);
413
424
  this.remoteAddress = message.candidate.address;
414
425
  this.remotePort = message.candidate.port;
415
426
  this.remoteProtocol = message.candidate.protocol;
@@ -419,12 +430,14 @@ constructor(options = {}) {
419
430
  try {
420
431
  if (message.sdp) {
421
432
  if (message.sdp.type === 'offer') {
433
+ debug(`incoming offer ${this.channelName}`);
422
434
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
423
435
  const answer = await this.#connection.createAnswer();
424
436
  await this.#connection.setLocalDescription(answer);
425
437
  this._sendMessage({'sdp': this.#connection.localDescription});
426
438
  }
427
439
  if (message.sdp.type === 'answer') {
440
+ debug(`incoming answer ${this.channelName}`);
428
441
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
429
442
  }
430
443
  }
@@ -434,6 +447,7 @@ constructor(options = {}) {
434
447
  }
435
448
 
436
449
  close() {
450
+ debug(`closing ${this.peerId}`);
437
451
  this.channel?.close();
438
452
  this.#connection?.close();
439
453
 
@@ -464,7 +478,6 @@ class Client {
464
478
  async _init(identifiers, stars = []) {
465
479
  if (stars.length === 0) {
466
480
  stars.push('wss://star.leofcoin.org');
467
- stars.push('ws://localhost:44444');
468
481
  }
469
482
  this.identifiers = identifiers;
470
483
  this.starsConfig = stars;
@@ -483,17 +496,9 @@ class Client {
483
496
  }
484
497
  const peers = await this.socketClient.peernet.join({id: this.id});
485
498
  for (const id of peers) {
486
- if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
499
+ if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
487
500
  }
488
501
  this.setupListeners();
489
-
490
- pubsub.subscribe('peer:connected', (peer) => {
491
- // peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
492
- console.log({peer: peer.to});
493
- console.log({id: peer.id});
494
- console.log({id: this.id});
495
- });
496
- // pubsub.subscribe('peer:data', (data) => console.log({data}))
497
502
  }
498
503
 
499
504
  setupListeners() {
@@ -542,7 +547,7 @@ class Client {
542
547
  delete this.#connections[id];
543
548
  }
544
549
  // reconnect
545
- if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
550
+ if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
546
551
  }
547
552
 
548
553
  }
@@ -552,10 +557,7 @@ class Client {
552
557
  }
553
558
  }
554
559
  }
555
-
556
- console.log(`star ${id} left`);
557
-
558
-
560
+ debug(`star ${id} left`);
559
561
  }
560
562
 
561
563
  peerLeft(id) {
@@ -563,7 +565,7 @@ class Client {
563
565
  this.#connections[id].close();
564
566
  delete this.#connections[id];
565
567
  }
566
- console.log(`peer ${id} left`);
568
+ debug(`peer ${id} left`);
567
569
  }
568
570
 
569
571
  peerJoined(id, signal) {
@@ -572,8 +574,8 @@ class Client {
572
574
  delete this.#connections[id];
573
575
  }
574
576
  // RTCPeerConnection
575
- this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
576
- console.log(`peer ${id} joined`);
577
+ this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
578
+ debug(`peer ${id} joined`);
577
579
  }
578
580
 
579
581
 
@@ -630,7 +632,7 @@ var codecs = {
630
632
  // just a hash
631
633
  'disco-hash': {
632
634
  codec: parseInt('30', 16),
633
- hashAlg: 'dbl-keccak-512', // ,
635
+ hashAlg: 'dbl-keccak-256', // ,
634
636
  // testnet: 'olivia'
635
637
  },
636
638
  'peernet-peer-response': {
@@ -661,7 +663,7 @@ var codecs = {
661
663
  // message
662
664
  'peernet-message': {
663
665
  codec: parseInt('706d65', 16),
664
- hashAlg: 'keccak-512',
666
+ hashAlg: 'keccak-256',
665
667
  },
666
668
  // pubsub
667
669
  'peernet-ps': {
@@ -702,7 +704,7 @@ var codecs = {
702
704
  // chat message
703
705
  'chat-message': {
704
706
  codec: parseInt('636d', 16),
705
- hashAlg: 'dbl-keccak-512',
707
+ hashAlg: 'dbl-keccak-256',
706
708
  },
707
709
  };
708
710
 
@@ -831,7 +833,7 @@ class PeernetHash {
831
833
  else this.name = 'disco-hash';
832
834
  if (options.codecs) this.codecs = options.codecs;
833
835
  if (buffer) {
834
- if (Buffer.isBuffer(buffer)) {
836
+ if (buffer instanceof Uint8Array) {
835
837
  this.discoCodec = new PeernetCodec(buffer, this.codecs);
836
838
  const name = this.discoCodec.name;
837
839
 
@@ -855,13 +857,9 @@ class PeernetHash {
855
857
  get prefix() {
856
858
  const length = this.length;
857
859
  const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
858
- for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
859
- uint8Array[i] = this.discoCodec.codecBuffer[i];
860
- }
860
+ uint8Array.set(length);
861
+ uint8Array.set(this.discoCodec.codecBuffer, length.length);
861
862
 
862
- for (let i = uint8Array.length - 1; i < length.length; i++) {
863
- uint8Array[i] = length[i];
864
- }
865
863
  return uint8Array
866
864
  }
867
865
 
@@ -920,10 +918,11 @@ class PeernetHash {
920
918
 
921
919
  this.codec = this.discoCodec.encode();
922
920
  this.codec = this.discoCodec.codecBuffer;
923
- this.hash = Buffer.concat([
924
- this.prefix,
925
- this.digest,
926
- ]);
921
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
922
+ uint8Array.set(this.prefix);
923
+ uint8Array.set(this.digest, this.prefix.length);
924
+
925
+ this.hash = uint8Array;
927
926
 
928
927
  return this.hash
929
928
  }
@@ -1374,7 +1373,7 @@ class ChatMessage extends FormatInterface {
1374
1373
  }
1375
1374
  }
1376
1375
 
1377
- const debug = (log) => {
1376
+ const debug$1 = (log) => {
1378
1377
  if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
1379
1378
  };
1380
1379
 
@@ -2037,7 +2036,7 @@ class MessageHandler {
2037
2036
  identity = JSON.parse(new TextDecoder().decode(identity));
2038
2037
  const wallet = new MultiWallet(this.network);
2039
2038
  wallet.import(identity.multiWIF);
2040
- return wallet.sign(hasher.hash.slice(0, 32))
2039
+ return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
2041
2040
  }
2042
2041
 
2043
2042
  /**
@@ -2311,9 +2310,9 @@ class Peernet {
2311
2310
  * @param {PeernetPeer} peer - peernet peer
2312
2311
  */
2313
2312
  async _protoHandler(message, peer, from) {
2313
+
2314
2314
  const {id, proto} = message;
2315
2315
  this.bw.down += proto.encoded.length;
2316
-
2317
2316
  if (proto.name === 'peernet-dht') {
2318
2317
  let { hash, store } = proto.decoded;
2319
2318
  let has;
@@ -2361,8 +2360,7 @@ class Peernet {
2361
2360
 
2362
2361
  this.bw.up += node.encoded.length;
2363
2362
  }
2364
- } else if (proto.name === 'peernet-ps' &&
2365
- this._getPeerId(peer.id) !== this.id.toString()) {
2363
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
2366
2364
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
2367
2365
  }
2368
2366
  // }
@@ -2377,17 +2375,14 @@ class Peernet {
2377
2375
  if (!hash) throw new Error('hash expected, received undefined')
2378
2376
  const data = new DHTMessage({hash});
2379
2377
  this.client.id;
2380
- for (const peer of this.connections) {
2381
- const node = await this.prepareMessage(peer.id, data.encoded);
2382
-
2383
- const result = await peer.request(node.encoded);
2384
-
2385
- let proto = protoFor(result.data);
2386
-
2378
+ const walk = async peer => {
2379
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
2380
+ let result = await peer.request(node.encoded);
2381
+ result = new Uint8Array(Object.values(result));
2382
+ let proto = protoFor(result);
2387
2383
  if (proto.name !== 'peernet-message') throw encapsulatedError()
2388
2384
  const from = proto.decoded.from;
2389
2385
  proto = protoFor(proto.decoded.data);
2390
-
2391
2386
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
2392
2387
 
2393
2388
  // TODO: give ip and port (just used for location)
@@ -2405,8 +2400,14 @@ class Peernet {
2405
2400
  };
2406
2401
 
2407
2402
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
2403
+ };
2404
+ let walks = [];
2405
+ for (const peer of this.connections) {
2406
+ if (peer.peerId !== this.id) {
2407
+ walks.push(walk(peer));
2408
+ }
2408
2409
  }
2409
- return
2410
+ return Promise.all(walks)
2410
2411
  }
2411
2412
 
2412
2413
  /**
@@ -2468,34 +2469,33 @@ class Peernet {
2468
2469
  async requestData(hash, store) {
2469
2470
  const providers = await this.providersFor(hash);
2470
2471
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
2471
- debug(`found ${providers.size} provider(s) for ${hash}`);
2472
+ debug$1(`found ${providers.size} provider(s) for ${hash}`);
2472
2473
  // get closest peer on earth
2473
2474
  const closestPeer = await this.dht.closestPeer(providers);
2474
2475
  // get peer instance by id
2475
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
2476
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
2476
2477
 
2477
- const id = closestPeer.id.toString();
2478
+ const id = closestPeer.id;
2478
2479
  if (this.connections) {
2479
2480
  let closest = this.connections.filter((peer) => {
2480
- if (peer.id === id) return peer
2481
+ if (peer.peerId === id) return peer
2481
2482
  });
2482
2483
 
2483
- let data = new DataMessage({hash, store: store.name ? store.name : store});
2484
+ let data = new DataMessage({hash, store: store?.name ? store?.name : store});
2484
2485
 
2485
2486
  const node = await this.prepareMessage(id, data.encoded);
2486
2487
  if (closest[0]) data = await closest[0].request(node.encoded);
2487
2488
  else {
2488
2489
  closest = this.connections.filter((peer) => {
2489
- if (peer.id === id) return peer
2490
+ if (peer.peerId === id) return peer
2490
2491
  });
2491
2492
  if (closest[0]) data = await closest[0].request(node.encoded);
2492
2493
  }
2493
- if (data.data) {
2494
- console.log(data.data);
2495
- let proto = protoFor(data.data);
2496
- proto = protoFor(proto.decoded.data);
2497
- return proto.decoded.data
2498
- }
2494
+ data = new Uint8Array(Object.values(data));
2495
+ let proto = protoFor(data);
2496
+ proto = protoFor(proto.decoded.data);
2497
+ // TODO: store data automaticly or not
2498
+ return proto.decoded.data
2499
2499
 
2500
2500
  // this.put(hash, proto.decoded.data)
2501
2501
  }
@@ -2511,7 +2511,7 @@ class Peernet {
2511
2511
  * @param {String} hash
2512
2512
  */
2513
2513
  get: async (hash) => {
2514
- debug(`get message ${hash}`);
2514
+ debug$1(`get message ${hash}`);
2515
2515
  const message = await messageStore.has(hash);
2516
2516
  if (message) return await messageStore.get(hash)
2517
2517
  return this.requestData(hash, 'message')
@@ -2539,7 +2539,7 @@ class Peernet {
2539
2539
  * @param {String} hash
2540
2540
  */
2541
2541
  get: async (hash) => {
2542
- debug(`get data ${hash}`);
2542
+ debug$1(`get data ${hash}`);
2543
2543
  const data = await dataStore.has(hash);
2544
2544
  if (data) return await dataStore.get(hash)
2545
2545
  return this.requestData(hash, 'data')
@@ -2581,14 +2581,14 @@ class Peernet {
2581
2581
  * @param {String} store - storeName to access
2582
2582
  */
2583
2583
  async get(hash, store) {
2584
- debug(`get ${hash}`);
2584
+ debug$1(`get ${hash}`);
2585
2585
  let data;
2586
2586
  if (store) store = globalThis[`${store}Store`];
2587
2587
  if (!store) store = await this.whichStore([...this.stores], hash);
2588
2588
  if (store && await store.has(hash)) data = await store.get(hash);
2589
2589
  if (data) return data
2590
2590
 
2591
- return this.requestData(hash, store.name ? store.name : store)
2591
+ return this.requestData(hash, store?.name ? store.name : store)
2592
2592
  }
2593
2593
 
2594
2594
  /**
@@ -2629,8 +2629,8 @@ class Peernet {
2629
2629
  data = new PsMessage({data, topic});
2630
2630
  for (const peer of this.connections) {
2631
2631
  if (peer.connected) {
2632
- if (peer.id !== this.peerId) {
2633
- const node = await this.prepareMessage(peer.id, data.encoded);
2632
+ if (peer.peerId !== this.peerId) {
2633
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
2634
2634
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
2635
2635
  }
2636
2636
  } else {
@@ -2655,7 +2655,7 @@ class Peernet {
2655
2655
  }
2656
2656
 
2657
2657
  async removePeer(peer) {
2658
- delete this.client.connections[peer.id];
2658
+ delete this.client.connections[peer.peerId];
2659
2659
  }
2660
2660
 
2661
2661
  get Buffer() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.6",
3
+ "version": "0.11.9",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "@leofcoin/generate-account": "^1.0.2",
27
27
  "@leofcoin/multi-wallet": "^2.1.2",
28
- "@leofcoin/peernet-swarm": "^0.1.9",
28
+ "@leofcoin/peernet-swarm": "^0.1.16",
29
29
  "@leofcoin/storage": "^2.3.0",
30
30
  "@vandeurenglenn/base32": "^1.1.0",
31
31
  "@vandeurenglenn/base58": "^1.1.0",
@@ -46,6 +46,7 @@
46
46
  "@rollup/plugin-eslint": "^8.0.1",
47
47
  "@rollup/plugin-json": "^4.1.0",
48
48
  "@rollup/plugin-node-resolve": "^13.0.4",
49
+ "@vandeurenglenn/debug": "^1.0.0",
49
50
  "coveralls": "^3.1.1",
50
51
  "esdoc": "^1.1.0",
51
52
  "esdoc-ecmascript-proposal-plugin": "^1.0.0",
@@ -2,7 +2,7 @@ export default {
2
2
  // just a hash
3
3
  'disco-hash': {
4
4
  codec: parseInt('30', 16),
5
- hashAlg: 'dbl-keccak-512', // ,
5
+ hashAlg: 'dbl-keccak-256', // ,
6
6
  // testnet: 'olivia'
7
7
  },
8
8
  'peernet-peer-response': {
@@ -33,7 +33,7 @@ export default {
33
33
  // message
34
34
  'peernet-message': {
35
35
  codec: parseInt('706d65', 16),
36
- hashAlg: 'keccak-512',
36
+ hashAlg: 'keccak-256',
37
37
  },
38
38
  // pubsub
39
39
  'peernet-ps': {
@@ -74,6 +74,6 @@ export default {
74
74
  // chat message
75
75
  'chat-message': {
76
76
  codec: parseInt('636d', 16),
77
- hashAlg: 'dbl-keccak-512',
77
+ hashAlg: 'dbl-keccak-256',
78
78
  },
79
79
  }
@@ -22,7 +22,7 @@ export default class MessageHandler {
22
22
  identity = JSON.parse(new TextDecoder().decode(identity))
23
23
  const wallet = new MultiWallet(this.network)
24
24
  wallet.import(identity.multiWIF)
25
- return wallet.sign(hasher.hash.slice(0, 32))
25
+ return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
26
26
  }
27
27
 
28
28
  /**
package/src/hash/hash.js CHANGED
@@ -11,7 +11,7 @@ export default class PeernetHash {
11
11
  else this.name = 'disco-hash'
12
12
  if (options.codecs) this.codecs = options.codecs
13
13
  if (buffer) {
14
- if (Buffer.isBuffer(buffer)) {
14
+ if (buffer instanceof Uint8Array) {
15
15
  this.discoCodec = new Codec(buffer, this.codecs)
16
16
  const name = this.discoCodec.name
17
17
 
@@ -35,13 +35,9 @@ export default class PeernetHash {
35
35
  get prefix() {
36
36
  const length = this.length
37
37
  const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
38
- for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
39
- uint8Array[i] = this.discoCodec.codecBuffer[i]
40
- }
38
+ uint8Array.set(length)
39
+ uint8Array.set(this.discoCodec.codecBuffer, length.length)
41
40
 
42
- for (let i = uint8Array.length - 1; i < length.length; i++) {
43
- uint8Array[i] = length[i]
44
- }
45
41
  return uint8Array
46
42
  }
47
43
 
@@ -100,10 +96,11 @@ export default class PeernetHash {
100
96
 
101
97
  this.codec = this.discoCodec.encode();
102
98
  this.codec = this.discoCodec.codecBuffer
103
- this.hash = Buffer.concat([
104
- this.prefix,
105
- this.digest,
106
- ])
99
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length)
100
+ uint8Array.set(this.prefix)
101
+ uint8Array.set(this.digest, this.prefix.length)
102
+
103
+ this.hash = uint8Array
107
104
 
108
105
  return this.hash
109
106
  }
package/src/peernet.js CHANGED
@@ -257,9 +257,9 @@ export default class Peernet {
257
257
  * @param {PeernetPeer} peer - peernet peer
258
258
  */
259
259
  async _protoHandler(message, peer, from) {
260
+
260
261
  const {id, proto} = message
261
262
  this.bw.down += proto.encoded.length
262
-
263
263
  if (proto.name === 'peernet-dht') {
264
264
  let { hash, store } = proto.decoded
265
265
  let has;
@@ -309,8 +309,7 @@ export default class Peernet {
309
309
 
310
310
  this.bw.up += node.encoded.length
311
311
  }
312
- } else if (proto.name === 'peernet-ps' &&
313
- this._getPeerId(peer.id) !== this.id.toString()) {
312
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
314
313
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString())
315
314
  }
316
315
  // }
@@ -325,17 +324,14 @@ export default class Peernet {
325
324
  if (!hash) throw new Error('hash expected, received undefined')
326
325
  const data = new DHTMessage({hash})
327
326
  const clientId = this.client.id
328
- for (const peer of this.connections) {
329
- const node = await this.prepareMessage(peer.id, data.encoded)
330
-
331
- const result = await peer.request(node.encoded)
332
-
333
- let proto = protoFor(result.data)
334
-
327
+ const walk = async peer => {
328
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
329
+ let result = await peer.request(node.encoded)
330
+ result = new Uint8Array(Object.values(result))
331
+ let proto = protoFor(result)
335
332
  if (proto.name !== 'peernet-message') throw encapsulatedError()
336
333
  const from = proto.decoded.from
337
334
  proto = protoFor(proto.decoded.data)
338
-
339
335
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
340
336
 
341
337
  // TODO: give ip and port (just used for location)
@@ -354,7 +350,13 @@ export default class Peernet {
354
350
 
355
351
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
356
352
  }
357
- return
353
+ let walks = []
354
+ for (const peer of this.connections) {
355
+ if (peer.peerId !== this.id) {
356
+ walks.push(walk(peer))
357
+ }
358
+ }
359
+ return Promise.all(walks)
358
360
  }
359
361
 
360
362
  /**
@@ -420,30 +422,29 @@ export default class Peernet {
420
422
  // get closest peer on earth
421
423
  const closestPeer = await this.dht.closestPeer(providers)
422
424
  // get peer instance by id
423
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
425
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
424
426
 
425
- const id = closestPeer.id.toString()
427
+ const id = closestPeer.id
426
428
  if (this.connections) {
427
429
  let closest = this.connections.filter((peer) => {
428
- if (peer.id === id) return peer
430
+ if (peer.peerId === id) return peer
429
431
  })
430
432
 
431
- let data = new DataMessage({hash, store: store.name ? store.name : store});
433
+ let data = new DataMessage({hash, store: store?.name ? store?.name : store});
432
434
 
433
435
  const node = await this.prepareMessage(id, data.encoded)
434
436
  if (closest[0]) data = await closest[0].request(node.encoded)
435
437
  else {
436
438
  closest = this.connections.filter((peer) => {
437
- if (peer.id === id) return peer
439
+ if (peer.peerId === id) return peer
438
440
  })
439
441
  if (closest[0]) data = await closest[0].request(node.encoded)
440
442
  }
441
- if (data.data) {
442
- console.log(data.data);
443
- let proto = protoFor(data.data)
444
- proto = protoFor(proto.decoded.data)
445
- return proto.decoded.data
446
- }
443
+ data = new Uint8Array(Object.values(data))
444
+ let proto = protoFor(data)
445
+ proto = protoFor(proto.decoded.data)
446
+ // TODO: store data automaticly or not
447
+ return proto.decoded.data
447
448
 
448
449
  // this.put(hash, proto.decoded.data)
449
450
  }
@@ -537,7 +538,7 @@ export default class Peernet {
537
538
  if (store && await store.has(hash)) data = await store.get(hash)
538
539
  if (data) return data
539
540
 
540
- return this.requestData(hash, store.name ? store.name : store)
541
+ return this.requestData(hash, store?.name ? store.name : store)
541
542
  }
542
543
 
543
544
  /**
@@ -578,8 +579,8 @@ export default class Peernet {
578
579
  data = new PsMessage({data, topic})
579
580
  for (const peer of this.connections) {
580
581
  if (peer.connected) {
581
- if (peer.id !== this.peerId) {
582
- const node = await this.prepareMessage(peer.id, data.encoded)
582
+ if (peer.peerId !== this.peerId) {
583
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
583
584
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
584
585
  }
585
586
  } else {
@@ -604,7 +605,7 @@ export default class Peernet {
604
605
  }
605
606
 
606
607
  async removePeer(peer) {
607
- delete this.client.connections[peer.id]
608
+ delete this.client.connections[peer.peerId]
608
609
  }
609
610
 
610
611
  get Buffer() {