@leofcoin/peernet 0.11.0 → 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/codec-format-interface.js.html +637 -0
- package/coverage/lcov-report/dht-response.js.html +193 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +199 -0
- package/dist/browser/326.peernet.js +1 -0
- package/dist/browser/peernet.js +2 -108930
- package/dist/browser/peernet.js.LICENSE.txt +40 -0
- package/dist/commonjs/{client-bd0caeb7.js → client-1a1f75e6.js} +41 -41
- package/dist/commonjs/{codec-4a768e5e.js → codec-8c8c652f.js} +118 -118
- package/dist/commonjs/codec-format-interface.js +167 -167
- package/dist/commonjs/codec.js +2 -2
- package/dist/commonjs/dht-response.js +11 -11
- package/dist/commonjs/dht.js +2 -2
- package/dist/commonjs/hash.js +149 -149
- package/dist/commonjs/{http-2b0735ef.js → http-e088ccdb.js} +1 -1
- package/dist/commonjs/peernet-message.js +4 -4
- package/dist/commonjs/peernet.js +850 -943
- package/dist/commonjs/request.js +2 -2
- package/dist/commonjs/response.js +3 -3
- package/dist/module/peernet.js +1373 -1466
- package/index.html +2 -2
- package/package.json +6 -21
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -172
- package/src/codec/codec.js +124 -124
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/handlers/message.js +2 -4
- package/src/hash/hash.js +155 -155
- package/src/http/client/http-client.js +44 -44
- package/src/messages/dht-response.js +14 -14
- package/src/peer.js +67 -67
- package/src/peernet.js +613 -612
- package/src/proto/chat-message.proto.js +7 -7
- package/src/proto/peernet.proto.js +2 -2
- package/src/proto/response.proto.js +1 -1
- package/src/utils/utils.js +78 -78
- package/test.js +1 -1
- package/webpack.config.js +10 -4
package/src/codec/codec.js
CHANGED
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
import varint from 'varint';
|
|
2
|
-
import bs32 from '@vandeurenglenn/base32';
|
|
3
|
-
import bs58 from '@vandeurenglenn/base58';
|
|
4
|
-
import isHex from 'is-hex';
|
|
5
|
-
import codecs from './codecs'
|
|
6
|
-
|
|
7
|
-
export default class PeernetCodec {
|
|
8
|
-
get codecs() {
|
|
9
|
-
return {...globalThis.peernet.codecs, ...codecs}
|
|
10
|
-
}
|
|
11
|
-
constructor(buffer) {
|
|
12
|
-
if (buffer) {
|
|
13
|
-
if (buffer instanceof Uint8Array) {
|
|
14
|
-
const codec = varint.decode(buffer);
|
|
15
|
-
const name = this.getCodecName(codec)
|
|
16
|
-
if (name) {
|
|
17
|
-
this.name = name
|
|
18
|
-
this.encoded = buffer
|
|
19
|
-
this.decode(buffer)
|
|
20
|
-
} else {
|
|
21
|
-
this.encode(buffer)
|
|
22
|
-
}
|
|
23
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
24
|
-
const encoded = new Uint8Array(buffer.byteLength)
|
|
25
|
-
|
|
26
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
27
|
-
encoded[i] = buffer[i]
|
|
28
|
-
}
|
|
29
|
-
this.encoded = encoded
|
|
30
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
31
|
-
this.decode(buffer)
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
if (typeof buffer === 'string') {
|
|
35
|
-
if (this.codecs[buffer]) this.fromName(buffer)
|
|
36
|
-
else if (isHex(buffer)) this.fromHex(buffer)
|
|
37
|
-
else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
|
|
38
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
|
|
39
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
40
|
-
}
|
|
41
|
-
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
fromEncoded(encoded) {
|
|
46
|
-
const codec = varint.decode(encoded);
|
|
47
|
-
const name = this.getCodecName(codec)
|
|
48
|
-
this.name = name
|
|
49
|
-
this.encoded = encoded
|
|
50
|
-
this.decode(encoded)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
fromHex(hex) {
|
|
54
|
-
this.encoded = Buffer.from(hex, 'hex')
|
|
55
|
-
this.decode()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
fromBs32(input) {
|
|
59
|
-
this.encoded = bs32.decode(input)
|
|
60
|
-
this.decode()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
fromBs58(input) {
|
|
64
|
-
this.encoded = bs58.decode(input)
|
|
65
|
-
this.decode()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getCodec(name) {
|
|
69
|
-
return this.codecs[name].codec
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
getCodecName(codec) {
|
|
73
|
-
return Object.keys(this.codecs).reduce((p, c) => {
|
|
74
|
-
const item = this.codecs[c]
|
|
75
|
-
if (item.codec === codec) return c;
|
|
76
|
-
else return p;
|
|
77
|
-
}, undefined)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
getHashAlg(name) {
|
|
81
|
-
return this.codecs[name].hashAlg
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
fromCodec(codec) {
|
|
85
|
-
this.name = this.getCodecName(codec)
|
|
86
|
-
this.hashAlg = this.getHashAlg(this.name)
|
|
87
|
-
|
|
88
|
-
this.codec = this.getCodec(this.name)
|
|
89
|
-
this.codecBuffer = varint.encode(codec)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
fromName(name) {
|
|
93
|
-
const codec = this.getCodec(name)
|
|
94
|
-
this.name = name
|
|
95
|
-
this.codec = codec
|
|
96
|
-
this.hashAlg = this.getHashAlg(name)
|
|
97
|
-
this.codecBuffer = varint.encode(codec)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
toBs32() {
|
|
101
|
-
this.encode()
|
|
102
|
-
return bs32.encode(this.encoded)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
toBs58() {
|
|
106
|
-
this.encode()
|
|
107
|
-
return bs58.encode(this.encoded)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
toHex() {
|
|
111
|
-
return this.encoded.toString('hex')
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
decode() {
|
|
115
|
-
const codec = varint.decode(this.encoded);
|
|
116
|
-
this.fromCodec(codec)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
encode() {
|
|
120
|
-
const codec = varint.encode(this.decoded)
|
|
121
|
-
this.encoded = codec
|
|
122
|
-
return this.encoded
|
|
123
|
-
}
|
|
124
|
-
}
|
|
1
|
+
import varint from 'varint';
|
|
2
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
3
|
+
import bs58 from '@vandeurenglenn/base58';
|
|
4
|
+
import isHex from '@vandeurenglenn/is-hex';
|
|
5
|
+
import codecs from './codecs'
|
|
6
|
+
|
|
7
|
+
export default class PeernetCodec {
|
|
8
|
+
get codecs() {
|
|
9
|
+
return {...globalThis.peernet.codecs, ...codecs}
|
|
10
|
+
}
|
|
11
|
+
constructor(buffer) {
|
|
12
|
+
if (buffer) {
|
|
13
|
+
if (buffer instanceof Uint8Array) {
|
|
14
|
+
const codec = varint.decode(buffer);
|
|
15
|
+
const name = this.getCodecName(codec)
|
|
16
|
+
if (name) {
|
|
17
|
+
this.name = name
|
|
18
|
+
this.encoded = buffer
|
|
19
|
+
this.decode(buffer)
|
|
20
|
+
} else {
|
|
21
|
+
this.encode(buffer)
|
|
22
|
+
}
|
|
23
|
+
} else if (buffer instanceof ArrayBuffer) {
|
|
24
|
+
const encoded = new Uint8Array(buffer.byteLength)
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < buffer.byteLength; i++) {
|
|
27
|
+
encoded[i] = buffer[i]
|
|
28
|
+
}
|
|
29
|
+
this.encoded = encoded
|
|
30
|
+
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
31
|
+
this.decode(buffer)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
if (typeof buffer === 'string') {
|
|
35
|
+
if (this.codecs[buffer]) this.fromName(buffer)
|
|
36
|
+
else if (isHex(buffer)) this.fromHex(buffer)
|
|
37
|
+
else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
|
|
38
|
+
else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
|
|
39
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
40
|
+
}
|
|
41
|
+
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fromEncoded(encoded) {
|
|
46
|
+
const codec = varint.decode(encoded);
|
|
47
|
+
const name = this.getCodecName(codec)
|
|
48
|
+
this.name = name
|
|
49
|
+
this.encoded = encoded
|
|
50
|
+
this.decode(encoded)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fromHex(hex) {
|
|
54
|
+
this.encoded = Buffer.from(hex, 'hex')
|
|
55
|
+
this.decode()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fromBs32(input) {
|
|
59
|
+
this.encoded = bs32.decode(input)
|
|
60
|
+
this.decode()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fromBs58(input) {
|
|
64
|
+
this.encoded = bs58.decode(input)
|
|
65
|
+
this.decode()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getCodec(name) {
|
|
69
|
+
return this.codecs[name].codec
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getCodecName(codec) {
|
|
73
|
+
return Object.keys(this.codecs).reduce((p, c) => {
|
|
74
|
+
const item = this.codecs[c]
|
|
75
|
+
if (item.codec === codec) return c;
|
|
76
|
+
else return p;
|
|
77
|
+
}, undefined)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getHashAlg(name) {
|
|
81
|
+
return this.codecs[name].hashAlg
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fromCodec(codec) {
|
|
85
|
+
this.name = this.getCodecName(codec)
|
|
86
|
+
this.hashAlg = this.getHashAlg(this.name)
|
|
87
|
+
|
|
88
|
+
this.codec = this.getCodec(this.name)
|
|
89
|
+
this.codecBuffer = varint.encode(codec)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fromName(name) {
|
|
93
|
+
const codec = this.getCodec(name)
|
|
94
|
+
this.name = name
|
|
95
|
+
this.codec = codec
|
|
96
|
+
this.hashAlg = this.getHashAlg(name)
|
|
97
|
+
this.codecBuffer = varint.encode(codec)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toBs32() {
|
|
101
|
+
this.encode()
|
|
102
|
+
return bs32.encode(this.encoded)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
toBs58() {
|
|
106
|
+
this.encode()
|
|
107
|
+
return bs58.encode(this.encoded)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
toHex() {
|
|
111
|
+
return this.encoded.toString('hex')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
decode() {
|
|
115
|
+
const codec = varint.decode(this.encoded);
|
|
116
|
+
this.fromCodec(codec)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
encode() {
|
|
120
|
+
const codec = varint.encode(this.decoded)
|
|
121
|
+
this.encoded = codec
|
|
122
|
+
return this.encoded
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/dht/dht.js
CHANGED
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import fetch from 'node-fetch'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Keep history of fetched address and ptr
|
|
5
|
-
* @property {Object} address
|
|
6
|
-
* @property {Object} ptr
|
|
7
|
-
*/
|
|
8
|
-
const lastFetched = {
|
|
9
|
-
address: {
|
|
10
|
-
value: undefined,
|
|
11
|
-
timestamp: 0,
|
|
12
|
-
},
|
|
13
|
-
ptr: {
|
|
14
|
-
value: undefined,
|
|
15
|
-
timestamp: 0,
|
|
16
|
-
},
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const getAddress = async () => {
|
|
20
|
-
const {address} = lastFetched
|
|
21
|
-
const now = Math.round(new Date().getTime() / 1000);
|
|
22
|
-
if (now - address.timestamp > 1200000) {
|
|
23
|
-
address.value = await fetch('https://icanhazip.com/')
|
|
24
|
-
address.value = await address.value.text()
|
|
25
|
-
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
26
|
-
lastFetched.address = address;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return address.value
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const degreesToRadians = (degrees) => {
|
|
33
|
-
return degrees * Math.PI / 180;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
37
|
-
const earthRadiusKm = 6371;
|
|
38
|
-
|
|
39
|
-
const dLat = degreesToRadians(lat2-lat1);
|
|
40
|
-
const dLon = degreesToRadians(lon2-lon1);
|
|
41
|
-
|
|
42
|
-
lat1 = degreesToRadians(lat1);
|
|
43
|
-
lat2 = degreesToRadians(lat2);
|
|
44
|
-
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
|
|
45
|
-
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
|
|
46
|
-
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
|
47
|
-
return earthRadiusKm * c;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default class DhtEarth {
|
|
51
|
-
/**
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
constructor() {
|
|
55
|
-
this.providerMap = new Map();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @param {Object} address
|
|
60
|
-
* @return {Object} {latitude: lat, longitude: lon}
|
|
61
|
-
*/
|
|
62
|
-
async getCoordinates(address) {
|
|
63
|
-
// const {address} = parseAddress(provider)
|
|
64
|
-
const request = `https://whereis.leofcoin.org/?ip=${address}`
|
|
65
|
-
let response = await fetch(request)
|
|
66
|
-
response = await response.json()
|
|
67
|
-
const {lat, lon} = response;
|
|
68
|
-
return {latitude: lat, longitude: lon}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @param {Object} peer
|
|
73
|
-
* @param {Object} provider
|
|
74
|
-
* @return {Object} {provider, distance}
|
|
75
|
-
*/
|
|
76
|
-
async getDistance(peer, provider) {
|
|
77
|
-
const {latitude, longitude} = await this.getCoordinates(provider.address)
|
|
78
|
-
return {provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @param {Array} providers
|
|
83
|
-
* @return {Object} closestPeer
|
|
84
|
-
*/
|
|
85
|
-
async closestPeer(providers) {
|
|
86
|
-
let all = []
|
|
87
|
-
const address = await getAddress();
|
|
88
|
-
const peerLoc = await this.getCoordinates(address)
|
|
89
|
-
|
|
90
|
-
for (const provider of providers) {
|
|
91
|
-
if (provider.address === '127.0.0.1') all.push({provider, distance: 0})
|
|
92
|
-
else all.push(this.getDistance(peerLoc, provider))
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
all = await Promise.all(all);
|
|
96
|
-
all = all.sort((previous, current) => previous.distance - current.distance)
|
|
97
|
-
return all[0].provider;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* @param {String} hash
|
|
102
|
-
* @return {Array} providers
|
|
103
|
-
*/
|
|
104
|
-
providersFor(hash) {
|
|
105
|
-
return this.providerMap.get(hash);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* @param {String} address
|
|
110
|
-
* @param {String} hash
|
|
111
|
-
* @return {Array} providers
|
|
112
|
-
*/
|
|
113
|
-
async addProvider(address, hash) {
|
|
114
|
-
let providers = [];
|
|
115
|
-
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
116
|
-
|
|
117
|
-
providers = new Set([...providers, address])
|
|
118
|
-
this.providerMap.set(hash, providers)
|
|
119
|
-
return providers;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keep history of fetched address and ptr
|
|
5
|
+
* @property {Object} address
|
|
6
|
+
* @property {Object} ptr
|
|
7
|
+
*/
|
|
8
|
+
const lastFetched = {
|
|
9
|
+
address: {
|
|
10
|
+
value: undefined,
|
|
11
|
+
timestamp: 0,
|
|
12
|
+
},
|
|
13
|
+
ptr: {
|
|
14
|
+
value: undefined,
|
|
15
|
+
timestamp: 0,
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const getAddress = async () => {
|
|
20
|
+
const {address} = lastFetched
|
|
21
|
+
const now = Math.round(new Date().getTime() / 1000);
|
|
22
|
+
if (now - address.timestamp > 1200000) {
|
|
23
|
+
address.value = await fetch('https://icanhazip.com/')
|
|
24
|
+
address.value = await address.value.text()
|
|
25
|
+
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
26
|
+
lastFetched.address = address;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return address.value
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const degreesToRadians = (degrees) => {
|
|
33
|
+
return degrees * Math.PI / 180;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
37
|
+
const earthRadiusKm = 6371;
|
|
38
|
+
|
|
39
|
+
const dLat = degreesToRadians(lat2-lat1);
|
|
40
|
+
const dLon = degreesToRadians(lon2-lon1);
|
|
41
|
+
|
|
42
|
+
lat1 = degreesToRadians(lat1);
|
|
43
|
+
lat2 = degreesToRadians(lat2);
|
|
44
|
+
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
|
|
45
|
+
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
|
|
46
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
|
47
|
+
return earthRadiusKm * c;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default class DhtEarth {
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
*/
|
|
54
|
+
constructor() {
|
|
55
|
+
this.providerMap = new Map();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {Object} address
|
|
60
|
+
* @return {Object} {latitude: lat, longitude: lon}
|
|
61
|
+
*/
|
|
62
|
+
async getCoordinates(address) {
|
|
63
|
+
// const {address} = parseAddress(provider)
|
|
64
|
+
const request = `https://whereis.leofcoin.org/?ip=${address}`
|
|
65
|
+
let response = await fetch(request)
|
|
66
|
+
response = await response.json()
|
|
67
|
+
const {lat, lon} = response;
|
|
68
|
+
return {latitude: lat, longitude: lon}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {Object} peer
|
|
73
|
+
* @param {Object} provider
|
|
74
|
+
* @return {Object} {provider, distance}
|
|
75
|
+
*/
|
|
76
|
+
async getDistance(peer, provider) {
|
|
77
|
+
const {latitude, longitude} = await this.getCoordinates(provider.address)
|
|
78
|
+
return {provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {Array} providers
|
|
83
|
+
* @return {Object} closestPeer
|
|
84
|
+
*/
|
|
85
|
+
async closestPeer(providers) {
|
|
86
|
+
let all = []
|
|
87
|
+
const address = await getAddress();
|
|
88
|
+
const peerLoc = await this.getCoordinates(address)
|
|
89
|
+
|
|
90
|
+
for (const provider of providers) {
|
|
91
|
+
if (provider.address === '127.0.0.1') all.push({provider, distance: 0})
|
|
92
|
+
else all.push(this.getDistance(peerLoc, provider))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
all = await Promise.all(all);
|
|
96
|
+
all = all.sort((previous, current) => previous.distance - current.distance)
|
|
97
|
+
return all[0].provider;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {String} hash
|
|
102
|
+
* @return {Array} providers
|
|
103
|
+
*/
|
|
104
|
+
providersFor(hash) {
|
|
105
|
+
return this.providerMap.get(hash);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {String} address
|
|
110
|
+
* @param {String} hash
|
|
111
|
+
* @return {Array} providers
|
|
112
|
+
*/
|
|
113
|
+
async addProvider(address, hash) {
|
|
114
|
+
let providers = [];
|
|
115
|
+
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
116
|
+
|
|
117
|
+
providers = new Set([...providers, address])
|
|
118
|
+
this.providerMap.set(hash, providers)
|
|
119
|
+
return providers;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { protoFor } from './../utils/utils.js'
|
|
2
|
-
|
|
3
|
-
export default class PeerDiscovery {
|
|
4
|
-
constructor(id) {
|
|
5
|
-
this.id = id
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
_getPeerId(id) {
|
|
9
|
-
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
10
|
-
|
|
11
|
-
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
-
for (const _id of entry[1]) {
|
|
13
|
-
if (_id === id) return entry[0]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async discover(peer) {
|
|
19
|
-
let id = this._getPeerId(peer.id)
|
|
20
|
-
if (id) return id
|
|
21
|
-
const data = new peernet.protos['peernet-peer']({id: this.id})
|
|
22
|
-
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
-
|
|
24
|
-
let response = await peer.request(node.encoded)
|
|
25
|
-
response = protoFor(response)
|
|
26
|
-
response = new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
-
|
|
28
|
-
id = response.decoded.id
|
|
29
|
-
if (id === this.id) return;
|
|
30
|
-
|
|
31
|
-
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
-
else {
|
|
33
|
-
const connections = peernet.peerMap.get(id)
|
|
34
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
-
connections.push(peer.id)
|
|
36
|
-
peernet.peerMap.set(peer.id, connections)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return id
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async discoverHandler(message, peer) {
|
|
43
|
-
const {id, proto} = message
|
|
44
|
-
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
-
if (proto.name === 'peernet-peer') {
|
|
46
|
-
const from = proto.decoded.id
|
|
47
|
-
if (from === this.id) return;
|
|
48
|
-
|
|
49
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
-
else {
|
|
51
|
-
const connections = peernet.peerMap.get(from)
|
|
52
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
-
connections.push(peer.id)
|
|
54
|
-
peernet.peerMap.set(from, connections)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const data = new peernet.protos['peernet-peer-response']({id: this.id})
|
|
58
|
-
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
-
|
|
60
|
-
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
61
|
-
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
-
const from = proto.decoded.id
|
|
63
|
-
if (from === this.id) return;
|
|
64
|
-
|
|
65
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
-
else {
|
|
67
|
-
const connections = peernet.peerMap.get(from)
|
|
68
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
-
connections.push(peer.id)
|
|
70
|
-
peernet.peerMap.set(from, connections)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
export default class PeerDiscovery {
|
|
4
|
+
constructor(id) {
|
|
5
|
+
this.id = id
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
_getPeerId(id) {
|
|
9
|
+
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
10
|
+
|
|
11
|
+
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
+
for (const _id of entry[1]) {
|
|
13
|
+
if (_id === id) return entry[0]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async discover(peer) {
|
|
19
|
+
let id = this._getPeerId(peer.id)
|
|
20
|
+
if (id) return id
|
|
21
|
+
const data = new peernet.protos['peernet-peer']({id: this.id})
|
|
22
|
+
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
+
|
|
24
|
+
let response = await peer.request(node.encoded)
|
|
25
|
+
response = protoFor(response)
|
|
26
|
+
response = new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
+
|
|
28
|
+
id = response.decoded.id
|
|
29
|
+
if (id === this.id) return;
|
|
30
|
+
|
|
31
|
+
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
+
else {
|
|
33
|
+
const connections = peernet.peerMap.get(id)
|
|
34
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
+
connections.push(peer.id)
|
|
36
|
+
peernet.peerMap.set(peer.id, connections)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return id
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async discoverHandler(message, peer) {
|
|
43
|
+
const {id, proto} = message
|
|
44
|
+
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
+
if (proto.name === 'peernet-peer') {
|
|
46
|
+
const from = proto.decoded.id
|
|
47
|
+
if (from === this.id) return;
|
|
48
|
+
|
|
49
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
+
else {
|
|
51
|
+
const connections = peernet.peerMap.get(from)
|
|
52
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
+
connections.push(peer.id)
|
|
54
|
+
peernet.peerMap.set(from, connections)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const data = new peernet.protos['peernet-peer-response']({id: this.id})
|
|
58
|
+
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
+
|
|
60
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
61
|
+
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
+
const from = proto.decoded.id
|
|
63
|
+
if (from === this.id) return;
|
|
64
|
+
|
|
65
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
+
else {
|
|
67
|
+
const connections = peernet.peerMap.get(from)
|
|
68
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
+
connections.push(peer.id)
|
|
70
|
+
peernet.peerMap.set(from, connections)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/handlers/message.js
CHANGED
|
@@ -18,8 +18,8 @@ export default class MessageHandler {
|
|
|
18
18
|
*/
|
|
19
19
|
async hashAndSignMessage(message) {
|
|
20
20
|
const hasher = new Hash(message, {name: 'peernet-message'})
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
let identity = await walletStore.get('identity')
|
|
22
|
+
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
23
23
|
const wallet = new MultiWallet(this.network)
|
|
24
24
|
wallet.import(identity.multiWIF)
|
|
25
25
|
return wallet.sign(hasher.hash.slice(0, 32))
|
|
@@ -32,8 +32,6 @@ export default class MessageHandler {
|
|
|
32
32
|
* or the messageNode itself
|
|
33
33
|
*/
|
|
34
34
|
async prepareMessage(from, to, data, id) {
|
|
35
|
-
if (!Buffer.isBuffer(from)) from = new Buffer.from(from)
|
|
36
|
-
if (!Buffer.isBuffer(to)) to = new Buffer.from(to)
|
|
37
35
|
if (data.encoded) data = data.encoded
|
|
38
36
|
|
|
39
37
|
const message = {
|