holepunch-hop 0.3.4 → 0.3.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "holepunch-hop",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -422,6 +422,7 @@ class HyperBee extends EventEmitter {
422
422
  savePeer = async function (peerInfo) {
423
423
  await this.dbPeers.put(peerInfo.publickey, peerInfo)
424
424
  let checkSave = await this.getPeer(peerInfo.publickey)
425
+ console.log('save peer over---------')
425
426
  return checkSave
426
427
  }
427
428
 
@@ -760,8 +761,6 @@ class HyperBee extends EventEmitter {
760
761
 
761
762
  }
762
763
 
763
-
764
-
765
764
  /**
766
765
  * delete nxp ref contract public
767
766
  * @method deleteRefcontPubliclibrary
@@ -788,7 +787,6 @@ class HyperBee extends EventEmitter {
788
787
  return deleteInfo
789
788
  }
790
789
 
791
-
792
790
  /**
793
791
  * repicate the publiclibrary peer to peer
794
792
  * @method replicatePubliclibrary
@@ -918,10 +916,10 @@ class HyperBee extends EventEmitter {
918
916
 
919
917
  /**
920
918
  * repicate the publiclibrary peer to peer
921
- * @method replicatePubliclibraryOLD
919
+ * @method ryOLD
922
920
  *
923
921
  */
924
- replicatePubliclibraryOLD = async function (key) {
922
+ ryOLD = async function (key) {
925
923
  // key = '3ec0f3b78a0cfe574c4be89b1d703a65f018c0b73ad77e52ac65645d8f51676a'
926
924
  const store = this.client.corestore('peerspace-hyperbeetemp')
927
925
  const core = this.store.get(key)
@@ -974,9 +972,6 @@ class HyperBee extends EventEmitter {
974
972
 
975
973
  await this.client.replicate(beeResults.feed) // fetch from the network
976
974
  await beeResults.ready()
977
- // console.log('value for key ie results dataPrint')
978
- // console.log(await beeResults.get('005ad9c1c29b6b730b6e9f73dd108f8c716a6075'))
979
- // console.log('after get uuid')
980
975
  let rs = beeResults.createReadStream() // anything >=a and <=d
981
976
 
982
977
  for await (const { key, value } of rs) {
package/src/index.js CHANGED
@@ -34,7 +34,9 @@ class HolepunchWorker extends EventEmitter {
34
34
  this.core3 = {}
35
35
  this.discKeypeer = ''
36
36
  this.readcore = null
37
- this.warmPeers = []
37
+ this.warmPeers = [] // ikeep track of live incoming sharing
38
+ this.codenameUpdates = []
39
+ this.topicExhange = []
38
40
  this.startHolepunch()
39
41
  this.networkListeners()
40
42
  }
@@ -110,12 +112,19 @@ class HolepunchWorker extends EventEmitter {
110
112
  this.wsocket.send(JSON.stringify(peerFail))
111
113
  })
112
114
  // save peer topic
113
- this.Peers.on('peer-topic-save', async (data) => {
114
- await this.emit('peer-topic-save', data)
115
+ this.Peers.on('peer-reconnect-topic', async (data) => {
116
+ data.prime = false
117
+ this.emit('peer-topic-update', data)
115
118
  })
116
119
  // peer reconnection topic ie. able to reconnect again
117
- this.Peers.on('peer-reconnect-topic', (data) => {
118
- this.emit('peer-reconnect-topic', data)
120
+ this.Peers.on('topic-formed-save', (data) => {
121
+ // put data into holder await for codename matching over
122
+ this.topicExhange.push(data)
123
+ })
124
+ // codename matching
125
+ this.Peers.on('peer-codename-match', (data) => {
126
+ // put in holding and then complete once save first complete
127
+ this.codenameUpdates.push(data)
119
128
  })
120
129
  // data for beebee
121
130
  this.Peers.on('beebee-data', (data) => {
@@ -124,6 +133,12 @@ class HolepunchWorker extends EventEmitter {
124
133
  // cue space share
125
134
  this.Peers.on('cuespace-notification', (data) => {
126
135
  this.emit('peer-cuespace', data)
136
+ // check if bentoboxN1 included?
137
+ if (data.data.content.bbn1.publicN1contracts.length > 0) {
138
+ for (let n1Cont of data.data.content.bbn1.publicN1contracts) {
139
+ this.BeeData.replicatePubliclibrary({ data: n1Cont })
140
+ }
141
+ }
127
142
  })
128
143
  // public library notification
129
144
  this.Peers.on('publiclibrarynotification', (data) => {
@@ -135,21 +150,37 @@ class HolepunchWorker extends EventEmitter {
135
150
  })
136
151
  // new warm incoming peer
137
152
  this.Peers.on('connect-warm-first', (data) => {
138
- let peerInfo = this.Peers.peerHolder[data]
139
- if (peerInfo === undefined) {
140
- // receiving peer
141
- peerInfo = { name: 'peernew'}
153
+ // check re establishing peer to peer of first time connection?
154
+ let peerInfoName = 'not-matched' //this.Peers.peerHolder[data]
155
+ // check role and match codename
156
+ if (data.roletaken === 'server') {
157
+ // not unique info to match on yet.
158
+ } else {
159
+ let peerRole = this.Peers.matchCodename(data.publickey)
160
+ if (peerRole.name.length > 0) {
161
+ // receiving peer
162
+ peerInfoName = peerRole.name
163
+ }
142
164
  }
165
+ // setup template for relationship
143
166
  let peerId = {}
144
- peerId.name = peerInfo.name
145
- peerId.publickey = data
167
+ peerId.name = peerInfoName
168
+ peerId.publickey = data.publickey
169
+ peerId.roletaken = data.roletaken
146
170
  peerId.longterm = true
147
171
  peerId.settopic = false
148
172
  peerId.topic = ''
149
173
  peerId.live = false
174
+ peerId.livePeerkey = ''
150
175
  this.warmPeers.push(peerId)
151
176
  this.emit('peer-incoming-save', peerId)
152
177
  })
178
+ // peer live on network?
179
+ this.Peers.on('peer-live-network', (data) => {
180
+ let peerLive = {}
181
+ peerLive.publickey = data
182
+ this.emit('peer-live-notify', peerLive)
183
+ })
153
184
  // drive listener
154
185
  this.DriveFiles.on('largefile-save', (data) => {
155
186
  this.emit('drive-save-large', data)
@@ -163,78 +194,119 @@ class HolepunchWorker extends EventEmitter {
163
194
  */
164
195
  networkPath = function (message) {
165
196
  if (message.action === 'share') {
166
- // has the peer joined already?
167
- let peerMatch = false
168
- for (let wpeer of this.warmPeers) {
169
- if (wpeer.publickey = message.data.publickey) {
170
- peerMatch = true
197
+ // check if joined now?
198
+ let reEstablishShort = this.Peers.checkConnectivityStatus(message, this.warmPeers, 'invite-gen')
199
+ if (message.task === 'peer-share-invite' || message.task === 'peer-share-topic') {
200
+ // has the peer joined already?
201
+ let peerMatch = false
202
+ for (let wpeer of this.warmPeers) {
203
+ if (wpeer.publickey = message.data.publickey) {
204
+ peerMatch = true
205
+ }
171
206
  }
172
- }
173
- if (message.task === 'peer-share-invite') {
174
- // keep track of role, reciving or extended invite
175
- this.Peers.setRole(message.data.publickey)
176
- if (peerMatch === true) {
177
- this.Peers.peerAlreadyJoinSetData(message.data)
178
- // this.Peers.writeTonetwork(message.data.publickey)
179
- this.warmPeerPrepare(message.data.publickey, true)
180
- } else {
207
+ // new peer?
208
+ let peerTopeerState = this.Peers.checkConnectivityStatus(message, this.warmPeers, 'share-path')
209
+ if (peerTopeerState.live === false && peerTopeerState.existing === false) {
210
+ // first time
211
+ this.Peers.setRole({ pubkey: message.data.publickey, codename: message.data.codename, name: message.data.name })
212
+ // new peer keep track and start join process
181
213
  this.warmPeers.push(message.data)
182
214
  this.Peers.peerAlreadyJoinSetData(message.data)
183
215
  this.Peers.peerJoin(message.data)
184
- }
185
- } else if (message.task === 'peer-share-topic') {
186
- // existing peers reconnecting via topic
187
- this.Peers.topicConnect(message.data)
188
- } else if (message.task === 'public-n1-experiment') {
189
- if (peerMatch === true) {
190
- this.Peers.peerAlreadyJoinSetData(message.data)
191
- this.Peers.writeToPublicLibrary(message.data.publickey)
192
216
  } else {
217
+ // twooptions peer still online so reconnect, or both been offline, reconnect via topic, if topic first time or subdquesnt?
218
+ // try to connect like first time
193
219
  this.warmPeers.push(message.data)
194
- this.Peers.peerJoin(message.data)
195
- // now set data and write to public library info.
196
220
  this.Peers.peerAlreadyJoinSetData(message.data)
197
- this.Peers.writeToPublicLibrary(message.data.publickey)
198
- }
199
- } else if (message.task = 'cue-space') {
200
- if (peerMatch === true) {
201
- this.Peers.peerAlreadyJoinSetData(message.data)
202
- this.Peers.writeToCueSpace(message.data.publickey)
203
- } else {
204
- this.warmPeers.push(message.data)
205
- this.Peers.peerJoin(message.data)
206
- // this.Peers.peerAlreadyJoinSetData(message.data)
207
- // this.Peers.writeToCueSpace(message.data.publickey)
221
+ // this.Peers.peerJoin(message.data)
222
+
223
+ this.Peers.setRestablished(message.data.publickey, reEstablishShort)
224
+ if (reEstablishShort.live === true) {
225
+ // first time use or returning use?
226
+ if (Object.keys(reEstablishShort.peer).length === 0) {
227
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
228
+ this.Peers.routeDataPath(message.data.publickey, peerActionData.data)
229
+ } else {
230
+ if (reEstablishShort.peer.value?.livePeerkey.length === 0) {
231
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
232
+ this.Peers.routeDataPath(message.data.publickey, peerActionData.data)
233
+ } else {
234
+ // returning peer via topic
235
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
236
+ this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
237
+ }
238
+ }
239
+ } else {
240
+ // one peer server one peer client on topic based upon who set the topic
241
+ if (reEstablishShort.peer.value.settopic === true) {
242
+ this.Peers.topicConnect(reEstablishShort.peer.value.topic)
243
+ } else {
244
+ this.Peers.topicListen(reEstablishShort.peer.value.topic, message.data.publickey)
245
+ }
246
+ }
208
247
  }
209
- } else if (message.task === 'peer-write') {
210
- this.emit('peer-write', message.data)
211
- } else if (message.task === 'topic') {
212
- // this.Peers.peerTopic(message.data.topic)
248
+ } else if (message.task === 'peer-share-codename') {
249
+ // from peer generating the invite
250
+ this.Peers.setRole({ pubkey: message.data.publickey, codename: message.data.codename, name: message.data.name })
251
+ } else if (message.task === 'cue-space') {
252
+ this.Peers.peerAlreadyJoinSetData(message.data)
253
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
254
+ this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
255
+ } else if (message.task === 'public-n1-experiment') {
256
+ this.Peers.peerAlreadyJoinSetData(message.data)
257
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
258
+ this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
213
259
  }
214
260
  }
215
261
  }
216
262
 
217
-
218
263
  /**
219
264
  * prepare data to send to a warm peer
220
265
  * @method warmPeerPrepare
221
266
  */
222
267
  warmPeerPrepare = function (data, existing) {
268
+ // check if codename holder has any data to process
269
+ this.processCodenameMatching(data)
223
270
  // two checks, if topic send to other peer
224
271
  if (existing !== true) {
225
- let peerRole = this.Peers.peersRole[data]
226
- if (peerRole === undefined) {
227
- this.Peers.writeTonetworkTopic(data)
228
- } else {
229
- console.log(' this peer set the topic')
272
+ // match publick key to warmpeers
273
+ let peerMatch = {}
274
+ for (let wpeer of this.warmPeers) {
275
+ if (wpeer.publickey === data) {
276
+ peerMatch = wpeer
277
+ }
278
+ }
279
+ // client of server Role?
280
+ // let role = this.Peers.getRole(data)
281
+ if (peerMatch.roletaken === 'client') {
282
+ let roleStatus = this.Peers.matchCodename(data)
283
+ let codenameInform = {}
284
+ codenameInform.type = 'peer-codename-inform'
285
+ codenameInform.action = 'set'
286
+ codenameInform.data = { inviteCode: roleStatus.codename , publickey: data, peerkey: this.swarm.keyPair.publicKey.toString('hex') }
287
+ this.Peers.writeTonetworkData(data, codenameInform)
288
+ // inform peer of codename
289
+ } else if (peerMatch.roletaken === 'server') {
290
+ // notify beebee peer to live
291
+ let codeNameInform = {}
292
+ codeNameInform.type = 'peer-codename-inform'
293
+ codeNameInform.action = 'set'
294
+ codeNameInform.data = { inviteCode: '' , publickey: data }
295
+ // in form beebee
296
+ this.emit('invite-live-peer', codeNameInform)
297
+ // send topic to allow peer to reconnect
298
+ this.Peers.writeTonetworkTopic(data, codeNameInform)
230
299
  }
231
300
  }
301
+
232
302
  // if data within coming then process that
233
303
  let peerDataExist = this.Peers.peerHolder[data]
234
304
  if (peerDataExist === undefined) {
235
305
  } else {
236
306
  // what type of data being shared?
237
307
  // check for data along with new peer?
308
+ this.Peers.routeDataPath(data, peerDataExist)
309
+ /*
238
310
  if (peerDataExist.data !== undefined) {
239
311
  if (peerDataExist.data.type === 'private-chart') {
240
312
  this.Peers.writeTonetworkData(data, peerDataExist.data)
@@ -248,8 +320,54 @@ class HolepunchWorker extends EventEmitter {
248
320
  // simpole text message
249
321
  this.Peers.writeTonetwork(data)
250
322
  }
323
+ } */
324
+ }
325
+ }
326
+
327
+ /**
328
+ * process codename matches after first save has happened.
329
+ * @method testCorestore
330
+ *
331
+ */
332
+ processCodenameMatching = async function (data) {
333
+ let updateCodeName = []
334
+ for (let cname of this.codenameUpdates) {
335
+ if (cname.data.peerkey === data) {
336
+ let matchCodename = this.Peers.matchPeersCodename(cname)
337
+ // need to matchs
338
+ let warmMatch = {}
339
+ for (let wpeer of this.warmPeers) {
340
+ if (wpeer.publickey === cname.data.peerkey) {
341
+ warmMatch = wpeer
342
+ }
343
+ }
344
+ matchCodename.peerkey = cname.data.peerkey
345
+ // update save for longterm and inform beebee
346
+ this.emit('peer-codename-update', matchCodename)
347
+ } else {
348
+ updateCodeName.push(cname)
349
+ }
350
+ }
351
+ this.codenameUpdates = updateCodeName
352
+ }
353
+
354
+ /**
355
+ * inform other peer of token for future connection
356
+ * @method topicSaveReturn
357
+ *
358
+ */
359
+ topicSaveReturn = function (data) {
360
+ let updateTopic = []
361
+ for (let ctopic of this.topicExhange) {
362
+ if (ctopic.key === data.publickey) {
363
+ this.emit('peer-reconnect-topic-notify', ctopic)
364
+ // update saved contract to add topic
365
+ this.emit('peer-topic-update', ctopic)
366
+ } else {
367
+ updateTopic.push(ctopic)
251
368
  }
252
369
  }
370
+ this.tpiocSaveReturn = updateTopic
253
371
  }
254
372
 
255
373
  /**
package/src/peers.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * @version $Id$
10
10
  */
11
11
  import EventEmitter from 'events'
12
-
12
+ import crypto from 'crypto'
13
13
 
14
14
  class NetworkPeers extends EventEmitter {
15
15
 
@@ -19,9 +19,14 @@ class NetworkPeers extends EventEmitter {
19
19
  this.store = store
20
20
  this.swarm = swarm
21
21
  this.drive = {}
22
+ this.peerPrime = ''
23
+ this.peerNetwork = [] // set on loading library via HOP
24
+ this.peerEstContext = {}
22
25
  this.peerHolder = {}
23
26
  this.peerConnect = {}
24
- this.peersRole = {}
27
+ this.topicHolder = {}
28
+ this.sendTopicHolder = []
29
+ this.peersRole = []
25
30
  }
26
31
 
27
32
  /**
@@ -31,16 +36,21 @@ class NetworkPeers extends EventEmitter {
31
36
  */
32
37
  networkKeys = function () {
33
38
  // console.log('swarm on start')
39
+ // console.log(this.swarm)
34
40
  // console.log(this.swarm._discovery) // .toString('hex'))
35
41
 
36
- /*this.swarm._discovery.forEach((value, key) => {
42
+ /*
43
+ this.swarm._discovery.forEach((value, key) => {
37
44
  console.log('key')
38
45
  console.log(key)
39
- console.log('-----------swarm discovery IN-------------------')
46
+ this.peerPrime = key
47
+ console.log(this.peerPrime)
48
+ console.log('-----------swarm discovery on START-------------------')
40
49
  console.log(Object.keys(value))
41
50
  console.log(value.topic)
42
51
  console.log(value.topic.toString('hex'))
43
- }) */
52
+ })
53
+ */
44
54
  let peerNxKeys = {}
45
55
  peerNxKeys.publickey = this.swarm.keyPair.publicKey.toString('hex')
46
56
  let networkMessage = {}
@@ -52,6 +62,44 @@ class NetworkPeers extends EventEmitter {
52
62
  this.peerJoinClient()
53
63
  }
54
64
 
65
+ /**
66
+ * set role in peer to peer relationship, invte or receive?
67
+ * @method setRole
68
+ *
69
+ */
70
+ setRole = function (peerData) {
71
+ let setRole = { send: 'prime' , invite: peerData}
72
+ this.peersRole.push(setRole)
73
+ }
74
+
75
+ /**
76
+ * look at role of each peer save and join or listen to network
77
+ * @method setupConnectionBegin
78
+ *
79
+ */
80
+ setupConnectionBegin = function (peerNetwork) {
81
+ for (let sPeer of peerNetwork) {
82
+ if (sPeer.value.settopic === true) {
83
+ // client role
84
+ this.topicConnect(sPeer.value.topic)
85
+ } else {
86
+ // server role
87
+ this.topicListen(sPeer.value.topic, sPeer.key)
88
+ }
89
+ }
90
+ }
91
+
92
+ /**
93
+ * incoming establsihed information
94
+ * @method setRestablished
95
+ *
96
+ */
97
+ setRestablished = function (pubKey, established) {
98
+ this.peerEstContext[pubKey] = established
99
+ }
100
+
101
+
102
+
55
103
  /**
56
104
  * connection listen
57
105
  * @method listenNetwork
@@ -59,12 +107,121 @@ class NetworkPeers extends EventEmitter {
59
107
  */
60
108
  listenNetwork = function () {
61
109
  this.swarm.on('connection', (conn, info) => {
110
+ // console.log('peerinfo FIRST------------')
111
+ // console.log(info)
62
112
  // save peer connection instance for ongoing communication
63
- let publicKeylive = info.publicKey.toString('hex')
64
- this.peerConnect[publicKeylive] = conn
65
- this.emit('connect-warm-first', publicKeylive)
66
113
  // listen for replication NEED UPTATED LOGIC
67
114
  this.store.replicate(conn)
115
+ // assess if token is present to match to exsing peer account ID?
116
+ let publicKeylive = info.publicKey.toString('hex')
117
+ let topicKeylive = info.topics
118
+ let roleTaken = info.client
119
+ // if now topics info then server, pass by
120
+ let discoveryTopicInfo = {}
121
+ if (topicKeylive.length === 0) {
122
+ // check if joined now?
123
+ discoveryTopicInfo = this.checkDisoveryStatus('server')
124
+ } else {
125
+ discoveryTopicInfo = {topic: ''}
126
+ }
127
+ let topicServer = this.topicHolder[discoveryTopicInfo.topic]
128
+ let serverStatus = false
129
+ if (topicServer !== undefined) {
130
+ if (Object.keys(topicServer).length > 0) {
131
+ serverStatus = true
132
+ }
133
+ }
134
+ // check if toppic via matching or being client receiving
135
+ if (topicKeylive.length > 0 || serverStatus === true) {
136
+ // keep track of connection
137
+ this.peerConnect[publicKeylive] = conn
138
+ if (topicKeylive.length > 0) {
139
+ let topicIn = topicKeylive[0].toString('hex')
140
+ let topicMatch = this.topicHolder[topicIn]
141
+ if (Object.keys(topicMatch).length > 0) {
142
+ // looks at what needs data needs cshared
143
+ // match new public key to saved publickey
144
+ topicMatch.currentPubkey = publicKeylive
145
+ this.topicHolder[topicIn] = topicMatch
146
+ this.dataFlowCheck(topicIn, 'client')
147
+ } else {
148
+ this.peerConnect[publicKeylive] = conn
149
+
150
+ }
151
+ // update to live client
152
+ let originalKey = ''
153
+ for (let savePeer of this.peerNetwork) {
154
+ if (savePeer.value.topic === topicIn) {
155
+ originalKey = savePeer.value.publickey
156
+ }
157
+ }
158
+ let updatePeerStatus = []
159
+ for (let savePeer of this.peerNetwork) {
160
+ if (savePeer.key === originalKey) {
161
+ let updatetoLive = savePeer
162
+ updatetoLive.value.live = true
163
+ updatetoLive.value.livePeerkey = publicKeylive
164
+ updatePeerStatus.push(updatetoLive)
165
+ this.emit('peer-live-network', originalKey)
166
+ } else {
167
+ updatePeerStatus.push(savePeer)
168
+ }
169
+ }
170
+ this.peerNetwork = updatePeerStatus
171
+ } else {
172
+ // keep track of connection
173
+ this.peerConnect[publicKeylive] = conn
174
+ this.dataFlowCheck(publicKeylive, 'server')
175
+ }
176
+
177
+ if (serverStatus === true) {
178
+ // set to live
179
+ let originalKey = ''
180
+ for (let savePeer of this.peerNetwork) {
181
+ if (savePeer.value.topic === topicServer.topic) {
182
+ originalKey = savePeer.value.publickey
183
+ }
184
+ }
185
+ // need original connection public key
186
+ let updatePeerStatus = []
187
+ for (let savePeer of this.peerNetwork) {
188
+ if (savePeer.key === originalKey) {
189
+ let updatetoLive = savePeer
190
+ updatetoLive.value.live = true
191
+ updatetoLive.value.livePeerkey = publicKeylive
192
+ updatePeerStatus.push(updatetoLive)
193
+ // update beebee
194
+ this.emit('peer-live-network', originalKey)
195
+ } else {
196
+ updatePeerStatus.push(savePeer)
197
+ }
198
+ }
199
+ this.peerNetwork = updatePeerStatus
200
+ }
201
+ } else {
202
+ // first time or from topic reset?
203
+ // need to check from client and server side
204
+ if (this.topicHolder[publicKeylive] === undefined) {
205
+ // is client or server role
206
+ let roleType = ''
207
+ if (roleTaken === false) {
208
+ roleType = 'server'
209
+ } else {
210
+ roleType = 'client'
211
+ }
212
+ let roleContext = {}
213
+ roleContext.publickey = publicKeylive
214
+ roleContext.roletaken = roleType
215
+ // first time cheeck for data long with it?
216
+ this.peerConnect[publicKeylive] = conn
217
+ this.dataFlowCheck(publicKeylive, 'first')
218
+ this.emit('connect-warm-first', roleContext)
219
+ } else {
220
+ this.peerConnect[publicKeylive] = conn
221
+ this.dataFlowCheck(publicKeylive, 'server')
222
+ }
223
+ }
224
+
68
225
  // process network message
69
226
  conn.on('data', data =>
70
227
  // assess data
@@ -74,16 +231,7 @@ class NetworkPeers extends EventEmitter {
74
231
  })
75
232
  }
76
233
 
77
- /**
78
- * set role in peer to peer relationship, invte or receive?
79
- * @method setRole
80
- *
81
- */
82
- setRole = function (pubKey) {
83
- let setRole = { send: 'prime' , invite: pubKey}
84
- this.peersRole[pubKey] = setRole
85
- }
86
- /**
234
+ /**
87
235
  *
88
236
  * @method assessData data and act
89
237
  *
@@ -103,9 +251,24 @@ class NetworkPeers extends EventEmitter {
103
251
  } else if (dataShareIn.type === 'public-library') {
104
252
  this.emit('publiclibrarynotification', dataShareIn)
105
253
  } else if (dataShareIn.type === 'peer') {
254
+ } else if (dataShareIn.type === 'peer-codename-inform') {
255
+ // all peer to match publicke to codename then update save and infom beebee
256
+ this.emit('peer-codename-match', dataShareIn)
106
257
  } else if (dataShareIn.type === 'topic-reconnect') {
107
258
  // peer has share a topic for future reconnect
108
- this.emit('peer-reconnect-topic', dataShareIn)
259
+ // check if publickey is topic key if yes, already active do nothing
260
+ let topicMatch = this.topicHolder[dataShareIn.topic]
261
+ if (topicMatch !== undefined) {
262
+ if (topicMatch.currentPubkey === dataShareIn.publickey) {
263
+ } else {
264
+ // server set topic in first connect flow
265
+ this.emit('peer-reconnect-topic', dataShareIn)
266
+ }
267
+ } else {
268
+ // client path
269
+ dataShareIn.settopic = false
270
+ this.emit('peer-reconnect-topic', dataShareIn)
271
+ }
109
272
  }
110
273
  } catch (e) {
111
274
  return console.error('ignore err')
@@ -113,6 +276,253 @@ class NetworkPeers extends EventEmitter {
113
276
  }
114
277
  }
115
278
 
279
+ /**
280
+ * what is the connectivity between two peers
281
+ * @method checkConnectivityStatus
282
+ */
283
+ checkConnectivityStatus = function (message, warmPeers, decodePath) {
284
+ let ptopStatus = {}
285
+ let savedPtoP = false
286
+ let livePtoP = false
287
+ let savedpeerInfo = {}
288
+ let peerMatch = false
289
+ // split invite to parts
290
+ if (decodePath === 'invite-gen') {
291
+ let parts = this.inviteDecoded(message.data)
292
+ message.data.publickey = parts[1]
293
+ message.data.codename = parts[2]
294
+ } else {
295
+ }
296
+ // check saved i.e. exsting known peer
297
+ for (let exPeer of this.peerNetwork) {
298
+ if (exPeer.key === message.data.publickey) {
299
+ savedPtoP = true
300
+ savedpeerInfo = exPeer
301
+ }
302
+ }
303
+ // check is peer is live
304
+ let peerLiveStatus = false
305
+ for (let sPeer of this.peerNetwork) {
306
+ if (sPeer.key === message.data.publickey) {
307
+ peerLiveStatus = sPeer.value.live
308
+ }
309
+ }
310
+ // is first time or ongoing?
311
+ if (peerLiveStatus === true) {
312
+ livePtoP = true
313
+ } else {
314
+ // first time connection
315
+ for (let wpeer of warmPeers) {
316
+ // connection open and live directly between two peers?
317
+ let openConn = this.peerConnect[message.data.publickey]
318
+ if (openConn !== undefined) {
319
+ livePtoP = true
320
+ }
321
+ // peer existing
322
+ if (wpeer.publickey = message.data.publickey) {
323
+ peerMatch = true
324
+ }
325
+ }
326
+ }
327
+ // set the status
328
+ ptopStatus.peer = savedpeerInfo
329
+ // settopic
330
+ ptopStatus.existing = savedPtoP
331
+ // live
332
+ ptopStatus.live = livePtoP
333
+ ptopStatus.role = this.peersRole[message.data.publickey]
334
+ ptopStatus.data = message.data
335
+ ptopStatus.action = message.action
336
+ ptopStatus.task = message.task
337
+ return ptopStatus
338
+ }
339
+
340
+ /**
341
+ * match codename to peer name
342
+ * @method matchCodename
343
+ *
344
+ */
345
+ matchCodename = function (data) {
346
+ let codeNameInvite = {}
347
+ let inviteIn = {}
348
+ for (let roleP of this.peersRole) {
349
+ if (roleP.invite.pubkey === data) {
350
+ inviteIn = roleP
351
+ }
352
+ }
353
+ codeNameInvite = { codename: inviteIn.invite.codename, invitePubkey: data , name: inviteIn.invite.name}
354
+ // match codename to role
355
+ let roleMatch = { publickey: data, role: inviteIn, codename: codeNameInvite.codename, name: codeNameInvite.name }
356
+ return roleMatch
357
+ }
358
+
359
+ /**
360
+ * match codename to peer codename
361
+ * @method matchPeersCodename
362
+ *
363
+ */
364
+ matchPeersCodename = function (data) {
365
+ let inviteIn = {}
366
+ for (let roleP of this.peersRole) {
367
+ if (roleP.invite.codename === data.data.inviteCode) {
368
+ inviteIn = roleP
369
+ }
370
+ }
371
+ // match codename to role
372
+ let roleMatch = { publickey: data, role: inviteIn, codename: inviteIn.invite.codename, name: inviteIn.invite.name }
373
+ return roleMatch
374
+ }
375
+
376
+ /**
377
+ * split invte code string
378
+ * @method inviteDecoded
379
+ *
380
+ */
381
+ inviteDecoded = function (invite) {
382
+ let splitInvite = invite.publickey.split('-')
383
+ if (splitInvite.length === 1) {
384
+ splitInvite = []
385
+ splitInvite.push('hop')
386
+ splitInvite.push(invite.publickey)
387
+ splitInvite.push(invite.codename)
388
+ } else {
389
+ // first time split fine
390
+ }
391
+ return splitInvite
392
+ }
393
+
394
+ /**
395
+ * check if any data actions along with connecting input?
396
+ * @method dataFlowCheck
397
+ *
398
+ */
399
+ dataFlowCheck = function (topicIn, role) {
400
+ // check if any data connection flow?
401
+ let peerTopeerState = {}
402
+ let matchPeer = {}
403
+ if (role === 'client') {
404
+ matchPeer = this.topicHolder[topicIn]
405
+ let peerActionData = this.peerHolder[matchPeer.peerKey]
406
+ if (peerActionData !== undefined) {
407
+ peerTopeerState = peerActionData.data
408
+ }
409
+ } else if (role === 'server') {
410
+ matchPeer.currentPubkey = topicIn
411
+ // loop over topics and see what info available??
412
+ let checkDiscoveryTopic = {}
413
+ for (let topicH of this.sendTopicHolder) {
414
+ const noisePublicKey = Buffer.from(topicH.topic, 'hex')
415
+ let discovery = this.swarm.status(noisePublicKey)
416
+ let discoverTopic = discovery.topic.toString('hex')
417
+ if (discoverTopic === topicH.topic) {
418
+ discovery.swarm.connections.forEach((value, key) => {
419
+ checkDiscoveryTopic.server = value.publicKey.toString('hex')
420
+ checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
421
+ checkDiscoveryTopic.topic = discoverTopic
422
+ })
423
+ }
424
+ }
425
+ // find out peer publickey first
426
+ let peerMTopic = {}
427
+ for (let peerh of this.peerNetwork) {
428
+ if (peerh.value.topic === checkDiscoveryTopic.topic) {
429
+ peerMTopic = peerh
430
+ }
431
+ }
432
+ // server from topic or first time direct?
433
+ if (Object.keys(peerMTopic).length === 0) {
434
+ // first time direct connection
435
+ peerMTopic.key = topicIn // note this is public key poor naming
436
+ } else {
437
+ }
438
+ // check for data
439
+ let peerActionData = this.peerHolder[peerMTopic.key]
440
+ if (peerActionData !== undefined) {
441
+ peerTopeerState = peerActionData.data
442
+ } else {
443
+ peerTopeerState = {}
444
+ }
445
+ } else if (role === 'first') {
446
+ let peerActionData = this.peerHolder[topicIn]
447
+ if (peerActionData === undefined) {
448
+ peerTopeerState = {}
449
+ } else {
450
+ if(peerActionData.data !== undefined) {
451
+ peerTopeerState = peerActionData.data
452
+ } else {
453
+ peerTopeerState = {}
454
+ }
455
+ }
456
+ }
457
+ // any data to write to network? NOT USED?
458
+ let checkDataShare = Object.keys(peerTopeerState).length
459
+ if (checkDataShare > 0) {
460
+ if (peerTopeerState.type === 'peer-share-invite') {
461
+ } else if (peerTopeerState.type === 'private-chart') {
462
+ this.writeTonetworkData(matchPeer.currentPubkey, peerTopeerState)
463
+ } else if (peerTopeerState.type === 'peer-share-topic') {
464
+ } else if (peerTopeerState.type === 'public-n1-experiment') {
465
+ } else if (peerTopeerState.type === 'cue-space') {
466
+ } else if (peerTopeerState.type === 'peer-write') {
467
+ }
468
+ }
469
+ }
470
+
471
+ /**
472
+ * check discovery status on network
473
+ * @method checkDisoveryStatus
474
+ *
475
+ */
476
+ checkDisoveryStatus = function (nodeRole) {
477
+ let topicList = []
478
+ if (nodeRole === 'server') {
479
+ topicList = this.sendTopicHolder
480
+ } else if (nodeRole === 'client') {
481
+ topicList = this.topicHolder
482
+ }
483
+ let checkDiscoveryTopic = {}
484
+ if (topicList.length > 0) {
485
+ for (let topicH of topicList) {
486
+ const noisePublicKey = Buffer.from(topicH.topic, 'hex')
487
+ let discovery = this.swarm.status(noisePublicKey)
488
+ let discoverTopic = discovery.topic.toString('hex')
489
+ if (discoverTopic === topicH.topic) {
490
+ discovery.swarm.connections.forEach((value, key) => {
491
+ checkDiscoveryTopic.server = value.publicKey.toString('hex')
492
+ checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
493
+ checkDiscoveryTopic.topic = discoverTopic
494
+ })
495
+ }
496
+ }
497
+ }
498
+ return checkDiscoveryTopic
499
+ }
500
+
501
+
502
+ /**
503
+ * where to route data share
504
+ * @method routeDataPath
505
+ *
506
+ */
507
+ routeDataPath = function (livePubkey, peerTopeerState) {
508
+ // any data to write to network?
509
+ let checkDataShare = Object.keys(peerTopeerState).length
510
+ if (checkDataShare > 0) {
511
+ if (peerTopeerState.type === 'peer-share-invite') {
512
+ } else if (peerTopeerState.type === 'private-chart') {
513
+ this.writeTonetworkData(livePubkey, peerTopeerState)
514
+ } else if (peerTopeerState.type === 'public-n1-experiment') {
515
+ this.writeToPublicLibrary(livePubkey, peerTopeerState)
516
+ } else if (peerTopeerState.type === 'private-cue-space') {
517
+ this.writeToCueSpace(livePubkey, peerTopeerState)
518
+ } else if (peerTopeerState.type === 'peer-write') {
519
+ this.Peers.writeTonetwork(peerTopeerState)
520
+ } else if (peerTopeerState.type === 'public-library') {
521
+ this.Peers.writeToPublicLibrary(data)
522
+ }
523
+ }
524
+ }
525
+
116
526
  /**
117
527
  * write message to network
118
528
  * @method writeTonetwork
@@ -129,15 +539,21 @@ class NetworkPeers extends EventEmitter {
129
539
  * @method writeTonetworkTopic
130
540
  *
131
541
  */
132
- writeTonetworkTopic = function (publickey) {
133
- let topicGeneration = 'kprel135811'
134
- // need to save the topic initiator of warm peer save relationship
135
- this.emit('peer-topic-save', { peerkey: publickey, topic: topicGeneration })
542
+ writeTonetworkTopic = function (publickey, codeName) {
543
+ const randomString = crypto.randomBytes(32).toString('hex')
544
+ // Convert the random string to a buffer
545
+ const buffer = Buffer.from(randomString, 'hex')
546
+ let topicGeneration = randomString
136
547
  // send to other peer topic to allow reconnection in future
137
548
  let topicShare = {}
138
549
  topicShare.type = 'topic-reconnect'
139
550
  topicShare.publickey = this.swarm.keyPair.publicKey.toString('hex')
551
+ topicShare.peerkey = this.swarm.keyPair.publicKey.toString('hex')
552
+ topicShare.prime = true
553
+ topicShare.topic = topicGeneration
554
+ topicShare.codename = codeName
140
555
  topicShare.data = topicGeneration
556
+ this.emit('topic-formed-save', topicShare)
141
557
  // inform peer that topic has been created
142
558
  this.peerConnect[publickey].write(JSON.stringify(topicShare))
143
559
  }
@@ -151,25 +567,17 @@ class NetworkPeers extends EventEmitter {
151
567
  this.peerConnect[publickey].write(JSON.stringify(dataShare))
152
568
  }
153
569
 
154
-
155
570
  /**
156
571
  * write message connect public library
157
572
  * @method writeToPublicLibrary
158
573
  *
159
574
  */
160
- writeToPublicLibrary = function (publickey) {
575
+ writeToPublicLibrary = function (publickey, data) {
161
576
  // check this peer has asked for chart data
162
- let connectTrue = publickey in this.peerConnect
163
- let libraryTrue = publickey in this.peerHolder
164
- if (connectTrue === true && libraryTrue === true) {
165
- let libraryData = this.peerHolder[publickey]
166
577
  let dataShare = {}
167
- dataShare.data = libraryData.data
578
+ dataShare.data = data
168
579
  dataShare.type = 'public-library'
169
580
  this.peerConnect[publickey].write(JSON.stringify(dataShare))
170
- } else {
171
- console.log('no board to write ie share with a peer')
172
- }
173
581
  }
174
582
 
175
583
  /**
@@ -177,19 +585,10 @@ class NetworkPeers extends EventEmitter {
177
585
  * @method writeToCueSpace
178
586
  *
179
587
  */
180
- writeToCueSpace = function (publickey) {
181
- // check this peer has asked for space data
182
- let connectTrue = publickey in this.peerConnect
183
- let spaceTrue = publickey in this.peerHolder
184
- if (connectTrue === true && spaceTrue === true) {
185
- let libraryData = this.peerHolder[publickey]
186
- this.peerConnect[publickey].write(JSON.stringify(libraryData))
187
- } else {
188
- console.log('no cuespace to write ie share with a peer')
189
- }
588
+ writeToCueSpace = function (publickey, data) {
589
+ this.peerConnect[publickey].write(JSON.stringify(data))
190
590
  }
191
591
 
192
-
193
592
  /**
194
593
  * join peer to peer direct private (server)
195
594
  * @method peerJoin
@@ -197,11 +596,11 @@ class NetworkPeers extends EventEmitter {
197
596
  */
198
597
  peerJoin = function (peerContext) {
199
598
  // set timeer to inform if not connection can be established
200
- this. checkTimerConnection(peerContext.publickey)
599
+ this.checkTimerConnection(peerContext.publickey)
201
600
  this.peerHolder[peerContext.publickey] = peerContext
202
601
  const noisePublicKey = Buffer.from(peerContext.publickey, 'hex') // must be 32 bytes
203
602
  if (noisePublicKey.length === 32) {
204
- const peerConnect = this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
603
+ this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
205
604
  }
206
605
  }
207
606
 
@@ -246,7 +645,6 @@ class NetworkPeers extends EventEmitter {
246
645
  return true
247
646
  }
248
647
 
249
-
250
648
  /**
251
649
  * join peer to peer private (client)
252
650
  * @method peerJoinClient
@@ -262,20 +660,34 @@ class NetworkPeers extends EventEmitter {
262
660
  *
263
661
  */
264
662
  topicConnect = async function (topic) {
265
- const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
266
- const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
267
- await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
268
- }
663
+ // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
664
+ const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
665
+ if (noisePublicKey.length === 32) {
666
+ let topicKeylive = noisePublicKey.toString('hex')
667
+ this.topicHolder[topic] = { role: 'server', livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic, key: topicKeylive, timestamp: '' }
668
+ this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic })
669
+ const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
670
+ await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
671
+ }
672
+ }
269
673
 
270
674
  /**
271
675
  * out message topics as a client
272
676
  * @method topicListen
273
677
  *
274
678
  */
275
- topicListen = async function (topic) {
276
- const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
277
- const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
278
- await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
679
+ topicListen = async function (topic, peerKey) {
680
+ // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
681
+ // let topicKeylive = noisePublicKey.toString('hex')
682
+ const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
683
+ if (noisePublicKey.length === 32) {
684
+ let topicKeylive = noisePublicKey.toString('hex')
685
+ this.topicHolder[topic] = { role: 'client', topic: topic, key: topicKeylive, peerKey: peerKey, timestamp: '' }
686
+ const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
687
+ await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
688
+ } else {
689
+ console.log('key lenght issue')
690
+ }
279
691
  }
280
692
 
281
693
  /**