@leofcoin/peernet 0.14.2 → 0.14.3

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.
@@ -358,6 +358,7 @@ function _interopNamespace(e) {
358
358
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
359
359
 
360
360
  const protoFor = (data) => {
361
+ console.log(data);
361
362
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
362
363
  const codec = new codecFormatInterface.Codec(data);
363
364
  if (!codec.name) throw new Error('proto not found')
@@ -708,10 +709,9 @@ class MessageHandler {
708
709
  * @param {Buffer} message.to peer id
709
710
  * @param {string} message.data Peernet message
710
711
  * (PeernetMessage excluded) encoded as a string
711
- * @return signature
712
+ * @return message
712
713
  */
713
714
  async hashAndSignMessage(message) {
714
- const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
715
715
  let identity = await walletStore.get('identity');
716
716
  identity = JSON.parse(identity);
717
717
  if (!globalThis.MultiWallet) {
@@ -720,7 +720,8 @@ class MessageHandler {
720
720
  }
721
721
  const wallet = new MultiWallet(this.network);
722
722
  wallet.recover(identity.mnemonic);
723
- return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
723
+ message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
724
+ return message
724
725
  }
725
726
 
726
727
  /**
@@ -729,21 +730,12 @@ class MessageHandler {
729
730
  * @param {String|PeernetMessage} data - data encoded message string
730
731
  * or the messageNode itself
731
732
  */
732
- async prepareMessage(from, to, data, id) {
733
- if (data.encoded) data = data.encoded;
734
-
735
- const message = {
736
- from,
737
- to,
738
- data,
739
- };
740
- const signature = await this.hashAndSignMessage(message);
741
- const node = await new globalThis.peernet.protos['peernet-message']({
742
- ...message,
743
- signature,
744
- });
733
+ async prepareMessage(message) {
734
+ if (message.keys.indexOf('signature') !== -1) {
735
+ message = await this.hashAndSignMessage(message);
736
+ }
745
737
 
746
- return node
738
+ return message
747
739
  }
748
740
  }
749
741
 
@@ -751,8 +743,10 @@ const dataHandler = async message => {
751
743
  if (!message) return
752
744
 
753
745
  const {data, id} = message;
754
-
746
+ console.log({data});
755
747
  message = await protoFor(data);
748
+ console.log(message.decoded.data);
749
+ console.log({message});
756
750
  const proto = await protoFor(message.decoded.data);
757
751
  const from = message.decoded.from;
758
752
 
@@ -857,8 +851,8 @@ class Peernet {
857
851
  /**
858
852
  * @see MessageHandler
859
853
  */
860
- prepareMessage(to, data) {
861
- return this._messageHandler.prepareMessage(this.id, to, data)
854
+ prepareMessage(data) {
855
+ return this._messageHandler.prepareMessage(data)
862
856
  }
863
857
 
864
858
  /**
@@ -1054,7 +1048,7 @@ class Peernet {
1054
1048
  else has = await store.has(hash);
1055
1049
  }
1056
1050
  const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
1057
- const node = await this.prepareMessage(from, data.encoded);
1051
+ const node = await this.prepareMessage(data);
1058
1052
 
1059
1053
  this.sendMessage(peer, id, node.encoded);
1060
1054
  } else if (proto.name === 'peernet-data') {
@@ -1071,7 +1065,7 @@ class Peernet {
1071
1065
  if (data) {
1072
1066
  data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
1073
1067
 
1074
- const node = await this.prepareMessage(from, data.encoded);
1068
+ const node = await this.prepareMessage(data);
1075
1069
  this.sendMessage(peer, id, node.encoded);
1076
1070
  }
1077
1071
  }
@@ -1080,7 +1074,7 @@ class Peernet {
1080
1074
  const method = this.requestProtos[proto.decoded.request];
1081
1075
  if (method) {
1082
1076
  const data = await method();
1083
- const node = await this.prepareMessage(from, data.encoded);
1077
+ const node = await this.prepareMessage(data);
1084
1078
  this.sendMessage(peer, id, node.encoded);
1085
1079
  }
1086
1080
  } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
@@ -1099,7 +1093,7 @@ class Peernet {
1099
1093
  const data = await new globalThis.peernet.protos['peernet-dht']({hash});
1100
1094
  this.client.id;
1101
1095
  const walk = async peer => {
1102
- const node = await this.prepareMessage(peer.peerId, data.encoded);
1096
+ const node = await this.prepareMessage(data);
1103
1097
  let result = await peer.request(node.encoded);
1104
1098
  result = new Uint8Array(Object.values(result));
1105
1099
  let proto = await protoFor(result);
@@ -1206,7 +1200,7 @@ class Peernet {
1206
1200
 
1207
1201
  let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
1208
1202
 
1209
- const node = await this.prepareMessage(id, data.encoded);
1203
+ const node = await this.prepareMessage(data);
1210
1204
  if (closest[0]) data = await closest[0].request(node.encoded);
1211
1205
  else {
1212
1206
  closest = this.connections.filter((peer) => {
@@ -1332,6 +1326,8 @@ class Peernet {
1332
1326
  else data = await this.requestData(hash, 'data');
1333
1327
 
1334
1328
  const node = await new peernet.protos['peernet-file'](data);
1329
+ await node.decode();
1330
+ console.log(data);
1335
1331
  const paths = [];
1336
1332
  if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
1337
1333
  for (const {path, hash} of node.decoded.links) {
@@ -1423,7 +1419,7 @@ class Peernet {
1423
1419
  data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
1424
1420
  for (const peer of this.connections) {
1425
1421
  if (peer.peerId !== this.peerId) {
1426
- const node = await this.prepareMessage(peer.peerId, data.encoded);
1422
+ const node = await this.prepareMessage(data);
1427
1423
  this.sendMessage(peer, id, node.encoded);
1428
1424
  }
1429
1425
  // TODO: if peer subscribed
@@ -1989,12 +1985,17 @@ class FormatInterface$1 extends BasicInterface$1 {
1989
1985
  * @return {Buffer}
1990
1986
  */
1991
1987
  async encode(decoded) {
1988
+ let encoded;
1992
1989
  if (!decoded) decoded = this.decoded;
1993
1990
  const codec = new PeernetCodec(this.name);
1994
- const encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded);
1991
+
1992
+ if (decoded instanceof Uint8Array) encoded = decoded;
1993
+ else encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded);
1994
+
1995
1995
  const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
1996
1996
  uint8Array.set(codec.codecBuffer);
1997
1997
  uint8Array.set(encoded, codec.codecBuffer.length);
1998
+
1998
1999
  this.encoded = uint8Array;
1999
2000
  return this.encoded
2000
2001
  }
@@ -29,6 +29,7 @@ var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
29
29
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
30
30
 
31
31
  const protoFor = (data) => {
32
+ console.log(data);
32
33
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
33
34
  const codec = new codecFormatInterface.Codec(data);
34
35
  if (!codec.name) throw new Error('proto not found')
@@ -379,10 +380,9 @@ class MessageHandler {
379
380
  * @param {Buffer} message.to peer id
380
381
  * @param {string} message.data Peernet message
381
382
  * (PeernetMessage excluded) encoded as a string
382
- * @return signature
383
+ * @return message
383
384
  */
384
385
  async hashAndSignMessage(message) {
385
- const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
386
386
  let identity = await walletStore.get('identity');
387
387
  identity = JSON.parse(identity);
388
388
  if (!globalThis.MultiWallet) {
@@ -391,7 +391,8 @@ class MessageHandler {
391
391
  }
392
392
  const wallet = new MultiWallet(this.network);
393
393
  wallet.recover(identity.mnemonic);
394
- return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
394
+ message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
395
+ return message
395
396
  }
396
397
 
397
398
  /**
@@ -400,21 +401,12 @@ class MessageHandler {
400
401
  * @param {String|PeernetMessage} data - data encoded message string
401
402
  * or the messageNode itself
402
403
  */
403
- async prepareMessage(from, to, data, id) {
404
- if (data.encoded) data = data.encoded;
405
-
406
- const message = {
407
- from,
408
- to,
409
- data,
410
- };
411
- const signature = await this.hashAndSignMessage(message);
412
- const node = await new globalThis.peernet.protos['peernet-message']({
413
- ...message,
414
- signature,
415
- });
404
+ async prepareMessage(message) {
405
+ if (message.keys.indexOf('signature') !== -1) {
406
+ message = await this.hashAndSignMessage(message);
407
+ }
416
408
 
417
- return node
409
+ return message
418
410
  }
419
411
  }
420
412
 
@@ -422,8 +414,10 @@ const dataHandler = async message => {
422
414
  if (!message) return
423
415
 
424
416
  const {data, id} = message;
425
-
417
+ console.log({data});
426
418
  message = await protoFor(data);
419
+ console.log(message.decoded.data);
420
+ console.log({message});
427
421
  const proto = await protoFor(message.decoded.data);
428
422
  const from = message.decoded.from;
429
423
 
@@ -528,8 +522,8 @@ class Peernet {
528
522
  /**
529
523
  * @see MessageHandler
530
524
  */
531
- prepareMessage(to, data) {
532
- return this._messageHandler.prepareMessage(this.id, to, data)
525
+ prepareMessage(data) {
526
+ return this._messageHandler.prepareMessage(data)
533
527
  }
534
528
 
535
529
  /**
@@ -725,7 +719,7 @@ class Peernet {
725
719
  else has = await store.has(hash);
726
720
  }
727
721
  const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
728
- const node = await this.prepareMessage(from, data.encoded);
722
+ const node = await this.prepareMessage(data);
729
723
 
730
724
  this.sendMessage(peer, id, node.encoded);
731
725
  } else if (proto.name === 'peernet-data') {
@@ -742,7 +736,7 @@ class Peernet {
742
736
  if (data) {
743
737
  data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
744
738
 
745
- const node = await this.prepareMessage(from, data.encoded);
739
+ const node = await this.prepareMessage(data);
746
740
  this.sendMessage(peer, id, node.encoded);
747
741
  }
748
742
  }
@@ -751,7 +745,7 @@ class Peernet {
751
745
  const method = this.requestProtos[proto.decoded.request];
752
746
  if (method) {
753
747
  const data = await method();
754
- const node = await this.prepareMessage(from, data.encoded);
748
+ const node = await this.prepareMessage(data);
755
749
  this.sendMessage(peer, id, node.encoded);
756
750
  }
757
751
  } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
@@ -770,7 +764,7 @@ class Peernet {
770
764
  const data = await new globalThis.peernet.protos['peernet-dht']({hash});
771
765
  this.client.id;
772
766
  const walk = async peer => {
773
- const node = await this.prepareMessage(peer.peerId, data.encoded);
767
+ const node = await this.prepareMessage(data);
774
768
  let result = await peer.request(node.encoded);
775
769
  result = new Uint8Array(Object.values(result));
776
770
  let proto = await protoFor(result);
@@ -877,7 +871,7 @@ class Peernet {
877
871
 
878
872
  let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
879
873
 
880
- const node = await this.prepareMessage(id, data.encoded);
874
+ const node = await this.prepareMessage(data);
881
875
  if (closest[0]) data = await closest[0].request(node.encoded);
882
876
  else {
883
877
  closest = this.connections.filter((peer) => {
@@ -1003,6 +997,8 @@ class Peernet {
1003
997
  else data = await this.requestData(hash, 'data');
1004
998
 
1005
999
  const node = await new peernet.protos['peernet-file'](data);
1000
+ await node.decode();
1001
+ console.log(data);
1006
1002
  const paths = [];
1007
1003
  if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
1008
1004
  for (const {path, hash} of node.decoded.links) {
@@ -1094,7 +1090,7 @@ class Peernet {
1094
1090
  data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
1095
1091
  for (const peer of this.connections) {
1096
1092
  if (peer.peerId !== this.peerId) {
1097
- const node = await this.prepareMessage(peer.peerId, data.encoded);
1093
+ const node = await this.prepareMessage(data);
1098
1094
  this.sendMessage(peer, id, node.encoded);
1099
1095
  }
1100
1096
  // TODO: if peer subscribed
@@ -27,6 +27,7 @@ function _interopNamespace(e) {
27
27
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
28
28
 
29
29
  const protoFor = (data) => {
30
+ console.log(data);
30
31
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
31
32
  const codec = new codecFormatInterface.Codec(data);
32
33
  if (!codec.name) throw new Error('proto not found')
@@ -377,10 +378,9 @@ class MessageHandler {
377
378
  * @param {Buffer} message.to peer id
378
379
  * @param {string} message.data Peernet message
379
380
  * (PeernetMessage excluded) encoded as a string
380
- * @return signature
381
+ * @return message
381
382
  */
382
383
  async hashAndSignMessage(message) {
383
- const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
384
384
  let identity = await walletStore.get('identity');
385
385
  identity = JSON.parse(identity);
386
386
  if (!globalThis.MultiWallet) {
@@ -389,7 +389,8 @@ class MessageHandler {
389
389
  }
390
390
  const wallet = new MultiWallet(this.network);
391
391
  wallet.recover(identity.mnemonic);
392
- return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
392
+ message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
393
+ return message
393
394
  }
394
395
 
395
396
  /**
@@ -398,21 +399,12 @@ class MessageHandler {
398
399
  * @param {String|PeernetMessage} data - data encoded message string
399
400
  * or the messageNode itself
400
401
  */
401
- async prepareMessage(from, to, data, id) {
402
- if (data.encoded) data = data.encoded;
403
-
404
- const message = {
405
- from,
406
- to,
407
- data,
408
- };
409
- const signature = await this.hashAndSignMessage(message);
410
- const node = await new globalThis.peernet.protos['peernet-message']({
411
- ...message,
412
- signature,
413
- });
402
+ async prepareMessage(message) {
403
+ if (message.keys.indexOf('signature') !== -1) {
404
+ message = await this.hashAndSignMessage(message);
405
+ }
414
406
 
415
- return node
407
+ return message
416
408
  }
417
409
  }
418
410
 
@@ -420,8 +412,10 @@ const dataHandler = async message => {
420
412
  if (!message) return
421
413
 
422
414
  const {data, id} = message;
423
-
415
+ console.log({data});
424
416
  message = await protoFor(data);
417
+ console.log(message.decoded.data);
418
+ console.log({message});
425
419
  const proto = await protoFor(message.decoded.data);
426
420
  const from = message.decoded.from;
427
421
 
@@ -526,8 +520,8 @@ class Peernet {
526
520
  /**
527
521
  * @see MessageHandler
528
522
  */
529
- prepareMessage(to, data) {
530
- return this._messageHandler.prepareMessage(this.id, to, data)
523
+ prepareMessage(data) {
524
+ return this._messageHandler.prepareMessage(data)
531
525
  }
532
526
 
533
527
  /**
@@ -723,7 +717,7 @@ class Peernet {
723
717
  else has = await store.has(hash);
724
718
  }
725
719
  const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
726
- const node = await this.prepareMessage(from, data.encoded);
720
+ const node = await this.prepareMessage(data);
727
721
 
728
722
  this.sendMessage(peer, id, node.encoded);
729
723
  } else if (proto.name === 'peernet-data') {
@@ -740,7 +734,7 @@ class Peernet {
740
734
  if (data) {
741
735
  data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
742
736
 
743
- const node = await this.prepareMessage(from, data.encoded);
737
+ const node = await this.prepareMessage(data);
744
738
  this.sendMessage(peer, id, node.encoded);
745
739
  }
746
740
  }
@@ -749,7 +743,7 @@ class Peernet {
749
743
  const method = this.requestProtos[proto.decoded.request];
750
744
  if (method) {
751
745
  const data = await method();
752
- const node = await this.prepareMessage(from, data.encoded);
746
+ const node = await this.prepareMessage(data);
753
747
  this.sendMessage(peer, id, node.encoded);
754
748
  }
755
749
  } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
@@ -768,7 +762,7 @@ class Peernet {
768
762
  const data = await new globalThis.peernet.protos['peernet-dht']({hash});
769
763
  this.client.id;
770
764
  const walk = async peer => {
771
- const node = await this.prepareMessage(peer.peerId, data.encoded);
765
+ const node = await this.prepareMessage(data);
772
766
  let result = await peer.request(node.encoded);
773
767
  result = new Uint8Array(Object.values(result));
774
768
  let proto = await protoFor(result);
@@ -875,7 +869,7 @@ class Peernet {
875
869
 
876
870
  let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
877
871
 
878
- const node = await this.prepareMessage(id, data.encoded);
872
+ const node = await this.prepareMessage(data);
879
873
  if (closest[0]) data = await closest[0].request(node.encoded);
880
874
  else {
881
875
  closest = this.connections.filter((peer) => {
@@ -1001,6 +995,8 @@ class Peernet {
1001
995
  else data = await this.requestData(hash, 'data');
1002
996
 
1003
997
  const node = await new peernet.protos['peernet-file'](data);
998
+ await node.decode();
999
+ console.log(data);
1004
1000
  const paths = [];
1005
1001
  if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
1006
1002
  for (const {path, hash} of node.decoded.links) {
@@ -1092,7 +1088,7 @@ class Peernet {
1092
1088
  data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
1093
1089
  for (const peer of this.connections) {
1094
1090
  if (peer.peerId !== this.peerId) {
1095
- const node = await this.prepareMessage(peer.peerId, data.encoded);
1091
+ const node = await this.prepareMessage(data);
1096
1092
  this.sendMessage(peer, id, node.encoded);
1097
1093
  }
1098
1094
  // TODO: if peer subscribed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.14.2",
3
+ "version": "0.14.3",
4
4
  "description": "",
5
5
  "source": "src/peernet.js",
6
6
  "main": "dist/commonjs/peernet.js",
@@ -28,7 +28,7 @@
28
28
  "license": "MIT",
29
29
  "browserslist": "> 5%, last 2 versions, not dead",
30
30
  "dependencies": {
31
- "@leofcoin/codec-format-interface": "^1.2.5",
31
+ "@leofcoin/codec-format-interface": "^1.4.0",
32
32
  "@leofcoin/generate-account": "^1.0.4",
33
33
  "@leofcoin/multi-wallet": "^2.1.2",
34
34
  "@leofcoin/peernet-swarm": "^0.3.3",
@@ -4,8 +4,10 @@ const dataHandler = async message => {
4
4
  if (!message) return
5
5
 
6
6
  const {data, id} = message
7
-
7
+ console.log({data});
8
8
  message = await protoFor(data)
9
+ console.log(message.decoded.data);
10
+ console.log({message});
9
11
  const proto = await protoFor(message.decoded.data)
10
12
  const from = message.decoded.from
11
13
 
@@ -12,10 +12,9 @@ export default class MessageHandler {
12
12
  * @param {Buffer} message.to peer id
13
13
  * @param {string} message.data Peernet message
14
14
  * (PeernetMessage excluded) encoded as a string
15
- * @return signature
15
+ * @return message
16
16
  */
17
17
  async hashAndSignMessage(message) {
18
- const hasher = new CodecHash(message, {name: 'peernet-message'})
19
18
  let identity = await walletStore.get('identity')
20
19
  identity = JSON.parse(identity)
21
20
  if (!globalThis.MultiWallet) {
@@ -24,7 +23,8 @@ export default class MessageHandler {
24
23
  }
25
24
  const wallet = new MultiWallet(this.network)
26
25
  wallet.recover(identity.mnemonic)
27
- return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
26
+ message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32))
27
+ return message
28
28
  }
29
29
 
30
30
  /**
@@ -33,20 +33,11 @@ export default class MessageHandler {
33
33
  * @param {String|PeernetMessage} data - data encoded message string
34
34
  * or the messageNode itself
35
35
  */
36
- async prepareMessage(from, to, data, id) {
37
- if (data.encoded) data = data.encoded
38
-
39
- const message = {
40
- from,
41
- to,
42
- data,
36
+ async prepareMessage(message) {
37
+ if (message.keys.indexOf('signature') !== -1) {
38
+ message = await this.hashAndSignMessage(message)
43
39
  }
44
- const signature = await this.hashAndSignMessage(message)
45
- const node = await new globalThis.peernet.protos['peernet-message']({
46
- ...message,
47
- signature,
48
- })
49
40
 
50
- return node
41
+ return message
51
42
  }
52
43
  }
package/src/peernet.js CHANGED
@@ -94,8 +94,8 @@ export default class Peernet {
94
94
  /**
95
95
  * @see MessageHandler
96
96
  */
97
- prepareMessage(to, data) {
98
- return this._messageHandler.prepareMessage(this.id, to, data)
97
+ prepareMessage(data) {
98
+ return this._messageHandler.prepareMessage(data)
99
99
  }
100
100
 
101
101
  /**
@@ -293,7 +293,7 @@ export default class Peernet {
293
293
  else has = await store.has(hash)
294
294
  }
295
295
  const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has})
296
- const node = await this.prepareMessage(from, data.encoded)
296
+ const node = await this.prepareMessage(data)
297
297
 
298
298
  this.sendMessage(peer, id, node.encoded)
299
299
  } else if (proto.name === 'peernet-data') {
@@ -310,7 +310,7 @@ export default class Peernet {
310
310
  if (data) {
311
311
  data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
312
312
 
313
- const node = await this.prepareMessage(from, data.encoded)
313
+ const node = await this.prepareMessage(data)
314
314
  this.sendMessage(peer, id, node.encoded)
315
315
  }
316
316
  } else {
@@ -321,7 +321,7 @@ export default class Peernet {
321
321
  const method = this.requestProtos[proto.decoded.request]
322
322
  if (method) {
323
323
  const data = await method()
324
- const node = await this.prepareMessage(from, data.encoded)
324
+ const node = await this.prepareMessage(data)
325
325
  this.sendMessage(peer, id, node.encoded)
326
326
  }
327
327
  } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
@@ -340,7 +340,7 @@ export default class Peernet {
340
340
  const data = await new globalThis.peernet.protos['peernet-dht']({hash})
341
341
  const clientId = this.client.id
342
342
  const walk = async peer => {
343
- const node = await this.prepareMessage(peer.peerId, data.encoded)
343
+ const node = await this.prepareMessage(data)
344
344
  let result = await peer.request(node.encoded)
345
345
  result = new Uint8Array(Object.values(result))
346
346
  let proto = await protoFor(result)
@@ -447,7 +447,7 @@ export default class Peernet {
447
447
 
448
448
  let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
449
449
 
450
- const node = await this.prepareMessage(id, data.encoded)
450
+ const node = await this.prepareMessage(data)
451
451
  if (closest[0]) data = await closest[0].request(node.encoded)
452
452
  else {
453
453
  closest = this.connections.filter((peer) => {
@@ -573,6 +573,8 @@ export default class Peernet {
573
573
  else data = await this.requestData(hash, 'data')
574
574
 
575
575
  const node = await new peernet.protos['peernet-file'](data)
576
+ await node.decode()
577
+ console.log(data);
576
578
  const paths = []
577
579
  if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
578
580
  for (const {path, hash} of node.decoded.links) {
@@ -665,7 +667,7 @@ export default class Peernet {
665
667
  data = await new globalThis.peernet.protos['peernet-ps']({data, topic})
666
668
  for (const peer of this.connections) {
667
669
  if (peer.peerId !== this.peerId) {
668
- const node = await this.prepareMessage(peer.peerId, data.encoded)
670
+ const node = await this.prepareMessage(data)
669
671
  this.sendMessage(peer, id, node.encoded)
670
672
  }
671
673
  // TODO: if peer subscribed
@@ -18,6 +18,7 @@ export const expected = (expected, actual) => {
18
18
  }
19
19
 
20
20
  export const protoFor = (data) => {
21
+ console.log(data);
21
22
  if (!Buffer.isBuffer(data)) data = Buffer.from(data)
22
23
  const codec = new Codec(data)
23
24
  if (!codec.name) throw new Error('proto not found')
package/test.js CHANGED
@@ -4,13 +4,13 @@ const Client = require('./dist/commonjs/peernet.js');
4
4
  (async () => {
5
5
  const client = await new Client({root: '.peernet/test'})
6
6
 
7
- peernet.addFolder([{
8
- path: 'assets/asset.png',
9
- content: Buffer.from('png')
10
- }, {
11
- path: 'index.html',
12
- content: Buffer.from('html')
13
- }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
7
+ // peernet.addFolder([{
8
+ // path: 'assets/asset.png',
9
+ // content: Buffer.from('png')
10
+ // }, {
11
+ // path: 'index.html',
12
+ // content: Buffer.from('html')
13
+ // }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
14
14
 
15
15
  pubsub.subscribe('peer:connected', async peer => {
16
16
  chainStore.put('localBlock', '00000')
@@ -19,8 +19,8 @@ const Client = require('./dist/commonjs/peernet.js');
19
19
  })
20
20
  const to = peer.id
21
21
  await peernet.data.put('hello', 'hi')
22
- console.log(request);
23
- const node = await peernet.prepareMessage(to, request.encoded)
22
+ console.log({request});
23
+ const node = await peernet.prepareMessage(request)
24
24
  console.log({node});
25
25
  let response = await peer.request(node.encoded)
26
26
  console.log({response});
@@ -29,9 +29,9 @@ const Client = require('./dist/commonjs/peernet.js');
29
29
  for (const key of keys) {
30
30
  uint8Array[Number(key)] = response[key]
31
31
  }
32
- const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
33
- console.log(proto.decoded.data);
34
- response = await new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
32
+ // const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
33
+ // console.log(proto.decoded.data);
34
+ response = await new globalThis.peernet.protos['peernet-response'](uint8Array)
35
35
  console.log({response});
36
36
 
37
37
  const block = new TextDecoder().decode(response.decoded.response)