holepunch-hop 0.4.5 → 0.4.8

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.
@@ -1,4 +1,5 @@
1
1
  'use strict'
2
+
2
3
  /**
3
4
  * Manage Peers connections
4
5
  *
@@ -12,7 +13,6 @@ import EventEmitter from 'events'
12
13
  import crypto from 'crypto'
13
14
 
14
15
  class NetworkPeers extends EventEmitter {
15
-
16
16
  constructor(store, swarm) {
17
17
  super()
18
18
  this.hello = 'hyperpeers'
@@ -28,6 +28,7 @@ class NetworkPeers extends EventEmitter {
28
28
  this.sendTopicHolder = []
29
29
  this.peersRole = []
30
30
  this.discoveryList = []
31
+ this.peerSwitchLiveID = []
31
32
  }
32
33
 
33
34
  /**
@@ -92,6 +93,17 @@ class NetworkPeers extends EventEmitter {
92
93
  }
93
94
  }
94
95
 
96
+ /**
97
+ * when new update or refresh peer network to get latest peers
98
+ * @method latestPeerNetwork
99
+ *
100
+ */
101
+ latestPeerNetwork = function (peerNetwork) {
102
+ // Use concat to merge arrays while preserving existing elements
103
+ this.peerNetwork = [...this.peerNetwork, ...peerNetwork]
104
+ }
105
+
106
+
95
107
  /**
96
108
  * incoming establsihed information
97
109
  * @method setRestablished
@@ -101,155 +113,180 @@ class NetworkPeers extends EventEmitter {
101
113
  this.peerEstContext[pubKey] = established
102
114
  }
103
115
 
104
- /**
105
- * connection listen
106
- * @method listenNetwork
107
- *
108
- */
109
- listenNetwork = function () {
116
+ /*
117
+ * Listen for network connections
118
+ *
119
+ **/
120
+ listenNetwork = function () {
110
121
  this.swarm.on('connection', (conn, info) => {
111
- // save peer connection instance for ongoing communication
112
- // listen for replication NEED UPTATED LOGIC
113
- this.store.replicate(conn)
114
- // assess if token is present to match to exsing peer account ID?
115
- let publicKeylive = info.publicKey.toString('hex')
116
- let topicKeylive = info.topics
117
- let roleTaken = info.client
118
- // if now topics info then server, pass by
119
- let discoveryTopicInfo = {}
120
- if (topicKeylive.length === 0) {
121
- // check if joined now?
122
- discoveryTopicInfo = this.checkDisoveryStatus('server')
122
+ const publicKey = info.publicKey.toString('hex')
123
+ this.peerConnect[publicKey] = conn
124
+ const connectionInfo = this.prepareConnectionInfo(info, publicKey)
125
+ // Determine which path to take
126
+ if (connectionInfo.discoveryTopicInfo.firstTime === false) {
127
+ this.handleReconnection(conn, info, connectionInfo)
128
+ } else if (connectionInfo.discoveryTopicInfo.firstTime === true) {
129
+ this.handleFirstTimeConnection(conn, info, connectionInfo)
123
130
  } else {
124
- discoveryTopicInfo = {topic: ''}
125
- }
126
- let topicServer = this.topicHolder[discoveryTopicInfo.topic]
127
- let serverStatus = false
128
- if (topicServer !== undefined) {
129
- if (Object.keys(topicServer).length > 0) {
130
- serverStatus = true
131
- }
132
- }
133
- // check if toppic via matching or being client receiving
134
- if (topicKeylive.length > 0 || serverStatus === true) {
135
- // keep track of connection
136
- this.peerConnect[publicKeylive] = conn
137
- if (topicKeylive.length > 0) {
138
- let topicIn = topicKeylive[0].toString('hex')
139
- let topicMatch = this.topicHolder[topicIn]
140
- if (Object.keys(topicMatch).length > 0) {
141
- // looks at what needs data needs cshared
142
- // match new public key to saved publickey
143
- topicMatch.currentPubkey = publicKeylive
144
- this.topicHolder[topicIn] = topicMatch
145
- this.dataFlowCheck(topicIn, 'client')
146
- } else {
147
- this.peerConnect[publicKeylive] = conn
148
-
149
- }
150
- // update to live client
151
- let originalKey = ''
152
- for (let savePeer of this.peerNetwork) {
153
- if (savePeer.value.topic === topicIn) {
154
- originalKey = savePeer.value.publickey
155
- }
156
- }
157
- let updatePeerStatus = []
158
- for (let savePeer of this.peerNetwork) {
159
- if (savePeer.key === originalKey) {
160
- let updatetoLive = savePeer
161
- updatetoLive.value.live = true
162
- updatetoLive.value.livePeerkey = publicKeylive
163
- updatePeerStatus.push(updatetoLive)
164
- this.emit('peer-live-network', originalKey)
165
- } else {
166
- updatePeerStatus.push(savePeer)
167
- }
168
- }
169
- this.peerNetwork = updatePeerStatus
170
- } else {
171
- // keep track of connection
172
- this.peerConnect[publicKeylive] = conn
173
- this.dataFlowCheck(publicKeylive, 'server')
174
- }
175
-
176
- if (serverStatus === true) {
177
- // set to live
178
- // update livePeerkey to true??
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
- }
131
+ // need to differenciate between first time and reconnect
132
+ this.peerSwitchLiveID.push({ publicKey: publicKey, discoveryTopicInfo: connectionInfo })
223
133
  }
134
+
135
+ // Common setup
136
+ this.store.replicate(conn);
224
137
 
225
138
  // process network message
226
139
  conn.on('data', data =>
227
140
  // assess data
228
- this.assessData(publicKeylive, data)
141
+ this.assessData(publicKey, data)
229
142
  )
230
143
 
231
144
  conn.on('error', data => {
232
145
  let connectLivekeys = Object.keys(this.peerConnect)
233
- for (let peer of this.peerNetwork) {
234
- for (let pconn of connectLivekeys) {
235
- if (peer.value.livePeerkey === pconn) {
236
- // check if connect is close?
237
- let keysNoise = Object.keys(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
238
- // console.log(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
239
- let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
240
- if (closeStatus === true) {
241
- // remove peer & inform beebee
242
- this.emit('peer-disconnect', { publickey: peer.key })
146
+ if (connectLivekeys.length > 0) {
147
+ for (let peer of this.peerNetwork) {
148
+ for (let pconn of connectLivekeys) {
149
+ if (peer.value.livePeerkey.length > 0) {
150
+ if (peer.value.livePeerkey === pconn) {
151
+ // check if connect is close?
152
+ // let keysNoise = Object.keys(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
153
+ // console.log(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
154
+ let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
155
+ if (closeStatus === true) {
156
+ // remove peer & inform beebee
157
+ this.emit('peer-disconnect', { publickey: peer.key })
158
+ }
159
+ }
160
+ } else {
161
+ // assume first time and use key
162
+ if (peer.key === pconn) {
163
+ // check if connect is close?
164
+ let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
165
+ if (closeStatus === true) {
166
+ // remove peer & inform beebee
167
+ this.emit('peer-disconnect', { publickey: peer.key })
168
+ }
169
+ }
243
170
  }
244
171
  }
245
172
  }
246
173
  }
247
174
  })
248
-
249
- // conn.end()
250
175
  })
251
176
  }
252
177
 
178
+ // Connection preparation methods
179
+ prepareConnectionInfo = function(info, publicKey) {
180
+ const topicKeylive = info.topics
181
+ const roleTaken = info.client
182
+ let discoveryTopicInfo = {}
183
+ if (topicKeylive.length === 0) {
184
+ if (roleTaken === false) {
185
+ discoveryTopicInfo = this.checkDisoveryStatus('server', publicKey, topicKeylive)
186
+ } else if (roleTaken === true) {
187
+ discoveryTopicInfo = this.checkDisoveryStatus('client', publicKey, topicKeylive)
188
+ }
189
+ if (discoveryTopicInfo === undefined) {
190
+ discoveryTopicInfo = { firstTime: false, topic: ''}
191
+ }
192
+ } else {
193
+ discoveryTopicInfo = { firstTime: false, topic: ''}
194
+ }
195
+
196
+ return {
197
+ topicKeylive,
198
+ roleTaken,
199
+ discoveryTopicInfo
200
+ };
201
+ }
202
+
203
+ handleFirstTimeConnection = function(conn, info, connectionInfo) {
204
+ const { publicKey } = info
205
+ const { topicKeylive } = connectionInfo
206
+ let publicKeylive = publicKey.toString('hex')
207
+ // First establish the connection
208
+ let roleTaken = info.client
209
+ // check status of topic first time no topic
210
+ if (this.topicHolder[publicKeylive] === undefined) {
211
+ // is client or server role
212
+ let roleType = ''
213
+ if (roleTaken === false) {
214
+ roleType = 'server'
215
+ } else {
216
+ roleType = 'client'
217
+ }
218
+ let roleContext = {}
219
+ roleContext.publickey = publicKeylive
220
+ roleContext.roletaken = roleType
221
+ // first time cheeck for data long with it?
222
+ this.dataFlowCheck(publicKeylive, 'first')
223
+ this.emit('connect-warm-first', roleContext)
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Reconnection handler
229
+ *
230
+ */
231
+ handleReconnection = function(conn, info, connectionInfo) {
232
+ const { publicKey } = info;
233
+ const { topicKeylive, discoveryTopicInfo, serverStatus } = connectionInfo
234
+ // Reconnection logic
235
+ let topic = topicKeylive[0].toString('hex')
236
+ // match topic to topic holder list to get original pub key ID
237
+ let originalKey = ''
238
+ for (let savePeer of this.peerNetwork) {
239
+ if (savePeer.value.topic === topic) {
240
+ originalKey = savePeer.value.publickey
241
+ break;
242
+ }
243
+ }
244
+ if (topic.length > 0) {
245
+ // Handle topic-based reconnection
246
+ const topicMatch = this.topicHolder[topic]
247
+ if (topicMatch && Object.keys(topicMatch).length > 0) {
248
+ topicMatch.currentPubkey = publicKey.toString('hex')
249
+ this.topicHolder[topic] = topicMatch
250
+ this.dataFlowCheck(topic, 'client')
251
+ this.updatePeerStatus(topic, publicKey.toString('hex'))
252
+ // inform other peer of peerkey id
253
+ this.writeTopicReconnect(originalKey, topicMatch)
254
+ }
255
+ // Handle non-topic reconnection
256
+ this.dataFlowCheck(publicKey.toString('hex'), 'server')
257
+ }
258
+ }
259
+
260
+ /*
261
+ * Update peer status
262
+ *
263
+ **/
264
+ updatePeerStatus = function(topic, publicKey) {
265
+ let originalKey = '';
266
+ for (let savePeer of this.peerNetwork) {
267
+ if (savePeer.value.topic === topic) {
268
+ originalKey = savePeer.value.publickey;
269
+ break;
270
+ }
271
+ }
272
+
273
+ const updatePeerStatus = this.peerNetwork.map(savePeer => {
274
+ if (savePeer.key === originalKey) {
275
+ return {
276
+ ...savePeer,
277
+ value: {
278
+ ...savePeer.value,
279
+ live: true,
280
+ livePeerkey: publicKey
281
+ }
282
+ };
283
+ }
284
+ return savePeer;
285
+ });
286
+ this.peerNetwork = updatePeerStatus;
287
+ this.emit('peer-live-network', originalKey)
288
+ }
289
+
253
290
  /**
254
291
  *
255
292
  * @method updateListen
@@ -301,6 +338,9 @@ class NetworkPeers extends EventEmitter {
301
338
  dataShareIn.settopic = false
302
339
  this.emit('peer-reconnect-topic', dataShareIn)
303
340
  }
341
+ } else if (dataShareIn.type === 'topic-reconnect-id') {
342
+ // update status of livepeer public key
343
+ this.emit('peer-reconnect-topic-id', peer, dataShareIn.data)
304
344
  }
305
345
  } catch (e) {
306
346
  return console.error('ignore err')
@@ -369,6 +409,27 @@ class NetworkPeers extends EventEmitter {
369
409
  return ptopStatus
370
410
  }
371
411
 
412
+ /**
413
+ * match to first time invite code
414
+ * @method matchInviteFirst
415
+ *
416
+ */
417
+ matchInviteFirst = function (data) {
418
+ let roleMatch = false
419
+ for(let peerRole of this.peersRole ) {
420
+ if (peerRole.invite.codename === data.data.inviteCode) {
421
+ roleMatch = true
422
+ break
423
+ }
424
+ }
425
+ if (roleMatch === true) {
426
+ let roleContext = {}
427
+ roleContext.publickey = data.data.peerkey
428
+ roleContext.roletaken = 'server'
429
+ this.emit('connect-warm-first', roleContext)
430
+ }
431
+ }
432
+
372
433
  /**
373
434
  * match codename to peer name
374
435
  * @method matchCodename
@@ -434,11 +495,25 @@ class NetworkPeers extends EventEmitter {
434
495
  discoverySettings = savePeer.discovery
435
496
  }
436
497
  }
437
- // now refresh
438
- discoverySettings.refresh()
439
498
  return true
440
499
  }
441
500
 
501
+ /**
502
+ * match topic live public key
503
+ * @method topicPublicKeyMatch
504
+ *
505
+ */
506
+ topicPublicKeyMatch = function (topic) {
507
+ // first match live pubkey to topic and then use topic to get original
508
+ let topicSettings = {}
509
+ for (let livePeer of this.peerSwitchLiveID) {
510
+ if (livePeer.connectionInfo.topic === topic) {
511
+ topicSettings = livePeer.publicKey
512
+ }
513
+ }
514
+ return topicSettings
515
+ }
516
+
442
517
  /**
443
518
  * match current publickey to peer id i.e. estbalshed on first connect and maps to 'peername' in UX
444
519
  * @method peerMatchbase
@@ -460,6 +535,22 @@ class NetworkPeers extends EventEmitter {
460
535
  }
461
536
  }
462
537
 
538
+ /**
539
+ * match peer to topic live
540
+ * @method matchPeerTopic
541
+ *
542
+ */
543
+ matchPeerTopic = function (topic) {
544
+ // first match live pubkey to topic and then use topic to get original
545
+ let peerSettings = {}
546
+ for (let savePeer of this.peerNetwork) {
547
+ if (savePeer.value.topic === topic) {
548
+ peerSettings = savePeer
549
+ }
550
+ }
551
+ return peerSettings
552
+ }
553
+
463
554
  /**
464
555
  * split invte code string
465
556
  * @method inviteDecoded
@@ -560,35 +651,271 @@ class NetworkPeers extends EventEmitter {
560
651
  }
561
652
  }
562
653
 
654
+ /* dataFlowCheck = function (topicIn, role) {
655
+ // check if any data connection flow?
656
+ let peerTopeerState = {}
657
+ let matchPeer = {}
658
+ if (role === 'client') {
659
+ matchPeer = this.topicHolder[topicIn]
660
+ let peerActionData = this.peerHolder[matchPeer.peerKey]
661
+ if (peerActionData !== undefined) {
662
+ peerTopeerState = peerActionData.data
663
+ }
664
+ } else if (role === 'server') {
665
+ matchPeer.currentPubkey = topicIn
666
+ // loop over topics and see what info available??
667
+ let checkDiscoveryTopic = {}
668
+ for (let topicH of this.sendTopicHolder) {
669
+ const noisePublicKey = Buffer.from(topicH.topic, 'hex')
670
+ let discovery = this.swarm.status(noisePublicKey)
671
+ let discoverTopic = discovery.topic.toString('hex')
672
+ if (discoverTopic === topicH.topic) {
673
+ discovery.swarm.connections.forEach((value, key) => {
674
+ checkDiscoveryTopic.server = value.publicKey.toString('hex')
675
+ checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
676
+ checkDiscoveryTopic.topic = discoverTopic
677
+ })
678
+ }
679
+ }
680
+ // find out peer publickey first
681
+ let peerMTopic = {}
682
+ for (let peerh of this.peerNetwork) {
683
+ if (peerh.value.topic === checkDiscoveryTopic.topic) {
684
+ peerMTopic = peerh
685
+ }
686
+ }
687
+ // server from topic or first time direct?
688
+ if (Object.keys(peerMTopic).length === 0) {
689
+ // first time direct connection
690
+ peerMTopic.key = topicIn // note this is public key poor naming
691
+ } else {
692
+ }
693
+ // check for data
694
+ let peerActionData = this.peerHolder[peerMTopic.key]
695
+ if (peerActionData !== undefined) {
696
+ peerTopeerState = peerActionData.data
697
+ } else {
698
+ peerTopeerState = {}
699
+ }
700
+ } else if (role === 'first') {
701
+ let peerActionData = this.peerHolder[topicIn]
702
+ if (peerActionData === undefined) {
703
+ peerTopeerState = {}
704
+ } else {
705
+ if(peerActionData.data !== undefined) {
706
+ peerTopeerState = peerActionData.data
707
+ } else {
708
+ peerTopeerState = {}
709
+ }
710
+ }
711
+ }
712
+ // any data to write to network? NOT USED?
713
+ let checkDataShare = Object.keys(peerTopeerState).length
714
+ if (checkDataShare > 0) {
715
+ if (peerTopeerState.type === 'peer-share-invite') {
716
+ } else if (peerTopeerState.type === 'private-chart') {
717
+ this.writeTonetworkData(matchPeer.currentPubkey, peerTopeerState)
718
+ } else if (peerTopeerState.type === 'peer-share-topic') {
719
+ } else if (peerTopeerState.type === 'public-n1-experiment') {
720
+ } else if (peerTopeerState.type === 'cue-space') {
721
+ } else if (peerTopeerState.type === 'peer-write') {
722
+ }
723
+ }
724
+ }*/
725
+
563
726
  /**
564
727
  * check discovery status on network
565
728
  * @method checkDisoveryStatus
566
729
  *
567
730
  */
568
- checkDisoveryStatus = function (nodeRole) {
731
+ checkDisoveryStatus = function (nodeRole, publicKey, topic) {
569
732
  let topicList = []
570
733
  if (nodeRole === 'server') {
571
734
  topicList = this.sendTopicHolder
572
735
  } else if (nodeRole === 'client') {
573
736
  topicList = this.topicHolder
574
737
  }
738
+ // previous info at hand
739
+ let firstTime = false
740
+ let emptyHolder = false
741
+
742
+ // we get public key -- discovery process to match to topic, has that topic been set before, then match live key to peerKey ie. the pubkey used an id from first time connection??
743
+ // scenerios
744
+ // 1. first time connection both client and server -- no save peers
745
+ // 2. first time connection client -- with saved peers
746
+ // 3. first time connection server -- with saved peers
747
+ // 4. reconnect with only one options
748
+ // 5. reconnect client with saved peers
749
+ // 6. reconnect server with saved peers
750
+ // 7 mix of peers acting as clients and server
751
+ let checkDiscoveryInfo = {}
752
+ // any existing peers
753
+ if (this.peerNetwork.length === 0) {
754
+ firstTime = true
755
+ } else {
756
+ // check if current publickey matches
757
+ let keyMatchTopic = false
758
+ if (topicList.length > 0) {
759
+ for (let topicE of topicList) {
760
+ if (topicE.livePubkey === publicKey) {
761
+ keyMatchTopic = true
762
+ }
763
+ }
764
+ } else {
765
+ keyMatchTopic = false
766
+ }
767
+ // need to also check if histic topic set?
768
+ if (keyMatchTopic === false) {
769
+ // check in client or Server roles
770
+ // client will have topic if returning peer
771
+ if (nodeRole === 'client') {
772
+ // check if topic in peerInfo on connect
773
+ if (topic.length === 0) {
774
+ firstTime = true
775
+ }
776
+ } else if (nodeRole === 'server') {
777
+ if (topicList.length === 0) {
778
+ firstTime = true
779
+ } else {
780
+ // could be first time connect
781
+ // check if public key in network
782
+ let existingPeerCheck = false
783
+ for (let peer of this.peerNetwork) {
784
+ if (peer.key === publicKey) {
785
+ existingPeerCheck = true
786
+ } else {
787
+ existingPeerCheck = false
788
+ }
789
+ }
790
+ // can rule out reconnection? not enough info at this time, peer if reconnect topic will send id to match direct
791
+ if (existingPeerCheck === false && keyMatchTopic === false) {
792
+ firstTime = 'wait-topic-confirm'
793
+ } else if (existingPeerCheck === false && keyMatchTopic === true) {
794
+ firstTime = 'wait-topic-confirm'
795
+ } else {
796
+ firstTime = false
797
+ }
798
+ }
799
+ }
800
+ }
801
+ }
802
+
803
+
804
+ checkDiscoveryInfo.firstTime = firstTime
805
+ checkDiscoveryInfo.emptyHolder = emptyHolder
806
+ checkDiscoveryInfo.role = nodeRole
807
+ checkDiscoveryInfo.server = ''
808
+ checkDiscoveryInfo.client = ''
809
+ checkDiscoveryInfo.topic = ''
810
+ return checkDiscoveryInfo
811
+
812
+
813
+ /*
814
+ if (this.peerNetwork.length > 0) {
815
+ emptyHolder = true
816
+ // match pubkey to peer to get set topic
817
+ let matchTopic = ''
818
+ for (let peer of this.peerNetwork) {
819
+ if (peer.key === publicKey) {
820
+ matchTopic = peer.value.topic
821
+ } else {
822
+ }
823
+ }
824
+ // now use match topic to see if topic set as server or client roles?
825
+ let sendLogic = []
826
+ let holderLogic = []
827
+ for (let sendPeerT of this.sendTopicHolder) {
828
+ if (sendPeerT.topic === matchTopic) {
829
+ sendLogic.push(sendPeerT)
830
+ } else {
831
+ }
832
+ }
833
+ let matchHolderKeys = Object.keys(this.topicHolder)
834
+ for (let topicH of matchHolderKeys) {
835
+ if (this.topicHolder[topicH].topic === matchTopic) {
836
+ holderLogic.push(this.topicHolder[topicH])
837
+ } else {
838
+ }
839
+ }
840
+ console.log('match topic')
841
+ console.log(matchTopic)
842
+ console.log('send logic')
843
+ console.log(sendLogic)
844
+ console.log('holder logic')
845
+ console.log(holderLogic)
846
+ if (nodeRole === 'client') {
847
+ console.log('extra workk for client')
848
+ if (matchTopic.length > 0 && sendLogic.length === 0 && holderLogic.length === 0) {
849
+ firstTime = true
850
+ } else {
851
+ firstTime = true
852
+ }
853
+ } else if (nodeRole === 'server') {
854
+ console.log('extra workk for server')
855
+ // check if public key match any exsting keyids?
856
+ /* for (let peer of this.peerNetwork) {
857
+ if (peer.key === publicKey) {
858
+ firstTime = false
859
+ } else {
860
+ firstTime = true
861
+ }
862
+ }
863
+ }
864
+
865
+ } else {
866
+ firstTime = true
867
+ }
868
+
869
+ // has a topic already been sent to this peer, if not must be first time
870
+ let beforeSendTopic = false
871
+ for (let topicS of this.sendTopicHolder) {
872
+ if (topicS.livePubkey === publicKey) {
873
+ beforeSendTopic = true
874
+ }
875
+ }
575
876
  let checkDiscoveryTopic = {}
877
+ checkDiscoveryTopic.firstTime = firstTime // should this be set???
576
878
  if (topicList.length > 0) {
577
879
  for (let topicH of topicList) {
578
880
  const noisePublicKey = Buffer.from(topicH.topic, 'hex')
579
881
  let discovery = this.swarm.status(noisePublicKey)
882
+ console.log('discovery000000000000000000000000')
883
+ console.log(discovery.topic.toString('hex'))
580
884
  let discoverTopic = discovery.topic.toString('hex')
581
885
  if (discoverTopic === topicH.topic) {
582
886
  // do the peerid match?
583
887
  discovery.swarm.connections.forEach((value, key) => {
888
+ console.log('value looooop')
584
889
  checkDiscoveryTopic.server = value.publicKey.toString('hex')
585
890
  checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
586
891
  checkDiscoveryTopic.topic = discoverTopic
587
892
  })
588
893
  if (checkDiscoveryTopic.client === topicH.peerKey) {
894
+ console.log('retrun rrrr1111')
895
+ checkDiscoveryTopic.firstTime = false
896
+ console.log(checkDiscoveryTopic)
589
897
  return checkDiscoveryTopic
590
898
  } else {
591
899
  if (checkDiscoveryTopic.topic === topicH.topic) {
900
+ // now check if returning
901
+ if (firstTime === false && emptyHolder === true) {
902
+ // match current public key to original key uses a permanent key
903
+ let permKeyID = ''
904
+ let topicSet = ''
905
+ if (checkDiscoveryTopic.client === publicKey) {
906
+ permKeyID = checkDiscoveryTopic.client
907
+ topicSet = checkDiscoveryTopic.topic
908
+ }
909
+ // now use this id to check if peer has been invited
910
+ let beforeTopicCheck = this.checkTopicModes(topicSet)
911
+ console.log('beftopicchchc-------------------')
912
+ console.log(beforeTopicCheck)
913
+ checkDiscoveryTopic.firstTime = beforeTopicCheck.firstTime
914
+ } else {
915
+ checkDiscoveryTopic.firstTime = true
916
+ }
917
+ console.log('return rrrr2222')
918
+ console.log(checkDiscoveryTopic)
592
919
  return checkDiscoveryTopic
593
920
  } else {
594
921
  checkDiscoveryTopic.topic = ''
@@ -602,10 +929,41 @@ class NetworkPeers extends EventEmitter {
602
929
  checkDiscoveryTopic.server = ''
603
930
  checkDiscoveryTopic.client = ''
604
931
  checkDiscoveryTopic.topic = ''
932
+ console.log('return rrrr3333')
933
+ return checkDiscoveryTopic
605
934
  }
606
- return checkDiscoveryTopic
935
+ */
607
936
  }
608
937
 
938
+ /**
939
+ * match topics already set send and holder modes
940
+ * @method checkTopicModes
941
+ *
942
+ */
943
+ checkTopicModes = function (matchTopic) {
944
+ // now use match topic to see if topic set as server or client roles?
945
+ let sendLogic = []
946
+ let holderLogic = []
947
+ for (let sendPeerT of this.sendTopicHolder) {
948
+ if (sendPeerT.topic === matchTopic) {
949
+ sendLogic.push(sendPeerT)
950
+ } else {
951
+ }
952
+ }
953
+ let matchHolderKeys = Object.keys(this.topicHolder)
954
+ for (let topicH of matchHolderKeys) {
955
+ if (this.topicHolder[topicH].topic === matchTopic) {
956
+ holderLogic.push(this.topicHolder[topicH])
957
+ } else {
958
+ }
959
+ }
960
+ let firstTime = false
961
+ if (sendLogic.length === 0 && holderLogic.length === 0) {
962
+ firstTime = true
963
+ }
964
+
965
+ return { firstTime: firstTime }
966
+ }
609
967
 
610
968
  /**
611
969
  * where to route data share
@@ -666,6 +1024,19 @@ class NetworkPeers extends EventEmitter {
666
1024
  this.peerConnect[publickey].write(JSON.stringify(topicShare))
667
1025
  }
668
1026
 
1027
+ /**
1028
+ * write message to topic reconnect peer to inform of id
1029
+ * @method writeTopicReconnect
1030
+ *
1031
+ */
1032
+ writeTopicReconnect = function (publickey, topicInfo) {
1033
+ let topicReconnectMessage = {}
1034
+ topicReconnectMessage.type = 'topic-reconnect-id'
1035
+ topicReconnectMessage.data = { topic: topicInfo.topic, peerKey: topicInfo.peerKey }
1036
+ this.peerConnect[topicInfo.currentPubkey].write(JSON.stringify(topicReconnectMessage))
1037
+ }
1038
+
1039
+
669
1040
  /**
670
1041
  * write message to network
671
1042
  * @method writeTonetworkData
@@ -786,7 +1157,7 @@ class NetworkPeers extends EventEmitter {
786
1157
  *
787
1158
  */
788
1159
  topicListen = async function (topic, peerKey) {
789
- // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
1160
+ // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
790
1161
  // let topicKeylive = noisePublicKey.toString('hex')
791
1162
  const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
792
1163
  if (noisePublicKey.length === 32) {