@leofcoin/peernet 0.9.5 → 0.9.6
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/package.json +1 -1
- package/src/client.js +71 -0
- package/src/codec/codec-format-interface.js +139 -0
- package/src/codec/codec.js +114 -0
- package/src/codec/codecs.js +79 -0
- package/src/dht/dht.js +125 -0
- package/src/discovery/peer-discovery.js +75 -0
- package/src/errors/errors.js +12 -0
- package/src/handlers/message.js +52 -0
- package/src/hash/hash.js +145 -0
- package/src/http/api.js +115 -0
- package/src/http/client/api.js +41 -0
- package/src/http/client/client.js +10 -0
- package/src/http/client/http-client.js +44 -0
- package/src/http/client/storage.js +36 -0
- package/src/http/http.js +28 -0
- package/src/messages/chat-message.js +14 -0
- package/src/messages/data-response.js +14 -0
- package/src/messages/data.js +14 -0
- package/src/messages/dht-response.js +15 -0
- package/src/messages/dht.js +25 -0
- package/src/messages/peer-response.js +14 -0
- package/src/messages/peer.js +14 -0
- package/src/messages/peernet-message.js +14 -0
- package/src/messages/ps.js +14 -0
- package/src/messages/request.js +14 -0
- package/src/messages/response.js +14 -0
- package/src/peer-info.js +9 -0
- package/src/peer.js +43 -0
- package/src/peernet.js +680 -0
- package/src/proto/chat-message.proto.js +7 -0
- package/src/proto/data-response.proto.js +7 -0
- package/src/proto/data.proto.js +7 -0
- package/src/proto/dht-response.proto.js +7 -0
- package/src/proto/dht.proto.js +7 -0
- package/src/proto/peer-response.proto.js +6 -0
- package/src/proto/peer.proto.js +6 -0
- package/src/proto/peernet.proto.js +9 -0
- package/src/proto/ps.proto.js +6 -0
- package/src/proto/request.proto.js +6 -0
- package/src/proto/response.proto.js +6 -0
- package/src/server.js +25 -0
- package/src/utils/utils.js +78 -0
- package/.nyc_output/39a61420-013f-4db1-a597-7c5444da26e7.json +0 -1
- package/.nyc_output/4b387323-32a3-4eee-8f05-d13f2e0a5bf4.json +0 -1
- package/.nyc_output/ef71cf24-d9d9-45dd-814f-8d53cb6769f3.json +0 -1
- package/.nyc_output/processinfo/39a61420-013f-4db1-a597-7c5444da26e7.json +0 -1
- package/.nyc_output/processinfo/4b387323-32a3-4eee-8f05-d13f2e0a5bf4.json +0 -1
- package/.nyc_output/processinfo/ef71cf24-d9d9-45dd-814f-8d53cb6769f3.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
package/package.json
CHANGED
package/src/client.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import Pubsub from '@vandeurenglenn/little-pubsub'
|
|
2
|
+
import PeernetPeer from './peer.js'
|
|
3
|
+
import sha256 from 'crypto-js/sha256'
|
|
4
|
+
|
|
5
|
+
import P2P from 'p2pt'
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Array of peers
|
|
10
|
+
* @type {Array}
|
|
11
|
+
* @property {PeernetPeer} peer Instance of Peer
|
|
12
|
+
*/
|
|
13
|
+
globalThis.connections = new Map()
|
|
14
|
+
globalThis.recentConnections = new Map()
|
|
15
|
+
globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
|
|
16
|
+
|
|
17
|
+
export default class PeernetClient {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
|
|
20
|
+
if (!options.networkVersion) options.networkVersion = 'v0.1.0'
|
|
21
|
+
if (!options.networkName) options.networkName = 'peernet'
|
|
22
|
+
this.id = options.id
|
|
23
|
+
|
|
24
|
+
this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
|
|
25
|
+
|
|
26
|
+
const trackers = [
|
|
27
|
+
'wss://star.leofcoin.org:7575',
|
|
28
|
+
'wss://tracker.openwebtorrent.com',
|
|
29
|
+
// 'wss://tracker.sloppyta.co:443/announce',
|
|
30
|
+
]
|
|
31
|
+
this.p2p = new P2P(trackers, this.topic.slice(0, 20))
|
|
32
|
+
this.p2p.on('peerconnect', (peer) => {
|
|
33
|
+
peer = new PeernetPeer(peer.id, peer)
|
|
34
|
+
connections.set(peer.id, peer)
|
|
35
|
+
pubsub.publish('peer:discovered', peer)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
this.p2p.on('peerclose', (peer) => {
|
|
39
|
+
// TODO: close peernetPeer
|
|
40
|
+
connections.delete(peer.id)
|
|
41
|
+
pubsub.publish('peer:disconnected', peer)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
this.p2p.start()
|
|
45
|
+
|
|
46
|
+
if (globalThis.process) {
|
|
47
|
+
process.on('SIGINT', async () => {
|
|
48
|
+
console.log('Caught interrupt signal')
|
|
49
|
+
this.close()
|
|
50
|
+
setTimeout(async () => {
|
|
51
|
+
process.exit();
|
|
52
|
+
}, 100);
|
|
53
|
+
})
|
|
54
|
+
} else {
|
|
55
|
+
globalThis.onbeforeunload = () => {
|
|
56
|
+
this.close()
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//
|
|
60
|
+
// this.sw.on('close', () => {
|
|
61
|
+
// })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
close() {
|
|
65
|
+
return this.p2p.destroy()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
_peers() {
|
|
69
|
+
return this.p2p.getPeers()
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import bs32 from 'bs32';
|
|
2
|
+
import bs58 from 'bs58';
|
|
3
|
+
import isHex from 'is-hex';
|
|
4
|
+
import Codec from './codec';
|
|
5
|
+
import Hash from './../hash/hash'
|
|
6
|
+
|
|
7
|
+
export default class FormatInterface {
|
|
8
|
+
/**
|
|
9
|
+
* @param {Buffer|String|Object} buffer -
|
|
10
|
+
* @param {Object} proto - {encode, decode}
|
|
11
|
+
* @param {Object} options - {hashFormat, name}
|
|
12
|
+
*/
|
|
13
|
+
constructor(buffer, proto, options = {}) {
|
|
14
|
+
this.protoEncode = proto.encode
|
|
15
|
+
this.protoDecode = proto.decode
|
|
16
|
+
if (options.name) this.name = options.name
|
|
17
|
+
this.hashFormat = options.hashFormat || 'bs32'
|
|
18
|
+
if (Buffer.isBuffer(buffer)) {
|
|
19
|
+
const codec = new Codec(buffer)
|
|
20
|
+
if (codec.name) {
|
|
21
|
+
this.fromEncoded(buffer)
|
|
22
|
+
} else {
|
|
23
|
+
this.create(buffer)
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
if (typeof buffer === 'string') {
|
|
27
|
+
if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
+
else if (bs32.test(buffer)) this.fromBs32(buffer)
|
|
29
|
+
else this.fromBs58(buffer)
|
|
30
|
+
} else if (typeof buffer === 'object' && !Array.isArray(buffer)) {
|
|
31
|
+
this.create(buffer)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @return {PeernetHash}
|
|
38
|
+
*/
|
|
39
|
+
get peernetHash() {
|
|
40
|
+
return new Hash(this.decoded, {name: this.name})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @return {peernetHash}
|
|
45
|
+
*/
|
|
46
|
+
get hash() {
|
|
47
|
+
const upper = this.hashFormat.charAt(0).toUpperCase()
|
|
48
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
|
|
49
|
+
return this.peernetHash[`to${format}`]()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @return {Object}
|
|
54
|
+
*/
|
|
55
|
+
decode() {
|
|
56
|
+
let encoded = this.encoded;
|
|
57
|
+
const discoCodec = new Codec(this.encoded.toString('hex'))
|
|
58
|
+
encoded = encoded.slice(discoCodec.codecBuffer.length)
|
|
59
|
+
this.name = discoCodec.name
|
|
60
|
+
this.decoded = this.protoDecode(encoded)
|
|
61
|
+
return this.decoded
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @return {Buffer}
|
|
66
|
+
*/
|
|
67
|
+
encode(decoded) {
|
|
68
|
+
if (!decoded) decoded = this.decoded;
|
|
69
|
+
const codec = new Codec(this.name)
|
|
70
|
+
this.encoded = Buffer.concat([codec.codecBuffer, this.protoEncode(decoded)])
|
|
71
|
+
return this.encoded
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {Buffer} encoded
|
|
76
|
+
*/
|
|
77
|
+
fromEncoded(encoded) {
|
|
78
|
+
const codec = new Codec(encoded)
|
|
79
|
+
this.name = codec.name
|
|
80
|
+
this.encoded = encoded
|
|
81
|
+
this.decode()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {String} encoded
|
|
86
|
+
*/
|
|
87
|
+
fromHex(encoded) {
|
|
88
|
+
this.encoded = Buffer.from(encoded, 'hex')
|
|
89
|
+
this.decode()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {String} encoded
|
|
94
|
+
*/
|
|
95
|
+
fromBs32(encoded) {
|
|
96
|
+
this.encoded = bs32.decode(encoded)
|
|
97
|
+
this.decode()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {String} encoded
|
|
102
|
+
*/
|
|
103
|
+
fromBs58(encoded) {
|
|
104
|
+
this.encoded = bs58.decode(encoded)
|
|
105
|
+
this.decode()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @return {String} encoded
|
|
110
|
+
*/
|
|
111
|
+
toHex() {
|
|
112
|
+
if (!this.encoded) this.encode()
|
|
113
|
+
return this.encoded.toString('hex')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @return {String} encoded
|
|
118
|
+
*/
|
|
119
|
+
toBs32() {
|
|
120
|
+
if (!this.encoded) this.encode()
|
|
121
|
+
return bs32.encode(this.encoded)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @return {String} encoded
|
|
126
|
+
*/
|
|
127
|
+
toBs58() {
|
|
128
|
+
if (!this.encoded) this.encode()
|
|
129
|
+
return bs58.encode(this.encoded)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {Object} data
|
|
134
|
+
*/
|
|
135
|
+
create(data) {
|
|
136
|
+
this.decoded = data
|
|
137
|
+
this.encode(data)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import varint from 'varint';
|
|
2
|
+
import bs32 from 'bs32';
|
|
3
|
+
import bs58 from 'bs58';
|
|
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.isBuffer(buffer)) {
|
|
14
|
+
const codec = varint.decode(buffer);
|
|
15
|
+
const name = this.getCodecName(codec)
|
|
16
|
+
|
|
17
|
+
if (name) {
|
|
18
|
+
this.name = name
|
|
19
|
+
this.encoded = buffer
|
|
20
|
+
this.decode(buffer)
|
|
21
|
+
} else {
|
|
22
|
+
this.encode(buffer)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (typeof buffer === 'string') {
|
|
26
|
+
if (this.codecs[buffer]) this.fromName(buffer)
|
|
27
|
+
else if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
+
else if (bs32.test(buffer)) this.fromBs32(buffer)
|
|
29
|
+
else this.fromBs58(buffer)
|
|
30
|
+
}
|
|
31
|
+
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fromEncoded(encoded) {
|
|
36
|
+
const codec = varint.decode(encoded);
|
|
37
|
+
const name = this.getCodecName(codec)
|
|
38
|
+
this.name = name
|
|
39
|
+
this.encoded = encoded
|
|
40
|
+
this.decode(encoded)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fromHex(hex) {
|
|
44
|
+
this.encoded = Buffer.from(hex, 'hex')
|
|
45
|
+
this.decode()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fromBs32(input) {
|
|
49
|
+
this.encoded = bs32.decode(input)
|
|
50
|
+
this.decode()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fromBs58(input) {
|
|
54
|
+
this.encoded = bs58.decode(input)
|
|
55
|
+
this.decode()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getCodec(name) {
|
|
59
|
+
return this.codecs[name].codec
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getCodecName(codec) {
|
|
63
|
+
return Object.keys(this.codecs).reduce((p, c) => {
|
|
64
|
+
if (parseInt(Buffer.from(`${this.getCodec(c)}`, 'hex').toString('hex'), 16) === codec) return c;
|
|
65
|
+
else return p;
|
|
66
|
+
}, undefined)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getHashAlg(name) {
|
|
70
|
+
return this.codecs[name].hashAlg
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fromCodec(codec) {
|
|
74
|
+
this.name = this.getCodecName(codec)
|
|
75
|
+
this.hashAlg = this.getHashAlg(this.name)
|
|
76
|
+
|
|
77
|
+
this.codec = this.getCodec(this.name)
|
|
78
|
+
this.codecBuffer = Buffer.from(varint.encode(parseInt(Buffer.from(`${this.codec}`, 'hex').toString('hex'), 16)), 'hex');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fromName(name) {
|
|
82
|
+
const codec = this.getCodec(name)
|
|
83
|
+
this.name = name
|
|
84
|
+
this.codec = codec
|
|
85
|
+
this.hashAlg = this.getHashAlg(name)
|
|
86
|
+
this.codecBuffer = Buffer.from(varint.encode(parseInt(Buffer.from(`${codec}`, 'hex').toString('hex'), 16)), 'hex');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toBs32() {
|
|
90
|
+
this.encode()
|
|
91
|
+
return bs32.encode(this.encoded)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
toBs58() {
|
|
95
|
+
this.encode()
|
|
96
|
+
return bs58.encode(this.encoded)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
toHex() {
|
|
100
|
+
return this.encoded.toString('hex')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
decode() {
|
|
104
|
+
const codec = varint.decode(this.encoded);
|
|
105
|
+
this.fromCodec(codec)
|
|
106
|
+
this.name = this.getCodecName(codec)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
encode() {
|
|
110
|
+
const codec = Buffer.from(varint.encode(parseInt(Buffer.from(`${this.codec}`, 'hex').toString('hex'), 16)), 'hex');
|
|
111
|
+
this.encoded = codec
|
|
112
|
+
return this.encoded
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
// just a hash
|
|
3
|
+
'disco-hash': {
|
|
4
|
+
codec: '30',
|
|
5
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
6
|
+
// testnet: 'olivia'
|
|
7
|
+
},
|
|
8
|
+
'peernet-peer-response': {
|
|
9
|
+
codec: '707072',
|
|
10
|
+
hashAlg: 'keccak-256',
|
|
11
|
+
},
|
|
12
|
+
'peernet-peer': {
|
|
13
|
+
codec: '7070',
|
|
14
|
+
hashAlg: 'keccak-256',
|
|
15
|
+
},
|
|
16
|
+
'peernet-dht': {
|
|
17
|
+
codec: '706468',
|
|
18
|
+
hashAlg: 'keccak-256',
|
|
19
|
+
},
|
|
20
|
+
'peernet-dht-response': {
|
|
21
|
+
codec: '706472',
|
|
22
|
+
hashAlg: 'keccak-256',
|
|
23
|
+
},
|
|
24
|
+
// data
|
|
25
|
+
'peernet-data': {
|
|
26
|
+
codec: '706461',
|
|
27
|
+
hashAlg: 'keccak-256',
|
|
28
|
+
},
|
|
29
|
+
'peernet-data-response': {
|
|
30
|
+
codec: '70646172',
|
|
31
|
+
hashAlg: 'keccak-256',
|
|
32
|
+
},
|
|
33
|
+
// message
|
|
34
|
+
'peernet-message': {
|
|
35
|
+
codec: '706d65',
|
|
36
|
+
hashAlg: 'keccak-512',
|
|
37
|
+
},
|
|
38
|
+
// pubsub
|
|
39
|
+
'peernet-ps': {
|
|
40
|
+
codec: '707073',
|
|
41
|
+
hashAlg: 'keccak-256',
|
|
42
|
+
},
|
|
43
|
+
'peernet-response': {
|
|
44
|
+
codec: '7072',
|
|
45
|
+
hashAlg: 'keccak-256',
|
|
46
|
+
},
|
|
47
|
+
'peernet-request': {
|
|
48
|
+
codec: '707271',
|
|
49
|
+
hashAlg: 'keccak-256',
|
|
50
|
+
},
|
|
51
|
+
// normal block
|
|
52
|
+
'leofcoin-block': {
|
|
53
|
+
codec: '6c62',
|
|
54
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
55
|
+
// testnet: 'olivia'
|
|
56
|
+
},
|
|
57
|
+
'leofcoin-tx': {
|
|
58
|
+
codec: '6c74',
|
|
59
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
60
|
+
// testnet: 'olivia'
|
|
61
|
+
},
|
|
62
|
+
// itx
|
|
63
|
+
'leofcoin-itx': {
|
|
64
|
+
codec: '6c69',
|
|
65
|
+
hashAlg: 'keccak-512', // ,
|
|
66
|
+
// testnet: 'olivia'
|
|
67
|
+
},
|
|
68
|
+
// peer reputation
|
|
69
|
+
'leofcoin-pr': {
|
|
70
|
+
codec: '6c70',
|
|
71
|
+
hashAlg: 'keccak-256', // ,
|
|
72
|
+
// testnet: 'olivia'
|
|
73
|
+
},
|
|
74
|
+
// chat message
|
|
75
|
+
'chat-message': {
|
|
76
|
+
codec: '636d',
|
|
77
|
+
hashAlg: 'dbl-keccak-512',
|
|
78
|
+
},
|
|
79
|
+
}
|
package/src/dht/dht.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
all.push(this.getDistance(peerLoc, provider))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
all = await Promise.all(all)
|
|
95
|
+
|
|
96
|
+
const closestPeer = all.reduce((p, c) => {
|
|
97
|
+
if (!c.distance || c.distance === NaN) c.distance = 0
|
|
98
|
+
if (c.distance < p || p === 0) return c.provider;
|
|
99
|
+
}, 0)
|
|
100
|
+
|
|
101
|
+
return closestPeer;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {String} hash
|
|
106
|
+
* @return {Array} providers
|
|
107
|
+
*/
|
|
108
|
+
providersFor(hash) {
|
|
109
|
+
return this.providerMap.get(hash);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {String} address
|
|
114
|
+
* @param {String} hash
|
|
115
|
+
* @return {Array} providers
|
|
116
|
+
*/
|
|
117
|
+
async addProvider(address, hash) {
|
|
118
|
+
let providers = [];
|
|
119
|
+
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
120
|
+
|
|
121
|
+
providers = new Set([...providers, address])
|
|
122
|
+
this.providerMap.set(hash, providers)
|
|
123
|
+
return providers;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +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(from, 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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import MultiWallet from '@leofcoin/multi-wallet'
|
|
2
|
+
import Hash from './../hash/hash.js'
|
|
3
|
+
import PeernetMessage from './../messages/peernet-message.js'
|
|
4
|
+
|
|
5
|
+
export default class MessageHandler {
|
|
6
|
+
constructor(network) {
|
|
7
|
+
this.network = network
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* hash and sign message
|
|
11
|
+
*
|
|
12
|
+
* @param {object} message
|
|
13
|
+
* @param {Buffer} message.from peer id
|
|
14
|
+
* @param {Buffer} message.to peer id
|
|
15
|
+
* @param {string} message.data Peernet message
|
|
16
|
+
* (PeernetMessage excluded) encoded as a string
|
|
17
|
+
* @return signature
|
|
18
|
+
*/
|
|
19
|
+
async hashAndSignMessage(message) {
|
|
20
|
+
const hasher = new Hash(message, {name: 'peernet-message'})
|
|
21
|
+
const identity = await walletStore.get('identity')
|
|
22
|
+
|
|
23
|
+
const wallet = new MultiWallet(this.network)
|
|
24
|
+
wallet.import(identity.multiWIF)
|
|
25
|
+
return wallet.sign(hasher.hash.slice(0, 32))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {String} from - peer id
|
|
30
|
+
* @param {String} to - peer id
|
|
31
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
32
|
+
* or the messageNode itself
|
|
33
|
+
*/
|
|
34
|
+
async prepareMessage(from, to, data) {
|
|
35
|
+
if (!Buffer.isBuffer(from)) from = new Buffer.from(from)
|
|
36
|
+
if (!Buffer.isBuffer(to)) to = new Buffer.from(to)
|
|
37
|
+
if (data.encoded) data = data.encoded
|
|
38
|
+
|
|
39
|
+
const message = {
|
|
40
|
+
from,
|
|
41
|
+
to,
|
|
42
|
+
data,
|
|
43
|
+
}
|
|
44
|
+
const signature = await this.hashAndSignMessage(message)
|
|
45
|
+
const node = new PeernetMessage({
|
|
46
|
+
...message,
|
|
47
|
+
signature,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return node
|
|
51
|
+
}
|
|
52
|
+
}
|