@leofcoin/peernet 0.11.4 → 0.11.7
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 +1 -1
- 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-da1c8d33.js → http-906864ff.js} +1 -1
- package/dist/commonjs/peernet-message.js +1 -1
- package/dist/commonjs/peernet.js +21 -13
- package/dist/commonjs/request.js +1 -1
- package/dist/commonjs/response.js +1 -1
- package/dist/module/peernet.js +52 -25
- package/package.json +1 -1
- 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 +18 -10
|
@@ -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.6";
|
|
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');
|
|
@@ -1437,7 +1437,7 @@ class MessageHandler {
|
|
|
1437
1437
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
1438
1438
|
const wallet = new MultiWallet(this.network);
|
|
1439
1439
|
wallet.import(identity.multiWIF);
|
|
1440
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
1440
|
+
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
1441
1441
|
}
|
|
1442
1442
|
|
|
1443
1443
|
/**
|
|
@@ -1563,7 +1563,15 @@ class Peernet {
|
|
|
1563
1563
|
* @return {Array} peerId
|
|
1564
1564
|
*/
|
|
1565
1565
|
get peers() {
|
|
1566
|
-
return this.client.connections
|
|
1566
|
+
return Object.keys(this.client.connections)
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
get connections() {
|
|
1570
|
+
return Object.values(this.client.connections)
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
get peerEntries() {
|
|
1574
|
+
return Object.entries(this.client.connections)
|
|
1567
1575
|
}
|
|
1568
1576
|
|
|
1569
1577
|
/**
|
|
@@ -1628,7 +1636,7 @@ class Peernet {
|
|
|
1628
1636
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1629
1637
|
});
|
|
1630
1638
|
} else {
|
|
1631
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1639
|
+
const http = await Promise.resolve().then(function () { return require('./http-906864ff.js'); });
|
|
1632
1640
|
if (environment !== 'browser') http.default(options);
|
|
1633
1641
|
}
|
|
1634
1642
|
|
|
@@ -1777,7 +1785,7 @@ class Peernet {
|
|
|
1777
1785
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
1778
1786
|
const data = new dht({hash});
|
|
1779
1787
|
this.client.id;
|
|
1780
|
-
for (const peer of this.
|
|
1788
|
+
for (const peer of this.connections) {
|
|
1781
1789
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
1782
1790
|
|
|
1783
1791
|
const result = await peer.request(node.encoded);
|
|
@@ -1875,9 +1883,9 @@ class Peernet {
|
|
|
1875
1883
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
1876
1884
|
|
|
1877
1885
|
const id = closestPeer.id.toString();
|
|
1878
|
-
if (this.
|
|
1879
|
-
let closest = this.
|
|
1880
|
-
if (
|
|
1886
|
+
if (this.connections) {
|
|
1887
|
+
let closest = this.connections.filter((peer) => {
|
|
1888
|
+
if (peer.id === id) return peer
|
|
1881
1889
|
});
|
|
1882
1890
|
|
|
1883
1891
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -1885,8 +1893,8 @@ class Peernet {
|
|
|
1885
1893
|
const node = await this.prepareMessage(id, data.encoded);
|
|
1886
1894
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1887
1895
|
else {
|
|
1888
|
-
closest = this.
|
|
1889
|
-
if (peer.id
|
|
1896
|
+
closest = this.connections.filter((peer) => {
|
|
1897
|
+
if (peer.id === id) return peer
|
|
1890
1898
|
});
|
|
1891
1899
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1892
1900
|
}
|
|
@@ -2027,9 +2035,9 @@ class Peernet {
|
|
|
2027
2035
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2028
2036
|
const id = Math.random().toString(36).slice(-12);
|
|
2029
2037
|
data = new PsMessage({data, topic});
|
|
2030
|
-
for (const peer of this.
|
|
2031
|
-
if (peer.
|
|
2032
|
-
if (peer.id
|
|
2038
|
+
for (const peer of this.connections) {
|
|
2039
|
+
if (peer.connected) {
|
|
2040
|
+
if (peer.id !== this.peerId) {
|
|
2033
2041
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2034
2042
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2035
2043
|
}
|
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
|
@@ -630,7 +630,7 @@ var codecs = {
|
|
|
630
630
|
// just a hash
|
|
631
631
|
'disco-hash': {
|
|
632
632
|
codec: parseInt('30', 16),
|
|
633
|
-
hashAlg: 'dbl-keccak-
|
|
633
|
+
hashAlg: 'dbl-keccak-256', // ,
|
|
634
634
|
// testnet: 'olivia'
|
|
635
635
|
},
|
|
636
636
|
'peernet-peer-response': {
|
|
@@ -661,7 +661,7 @@ var codecs = {
|
|
|
661
661
|
// message
|
|
662
662
|
'peernet-message': {
|
|
663
663
|
codec: parseInt('706d65', 16),
|
|
664
|
-
hashAlg: 'keccak-
|
|
664
|
+
hashAlg: 'keccak-256',
|
|
665
665
|
},
|
|
666
666
|
// pubsub
|
|
667
667
|
'peernet-ps': {
|
|
@@ -702,7 +702,7 @@ var codecs = {
|
|
|
702
702
|
// chat message
|
|
703
703
|
'chat-message': {
|
|
704
704
|
codec: parseInt('636d', 16),
|
|
705
|
-
hashAlg: 'dbl-keccak-
|
|
705
|
+
hashAlg: 'dbl-keccak-256',
|
|
706
706
|
},
|
|
707
707
|
};
|
|
708
708
|
|
|
@@ -831,7 +831,7 @@ class PeernetHash {
|
|
|
831
831
|
else this.name = 'disco-hash';
|
|
832
832
|
if (options.codecs) this.codecs = options.codecs;
|
|
833
833
|
if (buffer) {
|
|
834
|
-
if (
|
|
834
|
+
if (buffer instanceof Uint8Array) {
|
|
835
835
|
this.discoCodec = new PeernetCodec(buffer, this.codecs);
|
|
836
836
|
const name = this.discoCodec.name;
|
|
837
837
|
|
|
@@ -855,13 +855,9 @@ class PeernetHash {
|
|
|
855
855
|
get prefix() {
|
|
856
856
|
const length = this.length;
|
|
857
857
|
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
}
|
|
858
|
+
uint8Array.set(length);
|
|
859
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
861
860
|
|
|
862
|
-
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
863
|
-
uint8Array[i] = length[i];
|
|
864
|
-
}
|
|
865
861
|
return uint8Array
|
|
866
862
|
}
|
|
867
863
|
|
|
@@ -920,10 +916,11 @@ class PeernetHash {
|
|
|
920
916
|
|
|
921
917
|
this.codec = this.discoCodec.encode();
|
|
922
918
|
this.codec = this.discoCodec.codecBuffer;
|
|
923
|
-
this.
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
919
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
920
|
+
uint8Array.set(this.prefix);
|
|
921
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
922
|
+
|
|
923
|
+
this.hash = uint8Array;
|
|
927
924
|
|
|
928
925
|
return this.hash
|
|
929
926
|
}
|
|
@@ -1062,6 +1059,28 @@ class FormatInterface {
|
|
|
1062
1059
|
else this.decode();
|
|
1063
1060
|
}
|
|
1064
1061
|
|
|
1062
|
+
toString() {
|
|
1063
|
+
return this.encoded.toString()
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
async toArray() {
|
|
1067
|
+
const array = [];
|
|
1068
|
+
for await (const value of this.encoded.values()) {
|
|
1069
|
+
array.push(value);
|
|
1070
|
+
}
|
|
1071
|
+
return array
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
fromString(string) {
|
|
1075
|
+
this.encoded = new Uint8Array(string.split(','));
|
|
1076
|
+
this.decode();
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
fromArray(array) {
|
|
1080
|
+
this.encoded = new Uint8Array([...array]);
|
|
1081
|
+
this.decode();
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1065
1084
|
/**
|
|
1066
1085
|
* @param {Buffer} encoded
|
|
1067
1086
|
*/
|
|
@@ -2015,7 +2034,7 @@ class MessageHandler {
|
|
|
2015
2034
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2016
2035
|
const wallet = new MultiWallet(this.network);
|
|
2017
2036
|
wallet.import(identity.multiWIF);
|
|
2018
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
2037
|
+
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
2019
2038
|
}
|
|
2020
2039
|
|
|
2021
2040
|
/**
|
|
@@ -2141,7 +2160,15 @@ class Peernet {
|
|
|
2141
2160
|
* @return {Array} peerId
|
|
2142
2161
|
*/
|
|
2143
2162
|
get peers() {
|
|
2144
|
-
return this.client.connections
|
|
2163
|
+
return Object.keys(this.client.connections)
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
get connections() {
|
|
2167
|
+
return Object.values(this.client.connections)
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
get peerEntries() {
|
|
2171
|
+
return Object.entries(this.client.connections)
|
|
2145
2172
|
}
|
|
2146
2173
|
|
|
2147
2174
|
/**
|
|
@@ -2347,7 +2374,7 @@ class Peernet {
|
|
|
2347
2374
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
2348
2375
|
const data = new DHTMessage({hash});
|
|
2349
2376
|
this.client.id;
|
|
2350
|
-
for (const peer of this.
|
|
2377
|
+
for (const peer of this.connections) {
|
|
2351
2378
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2352
2379
|
|
|
2353
2380
|
const result = await peer.request(node.encoded);
|
|
@@ -2445,9 +2472,9 @@ class Peernet {
|
|
|
2445
2472
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
2446
2473
|
|
|
2447
2474
|
const id = closestPeer.id.toString();
|
|
2448
|
-
if (this.
|
|
2449
|
-
let closest = this.
|
|
2450
|
-
if (
|
|
2475
|
+
if (this.connections) {
|
|
2476
|
+
let closest = this.connections.filter((peer) => {
|
|
2477
|
+
if (peer.id === id) return peer
|
|
2451
2478
|
});
|
|
2452
2479
|
|
|
2453
2480
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -2455,8 +2482,8 @@ class Peernet {
|
|
|
2455
2482
|
const node = await this.prepareMessage(id, data.encoded);
|
|
2456
2483
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2457
2484
|
else {
|
|
2458
|
-
closest = this.
|
|
2459
|
-
if (peer.id
|
|
2485
|
+
closest = this.connections.filter((peer) => {
|
|
2486
|
+
if (peer.id === id) return peer
|
|
2460
2487
|
});
|
|
2461
2488
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2462
2489
|
}
|
|
@@ -2597,9 +2624,9 @@ class Peernet {
|
|
|
2597
2624
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2598
2625
|
const id = Math.random().toString(36).slice(-12);
|
|
2599
2626
|
data = new PsMessage({data, topic});
|
|
2600
|
-
for (const peer of this.
|
|
2601
|
-
if (peer.
|
|
2602
|
-
if (peer.id
|
|
2627
|
+
for (const peer of this.connections) {
|
|
2628
|
+
if (peer.connected) {
|
|
2629
|
+
if (peer.id !== this.peerId) {
|
|
2603
2630
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2604
2631
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2605
2632
|
}
|
package/package.json
CHANGED
|
@@ -92,6 +92,28 @@ export default class FormatInterface {
|
|
|
92
92
|
else this.decode()
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
toString() {
|
|
96
|
+
return this.encoded.toString()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async toArray() {
|
|
100
|
+
const array = []
|
|
101
|
+
for await (const value of this.encoded.values()) {
|
|
102
|
+
array.push(value)
|
|
103
|
+
}
|
|
104
|
+
return array
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fromString(string) {
|
|
108
|
+
this.encoded = new Uint8Array(string.split(','))
|
|
109
|
+
this.decode()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fromArray(array) {
|
|
113
|
+
this.encoded = new Uint8Array([...array])
|
|
114
|
+
this.decode()
|
|
115
|
+
}
|
|
116
|
+
|
|
95
117
|
/**
|
|
96
118
|
* @param {Buffer} encoded
|
|
97
119
|
*/
|
package/src/codec/codecs.js
CHANGED
|
@@ -2,7 +2,7 @@ export default {
|
|
|
2
2
|
// just a hash
|
|
3
3
|
'disco-hash': {
|
|
4
4
|
codec: parseInt('30', 16),
|
|
5
|
-
hashAlg: 'dbl-keccak-
|
|
5
|
+
hashAlg: 'dbl-keccak-256', // ,
|
|
6
6
|
// testnet: 'olivia'
|
|
7
7
|
},
|
|
8
8
|
'peernet-peer-response': {
|
|
@@ -33,7 +33,7 @@ export default {
|
|
|
33
33
|
// message
|
|
34
34
|
'peernet-message': {
|
|
35
35
|
codec: parseInt('706d65', 16),
|
|
36
|
-
hashAlg: 'keccak-
|
|
36
|
+
hashAlg: 'keccak-256',
|
|
37
37
|
},
|
|
38
38
|
// pubsub
|
|
39
39
|
'peernet-ps': {
|
|
@@ -74,6 +74,6 @@ export default {
|
|
|
74
74
|
// chat message
|
|
75
75
|
'chat-message': {
|
|
76
76
|
codec: parseInt('636d', 16),
|
|
77
|
-
hashAlg: 'dbl-keccak-
|
|
77
|
+
hashAlg: 'dbl-keccak-256',
|
|
78
78
|
},
|
|
79
79
|
}
|
package/src/handlers/message.js
CHANGED
|
@@ -22,7 +22,7 @@ export default class MessageHandler {
|
|
|
22
22
|
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
23
23
|
const wallet = new MultiWallet(this.network)
|
|
24
24
|
wallet.import(identity.multiWIF)
|
|
25
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
25
|
+
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
package/src/hash/hash.js
CHANGED
|
@@ -11,7 +11,7 @@ export default class PeernetHash {
|
|
|
11
11
|
else this.name = 'disco-hash'
|
|
12
12
|
if (options.codecs) this.codecs = options.codecs
|
|
13
13
|
if (buffer) {
|
|
14
|
-
if (
|
|
14
|
+
if (buffer instanceof Uint8Array) {
|
|
15
15
|
this.discoCodec = new Codec(buffer, this.codecs)
|
|
16
16
|
const name = this.discoCodec.name
|
|
17
17
|
|
|
@@ -35,13 +35,9 @@ export default class PeernetHash {
|
|
|
35
35
|
get prefix() {
|
|
36
36
|
const length = this.length
|
|
37
37
|
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
38
|
+
uint8Array.set(length)
|
|
39
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length)
|
|
41
40
|
|
|
42
|
-
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
43
|
-
uint8Array[i] = length[i]
|
|
44
|
-
}
|
|
45
41
|
return uint8Array
|
|
46
42
|
}
|
|
47
43
|
|
|
@@ -100,10 +96,11 @@ export default class PeernetHash {
|
|
|
100
96
|
|
|
101
97
|
this.codec = this.discoCodec.encode();
|
|
102
98
|
this.codec = this.discoCodec.codecBuffer
|
|
103
|
-
this.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length)
|
|
100
|
+
uint8Array.set(this.prefix)
|
|
101
|
+
uint8Array.set(this.digest, this.prefix.length)
|
|
102
|
+
|
|
103
|
+
this.hash = uint8Array
|
|
107
104
|
|
|
108
105
|
return this.hash
|
|
109
106
|
}
|
package/src/peernet.js
CHANGED
|
@@ -108,7 +108,15 @@ export default class Peernet {
|
|
|
108
108
|
* @return {Array} peerId
|
|
109
109
|
*/
|
|
110
110
|
get peers() {
|
|
111
|
-
return this.client.connections
|
|
111
|
+
return Object.keys(this.client.connections)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get connections() {
|
|
115
|
+
return Object.values(this.client.connections)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get peerEntries() {
|
|
119
|
+
return Object.entries(this.client.connections)
|
|
112
120
|
}
|
|
113
121
|
|
|
114
122
|
/**
|
|
@@ -317,7 +325,7 @@ export default class Peernet {
|
|
|
317
325
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
318
326
|
const data = new DHTMessage({hash})
|
|
319
327
|
const clientId = this.client.id
|
|
320
|
-
for (const peer of this.
|
|
328
|
+
for (const peer of this.connections) {
|
|
321
329
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
322
330
|
|
|
323
331
|
const result = await peer.request(node.encoded)
|
|
@@ -415,9 +423,9 @@ export default class Peernet {
|
|
|
415
423
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
416
424
|
|
|
417
425
|
const id = closestPeer.id.toString()
|
|
418
|
-
if (this.
|
|
419
|
-
let closest = this.
|
|
420
|
-
if (
|
|
426
|
+
if (this.connections) {
|
|
427
|
+
let closest = this.connections.filter((peer) => {
|
|
428
|
+
if (peer.id === id) return peer
|
|
421
429
|
})
|
|
422
430
|
|
|
423
431
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -425,8 +433,8 @@ export default class Peernet {
|
|
|
425
433
|
const node = await this.prepareMessage(id, data.encoded)
|
|
426
434
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
427
435
|
else {
|
|
428
|
-
closest = this.
|
|
429
|
-
if (peer.id
|
|
436
|
+
closest = this.connections.filter((peer) => {
|
|
437
|
+
if (peer.id === id) return peer
|
|
430
438
|
})
|
|
431
439
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
432
440
|
}
|
|
@@ -568,9 +576,9 @@ export default class Peernet {
|
|
|
568
576
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
|
|
569
577
|
const id = Math.random().toString(36).slice(-12)
|
|
570
578
|
data = new PsMessage({data, topic})
|
|
571
|
-
for (const peer of this.
|
|
572
|
-
if (peer.
|
|
573
|
-
if (peer.id
|
|
579
|
+
for (const peer of this.connections) {
|
|
580
|
+
if (peer.connected) {
|
|
581
|
+
if (peer.id !== this.peerId) {
|
|
574
582
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
575
583
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
576
584
|
}
|