@leofcoin/peernet 0.11.5 → 0.11.8
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/326.peernet.js +29 -1
- package/dist/browser/peernet.js +85129 -2
- package/dist/commonjs/{codec-8c8c652f.js → codec-45796010.js} +3 -3
- package/dist/commonjs/codec-format-interface.js +23 -1
- package/dist/commonjs/codec.js +1 -1
- package/dist/commonjs/dht-response.js +1 -1
- package/dist/commonjs/dht.js +1 -1
- package/dist/commonjs/hash.js +9 -12
- package/dist/commonjs/{http-ee1b2071.js → http-12850e18.js} +1 -1
- package/dist/commonjs/peernet-message.js +1 -1
- package/dist/commonjs/peernet.js +36 -24
- package/dist/commonjs/request.js +1 -1
- package/dist/commonjs/response.js +1 -1
- package/dist/module/peernet.js +67 -36
- package/package.json +2 -2
- package/src/codec/codec-format-interface.js +22 -0
- package/src/codec/codecs.js +3 -3
- package/src/handlers/message.js +1 -1
- package/src/hash/hash.js +8 -11
- package/src/peernet.js +19 -14
- package/test4.js +7 -0
- package/webpack.config.js +1 -1
- package/dist/browser/peernet.js.LICENSE.txt +0 -40
|
@@ -16,7 +16,7 @@ var codecs = {
|
|
|
16
16
|
// just a hash
|
|
17
17
|
'disco-hash': {
|
|
18
18
|
codec: parseInt('30', 16),
|
|
19
|
-
hashAlg: 'dbl-keccak-
|
|
19
|
+
hashAlg: 'dbl-keccak-256', // ,
|
|
20
20
|
// testnet: 'olivia'
|
|
21
21
|
},
|
|
22
22
|
'peernet-peer-response': {
|
|
@@ -47,7 +47,7 @@ var codecs = {
|
|
|
47
47
|
// message
|
|
48
48
|
'peernet-message': {
|
|
49
49
|
codec: parseInt('706d65', 16),
|
|
50
|
-
hashAlg: 'keccak-
|
|
50
|
+
hashAlg: 'keccak-256',
|
|
51
51
|
},
|
|
52
52
|
// pubsub
|
|
53
53
|
'peernet-ps': {
|
|
@@ -88,7 +88,7 @@ var codecs = {
|
|
|
88
88
|
// chat message
|
|
89
89
|
'chat-message': {
|
|
90
90
|
codec: parseInt('636d', 16),
|
|
91
|
-
hashAlg: 'dbl-keccak-
|
|
91
|
+
hashAlg: 'dbl-keccak-256',
|
|
92
92
|
},
|
|
93
93
|
};
|
|
94
94
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var bs32 = require('@vandeurenglenn/base32');
|
|
4
4
|
var bs58 = require('@vandeurenglenn/base58');
|
|
5
5
|
var isHex = require('@vandeurenglenn/is-hex');
|
|
6
|
-
var codec = require('./codec-
|
|
6
|
+
var codec = require('./codec-45796010.js');
|
|
7
7
|
var hash = require('./hash.js');
|
|
8
8
|
require('varint');
|
|
9
9
|
require('keccak');
|
|
@@ -102,6 +102,28 @@ class FormatInterface {
|
|
|
102
102
|
else this.decode();
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
toString() {
|
|
106
|
+
return this.encoded.toString()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async toArray() {
|
|
110
|
+
const array = [];
|
|
111
|
+
for await (const value of this.encoded.values()) {
|
|
112
|
+
array.push(value);
|
|
113
|
+
}
|
|
114
|
+
return array
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
fromString(string) {
|
|
118
|
+
this.encoded = new Uint8Array(string.split(','));
|
|
119
|
+
this.decode();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fromArray(array) {
|
|
123
|
+
this.encoded = new Uint8Array([...array]);
|
|
124
|
+
this.decode();
|
|
125
|
+
}
|
|
126
|
+
|
|
105
127
|
/**
|
|
106
128
|
* @param {Buffer} encoded
|
|
107
129
|
*/
|
package/dist/commonjs/codec.js
CHANGED
|
@@ -5,7 +5,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
5
5
|
require('@vandeurenglenn/base32');
|
|
6
6
|
require('@vandeurenglenn/base58');
|
|
7
7
|
require('@vandeurenglenn/is-hex');
|
|
8
|
-
require('./codec-
|
|
8
|
+
require('./codec-45796010.js');
|
|
9
9
|
require('varint');
|
|
10
10
|
require('./hash.js');
|
|
11
11
|
require('keccak');
|
package/dist/commonjs/dht.js
CHANGED
|
@@ -5,7 +5,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
5
5
|
require('@vandeurenglenn/base32');
|
|
6
6
|
require('@vandeurenglenn/base58');
|
|
7
7
|
require('@vandeurenglenn/is-hex');
|
|
8
|
-
require('./codec-
|
|
8
|
+
require('./codec-45796010.js');
|
|
9
9
|
require('varint');
|
|
10
10
|
require('./hash.js');
|
|
11
11
|
require('keccak');
|
package/dist/commonjs/hash.js
CHANGED
|
@@ -5,7 +5,7 @@ var varint = require('varint');
|
|
|
5
5
|
var bs32 = require('@vandeurenglenn/base32');
|
|
6
6
|
var bs58 = require('@vandeurenglenn/base58');
|
|
7
7
|
var isHex = require('@vandeurenglenn/is-hex');
|
|
8
|
-
var codec = require('./codec-
|
|
8
|
+
var codec = require('./codec-45796010.js');
|
|
9
9
|
|
|
10
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ class PeernetHash {
|
|
|
21
21
|
else this.name = 'disco-hash';
|
|
22
22
|
if (options.codecs) this.codecs = options.codecs;
|
|
23
23
|
if (buffer) {
|
|
24
|
-
if (
|
|
24
|
+
if (buffer instanceof Uint8Array) {
|
|
25
25
|
this.discoCodec = new codec.PeernetCodec(buffer, this.codecs);
|
|
26
26
|
const name = this.discoCodec.name;
|
|
27
27
|
|
|
@@ -45,13 +45,9 @@ class PeernetHash {
|
|
|
45
45
|
get prefix() {
|
|
46
46
|
const length = this.length;
|
|
47
47
|
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
48
|
+
uint8Array.set(length);
|
|
49
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
51
50
|
|
|
52
|
-
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
53
|
-
uint8Array[i] = length[i];
|
|
54
|
-
}
|
|
55
51
|
return uint8Array
|
|
56
52
|
}
|
|
57
53
|
|
|
@@ -110,10 +106,11 @@ class PeernetHash {
|
|
|
110
106
|
|
|
111
107
|
this.codec = this.discoCodec.encode();
|
|
112
108
|
this.codec = this.discoCodec.codecBuffer;
|
|
113
|
-
this.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
109
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
110
|
+
uint8Array.set(this.prefix);
|
|
111
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
112
|
+
|
|
113
|
+
this.hash = uint8Array;
|
|
117
114
|
|
|
118
115
|
return this.hash
|
|
119
116
|
}
|
|
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
11
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
12
|
|
|
13
|
-
var version = "0.11.
|
|
13
|
+
var version = "0.11.7";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -5,7 +5,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
5
5
|
require('@vandeurenglenn/base32');
|
|
6
6
|
require('@vandeurenglenn/base58');
|
|
7
7
|
require('@vandeurenglenn/is-hex');
|
|
8
|
-
require('./codec-
|
|
8
|
+
require('./codec-45796010.js');
|
|
9
9
|
require('varint');
|
|
10
10
|
require('./hash.js');
|
|
11
11
|
require('keccak');
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -9,7 +9,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
9
9
|
var request = require('./request.js');
|
|
10
10
|
var response = require('./response.js');
|
|
11
11
|
var fetch = require('node-fetch');
|
|
12
|
-
var codec = require('./codec-
|
|
12
|
+
var codec = require('./codec-45796010.js');
|
|
13
13
|
var hash = require('./hash.js');
|
|
14
14
|
var generateAccount = require('@leofcoin/generate-account');
|
|
15
15
|
var bs58check = require('bs58check');
|
|
@@ -307,6 +307,7 @@ class Peer {
|
|
|
307
307
|
#senderMap = new Map()
|
|
308
308
|
#iceCompleteTimer
|
|
309
309
|
#channel
|
|
310
|
+
#peerId
|
|
310
311
|
|
|
311
312
|
get connection() {
|
|
312
313
|
return this.#connection
|
|
@@ -333,12 +334,17 @@ constructor(options = {}) {
|
|
|
333
334
|
down: 0
|
|
334
335
|
};
|
|
335
336
|
|
|
336
|
-
this.channelName = options.channelName
|
|
337
|
-
|
|
337
|
+
this.channelName = options.channelName;
|
|
338
|
+
|
|
339
|
+
this.#peerId = options.peerId;
|
|
338
340
|
this.options = options;
|
|
339
341
|
this.#init();
|
|
340
342
|
}
|
|
341
343
|
|
|
344
|
+
get peerId() {
|
|
345
|
+
return this.#peerId
|
|
346
|
+
}
|
|
347
|
+
|
|
342
348
|
set socketClient(value) {
|
|
343
349
|
// this.socketClient?.pubsub.unsubscribe('signal', this._in)
|
|
344
350
|
this._socketClient = value;
|
|
@@ -357,6 +363,7 @@ constructor(options = {}) {
|
|
|
357
363
|
request(data) {
|
|
358
364
|
return new Promise((resolve, reject) => {
|
|
359
365
|
const id = Math.random().toString(36).slice(-12);
|
|
366
|
+
// TODO: get rid of JSON
|
|
360
367
|
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
361
368
|
const _onData = message => {
|
|
362
369
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
@@ -405,7 +412,7 @@ constructor(options = {}) {
|
|
|
405
412
|
if (message.to) {
|
|
406
413
|
if (message.to === this.id) pubsub.publish('peer:data', message);
|
|
407
414
|
} else pubsub.publish('peer:data', message);
|
|
408
|
-
this.bw.down += message.length;
|
|
415
|
+
this.bw.down += message.length || message.byteLength;
|
|
409
416
|
};
|
|
410
417
|
this.channel = message.channel;
|
|
411
418
|
};
|
|
@@ -423,7 +430,7 @@ constructor(options = {}) {
|
|
|
423
430
|
if (message.to) {
|
|
424
431
|
if (message.to === this.id) pubsub.publish('peer:data', message);
|
|
425
432
|
} else pubsub.publish('peer:data', message);
|
|
426
|
-
this.bw.down += message.length;
|
|
433
|
+
this.bw.down += message.length || message.byteLength;
|
|
427
434
|
};
|
|
428
435
|
|
|
429
436
|
const offer = await this.#connection.createOffer();
|
|
@@ -526,7 +533,7 @@ class Client {
|
|
|
526
533
|
}
|
|
527
534
|
const peers = await this.socketClient.peernet.join({id: this.id});
|
|
528
535
|
for (const id of peers) {
|
|
529
|
-
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
536
|
+
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
530
537
|
}
|
|
531
538
|
this.setupListeners();
|
|
532
539
|
|
|
@@ -585,7 +592,7 @@ class Client {
|
|
|
585
592
|
delete this.#connections[id];
|
|
586
593
|
}
|
|
587
594
|
// reconnect
|
|
588
|
-
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
595
|
+
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
589
596
|
}
|
|
590
597
|
|
|
591
598
|
}
|
|
@@ -615,7 +622,7 @@ class Client {
|
|
|
615
622
|
delete this.#connections[id];
|
|
616
623
|
}
|
|
617
624
|
// RTCPeerConnection
|
|
618
|
-
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
625
|
+
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
619
626
|
console.log(`peer ${id} joined`);
|
|
620
627
|
}
|
|
621
628
|
|
|
@@ -1437,7 +1444,7 @@ class MessageHandler {
|
|
|
1437
1444
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
1438
1445
|
const wallet = new MultiWallet(this.network);
|
|
1439
1446
|
wallet.import(identity.multiWIF);
|
|
1440
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
1447
|
+
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
1441
1448
|
}
|
|
1442
1449
|
|
|
1443
1450
|
/**
|
|
@@ -1636,7 +1643,7 @@ class Peernet {
|
|
|
1636
1643
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1637
1644
|
});
|
|
1638
1645
|
} else {
|
|
1639
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1646
|
+
const http = await Promise.resolve().then(function () { return require('./http-12850e18.js'); });
|
|
1640
1647
|
if (environment !== 'browser') http.default(options);
|
|
1641
1648
|
}
|
|
1642
1649
|
|
|
@@ -1719,6 +1726,7 @@ class Peernet {
|
|
|
1719
1726
|
* @param {PeernetPeer} peer - peernet peer
|
|
1720
1727
|
*/
|
|
1721
1728
|
async _protoHandler(message, peer, from) {
|
|
1729
|
+
|
|
1722
1730
|
const {id, proto} = message;
|
|
1723
1731
|
this.bw.down += proto.encoded.length;
|
|
1724
1732
|
|
|
@@ -1769,8 +1777,7 @@ class Peernet {
|
|
|
1769
1777
|
|
|
1770
1778
|
this.bw.up += node.encoded.length;
|
|
1771
1779
|
}
|
|
1772
|
-
} else if (proto.name === 'peernet-ps' &&
|
|
1773
|
-
this._getPeerId(peer.id) !== this.id.toString()) {
|
|
1780
|
+
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
1774
1781
|
globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
|
|
1775
1782
|
}
|
|
1776
1783
|
// }
|
|
@@ -1785,12 +1792,11 @@ class Peernet {
|
|
|
1785
1792
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
1786
1793
|
const data = new dht({hash});
|
|
1787
1794
|
this.client.id;
|
|
1788
|
-
|
|
1789
|
-
const node = await this.prepareMessage(peer.
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
let proto = protoFor(result.data);
|
|
1795
|
+
const walk = async peer => {
|
|
1796
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
1797
|
+
let result = await peer.request(node.encoded);
|
|
1798
|
+
result = new Uint8Array(Object.values(result));
|
|
1799
|
+
let proto = protoFor(result);
|
|
1794
1800
|
|
|
1795
1801
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
1796
1802
|
const from = proto.decoded.from;
|
|
@@ -1813,8 +1819,14 @@ class Peernet {
|
|
|
1813
1819
|
};
|
|
1814
1820
|
|
|
1815
1821
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
1822
|
+
};
|
|
1823
|
+
let walks = [];
|
|
1824
|
+
for (const peer of this.connections) {
|
|
1825
|
+
if (peer.peerId !== this.id) {
|
|
1826
|
+
walks.push(walk(peer));
|
|
1827
|
+
}
|
|
1816
1828
|
}
|
|
1817
|
-
return
|
|
1829
|
+
return Promise.all(walks)
|
|
1818
1830
|
}
|
|
1819
1831
|
|
|
1820
1832
|
/**
|
|
@@ -1885,7 +1897,7 @@ class Peernet {
|
|
|
1885
1897
|
const id = closestPeer.id.toString();
|
|
1886
1898
|
if (this.connections) {
|
|
1887
1899
|
let closest = this.connections.filter((peer) => {
|
|
1888
|
-
if (peer.
|
|
1900
|
+
if (peer.peerId === id) return peer
|
|
1889
1901
|
});
|
|
1890
1902
|
|
|
1891
1903
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -1894,7 +1906,7 @@ class Peernet {
|
|
|
1894
1906
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1895
1907
|
else {
|
|
1896
1908
|
closest = this.connections.filter((peer) => {
|
|
1897
|
-
if (peer.
|
|
1909
|
+
if (peer.peerId === id) return peer
|
|
1898
1910
|
});
|
|
1899
1911
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1900
1912
|
}
|
|
@@ -2037,8 +2049,8 @@ class Peernet {
|
|
|
2037
2049
|
data = new PsMessage({data, topic});
|
|
2038
2050
|
for (const peer of this.connections) {
|
|
2039
2051
|
if (peer.connected) {
|
|
2040
|
-
if (peer.
|
|
2041
|
-
const node = await this.prepareMessage(peer.
|
|
2052
|
+
if (peer.peerId !== this.peerId) {
|
|
2053
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2042
2054
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2043
2055
|
}
|
|
2044
2056
|
} else {
|
|
@@ -2063,7 +2075,7 @@ class Peernet {
|
|
|
2063
2075
|
}
|
|
2064
2076
|
|
|
2065
2077
|
async removePeer(peer) {
|
|
2066
|
-
delete this.client.connections[peer.
|
|
2078
|
+
delete this.client.connections[peer.peerId];
|
|
2067
2079
|
}
|
|
2068
2080
|
|
|
2069
2081
|
get Buffer() {
|
package/dist/commonjs/request.js
CHANGED
|
@@ -5,7 +5,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
5
5
|
require('@vandeurenglenn/base32');
|
|
6
6
|
require('@vandeurenglenn/base58');
|
|
7
7
|
require('@vandeurenglenn/is-hex');
|
|
8
|
-
require('./codec-
|
|
8
|
+
require('./codec-45796010.js');
|
|
9
9
|
require('varint');
|
|
10
10
|
require('./hash.js');
|
|
11
11
|
require('keccak');
|
|
@@ -5,7 +5,7 @@ var codecFormatInterface = require('./codec-format-interface.js');
|
|
|
5
5
|
require('@vandeurenglenn/base32');
|
|
6
6
|
require('@vandeurenglenn/base58');
|
|
7
7
|
require('@vandeurenglenn/is-hex');
|
|
8
|
-
require('./codec-
|
|
8
|
+
require('./codec-45796010.js');
|
|
9
9
|
require('varint');
|
|
10
10
|
require('./hash.js');
|
|
11
11
|
require('keccak');
|
package/dist/module/peernet.js
CHANGED
|
@@ -264,6 +264,7 @@ class Peer {
|
|
|
264
264
|
#senderMap = new Map()
|
|
265
265
|
#iceCompleteTimer
|
|
266
266
|
#channel
|
|
267
|
+
#peerId
|
|
267
268
|
|
|
268
269
|
get connection() {
|
|
269
270
|
return this.#connection
|
|
@@ -290,12 +291,17 @@ constructor(options = {}) {
|
|
|
290
291
|
down: 0
|
|
291
292
|
};
|
|
292
293
|
|
|
293
|
-
this.channelName = options.channelName
|
|
294
|
-
|
|
294
|
+
this.channelName = options.channelName;
|
|
295
|
+
|
|
296
|
+
this.#peerId = options.peerId;
|
|
295
297
|
this.options = options;
|
|
296
298
|
this.#init();
|
|
297
299
|
}
|
|
298
300
|
|
|
301
|
+
get peerId() {
|
|
302
|
+
return this.#peerId
|
|
303
|
+
}
|
|
304
|
+
|
|
299
305
|
set socketClient(value) {
|
|
300
306
|
// this.socketClient?.pubsub.unsubscribe('signal', this._in)
|
|
301
307
|
this._socketClient = value;
|
|
@@ -314,6 +320,7 @@ constructor(options = {}) {
|
|
|
314
320
|
request(data) {
|
|
315
321
|
return new Promise((resolve, reject) => {
|
|
316
322
|
const id = Math.random().toString(36).slice(-12);
|
|
323
|
+
// TODO: get rid of JSON
|
|
317
324
|
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
318
325
|
const _onData = message => {
|
|
319
326
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
@@ -362,7 +369,7 @@ constructor(options = {}) {
|
|
|
362
369
|
if (message.to) {
|
|
363
370
|
if (message.to === this.id) pubsub.publish('peer:data', message);
|
|
364
371
|
} else pubsub.publish('peer:data', message);
|
|
365
|
-
this.bw.down += message.length;
|
|
372
|
+
this.bw.down += message.length || message.byteLength;
|
|
366
373
|
};
|
|
367
374
|
this.channel = message.channel;
|
|
368
375
|
};
|
|
@@ -380,7 +387,7 @@ constructor(options = {}) {
|
|
|
380
387
|
if (message.to) {
|
|
381
388
|
if (message.to === this.id) pubsub.publish('peer:data', message);
|
|
382
389
|
} else pubsub.publish('peer:data', message);
|
|
383
|
-
this.bw.down += message.length;
|
|
390
|
+
this.bw.down += message.length || message.byteLength;
|
|
384
391
|
};
|
|
385
392
|
|
|
386
393
|
const offer = await this.#connection.createOffer();
|
|
@@ -483,7 +490,7 @@ class Client {
|
|
|
483
490
|
}
|
|
484
491
|
const peers = await this.socketClient.peernet.join({id: this.id});
|
|
485
492
|
for (const id of peers) {
|
|
486
|
-
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
493
|
+
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
487
494
|
}
|
|
488
495
|
this.setupListeners();
|
|
489
496
|
|
|
@@ -542,7 +549,7 @@ class Client {
|
|
|
542
549
|
delete this.#connections[id];
|
|
543
550
|
}
|
|
544
551
|
// reconnect
|
|
545
|
-
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
552
|
+
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
546
553
|
}
|
|
547
554
|
|
|
548
555
|
}
|
|
@@ -572,7 +579,7 @@ class Client {
|
|
|
572
579
|
delete this.#connections[id];
|
|
573
580
|
}
|
|
574
581
|
// RTCPeerConnection
|
|
575
|
-
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
582
|
+
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
576
583
|
console.log(`peer ${id} joined`);
|
|
577
584
|
}
|
|
578
585
|
|
|
@@ -630,7 +637,7 @@ var codecs = {
|
|
|
630
637
|
// just a hash
|
|
631
638
|
'disco-hash': {
|
|
632
639
|
codec: parseInt('30', 16),
|
|
633
|
-
hashAlg: 'dbl-keccak-
|
|
640
|
+
hashAlg: 'dbl-keccak-256', // ,
|
|
634
641
|
// testnet: 'olivia'
|
|
635
642
|
},
|
|
636
643
|
'peernet-peer-response': {
|
|
@@ -661,7 +668,7 @@ var codecs = {
|
|
|
661
668
|
// message
|
|
662
669
|
'peernet-message': {
|
|
663
670
|
codec: parseInt('706d65', 16),
|
|
664
|
-
hashAlg: 'keccak-
|
|
671
|
+
hashAlg: 'keccak-256',
|
|
665
672
|
},
|
|
666
673
|
// pubsub
|
|
667
674
|
'peernet-ps': {
|
|
@@ -702,7 +709,7 @@ var codecs = {
|
|
|
702
709
|
// chat message
|
|
703
710
|
'chat-message': {
|
|
704
711
|
codec: parseInt('636d', 16),
|
|
705
|
-
hashAlg: 'dbl-keccak-
|
|
712
|
+
hashAlg: 'dbl-keccak-256',
|
|
706
713
|
},
|
|
707
714
|
};
|
|
708
715
|
|
|
@@ -831,7 +838,7 @@ class PeernetHash {
|
|
|
831
838
|
else this.name = 'disco-hash';
|
|
832
839
|
if (options.codecs) this.codecs = options.codecs;
|
|
833
840
|
if (buffer) {
|
|
834
|
-
if (
|
|
841
|
+
if (buffer instanceof Uint8Array) {
|
|
835
842
|
this.discoCodec = new PeernetCodec(buffer, this.codecs);
|
|
836
843
|
const name = this.discoCodec.name;
|
|
837
844
|
|
|
@@ -855,13 +862,9 @@ class PeernetHash {
|
|
|
855
862
|
get prefix() {
|
|
856
863
|
const length = this.length;
|
|
857
864
|
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
}
|
|
865
|
+
uint8Array.set(length);
|
|
866
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
861
867
|
|
|
862
|
-
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
863
|
-
uint8Array[i] = length[i];
|
|
864
|
-
}
|
|
865
868
|
return uint8Array
|
|
866
869
|
}
|
|
867
870
|
|
|
@@ -920,10 +923,11 @@ class PeernetHash {
|
|
|
920
923
|
|
|
921
924
|
this.codec = this.discoCodec.encode();
|
|
922
925
|
this.codec = this.discoCodec.codecBuffer;
|
|
923
|
-
this.
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
926
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
927
|
+
uint8Array.set(this.prefix);
|
|
928
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
929
|
+
|
|
930
|
+
this.hash = uint8Array;
|
|
927
931
|
|
|
928
932
|
return this.hash
|
|
929
933
|
}
|
|
@@ -1062,6 +1066,28 @@ class FormatInterface {
|
|
|
1062
1066
|
else this.decode();
|
|
1063
1067
|
}
|
|
1064
1068
|
|
|
1069
|
+
toString() {
|
|
1070
|
+
return this.encoded.toString()
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
async toArray() {
|
|
1074
|
+
const array = [];
|
|
1075
|
+
for await (const value of this.encoded.values()) {
|
|
1076
|
+
array.push(value);
|
|
1077
|
+
}
|
|
1078
|
+
return array
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
fromString(string) {
|
|
1082
|
+
this.encoded = new Uint8Array(string.split(','));
|
|
1083
|
+
this.decode();
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
fromArray(array) {
|
|
1087
|
+
this.encoded = new Uint8Array([...array]);
|
|
1088
|
+
this.decode();
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1065
1091
|
/**
|
|
1066
1092
|
* @param {Buffer} encoded
|
|
1067
1093
|
*/
|
|
@@ -2015,7 +2041,7 @@ class MessageHandler {
|
|
|
2015
2041
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2016
2042
|
const wallet = new MultiWallet(this.network);
|
|
2017
2043
|
wallet.import(identity.multiWIF);
|
|
2018
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
2044
|
+
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
2019
2045
|
}
|
|
2020
2046
|
|
|
2021
2047
|
/**
|
|
@@ -2289,6 +2315,7 @@ class Peernet {
|
|
|
2289
2315
|
* @param {PeernetPeer} peer - peernet peer
|
|
2290
2316
|
*/
|
|
2291
2317
|
async _protoHandler(message, peer, from) {
|
|
2318
|
+
|
|
2292
2319
|
const {id, proto} = message;
|
|
2293
2320
|
this.bw.down += proto.encoded.length;
|
|
2294
2321
|
|
|
@@ -2339,8 +2366,7 @@ class Peernet {
|
|
|
2339
2366
|
|
|
2340
2367
|
this.bw.up += node.encoded.length;
|
|
2341
2368
|
}
|
|
2342
|
-
} else if (proto.name === 'peernet-ps' &&
|
|
2343
|
-
this._getPeerId(peer.id) !== this.id.toString()) {
|
|
2369
|
+
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
2344
2370
|
globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
|
|
2345
2371
|
}
|
|
2346
2372
|
// }
|
|
@@ -2355,12 +2381,11 @@ class Peernet {
|
|
|
2355
2381
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
2356
2382
|
const data = new DHTMessage({hash});
|
|
2357
2383
|
this.client.id;
|
|
2358
|
-
|
|
2359
|
-
const node = await this.prepareMessage(peer.
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
let proto = protoFor(result.data);
|
|
2384
|
+
const walk = async peer => {
|
|
2385
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2386
|
+
let result = await peer.request(node.encoded);
|
|
2387
|
+
result = new Uint8Array(Object.values(result));
|
|
2388
|
+
let proto = protoFor(result);
|
|
2364
2389
|
|
|
2365
2390
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
2366
2391
|
const from = proto.decoded.from;
|
|
@@ -2383,8 +2408,14 @@ class Peernet {
|
|
|
2383
2408
|
};
|
|
2384
2409
|
|
|
2385
2410
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
2411
|
+
};
|
|
2412
|
+
let walks = [];
|
|
2413
|
+
for (const peer of this.connections) {
|
|
2414
|
+
if (peer.peerId !== this.id) {
|
|
2415
|
+
walks.push(walk(peer));
|
|
2416
|
+
}
|
|
2386
2417
|
}
|
|
2387
|
-
return
|
|
2418
|
+
return Promise.all(walks)
|
|
2388
2419
|
}
|
|
2389
2420
|
|
|
2390
2421
|
/**
|
|
@@ -2455,7 +2486,7 @@ class Peernet {
|
|
|
2455
2486
|
const id = closestPeer.id.toString();
|
|
2456
2487
|
if (this.connections) {
|
|
2457
2488
|
let closest = this.connections.filter((peer) => {
|
|
2458
|
-
if (peer.
|
|
2489
|
+
if (peer.peerId === id) return peer
|
|
2459
2490
|
});
|
|
2460
2491
|
|
|
2461
2492
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -2464,7 +2495,7 @@ class Peernet {
|
|
|
2464
2495
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2465
2496
|
else {
|
|
2466
2497
|
closest = this.connections.filter((peer) => {
|
|
2467
|
-
if (peer.
|
|
2498
|
+
if (peer.peerId === id) return peer
|
|
2468
2499
|
});
|
|
2469
2500
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2470
2501
|
}
|
|
@@ -2607,8 +2638,8 @@ class Peernet {
|
|
|
2607
2638
|
data = new PsMessage({data, topic});
|
|
2608
2639
|
for (const peer of this.connections) {
|
|
2609
2640
|
if (peer.connected) {
|
|
2610
|
-
if (peer.
|
|
2611
|
-
const node = await this.prepareMessage(peer.
|
|
2641
|
+
if (peer.peerId !== this.peerId) {
|
|
2642
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2612
2643
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2613
2644
|
}
|
|
2614
2645
|
} else {
|
|
@@ -2633,7 +2664,7 @@ class Peernet {
|
|
|
2633
2664
|
}
|
|
2634
2665
|
|
|
2635
2666
|
async removePeer(peer) {
|
|
2636
|
-
delete this.client.connections[peer.
|
|
2667
|
+
delete this.client.connections[peer.peerId];
|
|
2637
2668
|
}
|
|
2638
2669
|
|
|
2639
2670
|
get Buffer() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.1.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.1.12",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|