@leofcoin/peernet 0.14.2 → 0.14.4
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.
- package/dist/browser/peernet.js +34 -46
- package/dist/commonjs/peernet.js +28 -45
- package/dist/module/peernet.js +28 -45
- package/package.json +2 -2
- package/src/handlers/data.js +2 -5
- package/src/handlers/message.js +7 -16
- package/src/peernet.js +15 -15
- package/src/utils/utils.js +3 -4
- package/test.js +12 -12
package/dist/browser/peernet.js
CHANGED
|
@@ -357,13 +357,12 @@ function _interopNamespace(e) {
|
|
|
357
357
|
|
|
358
358
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
359
359
|
|
|
360
|
-
const protoFor = (
|
|
361
|
-
|
|
362
|
-
const codec = new codecFormatInterface.Codec(data);
|
|
360
|
+
const protoFor = (message) => {
|
|
361
|
+
const codec = new codecFormatInterface.Codec(message);
|
|
363
362
|
if (!codec.name) throw new Error('proto not found')
|
|
364
363
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
365
364
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
366
|
-
return new Proto(
|
|
365
|
+
return new Proto(message)
|
|
367
366
|
};
|
|
368
367
|
|
|
369
368
|
/**
|
|
@@ -708,10 +707,9 @@ class MessageHandler {
|
|
|
708
707
|
* @param {Buffer} message.to peer id
|
|
709
708
|
* @param {string} message.data Peernet message
|
|
710
709
|
* (PeernetMessage excluded) encoded as a string
|
|
711
|
-
* @return
|
|
710
|
+
* @return message
|
|
712
711
|
*/
|
|
713
712
|
async hashAndSignMessage(message) {
|
|
714
|
-
const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
|
|
715
713
|
let identity = await walletStore.get('identity');
|
|
716
714
|
identity = JSON.parse(identity);
|
|
717
715
|
if (!globalThis.MultiWallet) {
|
|
@@ -720,7 +718,8 @@ class MessageHandler {
|
|
|
720
718
|
}
|
|
721
719
|
const wallet = new MultiWallet(this.network);
|
|
722
720
|
wallet.recover(identity.mnemonic);
|
|
723
|
-
|
|
721
|
+
message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
|
|
722
|
+
return message
|
|
724
723
|
}
|
|
725
724
|
|
|
726
725
|
/**
|
|
@@ -729,40 +728,24 @@ class MessageHandler {
|
|
|
729
728
|
* @param {String|PeernetMessage} data - data encoded message string
|
|
730
729
|
* or the messageNode itself
|
|
731
730
|
*/
|
|
732
|
-
async prepareMessage(
|
|
733
|
-
if (
|
|
734
|
-
|
|
735
|
-
|
|
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
|
-
});
|
|
731
|
+
async prepareMessage(message) {
|
|
732
|
+
if (message.keys.indexOf('signature') !== -1) {
|
|
733
|
+
message = await this.hashAndSignMessage(message);
|
|
734
|
+
}
|
|
745
735
|
|
|
746
|
-
return
|
|
736
|
+
return message
|
|
747
737
|
}
|
|
748
738
|
}
|
|
749
739
|
|
|
750
740
|
const dataHandler = async message => {
|
|
751
741
|
if (!message) return
|
|
752
742
|
|
|
753
|
-
const {data, id} = message;
|
|
754
|
-
|
|
755
|
-
message = await protoFor(data);
|
|
756
|
-
const proto = await protoFor(message.decoded.data);
|
|
757
|
-
const from = message.decoded.from;
|
|
743
|
+
const {data, id, from} = message;
|
|
744
|
+
const proto = await protoFor(data);
|
|
758
745
|
|
|
759
746
|
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
760
747
|
};
|
|
761
748
|
|
|
762
|
-
const encapsulatedError = () => {
|
|
763
|
-
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
764
|
-
};
|
|
765
|
-
|
|
766
749
|
const dhtError = (proto) => {
|
|
767
750
|
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
768
751
|
return new Error(`Routing error: ${text}`)
|
|
@@ -857,8 +840,8 @@ class Peernet {
|
|
|
857
840
|
/**
|
|
858
841
|
* @see MessageHandler
|
|
859
842
|
*/
|
|
860
|
-
prepareMessage(
|
|
861
|
-
return this._messageHandler.prepareMessage(
|
|
843
|
+
prepareMessage(data) {
|
|
844
|
+
return this._messageHandler.prepareMessage(data)
|
|
862
845
|
}
|
|
863
846
|
|
|
864
847
|
/**
|
|
@@ -1054,7 +1037,7 @@ class Peernet {
|
|
|
1054
1037
|
else has = await store.has(hash);
|
|
1055
1038
|
}
|
|
1056
1039
|
const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
|
|
1057
|
-
const node = await this.prepareMessage(
|
|
1040
|
+
const node = await this.prepareMessage(data);
|
|
1058
1041
|
|
|
1059
1042
|
this.sendMessage(peer, id, node.encoded);
|
|
1060
1043
|
} else if (proto.name === 'peernet-data') {
|
|
@@ -1071,7 +1054,7 @@ class Peernet {
|
|
|
1071
1054
|
if (data) {
|
|
1072
1055
|
data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
|
|
1073
1056
|
|
|
1074
|
-
const node = await this.prepareMessage(
|
|
1057
|
+
const node = await this.prepareMessage(data);
|
|
1075
1058
|
this.sendMessage(peer, id, node.encoded);
|
|
1076
1059
|
}
|
|
1077
1060
|
}
|
|
@@ -1080,7 +1063,7 @@ class Peernet {
|
|
|
1080
1063
|
const method = this.requestProtos[proto.decoded.request];
|
|
1081
1064
|
if (method) {
|
|
1082
1065
|
const data = await method();
|
|
1083
|
-
const node = await this.prepareMessage(
|
|
1066
|
+
const node = await this.prepareMessage(data);
|
|
1084
1067
|
this.sendMessage(peer, id, node.encoded);
|
|
1085
1068
|
}
|
|
1086
1069
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
@@ -1099,13 +1082,11 @@ class Peernet {
|
|
|
1099
1082
|
const data = await new globalThis.peernet.protos['peernet-dht']({hash});
|
|
1100
1083
|
this.client.id;
|
|
1101
1084
|
const walk = async peer => {
|
|
1102
|
-
const node = await this.prepareMessage(
|
|
1085
|
+
const node = await this.prepareMessage(data);
|
|
1103
1086
|
let result = await peer.request(node.encoded);
|
|
1087
|
+
console.log({result});
|
|
1104
1088
|
result = new Uint8Array(Object.values(result));
|
|
1105
|
-
|
|
1106
|
-
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
1107
|
-
const from = proto.decoded.from;
|
|
1108
|
-
proto = await protoFor(proto.decoded.data);
|
|
1089
|
+
const proto = await protoFor(result);
|
|
1109
1090
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
1110
1091
|
|
|
1111
1092
|
// TODO: give ip and port (just used for location)
|
|
@@ -1119,7 +1100,7 @@ class Peernet {
|
|
|
1119
1100
|
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
1120
1101
|
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
1121
1102
|
port: peer.connection.remotePort || peer.connection.localPort,
|
|
1122
|
-
id:
|
|
1103
|
+
id: peerId,
|
|
1123
1104
|
};
|
|
1124
1105
|
|
|
1125
1106
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
@@ -1206,7 +1187,7 @@ class Peernet {
|
|
|
1206
1187
|
|
|
1207
1188
|
let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
|
|
1208
1189
|
|
|
1209
|
-
const node = await this.prepareMessage(
|
|
1190
|
+
const node = await this.prepareMessage(data);
|
|
1210
1191
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1211
1192
|
else {
|
|
1212
1193
|
closest = this.connections.filter((peer) => {
|
|
@@ -1214,9 +1195,9 @@ class Peernet {
|
|
|
1214
1195
|
});
|
|
1215
1196
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1216
1197
|
}
|
|
1198
|
+
console.log({data});
|
|
1217
1199
|
data = new Uint8Array(Object.values(data));
|
|
1218
|
-
|
|
1219
|
-
proto = await protoFor(proto.decoded.data);
|
|
1200
|
+
proto = await protoFor(data);
|
|
1220
1201
|
// TODO: store data automaticly or not
|
|
1221
1202
|
return proto.decoded.data
|
|
1222
1203
|
|
|
@@ -1332,6 +1313,8 @@ class Peernet {
|
|
|
1332
1313
|
else data = await this.requestData(hash, 'data');
|
|
1333
1314
|
|
|
1334
1315
|
const node = await new peernet.protos['peernet-file'](data);
|
|
1316
|
+
await node.decode();
|
|
1317
|
+
console.log(data);
|
|
1335
1318
|
const paths = [];
|
|
1336
1319
|
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
1337
1320
|
for (const {path, hash} of node.decoded.links) {
|
|
@@ -1423,7 +1406,7 @@ class Peernet {
|
|
|
1423
1406
|
data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
|
|
1424
1407
|
for (const peer of this.connections) {
|
|
1425
1408
|
if (peer.peerId !== this.peerId) {
|
|
1426
|
-
const node = await this.prepareMessage(
|
|
1409
|
+
const node = await this.prepareMessage(data);
|
|
1427
1410
|
this.sendMessage(peer, id, node.encoded);
|
|
1428
1411
|
}
|
|
1429
1412
|
// TODO: if peer subscribed
|
|
@@ -1989,12 +1972,17 @@ class FormatInterface$1 extends BasicInterface$1 {
|
|
|
1989
1972
|
* @return {Buffer}
|
|
1990
1973
|
*/
|
|
1991
1974
|
async encode(decoded) {
|
|
1975
|
+
let encoded;
|
|
1992
1976
|
if (!decoded) decoded = this.decoded;
|
|
1993
1977
|
const codec = new PeernetCodec(this.name);
|
|
1994
|
-
|
|
1978
|
+
|
|
1979
|
+
if (decoded instanceof Uint8Array) encoded = decoded;
|
|
1980
|
+
else encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded);
|
|
1981
|
+
|
|
1995
1982
|
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
1996
1983
|
uint8Array.set(codec.codecBuffer);
|
|
1997
1984
|
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
1985
|
+
|
|
1998
1986
|
this.encoded = uint8Array;
|
|
1999
1987
|
return this.encoded
|
|
2000
1988
|
}
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -28,13 +28,12 @@ function _interopNamespace(e) {
|
|
|
28
28
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
29
29
|
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
30
30
|
|
|
31
|
-
const protoFor = (
|
|
32
|
-
|
|
33
|
-
const codec = new codecFormatInterface.Codec(data);
|
|
31
|
+
const protoFor = (message) => {
|
|
32
|
+
const codec = new codecFormatInterface.Codec(message);
|
|
34
33
|
if (!codec.name) throw new Error('proto not found')
|
|
35
34
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
36
35
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
37
|
-
return new Proto(
|
|
36
|
+
return new Proto(message)
|
|
38
37
|
};
|
|
39
38
|
|
|
40
39
|
/**
|
|
@@ -379,10 +378,9 @@ class MessageHandler {
|
|
|
379
378
|
* @param {Buffer} message.to peer id
|
|
380
379
|
* @param {string} message.data Peernet message
|
|
381
380
|
* (PeernetMessage excluded) encoded as a string
|
|
382
|
-
* @return
|
|
381
|
+
* @return message
|
|
383
382
|
*/
|
|
384
383
|
async hashAndSignMessage(message) {
|
|
385
|
-
const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
|
|
386
384
|
let identity = await walletStore.get('identity');
|
|
387
385
|
identity = JSON.parse(identity);
|
|
388
386
|
if (!globalThis.MultiWallet) {
|
|
@@ -391,7 +389,8 @@ class MessageHandler {
|
|
|
391
389
|
}
|
|
392
390
|
const wallet = new MultiWallet(this.network);
|
|
393
391
|
wallet.recover(identity.mnemonic);
|
|
394
|
-
|
|
392
|
+
message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
|
|
393
|
+
return message
|
|
395
394
|
}
|
|
396
395
|
|
|
397
396
|
/**
|
|
@@ -400,40 +399,24 @@ class MessageHandler {
|
|
|
400
399
|
* @param {String|PeernetMessage} data - data encoded message string
|
|
401
400
|
* or the messageNode itself
|
|
402
401
|
*/
|
|
403
|
-
async prepareMessage(
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
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
|
-
});
|
|
402
|
+
async prepareMessage(message) {
|
|
403
|
+
if (message.keys.indexOf('signature') !== -1) {
|
|
404
|
+
message = await this.hashAndSignMessage(message);
|
|
405
|
+
}
|
|
416
406
|
|
|
417
|
-
return
|
|
407
|
+
return message
|
|
418
408
|
}
|
|
419
409
|
}
|
|
420
410
|
|
|
421
411
|
const dataHandler = async message => {
|
|
422
412
|
if (!message) return
|
|
423
413
|
|
|
424
|
-
const {data, id} = message;
|
|
425
|
-
|
|
426
|
-
message = await protoFor(data);
|
|
427
|
-
const proto = await protoFor(message.decoded.data);
|
|
428
|
-
const from = message.decoded.from;
|
|
414
|
+
const {data, id, from} = message;
|
|
415
|
+
const proto = await protoFor(data);
|
|
429
416
|
|
|
430
417
|
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
431
418
|
};
|
|
432
419
|
|
|
433
|
-
const encapsulatedError = () => {
|
|
434
|
-
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
435
|
-
};
|
|
436
|
-
|
|
437
420
|
const dhtError = (proto) => {
|
|
438
421
|
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
439
422
|
return new Error(`Routing error: ${text}`)
|
|
@@ -528,8 +511,8 @@ class Peernet {
|
|
|
528
511
|
/**
|
|
529
512
|
* @see MessageHandler
|
|
530
513
|
*/
|
|
531
|
-
prepareMessage(
|
|
532
|
-
return this._messageHandler.prepareMessage(
|
|
514
|
+
prepareMessage(data) {
|
|
515
|
+
return this._messageHandler.prepareMessage(data)
|
|
533
516
|
}
|
|
534
517
|
|
|
535
518
|
/**
|
|
@@ -725,7 +708,7 @@ class Peernet {
|
|
|
725
708
|
else has = await store.has(hash);
|
|
726
709
|
}
|
|
727
710
|
const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
|
|
728
|
-
const node = await this.prepareMessage(
|
|
711
|
+
const node = await this.prepareMessage(data);
|
|
729
712
|
|
|
730
713
|
this.sendMessage(peer, id, node.encoded);
|
|
731
714
|
} else if (proto.name === 'peernet-data') {
|
|
@@ -742,7 +725,7 @@ class Peernet {
|
|
|
742
725
|
if (data) {
|
|
743
726
|
data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
|
|
744
727
|
|
|
745
|
-
const node = await this.prepareMessage(
|
|
728
|
+
const node = await this.prepareMessage(data);
|
|
746
729
|
this.sendMessage(peer, id, node.encoded);
|
|
747
730
|
}
|
|
748
731
|
}
|
|
@@ -751,7 +734,7 @@ class Peernet {
|
|
|
751
734
|
const method = this.requestProtos[proto.decoded.request];
|
|
752
735
|
if (method) {
|
|
753
736
|
const data = await method();
|
|
754
|
-
const node = await this.prepareMessage(
|
|
737
|
+
const node = await this.prepareMessage(data);
|
|
755
738
|
this.sendMessage(peer, id, node.encoded);
|
|
756
739
|
}
|
|
757
740
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
@@ -770,13 +753,11 @@ class Peernet {
|
|
|
770
753
|
const data = await new globalThis.peernet.protos['peernet-dht']({hash});
|
|
771
754
|
this.client.id;
|
|
772
755
|
const walk = async peer => {
|
|
773
|
-
const node = await this.prepareMessage(
|
|
756
|
+
const node = await this.prepareMessage(data);
|
|
774
757
|
let result = await peer.request(node.encoded);
|
|
758
|
+
console.log({result});
|
|
775
759
|
result = new Uint8Array(Object.values(result));
|
|
776
|
-
|
|
777
|
-
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
778
|
-
const from = proto.decoded.from;
|
|
779
|
-
proto = await protoFor(proto.decoded.data);
|
|
760
|
+
const proto = await protoFor(result);
|
|
780
761
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
781
762
|
|
|
782
763
|
// TODO: give ip and port (just used for location)
|
|
@@ -790,7 +771,7 @@ class Peernet {
|
|
|
790
771
|
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
791
772
|
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
792
773
|
port: peer.connection.remotePort || peer.connection.localPort,
|
|
793
|
-
id:
|
|
774
|
+
id: peerId,
|
|
794
775
|
};
|
|
795
776
|
|
|
796
777
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
@@ -877,7 +858,7 @@ class Peernet {
|
|
|
877
858
|
|
|
878
859
|
let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
|
|
879
860
|
|
|
880
|
-
const node = await this.prepareMessage(
|
|
861
|
+
const node = await this.prepareMessage(data);
|
|
881
862
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
882
863
|
else {
|
|
883
864
|
closest = this.connections.filter((peer) => {
|
|
@@ -885,9 +866,9 @@ class Peernet {
|
|
|
885
866
|
});
|
|
886
867
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
887
868
|
}
|
|
869
|
+
console.log({data});
|
|
888
870
|
data = new Uint8Array(Object.values(data));
|
|
889
|
-
|
|
890
|
-
proto = await protoFor(proto.decoded.data);
|
|
871
|
+
proto = await protoFor(data);
|
|
891
872
|
// TODO: store data automaticly or not
|
|
892
873
|
return proto.decoded.data
|
|
893
874
|
|
|
@@ -1003,6 +984,8 @@ class Peernet {
|
|
|
1003
984
|
else data = await this.requestData(hash, 'data');
|
|
1004
985
|
|
|
1005
986
|
const node = await new peernet.protos['peernet-file'](data);
|
|
987
|
+
await node.decode();
|
|
988
|
+
console.log(data);
|
|
1006
989
|
const paths = [];
|
|
1007
990
|
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
1008
991
|
for (const {path, hash} of node.decoded.links) {
|
|
@@ -1094,7 +1077,7 @@ class Peernet {
|
|
|
1094
1077
|
data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
|
|
1095
1078
|
for (const peer of this.connections) {
|
|
1096
1079
|
if (peer.peerId !== this.peerId) {
|
|
1097
|
-
const node = await this.prepareMessage(
|
|
1080
|
+
const node = await this.prepareMessage(data);
|
|
1098
1081
|
this.sendMessage(peer, id, node.encoded);
|
|
1099
1082
|
}
|
|
1100
1083
|
// TODO: if peer subscribed
|
package/dist/module/peernet.js
CHANGED
|
@@ -26,13 +26,12 @@ function _interopNamespace(e) {
|
|
|
26
26
|
|
|
27
27
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
28
28
|
|
|
29
|
-
const protoFor = (
|
|
30
|
-
|
|
31
|
-
const codec = new codecFormatInterface.Codec(data);
|
|
29
|
+
const protoFor = (message) => {
|
|
30
|
+
const codec = new codecFormatInterface.Codec(message);
|
|
32
31
|
if (!codec.name) throw new Error('proto not found')
|
|
33
32
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
34
33
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
35
|
-
return new Proto(
|
|
34
|
+
return new Proto(message)
|
|
36
35
|
};
|
|
37
36
|
|
|
38
37
|
/**
|
|
@@ -377,10 +376,9 @@ class MessageHandler {
|
|
|
377
376
|
* @param {Buffer} message.to peer id
|
|
378
377
|
* @param {string} message.data Peernet message
|
|
379
378
|
* (PeernetMessage excluded) encoded as a string
|
|
380
|
-
* @return
|
|
379
|
+
* @return message
|
|
381
380
|
*/
|
|
382
381
|
async hashAndSignMessage(message) {
|
|
383
|
-
const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
|
|
384
382
|
let identity = await walletStore.get('identity');
|
|
385
383
|
identity = JSON.parse(identity);
|
|
386
384
|
if (!globalThis.MultiWallet) {
|
|
@@ -389,7 +387,8 @@ class MessageHandler {
|
|
|
389
387
|
}
|
|
390
388
|
const wallet = new MultiWallet(this.network);
|
|
391
389
|
wallet.recover(identity.mnemonic);
|
|
392
|
-
|
|
390
|
+
message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
|
|
391
|
+
return message
|
|
393
392
|
}
|
|
394
393
|
|
|
395
394
|
/**
|
|
@@ -398,40 +397,24 @@ class MessageHandler {
|
|
|
398
397
|
* @param {String|PeernetMessage} data - data encoded message string
|
|
399
398
|
* or the messageNode itself
|
|
400
399
|
*/
|
|
401
|
-
async prepareMessage(
|
|
402
|
-
if (
|
|
403
|
-
|
|
404
|
-
|
|
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
|
-
});
|
|
400
|
+
async prepareMessage(message) {
|
|
401
|
+
if (message.keys.indexOf('signature') !== -1) {
|
|
402
|
+
message = await this.hashAndSignMessage(message);
|
|
403
|
+
}
|
|
414
404
|
|
|
415
|
-
return
|
|
405
|
+
return message
|
|
416
406
|
}
|
|
417
407
|
}
|
|
418
408
|
|
|
419
409
|
const dataHandler = async message => {
|
|
420
410
|
if (!message) return
|
|
421
411
|
|
|
422
|
-
const {data, id} = message;
|
|
423
|
-
|
|
424
|
-
message = await protoFor(data);
|
|
425
|
-
const proto = await protoFor(message.decoded.data);
|
|
426
|
-
const from = message.decoded.from;
|
|
412
|
+
const {data, id, from} = message;
|
|
413
|
+
const proto = await protoFor(data);
|
|
427
414
|
|
|
428
415
|
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
429
416
|
};
|
|
430
417
|
|
|
431
|
-
const encapsulatedError = () => {
|
|
432
|
-
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
433
|
-
};
|
|
434
|
-
|
|
435
418
|
const dhtError = (proto) => {
|
|
436
419
|
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
437
420
|
return new Error(`Routing error: ${text}`)
|
|
@@ -526,8 +509,8 @@ class Peernet {
|
|
|
526
509
|
/**
|
|
527
510
|
* @see MessageHandler
|
|
528
511
|
*/
|
|
529
|
-
prepareMessage(
|
|
530
|
-
return this._messageHandler.prepareMessage(
|
|
512
|
+
prepareMessage(data) {
|
|
513
|
+
return this._messageHandler.prepareMessage(data)
|
|
531
514
|
}
|
|
532
515
|
|
|
533
516
|
/**
|
|
@@ -723,7 +706,7 @@ class Peernet {
|
|
|
723
706
|
else has = await store.has(hash);
|
|
724
707
|
}
|
|
725
708
|
const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has});
|
|
726
|
-
const node = await this.prepareMessage(
|
|
709
|
+
const node = await this.prepareMessage(data);
|
|
727
710
|
|
|
728
711
|
this.sendMessage(peer, id, node.encoded);
|
|
729
712
|
} else if (proto.name === 'peernet-data') {
|
|
@@ -740,7 +723,7 @@ class Peernet {
|
|
|
740
723
|
if (data) {
|
|
741
724
|
data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
|
|
742
725
|
|
|
743
|
-
const node = await this.prepareMessage(
|
|
726
|
+
const node = await this.prepareMessage(data);
|
|
744
727
|
this.sendMessage(peer, id, node.encoded);
|
|
745
728
|
}
|
|
746
729
|
}
|
|
@@ -749,7 +732,7 @@ class Peernet {
|
|
|
749
732
|
const method = this.requestProtos[proto.decoded.request];
|
|
750
733
|
if (method) {
|
|
751
734
|
const data = await method();
|
|
752
|
-
const node = await this.prepareMessage(
|
|
735
|
+
const node = await this.prepareMessage(data);
|
|
753
736
|
this.sendMessage(peer, id, node.encoded);
|
|
754
737
|
}
|
|
755
738
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
@@ -768,13 +751,11 @@ class Peernet {
|
|
|
768
751
|
const data = await new globalThis.peernet.protos['peernet-dht']({hash});
|
|
769
752
|
this.client.id;
|
|
770
753
|
const walk = async peer => {
|
|
771
|
-
const node = await this.prepareMessage(
|
|
754
|
+
const node = await this.prepareMessage(data);
|
|
772
755
|
let result = await peer.request(node.encoded);
|
|
756
|
+
console.log({result});
|
|
773
757
|
result = new Uint8Array(Object.values(result));
|
|
774
|
-
|
|
775
|
-
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
776
|
-
const from = proto.decoded.from;
|
|
777
|
-
proto = await protoFor(proto.decoded.data);
|
|
758
|
+
const proto = await protoFor(result);
|
|
778
759
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
779
760
|
|
|
780
761
|
// TODO: give ip and port (just used for location)
|
|
@@ -788,7 +769,7 @@ class Peernet {
|
|
|
788
769
|
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
789
770
|
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
790
771
|
port: peer.connection.remotePort || peer.connection.localPort,
|
|
791
|
-
id:
|
|
772
|
+
id: peerId,
|
|
792
773
|
};
|
|
793
774
|
|
|
794
775
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
@@ -875,7 +856,7 @@ class Peernet {
|
|
|
875
856
|
|
|
876
857
|
let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
|
|
877
858
|
|
|
878
|
-
const node = await this.prepareMessage(
|
|
859
|
+
const node = await this.prepareMessage(data);
|
|
879
860
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
880
861
|
else {
|
|
881
862
|
closest = this.connections.filter((peer) => {
|
|
@@ -883,9 +864,9 @@ class Peernet {
|
|
|
883
864
|
});
|
|
884
865
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
885
866
|
}
|
|
867
|
+
console.log({data});
|
|
886
868
|
data = new Uint8Array(Object.values(data));
|
|
887
|
-
|
|
888
|
-
proto = await protoFor(proto.decoded.data);
|
|
869
|
+
proto = await protoFor(data);
|
|
889
870
|
// TODO: store data automaticly or not
|
|
890
871
|
return proto.decoded.data
|
|
891
872
|
|
|
@@ -1001,6 +982,8 @@ class Peernet {
|
|
|
1001
982
|
else data = await this.requestData(hash, 'data');
|
|
1002
983
|
|
|
1003
984
|
const node = await new peernet.protos['peernet-file'](data);
|
|
985
|
+
await node.decode();
|
|
986
|
+
console.log(data);
|
|
1004
987
|
const paths = [];
|
|
1005
988
|
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
1006
989
|
for (const {path, hash} of node.decoded.links) {
|
|
@@ -1092,7 +1075,7 @@ class Peernet {
|
|
|
1092
1075
|
data = await new globalThis.peernet.protos['peernet-ps']({data, topic});
|
|
1093
1076
|
for (const peer of this.connections) {
|
|
1094
1077
|
if (peer.peerId !== this.peerId) {
|
|
1095
|
-
const node = await this.prepareMessage(
|
|
1078
|
+
const node = await this.prepareMessage(data);
|
|
1096
1079
|
this.sendMessage(peer, id, node.encoded);
|
|
1097
1080
|
}
|
|
1098
1081
|
// TODO: if peer subscribed
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.4",
|
|
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.
|
|
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",
|
package/src/handlers/data.js
CHANGED
|
@@ -3,11 +3,8 @@ import { protoFor } from './../utils/utils.js'
|
|
|
3
3
|
const dataHandler = async message => {
|
|
4
4
|
if (!message) return
|
|
5
5
|
|
|
6
|
-
const {data, id} = message
|
|
7
|
-
|
|
8
|
-
message = await protoFor(data)
|
|
9
|
-
const proto = await protoFor(message.decoded.data)
|
|
10
|
-
const from = message.decoded.from
|
|
6
|
+
const {data, id, from} = message
|
|
7
|
+
const proto = await protoFor(data)
|
|
11
8
|
|
|
12
9
|
peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
|
|
13
10
|
}
|
package/src/handlers/message.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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(
|
|
37
|
-
if (
|
|
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
|
|
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(
|
|
98
|
-
return this._messageHandler.prepareMessage(
|
|
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(
|
|
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(
|
|
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(
|
|
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,13 +340,11 @@ 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(
|
|
343
|
+
const node = await this.prepareMessage(data)
|
|
344
344
|
let result = await peer.request(node.encoded)
|
|
345
|
+
console.log({result});
|
|
345
346
|
result = new Uint8Array(Object.values(result))
|
|
346
|
-
|
|
347
|
-
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
348
|
-
const from = proto.decoded.from
|
|
349
|
-
proto = await protoFor(proto.decoded.data)
|
|
347
|
+
const proto = await protoFor(result)
|
|
350
348
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
351
349
|
|
|
352
350
|
// TODO: give ip and port (just used for location)
|
|
@@ -360,7 +358,7 @@ export default class Peernet {
|
|
|
360
358
|
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
361
359
|
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
362
360
|
port: peer.connection.remotePort || peer.connection.localPort,
|
|
363
|
-
id:
|
|
361
|
+
id: peerId,
|
|
364
362
|
}
|
|
365
363
|
|
|
366
364
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
|
|
@@ -447,7 +445,7 @@ export default class Peernet {
|
|
|
447
445
|
|
|
448
446
|
let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
|
|
449
447
|
|
|
450
|
-
const node = await this.prepareMessage(
|
|
448
|
+
const node = await this.prepareMessage(data)
|
|
451
449
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
452
450
|
else {
|
|
453
451
|
closest = this.connections.filter((peer) => {
|
|
@@ -455,9 +453,9 @@ export default class Peernet {
|
|
|
455
453
|
})
|
|
456
454
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
457
455
|
}
|
|
456
|
+
console.log({data});
|
|
458
457
|
data = new Uint8Array(Object.values(data))
|
|
459
|
-
|
|
460
|
-
proto = await protoFor(proto.decoded.data)
|
|
458
|
+
proto = await protoFor(data)
|
|
461
459
|
// TODO: store data automaticly or not
|
|
462
460
|
return proto.decoded.data
|
|
463
461
|
|
|
@@ -573,6 +571,8 @@ export default class Peernet {
|
|
|
573
571
|
else data = await this.requestData(hash, 'data')
|
|
574
572
|
|
|
575
573
|
const node = await new peernet.protos['peernet-file'](data)
|
|
574
|
+
await node.decode()
|
|
575
|
+
console.log(data);
|
|
576
576
|
const paths = []
|
|
577
577
|
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
578
578
|
for (const {path, hash} of node.decoded.links) {
|
|
@@ -665,7 +665,7 @@ export default class Peernet {
|
|
|
665
665
|
data = await new globalThis.peernet.protos['peernet-ps']({data, topic})
|
|
666
666
|
for (const peer of this.connections) {
|
|
667
667
|
if (peer.peerId !== this.peerId) {
|
|
668
|
-
const node = await this.prepareMessage(
|
|
668
|
+
const node = await this.prepareMessage(data)
|
|
669
669
|
this.sendMessage(peer, id, node.encoded)
|
|
670
670
|
}
|
|
671
671
|
// TODO: if peer subscribed
|
package/src/utils/utils.js
CHANGED
|
@@ -17,13 +17,12 @@ export const expected = (expected, actual) => {
|
|
|
17
17
|
${entries.join('\n\t')}`;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export const protoFor = (
|
|
21
|
-
|
|
22
|
-
const codec = new Codec(data)
|
|
20
|
+
export const protoFor = (message) => {
|
|
21
|
+
const codec = new Codec(message)
|
|
23
22
|
if (!codec.name) throw new Error('proto not found')
|
|
24
23
|
const Proto = globalThis.peernet.protos[codec.name]
|
|
25
24
|
if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
|
|
26
|
-
return new Proto(
|
|
25
|
+
return new Proto(message)
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
/**
|
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
|
-
|
|
9
|
-
|
|
10
|
-
}, {
|
|
11
|
-
|
|
12
|
-
|
|
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(
|
|
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'](
|
|
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)
|