@leofcoin/peernet 1.1.58 → 1.1.60
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/.prettierrc +7 -0
- package/.travis.yml +4 -4
- package/exports/browser/browser-store.js +1 -1
- package/exports/browser/{index-8868bdd8.js → index-5cd0a662.js} +1 -1
- package/exports/browser/{messages-eb6e5c71.js → messages-638774c5.js} +15 -25
- package/exports/browser/{peernet-87ea02a4.js → peernet-61545c25.js} +7165 -292
- package/exports/browser/peernet.d.ts +13 -16
- package/exports/browser/peernet.js +1 -1
- package/exports/browser/types.d.ts +5 -5
- package/exports/{messages-b9a32987.js → messages-42b2109e.js} +14 -24
- package/exports/peernet.js +110 -132
- package/exports/src/prompts/password.js +1 -1
- package/exports/store.js +3 -1
- package/exports/types/peernet.d.ts +13 -16
- package/exports/types/types.d.ts +5 -5
- package/package.json +1 -1
- package/src/dht/dht.ts +39 -33
- package/src/discovery/peer-discovery.js +8 -8
- package/src/handlers/data.js +3 -3
- package/src/handlers/message.js +1 -1
- package/src/identity.ts +12 -11
- package/src/messages/chat.js +1 -2
- package/src/messages/data-response.js +1 -2
- package/src/messages/data.js +1 -2
- package/src/messages/dht-response.js +1 -2
- package/src/messages/dht.js +1 -2
- package/src/messages/file-link.js +1 -2
- package/src/messages/file.js +1 -2
- package/src/messages/peer-response.js +1 -2
- package/src/messages/peer.js +1 -1
- package/src/messages/peernet.js +1 -2
- package/src/messages/ps.js +1 -1
- package/src/messages/request.js +1 -2
- package/src/messages/response.js +1 -2
- package/src/peernet.ts +333 -394
- package/src/prompts/password/browser.js +1 -1
- package/src/prompts/password/node.js +2 -2
- package/src/proto/chat-message.proto.js +1 -1
- package/src/proto/data-response.proto.js +1 -1
- package/src/types.ts +7 -9
- package/src/utils/utils.js +7 -8
- package/exports/browser/identity.d.ts +0 -18
- package/exports/types/identity.d.ts +0 -18
package/exports/peernet.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug';
|
|
2
2
|
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
3
3
|
import { Codec } from '@leofcoin/codec-format-interface';
|
|
4
|
-
import
|
|
4
|
+
import { Storage } from '@leofcoin/storage';
|
|
5
5
|
import { utils } from '@leofcoin/codecs';
|
|
6
6
|
import MultiWallet from '@leofcoin/multi-wallet';
|
|
7
7
|
import base58 from '@vandeurenglenn/base58';
|
|
@@ -9,7 +9,7 @@ import { decrypt } from '@leofcoin/identity-utils';
|
|
|
9
9
|
import QrScanner from 'qr-scanner';
|
|
10
10
|
import qrcode from 'qrcode';
|
|
11
11
|
|
|
12
|
-
const BufferToUint8Array = data => {
|
|
12
|
+
const BufferToUint8Array = (data) => {
|
|
13
13
|
if (data.type === 'Buffer') {
|
|
14
14
|
data = new Uint8Array(data.data);
|
|
15
15
|
}
|
|
@@ -20,7 +20,7 @@ const protoFor = (message) => {
|
|
|
20
20
|
const codec = new Codec(message);
|
|
21
21
|
if (!codec.name) throw new Error('proto not found')
|
|
22
22
|
const Proto = globalThis.peernet.protos[codec.name];
|
|
23
|
-
if (!Proto) throw
|
|
23
|
+
if (!Proto) throw new Error(`No proto defined for ${codec.name}`)
|
|
24
24
|
return new Proto(message)
|
|
25
25
|
};
|
|
26
26
|
|
|
@@ -39,7 +39,7 @@ const hasDaemon = async () => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
const https = () => {
|
|
42
|
-
if (!globalThis.location) return false
|
|
42
|
+
if (!globalThis.location) return false
|
|
43
43
|
return Boolean(globalThis.location.protocol === 'https:')
|
|
44
44
|
};
|
|
45
45
|
|
|
@@ -68,7 +68,7 @@ const target = async () => {
|
|
|
68
68
|
let daemon = false;
|
|
69
69
|
if (!https()) daemon = await hasDaemon();
|
|
70
70
|
|
|
71
|
-
return {daemon, environment: environment()}
|
|
71
|
+
return { daemon, environment: environment() }
|
|
72
72
|
};
|
|
73
73
|
|
|
74
74
|
class PeerDiscovery {
|
|
@@ -77,7 +77,7 @@ class PeerDiscovery {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
_getPeerId(id) {
|
|
80
|
-
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
80
|
+
if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
|
|
81
81
|
|
|
82
82
|
for (const entry of [...peernet.peerMap.entries()]) {
|
|
83
83
|
for (const _id of entry[1]) {
|
|
@@ -89,7 +89,7 @@ class PeerDiscovery {
|
|
|
89
89
|
async discover(peer) {
|
|
90
90
|
let id = this._getPeerId(peer.id);
|
|
91
91
|
if (id) return id
|
|
92
|
-
const data = await new peernet.protos['peernet-peer']({id: this.id});
|
|
92
|
+
const data = await new peernet.protos['peernet-peer']({ id: this.id });
|
|
93
93
|
const node = await peernet.prepareMessage(peer.id, data.encoded);
|
|
94
94
|
|
|
95
95
|
let response = await peer.request(node.encoded);
|
|
@@ -97,7 +97,7 @@ class PeerDiscovery {
|
|
|
97
97
|
response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
|
|
98
98
|
|
|
99
99
|
id = response.decoded.id;
|
|
100
|
-
if (id === this.id) return
|
|
100
|
+
if (id === this.id) return
|
|
101
101
|
|
|
102
102
|
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id]);
|
|
103
103
|
else {
|
|
@@ -111,11 +111,11 @@ class PeerDiscovery {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
async discoverHandler(message, peer) {
|
|
114
|
-
const {id, proto} = message;
|
|
114
|
+
const { id, proto } = message;
|
|
115
115
|
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
116
116
|
if (proto.name === 'peernet-peer') {
|
|
117
117
|
const from = proto.decoded.id;
|
|
118
|
-
if (from === this.id) return
|
|
118
|
+
if (from === this.id) return
|
|
119
119
|
|
|
120
120
|
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
|
|
121
121
|
else {
|
|
@@ -125,13 +125,13 @@ class PeerDiscovery {
|
|
|
125
125
|
peernet.peerMap.set(from, connections);
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
const data = await new peernet.protos['peernet-peer-response']({id: this.id});
|
|
128
|
+
const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
|
|
129
129
|
const node = await peernet.prepareMessage(from, data.encoded);
|
|
130
130
|
|
|
131
|
-
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
131
|
+
peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
|
|
132
132
|
} else if (proto.name === 'peernet-peer-response') {
|
|
133
133
|
const from = proto.decoded.id;
|
|
134
|
-
if (from === this.id) return
|
|
134
|
+
if (from === this.id) return
|
|
135
135
|
|
|
136
136
|
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
|
|
137
137
|
else {
|
|
@@ -153,12 +153,12 @@ class PeerDiscovery {
|
|
|
153
153
|
const lastFetched = {
|
|
154
154
|
address: {
|
|
155
155
|
value: undefined,
|
|
156
|
-
timestamp: 0
|
|
156
|
+
timestamp: 0
|
|
157
157
|
},
|
|
158
158
|
ptr: {
|
|
159
159
|
value: undefined,
|
|
160
|
-
timestamp: 0
|
|
161
|
-
}
|
|
160
|
+
timestamp: 0
|
|
161
|
+
}
|
|
162
162
|
};
|
|
163
163
|
const fetchedCoordinates = {};
|
|
164
164
|
const getAddress = async () => {
|
|
@@ -172,7 +172,7 @@ const getAddress = async () => {
|
|
|
172
172
|
return address.value;
|
|
173
173
|
};
|
|
174
174
|
const degreesToRadians = (degrees) => {
|
|
175
|
-
return degrees * Math.PI / 180;
|
|
175
|
+
return (degrees * Math.PI) / 180;
|
|
176
176
|
};
|
|
177
177
|
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
178
178
|
const earthRadiusKm = 6371;
|
|
@@ -180,13 +180,12 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
|
180
180
|
const dLon = degreesToRadians(lon2 - lon1);
|
|
181
181
|
lat1 = degreesToRadians(lat1);
|
|
182
182
|
lat2 = degreesToRadians(lat2);
|
|
183
|
-
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
184
|
-
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
183
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
185
184
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
186
185
|
return earthRadiusKm * c;
|
|
187
186
|
};
|
|
188
187
|
class DhtEarth {
|
|
189
|
-
providerMap = new Map;
|
|
188
|
+
providerMap = new Map();
|
|
190
189
|
/**
|
|
191
190
|
*
|
|
192
191
|
*/
|
|
@@ -197,7 +196,7 @@ class DhtEarth {
|
|
|
197
196
|
if (!fetchedCoordinates[address]) {
|
|
198
197
|
const request = `https://whereis.leofcoin.org/?ip=${address}`;
|
|
199
198
|
let response = await fetch(request);
|
|
200
|
-
const { lat, lon } = await response.json();
|
|
199
|
+
const { lat, lon } = (await response.json());
|
|
201
200
|
fetchedCoordinates[address] = { latitude: lat, longitude: lon };
|
|
202
201
|
}
|
|
203
202
|
return fetchedCoordinates[address];
|
|
@@ -209,7 +208,10 @@ class DhtEarth {
|
|
|
209
208
|
*/
|
|
210
209
|
async getDistance(peer, provider) {
|
|
211
210
|
const { latitude, longitude } = await this.getCoordinates(provider.address);
|
|
212
|
-
return {
|
|
211
|
+
return {
|
|
212
|
+
provider,
|
|
213
|
+
distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)
|
|
214
|
+
};
|
|
213
215
|
}
|
|
214
216
|
async closestPeer(providers) {
|
|
215
217
|
let all = [];
|
|
@@ -278,7 +280,7 @@ class MessageHandler {
|
|
|
278
280
|
* @param {String|PeernetMessage} data - data encoded message string
|
|
279
281
|
* or the messageNode itself
|
|
280
282
|
*/
|
|
281
|
-
async prepareMessage(message) {
|
|
283
|
+
async prepareMessage(message) {
|
|
282
284
|
if (message.keys.includes('signature')) {
|
|
283
285
|
message = await this.hashAndSignMessage(message);
|
|
284
286
|
}
|
|
@@ -287,12 +289,12 @@ class MessageHandler {
|
|
|
287
289
|
}
|
|
288
290
|
}
|
|
289
291
|
|
|
290
|
-
const dataHandler = async message => {
|
|
292
|
+
const dataHandler = async (message) => {
|
|
291
293
|
if (!message) return
|
|
292
294
|
|
|
293
|
-
const {data, id, from, peer} = message;
|
|
295
|
+
const { data, id, from, peer } = message;
|
|
294
296
|
const proto = await protoFor(data);
|
|
295
|
-
peernet._protoHandler({id, proto}, peernet.connections[from] || peer, from);
|
|
297
|
+
peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
|
|
296
298
|
};
|
|
297
299
|
|
|
298
300
|
const dhtError = (proto) => {
|
|
@@ -379,7 +381,9 @@ class Identity {
|
|
|
379
381
|
}
|
|
380
382
|
async exportQR(password) {
|
|
381
383
|
const exported = await this.export(password);
|
|
382
|
-
return globalThis.navigator
|
|
384
|
+
return globalThis.navigator
|
|
385
|
+
? await qrcode.toDataURL(exported)
|
|
386
|
+
: await qrcode.toString(exported, { type: 'terminal' });
|
|
383
387
|
}
|
|
384
388
|
async importQR(image, password) {
|
|
385
389
|
const multiWIF = await QrScanner.default.scanImage(image);
|
|
@@ -387,7 +391,7 @@ class Identity {
|
|
|
387
391
|
}
|
|
388
392
|
}
|
|
389
393
|
|
|
390
|
-
globalThis.LeofcoinStorage =
|
|
394
|
+
globalThis.LeofcoinStorage = Storage;
|
|
391
395
|
globalThis.leofcoin = globalThis.leofcoin || {};
|
|
392
396
|
globalThis.pubsub = globalThis.pubsub || new PubSub();
|
|
393
397
|
globalThis.globalSub = globalThis.globalSub || new PubSub();
|
|
@@ -439,25 +443,22 @@ class Peernet {
|
|
|
439
443
|
/**
|
|
440
444
|
* @property {String} network - current network
|
|
441
445
|
*/
|
|
442
|
-
this.network = options.network ||
|
|
446
|
+
this.network = options.network || 'leofcoin';
|
|
443
447
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart;
|
|
444
448
|
this.stars = options.stars;
|
|
445
|
-
const parts = this.network.split(
|
|
446
|
-
this.networkVersion =
|
|
447
|
-
options.networkVersion || parts.length > 1 ? parts[1] : "mainnet";
|
|
449
|
+
const parts = this.network.split(':');
|
|
450
|
+
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
448
451
|
if (!options.storePrefix)
|
|
449
|
-
options.storePrefix =
|
|
452
|
+
options.storePrefix = 'lfc';
|
|
450
453
|
if (!options.port)
|
|
451
454
|
options.port = 2000;
|
|
452
455
|
if (!options.root) {
|
|
453
|
-
parts[1]
|
|
454
|
-
? (options.root = `.${parts[0]}/${parts[1]}`)
|
|
455
|
-
: (options.root = `.${this.network}`);
|
|
456
|
+
parts[1] ? (options.root = `.${parts[0]}/${parts[1]}`) : (options.root = `.${this.network}`);
|
|
456
457
|
}
|
|
457
458
|
globalThis.peernet = this;
|
|
458
459
|
this.bw = {
|
|
459
460
|
up: 0,
|
|
460
|
-
down: 0
|
|
461
|
+
down: 0
|
|
461
462
|
};
|
|
462
463
|
// @ts-ignore
|
|
463
464
|
return this._init(options, password);
|
|
@@ -472,15 +473,7 @@ class Peernet {
|
|
|
472
473
|
return this.identity.accounts;
|
|
473
474
|
}
|
|
474
475
|
get defaultStores() {
|
|
475
|
-
return [
|
|
476
|
-
"account",
|
|
477
|
-
"wallet",
|
|
478
|
-
"block",
|
|
479
|
-
"transaction",
|
|
480
|
-
"chain",
|
|
481
|
-
"data",
|
|
482
|
-
"message",
|
|
483
|
-
];
|
|
476
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message'];
|
|
484
477
|
}
|
|
485
478
|
selectAccount(account) {
|
|
486
479
|
return this.identity.selectAccount(account);
|
|
@@ -493,16 +486,10 @@ class Peernet {
|
|
|
493
486
|
return utils.addCodec(codec);
|
|
494
487
|
}
|
|
495
488
|
async addStore(name, prefix, root, isPrivate = true) {
|
|
496
|
-
if (name ===
|
|
497
|
-
name === "transaction" ||
|
|
498
|
-
name === "chain" ||
|
|
499
|
-
name === "data" ||
|
|
500
|
-
name === "message")
|
|
489
|
+
if (name === 'block' || name === 'transaction' || name === 'chain' || name === 'data' || name === 'message')
|
|
501
490
|
isPrivate = false;
|
|
502
491
|
let Storage;
|
|
503
|
-
this.hasDaemon
|
|
504
|
-
? (Storage = LeofcoinStorageClient)
|
|
505
|
-
: (Storage = LeofcoinStorage);
|
|
492
|
+
this.hasDaemon ? (Storage = LeofcoinStorageClient) : (Storage = LeofcoinStorage);
|
|
506
493
|
if (!globalThis[`${name}Store`]) {
|
|
507
494
|
globalThis[`${name}Store`] = new Storage(name, root);
|
|
508
495
|
await globalThis[`${name}Store`].init();
|
|
@@ -548,9 +535,9 @@ class Peernet {
|
|
|
548
535
|
async _init(options, password) {
|
|
549
536
|
this.storePrefix = options.storePrefix;
|
|
550
537
|
this.root = options.root;
|
|
551
|
-
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
538
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
552
539
|
// FolderMessageResponse
|
|
553
|
-
} = await import(/* webpackChunkName: "messages" */ './messages-
|
|
540
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-42b2109e.js');
|
|
554
541
|
/**
|
|
555
542
|
* proto Object containing protos
|
|
556
543
|
* @type {Object}
|
|
@@ -561,18 +548,18 @@ class Peernet {
|
|
|
561
548
|
* @property {DataMessageResponse} protos[peernet-data-response] messageNode
|
|
562
549
|
*/
|
|
563
550
|
globalThis.peernet.protos = {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
551
|
+
'peernet-request': RequestMessage,
|
|
552
|
+
'peernet-response': ResponseMessage,
|
|
553
|
+
'peernet-peer': PeerMessage,
|
|
554
|
+
'peernet-peer-response': PeerMessageResponse,
|
|
555
|
+
'peernet-message': PeernetMessage,
|
|
556
|
+
'peernet-dht': DHTMessage,
|
|
557
|
+
'peernet-dht-response': DHTMessageResponse,
|
|
558
|
+
'peernet-data': DataMessage,
|
|
559
|
+
'peernet-data-response': DataMessageResponse,
|
|
560
|
+
'peernet-ps': PsMessage,
|
|
561
|
+
'chat-message': ChatMessage,
|
|
562
|
+
'peernet-file': PeernetFile
|
|
576
563
|
};
|
|
577
564
|
this._messageHandler = new MessageHandler(this.network);
|
|
578
565
|
const { daemon, environment } = await target();
|
|
@@ -584,44 +571,44 @@ class Peernet {
|
|
|
584
571
|
await this.identity.load(password);
|
|
585
572
|
this._peerHandler = new PeerDiscovery(this.id);
|
|
586
573
|
this.peerId = this.id;
|
|
587
|
-
this.addRequestHandler(
|
|
588
|
-
return new peernet.protos[
|
|
589
|
-
response: { peerId: this.id }
|
|
574
|
+
this.addRequestHandler('handshake', () => {
|
|
575
|
+
return new peernet.protos['peernet-response']({
|
|
576
|
+
response: { peerId: this.id }
|
|
590
577
|
});
|
|
591
578
|
});
|
|
592
|
-
pubsub.subscribe(
|
|
579
|
+
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
593
580
|
// console.log(peer);
|
|
594
|
-
if (this.requestProtos[
|
|
595
|
-
let data = await new globalThis.peernet.protos[
|
|
596
|
-
request:
|
|
581
|
+
if (this.requestProtos['version']) {
|
|
582
|
+
let data = await new globalThis.peernet.protos['peernet-request']({
|
|
583
|
+
request: 'version'
|
|
597
584
|
});
|
|
598
585
|
let node = await globalThis.peernet.prepareMessage(data);
|
|
599
586
|
let response = await peer.request(node.encoded);
|
|
600
|
-
response = await new globalThis.peernet.protos[
|
|
587
|
+
response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
|
|
601
588
|
peer.version = response.decoded.response.version;
|
|
602
589
|
}
|
|
603
|
-
let data = await new globalThis.peernet.protos[
|
|
604
|
-
request:
|
|
590
|
+
let data = await new globalThis.peernet.protos['peernet-request']({
|
|
591
|
+
request: 'handshake'
|
|
605
592
|
});
|
|
606
593
|
let node = await globalThis.peernet.prepareMessage(data);
|
|
607
594
|
let response = await peer.request(node.encoded);
|
|
608
|
-
response = await new globalThis.peernet.protos[
|
|
595
|
+
response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
|
|
609
596
|
// todo: response.decoded should be the response and not response.peerId
|
|
610
597
|
this.#connections[response.decoded.response.peerId] = peer;
|
|
611
|
-
pubsub.publish(
|
|
598
|
+
pubsub.publish('peer:connected', peer);
|
|
612
599
|
// todo: cleanup discovered
|
|
613
600
|
});
|
|
614
|
-
pubsub.subscribe(
|
|
601
|
+
pubsub.subscribe('peer:left', this.#peerLeft.bind(this));
|
|
615
602
|
/**
|
|
616
603
|
* converts data -> message -> proto
|
|
617
604
|
* @see DataHandler
|
|
618
605
|
*/
|
|
619
|
-
pubsub.subscribe(
|
|
606
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
620
607
|
if (globalThis.navigator) {
|
|
621
|
-
globalThis.addEventListener(
|
|
608
|
+
globalThis.addEventListener('beforeunload', async () => this.client.destroy());
|
|
622
609
|
}
|
|
623
610
|
else {
|
|
624
|
-
process.on(
|
|
611
|
+
process.on('SIGTERM', async () => {
|
|
625
612
|
process.stdin.resume();
|
|
626
613
|
try {
|
|
627
614
|
await this.client.destroy();
|
|
@@ -652,9 +639,7 @@ class Peernet {
|
|
|
652
639
|
}
|
|
653
640
|
#peerLeft(peer) {
|
|
654
641
|
for (const [id, _peer] of Object.entries(this.#connections)) {
|
|
655
|
-
if (_peer.id === peer.id &&
|
|
656
|
-
this.#connections[id] &&
|
|
657
|
-
!this.#connections[id].connected) {
|
|
642
|
+
if (_peer.id === peer.id && this.#connections[id] && !this.#connections[id].connected) {
|
|
658
643
|
delete this.#connections[id];
|
|
659
644
|
this.removePeer(_peer);
|
|
660
645
|
}
|
|
@@ -681,9 +666,9 @@ class Peernet {
|
|
|
681
666
|
else {
|
|
682
667
|
has = await this.has(hash);
|
|
683
668
|
}
|
|
684
|
-
const data = await new globalThis.peernet.protos[
|
|
669
|
+
const data = await new globalThis.peernet.protos['peernet-dht-response']({
|
|
685
670
|
hash,
|
|
686
|
-
has
|
|
671
|
+
has
|
|
687
672
|
});
|
|
688
673
|
const node = await this.prepareMessage(data);
|
|
689
674
|
this.sendMessage(peer, id, node.encoded);
|
|
@@ -691,15 +676,13 @@ class Peernet {
|
|
|
691
676
|
async handleData(peer, id, proto) {
|
|
692
677
|
let { hash, store } = proto.decoded;
|
|
693
678
|
let data;
|
|
694
|
-
store =
|
|
695
|
-
globalThis[`${store}Store`] ||
|
|
696
|
-
(await this.whichStore([...this.stores], hash));
|
|
679
|
+
store = globalThis[`${store}Store`] || (await this.whichStore([...this.stores], hash));
|
|
697
680
|
if (store && !store.private) {
|
|
698
681
|
data = await store.get(hash);
|
|
699
682
|
if (data) {
|
|
700
|
-
data = await new globalThis.peernet.protos[
|
|
683
|
+
data = await new globalThis.peernet.protos['peernet-data-response']({
|
|
701
684
|
hash,
|
|
702
|
-
data
|
|
685
|
+
data
|
|
703
686
|
});
|
|
704
687
|
const node = await this.prepareMessage(data);
|
|
705
688
|
this.sendMessage(peer, id, node.encoded);
|
|
@@ -724,19 +707,19 @@ class Peernet {
|
|
|
724
707
|
const { id, proto } = message;
|
|
725
708
|
this.bw.down += proto.encoded.length;
|
|
726
709
|
switch (proto.name) {
|
|
727
|
-
case
|
|
710
|
+
case 'peernet-dht': {
|
|
728
711
|
this.handleDHT(peer, id, proto);
|
|
729
712
|
break;
|
|
730
713
|
}
|
|
731
|
-
case
|
|
714
|
+
case 'peernet-data': {
|
|
732
715
|
this.handleData(peer, id, proto);
|
|
733
716
|
break;
|
|
734
717
|
}
|
|
735
|
-
case
|
|
718
|
+
case 'peernet-request': {
|
|
736
719
|
this.handleRequest(peer, id, proto);
|
|
737
720
|
break;
|
|
738
721
|
}
|
|
739
|
-
case
|
|
722
|
+
case 'peernet-ps': {
|
|
740
723
|
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
741
724
|
}
|
|
742
725
|
}
|
|
@@ -748,18 +731,18 @@ class Peernet {
|
|
|
748
731
|
*/
|
|
749
732
|
async walk(hash) {
|
|
750
733
|
if (!hash)
|
|
751
|
-
throw new Error(
|
|
752
|
-
const data = await new globalThis.peernet.protos[
|
|
734
|
+
throw new Error('hash expected, received undefined');
|
|
735
|
+
const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
|
|
753
736
|
const walk = async (peer, peerId) => {
|
|
754
737
|
const node = await this.prepareMessage(data);
|
|
755
738
|
let result = await peer.request(node.encoded);
|
|
756
739
|
result = new Uint8Array(Object.values(result));
|
|
757
740
|
const proto = await protoFor(result);
|
|
758
|
-
if (proto.name !==
|
|
741
|
+
if (proto.name !== 'peernet-dht-response')
|
|
759
742
|
throw dhtError(proto.name);
|
|
760
743
|
const peerInfo = {
|
|
761
744
|
...peer.connectionStats,
|
|
762
|
-
id: peerId
|
|
745
|
+
id: peerId
|
|
763
746
|
};
|
|
764
747
|
if (proto.decoded.has)
|
|
765
748
|
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
@@ -781,8 +764,7 @@ class Peernet {
|
|
|
781
764
|
let providers = this.dht.providersFor(hash);
|
|
782
765
|
// walk the network to find a provider
|
|
783
766
|
let tries = 0;
|
|
784
|
-
while ((!providers && tries < 3) ||
|
|
785
|
-
(Object.keys(providers).length === 0 && tries < 3)) {
|
|
767
|
+
while ((!providers && tries < 3) || (Object.keys(providers).length === 0 && tries < 3)) {
|
|
786
768
|
tries += 1;
|
|
787
769
|
await this.walk(hash);
|
|
788
770
|
providers = this.dht.providersFor(hash);
|
|
@@ -796,14 +778,14 @@ class Peernet {
|
|
|
796
778
|
const data = await blockStore.has(hash);
|
|
797
779
|
if (data)
|
|
798
780
|
return blockStore.get(hash);
|
|
799
|
-
return this.requestData(hash,
|
|
781
|
+
return this.requestData(hash, 'block');
|
|
800
782
|
},
|
|
801
783
|
put: async (hash, data) => {
|
|
802
784
|
if (await blockStore.has(hash))
|
|
803
785
|
return;
|
|
804
786
|
return await blockStore.put(hash, data);
|
|
805
787
|
},
|
|
806
|
-
has: async (hash) => await blockStore.has(hash)
|
|
788
|
+
has: async (hash) => await blockStore.has(hash)
|
|
807
789
|
};
|
|
808
790
|
}
|
|
809
791
|
get transaction() {
|
|
@@ -812,14 +794,14 @@ class Peernet {
|
|
|
812
794
|
const data = await transactionStore.has(hash);
|
|
813
795
|
if (data)
|
|
814
796
|
return await transactionStore.get(hash);
|
|
815
|
-
return this.requestData(hash,
|
|
797
|
+
return this.requestData(hash, 'transaction');
|
|
816
798
|
},
|
|
817
799
|
put: async (hash, data) => {
|
|
818
800
|
if (await transactionStore.has(hash))
|
|
819
801
|
return;
|
|
820
802
|
return await transactionStore.put(hash, data);
|
|
821
803
|
},
|
|
822
|
-
has: async (hash) => await transactionStore.has(hash)
|
|
804
|
+
has: async (hash) => await transactionStore.has(hash)
|
|
823
805
|
};
|
|
824
806
|
}
|
|
825
807
|
async requestData(hash, store) {
|
|
@@ -835,9 +817,9 @@ class Peernet {
|
|
|
835
817
|
const id = closestPeer.id;
|
|
836
818
|
const peer = this.#connections[id];
|
|
837
819
|
if (peer?.connected) {
|
|
838
|
-
let data = await new globalThis.peernet.protos[
|
|
820
|
+
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
839
821
|
hash,
|
|
840
|
-
store: store?.name || store
|
|
822
|
+
store: store?.name || store
|
|
841
823
|
});
|
|
842
824
|
const node = await this.prepareMessage(data);
|
|
843
825
|
if (peer)
|
|
@@ -877,7 +859,7 @@ class Peernet {
|
|
|
877
859
|
const message = await messageStore.has(hash);
|
|
878
860
|
if (message)
|
|
879
861
|
return await messageStore.get(hash);
|
|
880
|
-
return this.requestData(hash,
|
|
862
|
+
return this.requestData(hash, 'message');
|
|
881
863
|
},
|
|
882
864
|
/**
|
|
883
865
|
* put message content
|
|
@@ -890,7 +872,7 @@ class Peernet {
|
|
|
890
872
|
* @param {String} hash
|
|
891
873
|
* @return {Boolean}
|
|
892
874
|
*/
|
|
893
|
-
has: async (hash) => await messageStore.has(hash)
|
|
875
|
+
has: async (hash) => await messageStore.has(hash)
|
|
894
876
|
};
|
|
895
877
|
}
|
|
896
878
|
get data() {
|
|
@@ -905,7 +887,7 @@ class Peernet {
|
|
|
905
887
|
const data = await dataStore.has(hash);
|
|
906
888
|
if (data)
|
|
907
889
|
return await dataStore.get(hash);
|
|
908
|
-
return this.requestData(hash,
|
|
890
|
+
return this.requestData(hash, 'data');
|
|
909
891
|
},
|
|
910
892
|
/**
|
|
911
893
|
* put data content
|
|
@@ -918,7 +900,7 @@ class Peernet {
|
|
|
918
900
|
* @param {String} hash
|
|
919
901
|
* @return {Boolean}
|
|
920
902
|
*/
|
|
921
|
-
has: async (hash) => await dataStore.has(hash)
|
|
903
|
+
has: async (hash) => await dataStore.has(hash)
|
|
922
904
|
};
|
|
923
905
|
}
|
|
924
906
|
get folder() {
|
|
@@ -933,7 +915,7 @@ class Peernet {
|
|
|
933
915
|
const data = await dataStore.has(hash);
|
|
934
916
|
if (data)
|
|
935
917
|
return await dataStore.get(hash);
|
|
936
|
-
return this.requestData(hash,
|
|
918
|
+
return this.requestData(hash, 'data');
|
|
937
919
|
},
|
|
938
920
|
/**
|
|
939
921
|
* put data content
|
|
@@ -946,20 +928,20 @@ class Peernet {
|
|
|
946
928
|
* @param {String} hash
|
|
947
929
|
* @return {Boolean}
|
|
948
930
|
*/
|
|
949
|
-
has: async (hash) => await dataStore.has(hash)
|
|
931
|
+
has: async (hash) => await dataStore.has(hash)
|
|
950
932
|
};
|
|
951
933
|
}
|
|
952
934
|
async addFolder(files) {
|
|
953
935
|
const links = [];
|
|
954
936
|
for (const file of files) {
|
|
955
|
-
const fileNode = await new globalThis.peernet.protos[
|
|
937
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
|
|
956
938
|
const hash = await fileNode.hash;
|
|
957
939
|
await dataStore.put(hash, fileNode.encoded);
|
|
958
940
|
links.push({ hash, path: file.path });
|
|
959
941
|
}
|
|
960
|
-
const node = await new globalThis.peernet.protos[
|
|
961
|
-
path:
|
|
962
|
-
links
|
|
942
|
+
const node = await new globalThis.peernet.protos['peernet-file']({
|
|
943
|
+
path: '/',
|
|
944
|
+
links
|
|
963
945
|
});
|
|
964
946
|
const hash = await node.hash;
|
|
965
947
|
await dataStore.put(hash, node.encoded);
|
|
@@ -968,10 +950,8 @@ class Peernet {
|
|
|
968
950
|
async ls(hash, options) {
|
|
969
951
|
let data;
|
|
970
952
|
const has = await dataStore.has(hash);
|
|
971
|
-
data = has
|
|
972
|
-
|
|
973
|
-
: await this.requestData(hash, "data");
|
|
974
|
-
const node = await new peernet.protos["peernet-file"](data);
|
|
953
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
954
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
975
955
|
await node.decode();
|
|
976
956
|
console.log(data);
|
|
977
957
|
const paths = [];
|
|
@@ -987,10 +967,8 @@ class Peernet {
|
|
|
987
967
|
async cat(hash, options) {
|
|
988
968
|
let data;
|
|
989
969
|
const has = await dataStore.has(hash);
|
|
990
|
-
data = has
|
|
991
|
-
|
|
992
|
-
: await this.requestData(hash, "data");
|
|
993
|
-
const node = await new peernet.protos["peernet-file"](data);
|
|
970
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
971
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
994
972
|
if (node.decoded?.links.length > 0)
|
|
995
973
|
throw new Error(`${hash} is a directory`);
|
|
996
974
|
if (options?.pin)
|
|
@@ -1041,7 +1019,7 @@ class Peernet {
|
|
|
1041
1019
|
* @param {Buffer} data
|
|
1042
1020
|
* @param {String} storeName - storeName to access
|
|
1043
1021
|
*/
|
|
1044
|
-
async put(hash, data, storeName =
|
|
1022
|
+
async put(hash, data, storeName = 'data') {
|
|
1045
1023
|
const store = globalThis[`${storeName}Store`];
|
|
1046
1024
|
return store.put(hash, data);
|
|
1047
1025
|
}
|
|
@@ -1064,7 +1042,7 @@ class Peernet {
|
|
|
1064
1042
|
async publish(topic, data) {
|
|
1065
1043
|
// globalSub.publish(topic, data)
|
|
1066
1044
|
const id = Math.random().toString(36).slice(-12);
|
|
1067
|
-
data = await new globalThis.peernet.protos[
|
|
1045
|
+
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
1068
1046
|
for (const [peerId, peer] of Object.entries(this.#connections)) {
|
|
1069
1047
|
if (peerId !== this.id) {
|
|
1070
1048
|
const node = await this.prepareMessage(data);
|
|
@@ -1086,7 +1064,7 @@ class Peernet {
|
|
|
1086
1064
|
globalSub.subscribe(topic, callback);
|
|
1087
1065
|
}
|
|
1088
1066
|
async removePeer(peer) {
|
|
1089
|
-
console.log(
|
|
1067
|
+
console.log('removepeer', peer.id);
|
|
1090
1068
|
const id = peer.id;
|
|
1091
1069
|
await this.client._removePeer(peer);
|
|
1092
1070
|
if (this.client.peers[id]) {
|
|
@@ -1094,7 +1072,7 @@ class Peernet {
|
|
|
1094
1072
|
// if (this.client.peers[id][connection].connected === false) delete this.client.peers[id][connection]
|
|
1095
1073
|
// @ts-ignore
|
|
1096
1074
|
if (this.client.peers[id][connection].connected)
|
|
1097
|
-
return this.client.emit(
|
|
1075
|
+
return this.client.emit('peerconnect', connection);
|
|
1098
1076
|
}
|
|
1099
1077
|
}
|
|
1100
1078
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import inquirer from 'inquirer';
|
|
2
2
|
|
|
3
3
|
var node = async () => {
|
|
4
|
-
const answers = await inquirer.prompt({type: 'password', name: 'password', message: 'Enter password'});
|
|
4
|
+
const answers = await inquirer.prompt({ type: 'password', name: 'password', message: 'Enter password' });
|
|
5
5
|
return answers.password
|
|
6
6
|
};
|
|
7
7
|
|
package/exports/store.js
CHANGED
|
@@ -111,7 +111,9 @@ class Store {
|
|
|
111
111
|
return value.uint8Array;
|
|
112
112
|
}
|
|
113
113
|
toUint8Array(buffer) {
|
|
114
|
-
return Buffer.isBuffer(buffer)
|
|
114
|
+
return Buffer.isBuffer(buffer)
|
|
115
|
+
? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT)
|
|
116
|
+
: buffer;
|
|
115
117
|
}
|
|
116
118
|
async get(key) {
|
|
117
119
|
return this.toUint8Array(await this.db.get(this.toKeyPath(key)));
|