holepunch-hop 0.3.4 → 0.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "holepunch-hop",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
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,132 @@ 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) {
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
213
+ this.warmPeers.push(message.data)
177
214
  this.Peers.peerAlreadyJoinSetData(message.data)
178
- // this.Peers.writeTonetwork(message.data.publickey)
179
- this.warmPeerPrepare(message.data.publickey, true)
215
+ this.Peers.peerJoin(message.data)
180
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
181
219
  this.warmPeers.push(message.data)
182
220
  this.Peers.peerAlreadyJoinSetData(message.data)
183
- this.Peers.peerJoin(message.data)
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
+ }
184
247
  }
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)
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
+ // first time or turning
253
+ if (Object.keys(reEstablishShort.peer).length === 0) {
254
+ this.Peers.peerAlreadyJoinSetData(message.data)
255
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
256
+ this.Peers.routeDataPath(message.data.publickey, peerActionData.data)
192
257
  } else {
193
- this.warmPeers.push(message.data)
194
- this.Peers.peerJoin(message.data)
195
- // now set data and write to public library info.
196
258
  this.Peers.peerAlreadyJoinSetData(message.data)
197
- this.Peers.writeToPublicLibrary(message.data.publickey)
259
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
260
+ this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
198
261
  }
199
- } else if (message.task = 'cue-space') {
200
- if (peerMatch === true) {
262
+ } else if (message.task === 'public-n1-experiment') {
263
+ if (Object.keys(reEstablishShort.peer).length === 0) {
201
264
  this.Peers.peerAlreadyJoinSetData(message.data)
202
- this.Peers.writeToCueSpace(message.data.publickey)
265
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
266
+ this.Peers.routeDataPath(message.data.publickey, peerActionData.data)
203
267
  } 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)
268
+ this.Peers.peerAlreadyJoinSetData(message.data)
269
+ let peerActionData = this.Peers.peerHolder[message.data.publickey]
270
+ this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
208
271
  }
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)
213
272
  }
214
273
  }
215
274
  }
216
275
 
217
-
218
276
  /**
219
277
  * prepare data to send to a warm peer
220
278
  * @method warmPeerPrepare
221
279
  */
222
280
  warmPeerPrepare = function (data, existing) {
281
+ // check if codename holder has any data to process
282
+ this.processCodenameMatching(data)
223
283
  // two checks, if topic send to other peer
224
284
  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')
285
+ // match publick key to warmpeers
286
+ let peerMatch = {}
287
+ for (let wpeer of this.warmPeers) {
288
+ if (wpeer.publickey === data) {
289
+ peerMatch = wpeer
290
+ }
291
+ }
292
+ // client of server Role?
293
+ // let role = this.Peers.getRole(data)
294
+ if (peerMatch.roletaken === 'client') {
295
+ let roleStatus = this.Peers.matchCodename(data)
296
+ let codenameInform = {}
297
+ codenameInform.type = 'peer-codename-inform'
298
+ codenameInform.action = 'set'
299
+ codenameInform.data = { inviteCode: roleStatus.codename , publickey: data, peerkey: this.swarm.keyPair.publicKey.toString('hex') }
300
+ this.Peers.writeTonetworkData(data, codenameInform)
301
+ // inform peer of codename
302
+ } else if (peerMatch.roletaken === 'server') {
303
+ // notify beebee peer to live
304
+ let codeNameInform = {}
305
+ codeNameInform.type = 'peer-codename-inform'
306
+ codeNameInform.action = 'set'
307
+ codeNameInform.data = { inviteCode: '' , publickey: data }
308
+ // in form beebee
309
+ this.emit('invite-live-peer', codeNameInform)
310
+ // send topic to allow peer to reconnect
311
+ this.Peers.writeTonetworkTopic(data, codeNameInform)
230
312
  }
231
313
  }
314
+
232
315
  // if data within coming then process that
233
316
  let peerDataExist = this.Peers.peerHolder[data]
234
317
  if (peerDataExist === undefined) {
235
318
  } else {
236
319
  // what type of data being shared?
237
320
  // check for data along with new peer?
321
+ this.Peers.routeDataPath(data, peerDataExist)
322
+ /*
238
323
  if (peerDataExist.data !== undefined) {
239
324
  if (peerDataExist.data.type === 'private-chart') {
240
325
  this.Peers.writeTonetworkData(data, peerDataExist.data)
@@ -248,8 +333,54 @@ class HolepunchWorker extends EventEmitter {
248
333
  // simpole text message
249
334
  this.Peers.writeTonetwork(data)
250
335
  }
336
+ } */
337
+ }
338
+ }
339
+
340
+ /**
341
+ * process codename matches after first save has happened.
342
+ * @method testCorestore
343
+ *
344
+ */
345
+ processCodenameMatching = async function (data) {
346
+ let updateCodeName = []
347
+ for (let cname of this.codenameUpdates) {
348
+ if (cname.data.peerkey === data) {
349
+ let matchCodename = this.Peers.matchPeersCodename(cname)
350
+ // need to matchs
351
+ let warmMatch = {}
352
+ for (let wpeer of this.warmPeers) {
353
+ if (wpeer.publickey === cname.data.peerkey) {
354
+ warmMatch = wpeer
355
+ }
356
+ }
357
+ matchCodename.peerkey = cname.data.peerkey
358
+ // update save for longterm and inform beebee
359
+ this.emit('peer-codename-update', matchCodename)
360
+ } else {
361
+ updateCodeName.push(cname)
362
+ }
363
+ }
364
+ this.codenameUpdates = updateCodeName
365
+ }
366
+
367
+ /**
368
+ * inform other peer of token for future connection
369
+ * @method topicSaveReturn
370
+ *
371
+ */
372
+ topicSaveReturn = function (data) {
373
+ let updateTopic = []
374
+ for (let ctopic of this.topicExhange) {
375
+ if (ctopic.key === data.publickey) {
376
+ this.emit('peer-reconnect-topic-notify', ctopic)
377
+ // update saved contract to add topic
378
+ this.emit('peer-topic-update', ctopic)
379
+ } else {
380
+ updateTopic.push(ctopic)
251
381
  }
252
382
  }
383
+ this.tpiocSaveReturn = updateTopic
253
384
  }
254
385
 
255
386
  /**
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,46 @@ 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
+ this.peerNetwork = peerNetwork
82
+ for (let sPeer of this.peerNetwork) {
83
+ // sPeer.value.key = publicKeylive
84
+ if (sPeer.value.settopic === true) {
85
+ // client role
86
+ this.topicConnect(sPeer.value.topic)
87
+ } else {
88
+ // server role
89
+ this.topicListen(sPeer.value.topic, sPeer.key)
90
+ }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * incoming establsihed information
96
+ * @method setRestablished
97
+ *
98
+ */
99
+ setRestablished = function (pubKey, established) {
100
+ this.peerEstContext[pubKey] = established
101
+ }
102
+
103
+
104
+
55
105
  /**
56
106
  * connection listen
57
107
  * @method listenNetwork
@@ -60,11 +110,119 @@ class NetworkPeers extends EventEmitter {
60
110
  listenNetwork = function () {
61
111
  this.swarm.on('connection', (conn, 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
+ // update livePeerkey to true??
180
+ let originalKey = ''
181
+ for (let savePeer of this.peerNetwork) {
182
+ if (savePeer.value.topic === topicServer.topic) {
183
+ originalKey = savePeer.value.publickey
184
+ }
185
+ }
186
+ // need original connection public key
187
+ let updatePeerStatus = []
188
+ for (let savePeer of this.peerNetwork) {
189
+ if (savePeer.key === originalKey) {
190
+ let updatetoLive = savePeer
191
+ updatetoLive.value.live = true
192
+ updatetoLive.value.livePeerkey = publicKeylive
193
+ updatePeerStatus.push(updatetoLive)
194
+ // update beebee
195
+ this.emit('peer-live-network', originalKey)
196
+ } else {
197
+ updatePeerStatus.push(savePeer)
198
+ }
199
+ }
200
+ this.peerNetwork = updatePeerStatus
201
+ }
202
+ } else {
203
+ // first time or from topic reset?
204
+ // need to check from client and server side
205
+ if (this.topicHolder[publicKeylive] === undefined) {
206
+ // is client or server role
207
+ let roleType = ''
208
+ if (roleTaken === false) {
209
+ roleType = 'server'
210
+ } else {
211
+ roleType = 'client'
212
+ }
213
+ let roleContext = {}
214
+ roleContext.publickey = publicKeylive
215
+ roleContext.roletaken = roleType
216
+ // first time cheeck for data long with it?
217
+ this.peerConnect[publicKeylive] = conn
218
+ this.dataFlowCheck(publicKeylive, 'first')
219
+ this.emit('connect-warm-first', roleContext)
220
+ } else {
221
+ this.peerConnect[publicKeylive] = conn
222
+ this.dataFlowCheck(publicKeylive, 'server')
223
+ }
224
+ }
225
+
68
226
  // process network message
69
227
  conn.on('data', data =>
70
228
  // assess data
@@ -74,16 +232,7 @@ class NetworkPeers extends EventEmitter {
74
232
  })
75
233
  }
76
234
 
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
- /**
235
+ /**
87
236
  *
88
237
  * @method assessData data and act
89
238
  *
@@ -103,9 +252,24 @@ class NetworkPeers extends EventEmitter {
103
252
  } else if (dataShareIn.type === 'public-library') {
104
253
  this.emit('publiclibrarynotification', dataShareIn)
105
254
  } else if (dataShareIn.type === 'peer') {
255
+ } else if (dataShareIn.type === 'peer-codename-inform') {
256
+ // all peer to match publicke to codename then update save and infom beebee
257
+ this.emit('peer-codename-match', dataShareIn)
106
258
  } else if (dataShareIn.type === 'topic-reconnect') {
107
259
  // peer has share a topic for future reconnect
108
- this.emit('peer-reconnect-topic', dataShareIn)
260
+ // check if publickey is topic key if yes, already active do nothing
261
+ let topicMatch = this.topicHolder[dataShareIn.topic]
262
+ if (topicMatch !== undefined) {
263
+ if (topicMatch.currentPubkey === dataShareIn.publickey) {
264
+ } else {
265
+ // server set topic in first connect flow
266
+ this.emit('peer-reconnect-topic', dataShareIn)
267
+ }
268
+ } else {
269
+ // client path
270
+ dataShareIn.settopic = false
271
+ this.emit('peer-reconnect-topic', dataShareIn)
272
+ }
109
273
  }
110
274
  } catch (e) {
111
275
  return console.error('ignore err')
@@ -113,6 +277,253 @@ class NetworkPeers extends EventEmitter {
113
277
  }
114
278
  }
115
279
 
280
+ /**
281
+ * what is the connectivity between two peers
282
+ * @method checkConnectivityStatus
283
+ */
284
+ checkConnectivityStatus = function (message, warmPeers, decodePath) {
285
+ let ptopStatus = {}
286
+ let savedPtoP = false
287
+ let livePtoP = false
288
+ let savedpeerInfo = {}
289
+ let peerMatch = false
290
+ // split invite to parts
291
+ if (decodePath === 'invite-gen') {
292
+ let parts = this.inviteDecoded(message.data)
293
+ message.data.publickey = parts[1]
294
+ message.data.codename = parts[2]
295
+ } else {
296
+ }
297
+ // check saved i.e. exsting known peer
298
+ for (let exPeer of this.peerNetwork) {
299
+ if (exPeer.key === message.data.publickey) {
300
+ savedPtoP = true
301
+ savedpeerInfo = exPeer
302
+ }
303
+ }
304
+ // check is peer is live
305
+ let peerLiveStatus = false
306
+ for (let sPeer of this.peerNetwork) {
307
+ if (sPeer.key === message.data.publickey) {
308
+ peerLiveStatus = sPeer.value.live
309
+ }
310
+ }
311
+ // is first time or ongoing?
312
+ if (peerLiveStatus === true) {
313
+ livePtoP = true
314
+ } else {
315
+ // first time connection
316
+ for (let wpeer of warmPeers) {
317
+ // connection open and live directly between two peers?
318
+ let openConn = this.peerConnect[message.data.publickey]
319
+ if (openConn !== undefined) {
320
+ livePtoP = true
321
+ }
322
+ // peer existing
323
+ if (wpeer.publickey = message.data.publickey) {
324
+ peerMatch = true
325
+ }
326
+ }
327
+ }
328
+ // set the status
329
+ ptopStatus.peer = savedpeerInfo
330
+ // settopic
331
+ ptopStatus.existing = savedPtoP
332
+ // live
333
+ ptopStatus.live = livePtoP
334
+ ptopStatus.role = this.peersRole[message.data.publickey]
335
+ ptopStatus.data = message.data
336
+ ptopStatus.action = message.action
337
+ ptopStatus.task = message.task
338
+ return ptopStatus
339
+ }
340
+
341
+ /**
342
+ * match codename to peer name
343
+ * @method matchCodename
344
+ *
345
+ */
346
+ matchCodename = function (data) {
347
+ let codeNameInvite = {}
348
+ let inviteIn = {}
349
+ for (let roleP of this.peersRole) {
350
+ if (roleP.invite.pubkey === data) {
351
+ inviteIn = roleP
352
+ }
353
+ }
354
+ codeNameInvite = { codename: inviteIn.invite.codename, invitePubkey: data , name: inviteIn.invite.name}
355
+ // match codename to role
356
+ let roleMatch = { publickey: data, role: inviteIn, codename: codeNameInvite.codename, name: codeNameInvite.name }
357
+ return roleMatch
358
+ }
359
+
360
+ /**
361
+ * match codename to peer codename
362
+ * @method matchPeersCodename
363
+ *
364
+ */
365
+ matchPeersCodename = function (data) {
366
+ let inviteIn = {}
367
+ for (let roleP of this.peersRole) {
368
+ if (roleP.invite.codename === data.data.inviteCode) {
369
+ inviteIn = roleP
370
+ }
371
+ }
372
+ // match codename to role
373
+ let roleMatch = { publickey: data, role: inviteIn, codename: inviteIn.invite.codename, name: inviteIn.invite.name }
374
+ return roleMatch
375
+ }
376
+
377
+ /**
378
+ * split invte code string
379
+ * @method inviteDecoded
380
+ *
381
+ */
382
+ inviteDecoded = function (invite) {
383
+ let splitInvite = invite.publickey.split('-')
384
+ if (splitInvite.length === 1) {
385
+ splitInvite = []
386
+ splitInvite.push('hop')
387
+ splitInvite.push(invite.publickey)
388
+ splitInvite.push(invite.codename)
389
+ } else {
390
+ // first time split fine
391
+ }
392
+ return splitInvite
393
+ }
394
+
395
+ /**
396
+ * check if any data actions along with connecting input?
397
+ * @method dataFlowCheck
398
+ *
399
+ */
400
+ dataFlowCheck = function (topicIn, role) {
401
+ // check if any data connection flow?
402
+ let peerTopeerState = {}
403
+ let matchPeer = {}
404
+ if (role === 'client') {
405
+ matchPeer = this.topicHolder[topicIn]
406
+ let peerActionData = this.peerHolder[matchPeer.peerKey]
407
+ if (peerActionData !== undefined) {
408
+ peerTopeerState = peerActionData.data
409
+ }
410
+ } else if (role === 'server') {
411
+ matchPeer.currentPubkey = topicIn
412
+ // loop over topics and see what info available??
413
+ let checkDiscoveryTopic = {}
414
+ for (let topicH of this.sendTopicHolder) {
415
+ const noisePublicKey = Buffer.from(topicH.topic, 'hex')
416
+ let discovery = this.swarm.status(noisePublicKey)
417
+ let discoverTopic = discovery.topic.toString('hex')
418
+ if (discoverTopic === topicH.topic) {
419
+ discovery.swarm.connections.forEach((value, key) => {
420
+ checkDiscoveryTopic.server = value.publicKey.toString('hex')
421
+ checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
422
+ checkDiscoveryTopic.topic = discoverTopic
423
+ })
424
+ }
425
+ }
426
+ // find out peer publickey first
427
+ let peerMTopic = {}
428
+ for (let peerh of this.peerNetwork) {
429
+ if (peerh.value.topic === checkDiscoveryTopic.topic) {
430
+ peerMTopic = peerh
431
+ }
432
+ }
433
+ // server from topic or first time direct?
434
+ if (Object.keys(peerMTopic).length === 0) {
435
+ // first time direct connection
436
+ peerMTopic.key = topicIn // note this is public key poor naming
437
+ } else {
438
+ }
439
+ // check for data
440
+ let peerActionData = this.peerHolder[peerMTopic.key]
441
+ if (peerActionData !== undefined) {
442
+ peerTopeerState = peerActionData.data
443
+ } else {
444
+ peerTopeerState = {}
445
+ }
446
+ } else if (role === 'first') {
447
+ let peerActionData = this.peerHolder[topicIn]
448
+ if (peerActionData === undefined) {
449
+ peerTopeerState = {}
450
+ } else {
451
+ if(peerActionData.data !== undefined) {
452
+ peerTopeerState = peerActionData.data
453
+ } else {
454
+ peerTopeerState = {}
455
+ }
456
+ }
457
+ }
458
+ // any data to write to network? NOT USED?
459
+ let checkDataShare = Object.keys(peerTopeerState).length
460
+ if (checkDataShare > 0) {
461
+ if (peerTopeerState.type === 'peer-share-invite') {
462
+ } else if (peerTopeerState.type === 'private-chart') {
463
+ this.writeTonetworkData(matchPeer.currentPubkey, peerTopeerState)
464
+ } else if (peerTopeerState.type === 'peer-share-topic') {
465
+ } else if (peerTopeerState.type === 'public-n1-experiment') {
466
+ } else if (peerTopeerState.type === 'cue-space') {
467
+ } else if (peerTopeerState.type === 'peer-write') {
468
+ }
469
+ }
470
+ }
471
+
472
+ /**
473
+ * check discovery status on network
474
+ * @method checkDisoveryStatus
475
+ *
476
+ */
477
+ checkDisoveryStatus = function (nodeRole) {
478
+ let topicList = []
479
+ if (nodeRole === 'server') {
480
+ topicList = this.sendTopicHolder
481
+ } else if (nodeRole === 'client') {
482
+ topicList = this.topicHolder
483
+ }
484
+ let checkDiscoveryTopic = {}
485
+ if (topicList.length > 0) {
486
+ for (let topicH of topicList) {
487
+ const noisePublicKey = Buffer.from(topicH.topic, 'hex')
488
+ let discovery = this.swarm.status(noisePublicKey)
489
+ let discoverTopic = discovery.topic.toString('hex')
490
+ if (discoverTopic === topicH.topic) {
491
+ discovery.swarm.connections.forEach((value, key) => {
492
+ checkDiscoveryTopic.server = value.publicKey.toString('hex')
493
+ checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
494
+ checkDiscoveryTopic.topic = discoverTopic
495
+ })
496
+ }
497
+ }
498
+ }
499
+ return checkDiscoveryTopic
500
+ }
501
+
502
+
503
+ /**
504
+ * where to route data share
505
+ * @method routeDataPath
506
+ *
507
+ */
508
+ routeDataPath = function (livePubkey, peerTopeerState) {
509
+ // any data to write to network?
510
+ let checkDataShare = Object.keys(peerTopeerState).length
511
+ if (checkDataShare > 0) {
512
+ if (peerTopeerState.type === 'peer-share-invite') {
513
+ } else if (peerTopeerState.type === 'private-chart') {
514
+ this.writeTonetworkData(livePubkey, peerTopeerState)
515
+ } else if (peerTopeerState.type === 'public-n1-experiment') {
516
+ this.writeToPublicLibrary(livePubkey, peerTopeerState)
517
+ } else if (peerTopeerState.type === 'private-cue-space') {
518
+ this.writeToCueSpace(livePubkey, peerTopeerState)
519
+ } else if (peerTopeerState.type === 'peer-write') {
520
+ this.Peers.writeTonetwork(peerTopeerState)
521
+ } else if (peerTopeerState.type === 'public-library') {
522
+ this.Peers.writeToPublicLibrary(data)
523
+ }
524
+ }
525
+ }
526
+
116
527
  /**
117
528
  * write message to network
118
529
  * @method writeTonetwork
@@ -129,15 +540,21 @@ class NetworkPeers extends EventEmitter {
129
540
  * @method writeTonetworkTopic
130
541
  *
131
542
  */
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 })
543
+ writeTonetworkTopic = function (publickey, codeName) {
544
+ const randomString = crypto.randomBytes(32).toString('hex')
545
+ // Convert the random string to a buffer
546
+ const buffer = Buffer.from(randomString, 'hex')
547
+ let topicGeneration = randomString
136
548
  // send to other peer topic to allow reconnection in future
137
549
  let topicShare = {}
138
550
  topicShare.type = 'topic-reconnect'
139
551
  topicShare.publickey = this.swarm.keyPair.publicKey.toString('hex')
552
+ topicShare.peerkey = this.swarm.keyPair.publicKey.toString('hex')
553
+ topicShare.prime = true
554
+ topicShare.topic = topicGeneration
555
+ topicShare.codename = codeName
140
556
  topicShare.data = topicGeneration
557
+ this.emit('topic-formed-save', topicShare)
141
558
  // inform peer that topic has been created
142
559
  this.peerConnect[publickey].write(JSON.stringify(topicShare))
143
560
  }
@@ -151,25 +568,17 @@ class NetworkPeers extends EventEmitter {
151
568
  this.peerConnect[publickey].write(JSON.stringify(dataShare))
152
569
  }
153
570
 
154
-
155
571
  /**
156
572
  * write message connect public library
157
573
  * @method writeToPublicLibrary
158
574
  *
159
575
  */
160
- writeToPublicLibrary = function (publickey) {
576
+ writeToPublicLibrary = function (publickey, data) {
161
577
  // 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
578
  let dataShare = {}
167
- dataShare.data = libraryData.data
579
+ dataShare.data = data
168
580
  dataShare.type = 'public-library'
169
581
  this.peerConnect[publickey].write(JSON.stringify(dataShare))
170
- } else {
171
- console.log('no board to write ie share with a peer')
172
- }
173
582
  }
174
583
 
175
584
  /**
@@ -177,19 +586,10 @@ class NetworkPeers extends EventEmitter {
177
586
  * @method writeToCueSpace
178
587
  *
179
588
  */
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
- }
589
+ writeToCueSpace = function (publickey, data) {
590
+ this.peerConnect[publickey].write(JSON.stringify(data))
190
591
  }
191
592
 
192
-
193
593
  /**
194
594
  * join peer to peer direct private (server)
195
595
  * @method peerJoin
@@ -197,11 +597,11 @@ class NetworkPeers extends EventEmitter {
197
597
  */
198
598
  peerJoin = function (peerContext) {
199
599
  // set timeer to inform if not connection can be established
200
- this. checkTimerConnection(peerContext.publickey)
600
+ this.checkTimerConnection(peerContext.publickey)
201
601
  this.peerHolder[peerContext.publickey] = peerContext
202
602
  const noisePublicKey = Buffer.from(peerContext.publickey, 'hex') // must be 32 bytes
203
603
  if (noisePublicKey.length === 32) {
204
- const peerConnect = this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
604
+ this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
205
605
  }
206
606
  }
207
607
 
@@ -246,7 +646,6 @@ class NetworkPeers extends EventEmitter {
246
646
  return true
247
647
  }
248
648
 
249
-
250
649
  /**
251
650
  * join peer to peer private (client)
252
651
  * @method peerJoinClient
@@ -262,20 +661,34 @@ class NetworkPeers extends EventEmitter {
262
661
  *
263
662
  */
264
663
  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
- }
664
+ // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
665
+ const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
666
+ if (noisePublicKey.length === 32) {
667
+ let topicKeylive = noisePublicKey.toString('hex')
668
+ this.topicHolder[topic] = { role: 'server', livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic, key: topicKeylive, timestamp: '' }
669
+ this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic })
670
+ const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
671
+ await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
672
+ }
673
+ }
269
674
 
270
675
  /**
271
676
  * out message topics as a client
272
677
  * @method topicListen
273
678
  *
274
679
  */
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
680
+ topicListen = async function (topic, peerKey) {
681
+ // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
682
+ // let topicKeylive = noisePublicKey.toString('hex')
683
+ const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
684
+ if (noisePublicKey.length === 32) {
685
+ let topicKeylive = noisePublicKey.toString('hex')
686
+ this.topicHolder[topic] = { role: 'client', topic: topic, key: topicKeylive, peerKey: peerKey, timestamp: '' }
687
+ const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
688
+ await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
689
+ } else {
690
+ console.log('key lenght issue')
691
+ }
279
692
  }
280
693
 
281
694
  /**