@leofcoin/peernet 1.1.57 → 1.1.58
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/.esdoc.json +10 -10
- package/.eslintrc.json +24 -24
- package/.gitattributes +2 -2
- package/.travis.yml +27 -27
- package/BREAKING_CHANGES.md +34 -34
- package/LICENSE +21 -21
- package/README.md +72 -72
- package/deploy.js +8 -8
- package/exports/browser/{index-329e0324.js → index-8868bdd8.js} +1 -1
- package/exports/browser/{messages-000b7f84.js → messages-eb6e5c71.js} +174 -174
- package/exports/browser/{peernet-bfbe6fff.js → peernet-87ea02a4.js} +2005 -1915
- package/exports/browser/peernet.d.ts +13 -11
- package/exports/browser/peernet.js +1 -1
- package/exports/{messages-dc960cb3.js → messages-b9a32987.js} +173 -173
- package/exports/peernet.js +288 -246
- package/exports/src/prompts/password.js +3 -3
- package/exports/store.js +7 -8
- package/exports/types/peernet.d.ts +13 -11
- package/index.html +19 -19
- package/package.json +70 -70
- package/rollup.config.js +76 -69
- package/src/dht/dht.ts +141 -141
- package/src/discovery/peer-discovery.js +75 -75
- package/src/errors/errors.js +12 -12
- package/src/handlers/data.js +11 -11
- package/src/handlers/message.js +34 -34
- package/src/identity.ts +101 -101
- package/src/messages/chat.js +14 -14
- package/src/messages/data-response.js +14 -14
- package/src/messages/data.js +18 -18
- package/src/messages/dht-response.js +14 -14
- package/src/messages/dht.js +22 -22
- package/src/messages/file-link.js +18 -18
- package/src/messages/file.js +18 -18
- package/src/messages/peer-response.js +14 -14
- package/src/messages/peer.js +13 -13
- package/src/messages/peernet.js +14 -14
- package/src/messages/ps.js +13 -13
- package/src/messages/request.js +14 -14
- package/src/messages/response.js +14 -14
- package/src/messages.js +13 -13
- package/src/peer-info.js +9 -9
- package/src/peernet.ts +848 -776
- package/src/prompts/password/node.js +5 -5
- package/src/proto/chat-message.proto.js +6 -6
- package/src/proto/data-response.proto.js +4 -4
- package/src/proto/data.proto.js +4 -4
- package/src/proto/dht-response.proto.js +4 -4
- package/src/proto/dht.proto.js +4 -4
- package/src/proto/file-link.proto.js +5 -5
- package/src/proto/file.proto.js +5 -5
- package/src/proto/peer-response.proto.js +3 -3
- package/src/proto/peer.proto.js +3 -3
- package/src/proto/peernet.proto.js +7 -7
- package/src/proto/ps.proto.js +4 -4
- package/src/proto/request.proto.js +4 -4
- package/src/proto/response.proto.js +3 -3
- package/src/types.ts +27 -27
- package/src/utils/utils.js +78 -78
- package/test/client.js +14 -14
- package/test/codec.js +56 -56
- package/test/hash.js +13 -13
- package/test/index.js +3 -3
- package/test/lastBlock.js +7 -7
- package/test/messages.js +26 -26
- package/test/peernet.js +17 -17
- package/test.js +64 -64
- package/test2.js +9 -9
- package/test3.js +15 -15
- package/test4.js +7 -7
- package/tsconfig.json +12 -12
package/src/dht/dht.ts
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
export declare type DHTProvider = {
|
|
2
|
-
address: string,
|
|
3
|
-
id: string
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export declare type DHTProviderDistanceResult = {
|
|
7
|
-
provider: DHTProvider,
|
|
8
|
-
/**
|
|
9
|
-
* distance on earth between peers in km
|
|
10
|
-
*/
|
|
11
|
-
distance: number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export declare type DHTProviderMapValue = {[index: string]: DHTProvider}
|
|
15
|
-
|
|
16
|
-
declare type Coordinates = {
|
|
17
|
-
longitude: number,
|
|
18
|
-
latitude: number
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Keep history of fetched address and ptr
|
|
23
|
-
* @property {Object} address
|
|
24
|
-
* @property {Object} ptr
|
|
25
|
-
*/
|
|
26
|
-
const lastFetched = {
|
|
27
|
-
address: {
|
|
28
|
-
value: undefined,
|
|
29
|
-
timestamp: 0,
|
|
30
|
-
},
|
|
31
|
-
ptr: {
|
|
32
|
-
value: undefined,
|
|
33
|
-
timestamp: 0,
|
|
34
|
-
},
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const fetchedCoordinates = {}
|
|
38
|
-
|
|
39
|
-
const getAddress = async () => {
|
|
40
|
-
const {address} = lastFetched
|
|
41
|
-
if (address) {
|
|
42
|
-
address.value = await fetch('https://icanhazip.com/')
|
|
43
|
-
address.value = await address.value.text()
|
|
44
|
-
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
45
|
-
lastFetched.address = address;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return address.value
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const degreesToRadians = (degrees) => {
|
|
52
|
-
return degrees * Math.PI / 180;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
56
|
-
const earthRadiusKm = 6371;
|
|
57
|
-
|
|
58
|
-
const dLat = degreesToRadians(lat2-lat1);
|
|
59
|
-
const dLon = degreesToRadians(lon2-lon1);
|
|
60
|
-
|
|
61
|
-
lat1 = degreesToRadians(lat1);
|
|
62
|
-
lat2 = degreesToRadians(lat2);
|
|
63
|
-
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
|
|
64
|
-
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
|
|
65
|
-
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
|
66
|
-
return earthRadiusKm * c;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export default class DhtEarth {
|
|
70
|
-
providerMap = new Map<string, DHTProviderMapValue>
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
*
|
|
74
|
-
*/
|
|
75
|
-
constructor() {
|
|
76
|
-
this.providerMap = new Map();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async getCoordinates(address: string): Promise<Coordinates> {
|
|
80
|
-
if (!fetchedCoordinates[address]) {
|
|
81
|
-
const request = `https://whereis.leofcoin.org/?ip=${address}`
|
|
82
|
-
let response = await fetch(request)
|
|
83
|
-
const {lat, lon} = await response.json() as { lat: number, lon: number}
|
|
84
|
-
fetchedCoordinates[address] = {latitude: lat, longitude: lon}
|
|
85
|
-
}
|
|
86
|
-
return fetchedCoordinates[address]
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @param {Object} peer
|
|
91
|
-
* @param {Object} provider
|
|
92
|
-
* @return {Object} {provider, distance}
|
|
93
|
-
*/
|
|
94
|
-
async getDistance(peer: { latitude: number, longitude: number } , provider: DHTProvider): Promise<DHTProviderDistanceResult> {
|
|
95
|
-
const {latitude, longitude} = await this.getCoordinates(provider.address)
|
|
96
|
-
return {provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async closestPeer(providers: Array<any>): Promise<DHTProvider> {
|
|
100
|
-
let all = []
|
|
101
|
-
const address = await getAddress();
|
|
102
|
-
const peerLoc = await this.getCoordinates(address)
|
|
103
|
-
|
|
104
|
-
for (const provider of providers) {
|
|
105
|
-
if (provider.address === '127.0.0.1' || provider.address === '::1') all.push({provider, distance: 0})
|
|
106
|
-
else all.push(this.getDistance(peerLoc, provider))
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// todo queue
|
|
110
|
-
all = await Promise.all(all);
|
|
111
|
-
all = all.sort((previous, current) => previous.distance - current.distance)
|
|
112
|
-
return all[0].provider;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
hasProvider(hash: string): boolean {
|
|
116
|
-
return this.providerMap.has(hash)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
providersFor(hash: string): DHTProviderMapValue {
|
|
120
|
-
let providers: DHTProviderMapValue
|
|
121
|
-
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
122
|
-
return providers
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
addProvider(provider: DHTProvider, hash: string) {
|
|
126
|
-
let providers: DHTProviderMapValue = {}
|
|
127
|
-
if (this.providerMap.has(hash)) {
|
|
128
|
-
providers = this.providerMap.get(hash)
|
|
129
|
-
}
|
|
130
|
-
providers[provider.address] = provider
|
|
131
|
-
this.providerMap.set(hash, providers)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
removeProvider(address: string, hash: string){
|
|
135
|
-
if (this.providerMap.has(hash)) {
|
|
136
|
-
const providers = this.providerMap.get(hash)
|
|
137
|
-
delete providers[address]
|
|
138
|
-
this.providerMap.set(hash, providers)
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
1
|
+
export declare type DHTProvider = {
|
|
2
|
+
address: string,
|
|
3
|
+
id: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export declare type DHTProviderDistanceResult = {
|
|
7
|
+
provider: DHTProvider,
|
|
8
|
+
/**
|
|
9
|
+
* distance on earth between peers in km
|
|
10
|
+
*/
|
|
11
|
+
distance: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare type DHTProviderMapValue = {[index: string]: DHTProvider}
|
|
15
|
+
|
|
16
|
+
declare type Coordinates = {
|
|
17
|
+
longitude: number,
|
|
18
|
+
latitude: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Keep history of fetched address and ptr
|
|
23
|
+
* @property {Object} address
|
|
24
|
+
* @property {Object} ptr
|
|
25
|
+
*/
|
|
26
|
+
const lastFetched = {
|
|
27
|
+
address: {
|
|
28
|
+
value: undefined,
|
|
29
|
+
timestamp: 0,
|
|
30
|
+
},
|
|
31
|
+
ptr: {
|
|
32
|
+
value: undefined,
|
|
33
|
+
timestamp: 0,
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fetchedCoordinates = {}
|
|
38
|
+
|
|
39
|
+
const getAddress = async () => {
|
|
40
|
+
const {address} = lastFetched
|
|
41
|
+
if (address) {
|
|
42
|
+
address.value = await fetch('https://icanhazip.com/')
|
|
43
|
+
address.value = await address.value.text()
|
|
44
|
+
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
45
|
+
lastFetched.address = address;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return address.value
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const degreesToRadians = (degrees) => {
|
|
52
|
+
return degrees * Math.PI / 180;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
56
|
+
const earthRadiusKm = 6371;
|
|
57
|
+
|
|
58
|
+
const dLat = degreesToRadians(lat2-lat1);
|
|
59
|
+
const dLon = degreesToRadians(lon2-lon1);
|
|
60
|
+
|
|
61
|
+
lat1 = degreesToRadians(lat1);
|
|
62
|
+
lat2 = degreesToRadians(lat2);
|
|
63
|
+
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
|
|
64
|
+
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
|
|
65
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
|
66
|
+
return earthRadiusKm * c;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export default class DhtEarth {
|
|
70
|
+
providerMap = new Map<string, DHTProviderMapValue>
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
*/
|
|
75
|
+
constructor() {
|
|
76
|
+
this.providerMap = new Map();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async getCoordinates(address: string): Promise<Coordinates> {
|
|
80
|
+
if (!fetchedCoordinates[address]) {
|
|
81
|
+
const request = `https://whereis.leofcoin.org/?ip=${address}`
|
|
82
|
+
let response = await fetch(request)
|
|
83
|
+
const {lat, lon} = await response.json() as { lat: number, lon: number}
|
|
84
|
+
fetchedCoordinates[address] = {latitude: lat, longitude: lon}
|
|
85
|
+
}
|
|
86
|
+
return fetchedCoordinates[address]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {Object} peer
|
|
91
|
+
* @param {Object} provider
|
|
92
|
+
* @return {Object} {provider, distance}
|
|
93
|
+
*/
|
|
94
|
+
async getDistance(peer: { latitude: number, longitude: number } , provider: DHTProvider): Promise<DHTProviderDistanceResult> {
|
|
95
|
+
const {latitude, longitude} = await this.getCoordinates(provider.address)
|
|
96
|
+
return {provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async closestPeer(providers: Array<any>): Promise<DHTProvider> {
|
|
100
|
+
let all = []
|
|
101
|
+
const address = await getAddress();
|
|
102
|
+
const peerLoc = await this.getCoordinates(address)
|
|
103
|
+
|
|
104
|
+
for (const provider of providers) {
|
|
105
|
+
if (provider.address === '127.0.0.1' || provider.address === '::1') all.push({provider, distance: 0})
|
|
106
|
+
else all.push(this.getDistance(peerLoc, provider))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// todo queue
|
|
110
|
+
all = await Promise.all(all);
|
|
111
|
+
all = all.sort((previous, current) => previous.distance - current.distance)
|
|
112
|
+
return all[0].provider;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
hasProvider(hash: string): boolean {
|
|
116
|
+
return this.providerMap.has(hash)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
providersFor(hash: string): DHTProviderMapValue {
|
|
120
|
+
let providers: DHTProviderMapValue
|
|
121
|
+
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
122
|
+
return providers
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
addProvider(provider: DHTProvider, hash: string) {
|
|
126
|
+
let providers: DHTProviderMapValue = {}
|
|
127
|
+
if (this.providerMap.has(hash)) {
|
|
128
|
+
providers = this.providerMap.get(hash)
|
|
129
|
+
}
|
|
130
|
+
providers[provider.address] = provider
|
|
131
|
+
this.providerMap.set(hash, providers)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
removeProvider(address: string, hash: string){
|
|
135
|
+
if (this.providerMap.has(hash)) {
|
|
136
|
+
const providers = this.providerMap.get(hash)
|
|
137
|
+
delete providers[address]
|
|
138
|
+
this.providerMap.set(hash, providers)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -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 = await 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 = await protoFor(response)
|
|
26
|
-
response = await 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 = await 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 = await 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 = await protoFor(response)
|
|
26
|
+
response = await 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 = await 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/errors/errors.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export const encapsulatedError = () => {
|
|
2
|
-
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export const dhtError = (proto) => {
|
|
6
|
-
const text = `Received proto ${proto.name} expected peernet-dht-response`
|
|
7
|
-
return new Error(`Routing error: ${text}`)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const nothingFoundError = (hash) => {
|
|
11
|
-
return new Error(`nothing found for ${hash}`)
|
|
12
|
-
}
|
|
1
|
+
export const encapsulatedError = () => {
|
|
2
|
+
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export const dhtError = (proto) => {
|
|
6
|
+
const text = `Received proto ${proto.name} expected peernet-dht-response`
|
|
7
|
+
return new Error(`Routing error: ${text}`)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const nothingFoundError = (hash) => {
|
|
11
|
+
return new Error(`nothing found for ${hash}`)
|
|
12
|
+
}
|
package/src/handlers/data.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { protoFor } from './../utils/utils.js'
|
|
2
|
-
|
|
3
|
-
const dataHandler = async message => {
|
|
4
|
-
if (!message) return
|
|
5
|
-
|
|
6
|
-
const {data, id, from, peer} = message
|
|
7
|
-
const proto = await protoFor(data)
|
|
8
|
-
peernet._protoHandler({id, proto}, peernet.connections[from] || peer, from)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default dataHandler
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
const dataHandler = async message => {
|
|
4
|
+
if (!message) return
|
|
5
|
+
|
|
6
|
+
const {data, id, from, peer} = message
|
|
7
|
+
const proto = await protoFor(data)
|
|
8
|
+
peernet._protoHandler({id, proto}, peernet.connections[from] || peer, from)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default dataHandler
|
package/src/handlers/message.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
export default class MessageHandler {
|
|
2
|
-
constructor(network) {
|
|
3
|
-
this.network = network
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* hash and sign message
|
|
7
|
-
*
|
|
8
|
-
* @param {object} message
|
|
9
|
-
* @param {Buffer} message.from peer id
|
|
10
|
-
* @param {Buffer} message.to peer id
|
|
11
|
-
* @param {string} message.data Peernet message
|
|
12
|
-
* (PeernetMessage excluded) encoded as a string
|
|
13
|
-
* @return message
|
|
14
|
-
*/
|
|
15
|
-
async hashAndSignMessage(message) {
|
|
16
|
-
const hash = await message.peernetHash
|
|
17
|
-
message.decoded.signature = globalThis.identity.sign(hash.buffer)
|
|
18
|
-
return message
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {String} from - peer id
|
|
23
|
-
* @param {String} to - peer id
|
|
24
|
-
* @param {String|PeernetMessage} data - data encoded message string
|
|
25
|
-
* or the messageNode itself
|
|
26
|
-
*/
|
|
27
|
-
async prepareMessage(message) {
|
|
28
|
-
if (message.keys.includes('signature')) {
|
|
29
|
-
message = await this.hashAndSignMessage(message)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return message
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
export default class MessageHandler {
|
|
2
|
+
constructor(network) {
|
|
3
|
+
this.network = network
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* hash and sign message
|
|
7
|
+
*
|
|
8
|
+
* @param {object} message
|
|
9
|
+
* @param {Buffer} message.from peer id
|
|
10
|
+
* @param {Buffer} message.to peer id
|
|
11
|
+
* @param {string} message.data Peernet message
|
|
12
|
+
* (PeernetMessage excluded) encoded as a string
|
|
13
|
+
* @return message
|
|
14
|
+
*/
|
|
15
|
+
async hashAndSignMessage(message) {
|
|
16
|
+
const hash = await message.peernetHash
|
|
17
|
+
message.decoded.signature = globalThis.identity.sign(hash.buffer)
|
|
18
|
+
return message
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {String} from - peer id
|
|
23
|
+
* @param {String} to - peer id
|
|
24
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
25
|
+
* or the messageNode itself
|
|
26
|
+
*/
|
|
27
|
+
async prepareMessage(message) {
|
|
28
|
+
if (message.keys.includes('signature')) {
|
|
29
|
+
message = await this.hashAndSignMessage(message)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return message
|
|
33
|
+
}
|
|
34
|
+
}
|