holepunch-hop 0.3.3 → 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.3",
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
@@ -46,15 +46,14 @@ class HyperBee extends EventEmitter {
46
46
  valueEncoding: 'json' // same options as above
47
47
  })
48
48
  await this.dbPublicLibrary.ready()
49
- beePubkeys.push({ store: 'publiclibrary', pubkey: b4a.toString(core.key, 'hex')})
49
+ beePubkeys.push({ store: 'publiclibrary', pubkey: b4a.toString(this.dbPublicLibrary.key, 'hex')})
50
50
  // allow other peer access to public library (need to check for DDOS ie over asked)
51
- // join a topic
51
+ // join a topic for network
52
52
  const discovery = this.swarm.join(this.dbPublicLibrary.discoveryKey)
53
53
  // Only display the key once the Hyperbee has been announced to the DHT
54
54
  discovery.flushed().then(() => {
55
55
  })
56
56
 
57
-
58
57
  const core2 = this.store.get({ name: 'peerlibrary' })
59
58
  this.dbPeerLibrary = new Hyperbee(core2, {
60
59
  keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
@@ -149,12 +148,6 @@ class HyperBee extends EventEmitter {
149
148
  })
150
149
  await this.dbBentomedia.ready()
151
150
  beePubkeys.push({store:'bentomedia', pubkey: b4a.toString(core12.key, 'hex')})
152
-
153
- // await this.deleteBentocue({ cueid: '7da21b8d50c72f94595fd9617bfa2609c90d5d9c'})
154
- // await this.deleteBentocue({ cueid: '8220c425a534634a44a69b6f56babaf26edf2a01'})
155
- // await this.deleteBentocue({ cueid: 'ec4890f6c0b0d8f4a313136b4e7c9f425b1977a5'})
156
- // await this.deleteBentocue({ cueid: 'eea2825cc91a1093594dffa9a953cbce3f77860c'})
157
- // await this.deleteBentocue({ cueid: 'eee097d964fae67a6dd39abc02b0e9fd7c5f71bc'})
158
151
 
159
152
  this.emit('hbee-live')
160
153
  // return beePubkeys
@@ -172,8 +165,6 @@ class HyperBee extends EventEmitter {
172
165
  *
173
166
  */
174
167
  savePubliclibrary = async function (refContract) {
175
- console.log('savePubliclibrary')
176
- console.log(refContract)
177
168
  let beeSave = await this.dbPublicLibrary.put(refContract.data.hash, refContract.data.contract)
178
169
  // go query the key are return the info. to ensure data save asplanned.
179
170
  let saveCheck = await this.getPublicLibrary(refContract.data.hash)
@@ -416,13 +407,61 @@ class HyperBee extends EventEmitter {
416
407
  * @method deleteBentomedia
417
408
  */
418
409
  deleteBentomedia = async function (media) {
419
- console.log(media)
420
410
  const deleteStatus = await this.dbBentomedia.del(media.id)
421
411
  let deleteInfo = {}
422
412
  deleteInfo.spaceid = media.id
423
413
  return deleteInfo
424
414
  }
425
415
 
416
+ /** PEERS */
417
+ /**
418
+ * save research
419
+ * @method savePeer
420
+ *
421
+ */
422
+ savePeer = async function (peerInfo) {
423
+ await this.dbPeers.put(peerInfo.publickey, peerInfo)
424
+ let checkSave = await this.getPeer(peerInfo.publickey)
425
+ console.log('save peer over---------')
426
+ return checkSave
427
+ }
428
+
429
+ /**
430
+ * get one peer by publickey
431
+ * @method getPeer
432
+ *
433
+ */
434
+ getPeer = async function (key) {
435
+ const nodeData = await this.dbPeers.get(key)
436
+ return nodeData
437
+ }
438
+
439
+ /**
440
+ * get all peers
441
+ * @method getPeersHistory
442
+ *
443
+ */
444
+ getPeersHistory = async function (key) {
445
+ const peerHistory = await this.dbPeers.createReadStream()
446
+ let peerData = []
447
+ for await (const { key, value } of peerHistory) {
448
+ peerData.push({ key, value })
449
+ }
450
+ return peerData
451
+ }
452
+
453
+ /**
454
+ * delete contract
455
+ * @method deletePeer
456
+ */
457
+ deletePeer = async function (pubkey) {
458
+ const deleteStatus = await this.dbPeers.del(pubkey)
459
+ let deleteInfo = {}
460
+ deleteInfo.publickey = pubkey
461
+ return deleteInfo
462
+ }
463
+
464
+
426
465
  /** RESEARCH */
427
466
  /**
428
467
  * save research
@@ -722,8 +761,6 @@ class HyperBee extends EventEmitter {
722
761
 
723
762
  }
724
763
 
725
-
726
-
727
764
  /**
728
765
  * delete nxp ref contract public
729
766
  * @method deleteRefcontPubliclibrary
@@ -750,7 +787,6 @@ class HyperBee extends EventEmitter {
750
787
  return deleteInfo
751
788
  }
752
789
 
753
-
754
790
  /**
755
791
  * repicate the publiclibrary peer to peer
756
792
  * @method replicatePubliclibrary
@@ -880,10 +916,10 @@ class HyperBee extends EventEmitter {
880
916
 
881
917
  /**
882
918
  * repicate the publiclibrary peer to peer
883
- * @method replicatePubliclibraryOLD
919
+ * @method ryOLD
884
920
  *
885
921
  */
886
- replicatePubliclibraryOLD = async function (key) {
922
+ ryOLD = async function (key) {
887
923
  // key = '3ec0f3b78a0cfe574c4be89b1d703a65f018c0b73ad77e52ac65645d8f51676a'
888
924
  const store = this.client.corestore('peerspace-hyperbeetemp')
889
925
  const core = this.store.get(key)
@@ -936,9 +972,6 @@ class HyperBee extends EventEmitter {
936
972
 
937
973
  await this.client.replicate(beeResults.feed) // fetch from the network
938
974
  await beeResults.ready()
939
- // console.log('value for key ie results dataPrint')
940
- // console.log(await beeResults.get('005ad9c1c29b6b730b6e9f73dd108f8c716a6075'))
941
- // console.log('after get uuid')
942
975
  let rs = beeResults.createReadStream() // anything >=a and <=d
943
976
 
944
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
  }
@@ -48,7 +50,7 @@ class HolepunchWorker extends EventEmitter {
48
50
  this.store = new Corestore(os.homedir() + '/.hop-storage')
49
51
  this.swarm = new Hyperswarm()
50
52
  // make replication possible
51
- // this.swarm.on('connection', conn => this.store.replicate(conn))
53
+ this.swarm.on('connection', conn => this.store.replicate(conn))
52
54
  goodbye(() => this.swarm.destroy())
53
55
  this.BeeData = new BeeWorker(this.store, this.swarm)
54
56
  this.DriveFiles = new DriveWorker(this.store, this.swarm)
@@ -97,69 +99,89 @@ class HolepunchWorker extends EventEmitter {
97
99
  this.Peers.on('peer-network', (data) => {
98
100
  this.wsocket.send(JSON.stringify(data))
99
101
  })
100
- // peer connection active for first time
102
+ // peer connection active for first time
101
103
  this.Peers.on('peer-connect', (data) => {
102
- let taskCheck = 0
103
- let firstCheck = this.Peers.peerHolder[data]
104
- if (firstCheck !== undefined) {
105
- if (this.Peers.peerHolder[data]?.data?.labels !== undefined) {
106
- taskCheck = this.Peers.peerHolder[data].data.labels.length
107
- }
108
- } else {
109
- taskCheck = 0
110
- }
111
- // if (this.Peers.peerHolder[data].data.boardID !== undefined) {
112
- // any existing peers
113
- let holderCheck = Object.keys(this.Peers.peerHolder)
114
- // now first time, check if any message can be send?
115
- if (holderCheck !== 0 && taskCheck === 0) {
116
- console.log('HOP--write publis lib???')
117
- // switch between
118
- console.log(holderCheck)
119
- console.log(taskCheck)
120
- let peerFirstID = holderCheck[0]
121
- console.log(peerFirstID)
122
- console.log(this.Peers.peerHolder)
123
- // which direction connection, client or from a peer on the network?
124
- if (this.Peers.peerHolder[peerFirstID]?.data !== undefined) {
125
- let dataLive = this.Peers.peerHolder[peerFirstID].data.name
126
- if (dataLive === 'cue-space') {
127
- this.Peers.writeToCueSpace(this.Peers.peerHolder[peerFirstID].publickey)
128
- } else {
129
- this.Peers.writeToPublicLibrary(data)
130
- }
131
-
132
- } else {
133
- console.log('write to entwoir on start')
134
- this.Peers.writeTonetwork(data)
135
- }
136
- }
137
-
104
+ //this.warmPeerPrepare(data)
105
+ })
106
+ // share connection failed
107
+ this.Peers.on('peer-share-fail', (data) => {
108
+ let peerFail = {}
109
+ peerFail.type = 'account'
110
+ peerFail.action = 'peer-share-fail'
111
+ peerFail.data = { publickey: data }
112
+ this.wsocket.send(JSON.stringify(peerFail))
113
+ })
114
+ // save peer topic
115
+ this.Peers.on('peer-reconnect-topic', async (data) => {
116
+ data.prime = false
117
+ this.emit('peer-topic-update', data)
118
+ })
119
+ // peer reconnection topic ie. able to reconnect again
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)
138
128
  })
139
129
  // data for beebee
140
130
  this.Peers.on('beebee-data', (data) => {
141
131
  this.emit('peer-topeer', data)
142
132
  })
133
+ // cue space share
143
134
  this.Peers.on('cuespace-notification', (data) => {
144
- console.log('cue space ntototoo')
145
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
+ }
146
142
  })
147
143
  // public library notification
148
144
  this.Peers.on('publiclibrarynotification', (data) => {
149
145
  this.BeeData.replicatePubliclibrary(data)
150
146
  })
147
+ // beebee notifications public
151
148
  this.BeeData.on('publibbeebee-notification', (data) => {
152
149
  this.emit('beebee-publib-notification', data)
153
150
  })
154
151
  // new warm incoming peer
155
- this.Peers.on('connect-warm', (data) => {
152
+ this.Peers.on('connect-warm-first', (data) => {
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
+ }
164
+ }
165
+ // setup template for relationship
156
166
  let peerId = {}
157
- peerId.name = 'new-peer'
158
- peerId.publickey = data
159
- peerId.datastore = ''
167
+ peerId.name = peerInfoName
168
+ peerId.publickey = data.publickey
169
+ peerId.roletaken = data.roletaken
170
+ peerId.longterm = true
171
+ peerId.settopic = false
172
+ peerId.topic = ''
173
+ peerId.live = false
174
+ peerId.livePeerkey = ''
160
175
  this.warmPeers.push(peerId)
161
- this.emit('peer-incoming', peerId)
176
+ this.emit('peer-incoming-save', peerId)
162
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
+ })
184
+ // drive listener
163
185
  this.DriveFiles.on('largefile-save', (data) => {
164
186
  this.emit('drive-save-large', data)
165
187
  })
@@ -172,54 +194,180 @@ class HolepunchWorker extends EventEmitter {
172
194
  */
173
195
  networkPath = function (message) {
174
196
  if (message.action === 'share') {
175
- // has the peer joined already?
176
- let peerMatch = false
177
- for (let wpeer of this.warmPeers) {
178
- if (wpeer.publickey = message.data.publickey) {
179
- 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
+ }
180
206
  }
181
- }
182
-
183
- if (message.task === 'peer-join') {
184
- if (peerMatch === true) {
185
- this.Peers.peerAlreadyJoinSetData(message.data)
186
- this.Peers.writeTonetwork(message.data.publickey)
187
- } 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
188
213
  this.warmPeers.push(message.data)
189
- this.Peers.peerJoin(message.data)
190
- }
191
- } else if (message.task = 'cue-space') {
192
- console.log('cus space send path')
193
- console.log(peerMatch)
194
- if (peerMatch === true) {
195
- console.log('already connect space')
196
214
  this.Peers.peerAlreadyJoinSetData(message.data)
197
- this.Peers.writeToCueSpace(message.data.publickey)
198
- } else {
199
- console.log('first space send')
200
- this.warmPeers.push(message.data)
201
215
  this.Peers.peerJoin(message.data)
202
- // this.Peers.peerAlreadyJoinSetData(message.data)
203
- // this.Peers.writeToCueSpace(message.data.publickey)
204
- console.log('space suce peepr ot sent')
205
- }
206
- } else if (message.task === 'peer-board') {
207
- if (peerMatch === true) {
208
- this.Peers.peerAlreadyJoinSetData(message.data)
209
- this.Peers.writeToPublicLibrary(message.data.publickey)
210
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
211
219
  this.warmPeers.push(message.data)
212
- this.Peers.peerJoin(message.data)
213
- // now set data and write to public library info.
214
220
  this.Peers.peerAlreadyJoinSetData(message.data)
215
- this.Peers.writeToPublicLibrary(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
+ }
247
+ }
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)
259
+ }
260
+ }
261
+ }
262
+
263
+ /**
264
+ * prepare data to send to a warm peer
265
+ * @method warmPeerPrepare
266
+ */
267
+ warmPeerPrepare = function (data, existing) {
268
+ // check if codename holder has any data to process
269
+ this.processCodenameMatching(data)
270
+ // two checks, if topic send to other peer
271
+ if (existing !== true) {
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)
299
+ }
300
+ }
301
+
302
+ // if data within coming then process that
303
+ let peerDataExist = this.Peers.peerHolder[data]
304
+ if (peerDataExist === undefined) {
305
+ } else {
306
+ // what type of data being shared?
307
+ // check for data along with new peer?
308
+ this.Peers.routeDataPath(data, peerDataExist)
309
+ /*
310
+ if (peerDataExist.data !== undefined) {
311
+ if (peerDataExist.data.type === 'private-chart') {
312
+ this.Peers.writeTonetworkData(data, peerDataExist.data)
313
+ } else if (peerDataExist.data.type === 'private-cue-space') {
314
+ this.Peers.writeToCueSpace(this.Peers.peerHolder[peerFirstID].publickey)
315
+ } else if (peerDataExist.data.type === 'public-n1-experiment') {
316
+ this.Peers.writeTonetworkData(data, peerDataExist.data)
317
+ } else if (peerDataExist.data.type === 'public-library') {
318
+ this.Peers.writeToPublicLibrary(data)
319
+ } else if (peerDataExist.data.type === 'text-message') {
320
+ // simpole text message
321
+ this.Peers.writeTonetwork(data)
216
322
  }
217
- } else if (message.task === 'peer-write') {
218
- this.emit('peer-write', message.data)
219
- } else if (message.task === 'topic') {
220
- // this.Peers.peerTopic(message.data.topic)
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)
221
368
  }
222
369
  }
370
+ this.tpiocSaveReturn = updateTopic
223
371
  }
224
372
 
225
373
  /**
package/src/peers.js CHANGED
@@ -9,10 +9,7 @@
9
9
  * @version $Id$
10
10
  */
11
11
  import EventEmitter from 'events'
12
- // import DHT from '@hyperswarm/dht'
13
- import goodbye from 'graceful-goodbye'
14
- import b4a from 'b4a'
15
-
12
+ import crypto from 'crypto'
16
13
 
17
14
  class NetworkPeers extends EventEmitter {
18
15
 
@@ -22,8 +19,14 @@ class NetworkPeers extends EventEmitter {
22
19
  this.store = store
23
20
  this.swarm = swarm
24
21
  this.drive = {}
22
+ this.peerPrime = ''
23
+ this.peerNetwork = [] // set on loading library via HOP
24
+ this.peerEstContext = {}
25
25
  this.peerHolder = {}
26
26
  this.peerConnect = {}
27
+ this.topicHolder = {}
28
+ this.sendTopicHolder = []
29
+ this.peersRole = []
27
30
  }
28
31
 
29
32
  /**
@@ -32,6 +35,22 @@ class NetworkPeers extends EventEmitter {
32
35
  *
33
36
  */
34
37
  networkKeys = function () {
38
+ // console.log('swarm on start')
39
+ // console.log(this.swarm)
40
+ // console.log(this.swarm._discovery) // .toString('hex'))
41
+
42
+ /*
43
+ this.swarm._discovery.forEach((value, key) => {
44
+ console.log('key')
45
+ console.log(key)
46
+ this.peerPrime = key
47
+ console.log(this.peerPrime)
48
+ console.log('-----------swarm discovery on START-------------------')
49
+ console.log(Object.keys(value))
50
+ console.log(value.topic)
51
+ console.log(value.topic.toString('hex'))
52
+ })
53
+ */
35
54
  let peerNxKeys = {}
36
55
  peerNxKeys.publickey = this.swarm.keyPair.publicKey.toString('hex')
37
56
  let networkMessage = {}
@@ -43,20 +62,166 @@ class NetworkPeers extends EventEmitter {
43
62
  this.peerJoinClient()
44
63
  }
45
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
+
46
103
  /**
47
104
  * connection listen
48
105
  * @method listenNetwork
49
106
  *
50
107
  */
51
- listenNetwork = function () {
108
+ listenNetwork = function () {
52
109
  this.swarm.on('connection', (conn, info) => {
53
- // listen for replication
110
+ // console.log('peerinfo FIRST------------')
111
+ // console.log(info)
112
+ // save peer connection instance for ongoing communication
113
+ // listen for replication NEED UPTATED LOGIC
54
114
  this.store.replicate(conn)
55
- // listener to write message to peers or network partial or broadcast
115
+ // assess if token is present to match to exsing peer account ID?
56
116
  let publicKeylive = info.publicKey.toString('hex')
57
- this.emit('connect-warm', publicKeylive)
58
- this.peerConnect[publicKeylive] = conn
59
- this.emit('peer-connect', publicKeylive)
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
+
60
225
  // process network message
61
226
  conn.on('data', data =>
62
227
  // assess data
@@ -66,7 +231,7 @@ class NetworkPeers extends EventEmitter {
66
231
  })
67
232
  }
68
233
 
69
- /**
234
+ /**
70
235
  *
71
236
  * @method assessData data and act
72
237
  *
@@ -75,46 +240,331 @@ class NetworkPeers extends EventEmitter {
75
240
  if (Buffer.isBuffer(data)) {
76
241
  try {
77
242
  let dataShareIn = JSON.parse(data.toString())
78
- console.log('data in listen bubfferr')
79
- console.log(dataShareIn.type)
80
- if (dataShareIn.type === 'chart') {
243
+ if (dataShareIn.type === 'private-chart') {
81
244
  this.emit('beebee-data', dataShareIn)
82
245
  // need to look at NXP, modules and within for reference contracts.
83
246
  // Need to replicate public library for contracts (repliate hyberbee)
84
247
  // Need to ask for data source e.g. file (replicate hyberdrive)
85
248
  // Lastly put together SafeFlowECS query to produce chart
86
- } else if (dataShareIn.type === 'cue-space') {
249
+ } else if (dataShareIn.type === 'private-cue-space') {
87
250
  this.emit('cuespace-notification', dataShareIn)
88
251
  } else if (dataShareIn.type === 'public-library') {
89
252
  this.emit('publiclibrarynotification', dataShareIn)
90
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)
257
+ } else if (dataShareIn.type === 'topic-reconnect') {
258
+ // peer has share a topic for future reconnect
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
+ }
91
272
  }
92
- console.log(a)
93
273
  } catch (e) {
94
274
  return console.error('ignore err')
95
275
  }
96
276
  }
97
277
  }
98
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
+
99
526
  /**
100
527
  * write message to network
101
528
  * @method writeTonetwork
102
529
  *
103
530
  */
104
- writeTonetwork = function (publickey) {
531
+ writeTonetwork = function (data, messType) {
105
532
  // check this peer has asked for chart data
106
- let connectTrue = publickey in this.peerConnect
107
- let chartTrue = publickey in this.peerHolder
108
- if (connectTrue === true && chartTrue === true) {
109
- let chartData = this.peerHolder[publickey]
110
- let dataShare = {}
111
- dataShare.hop = chartData.hop
112
- dataShare.data = chartData.data
113
- dataShare.type = 'chart'
114
- this.peerConnect[publickey].write(JSON.stringify(dataShare))
115
- } else {
116
- console.log('non chart write')
117
- }
533
+ let dataSend = data
534
+ this.peerConnect[data].write(JSON.stringify(dataSend))
535
+ }
536
+
537
+ /**
538
+ * write message to network
539
+ * @method writeTonetworkTopic
540
+ *
541
+ */
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
547
+ // send to other peer topic to allow reconnection in future
548
+ let topicShare = {}
549
+ topicShare.type = 'topic-reconnect'
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
555
+ topicShare.data = topicGeneration
556
+ this.emit('topic-formed-save', topicShare)
557
+ // inform peer that topic has been created
558
+ this.peerConnect[publickey].write(JSON.stringify(topicShare))
559
+ }
560
+
561
+ /**
562
+ * write message to network
563
+ * @method writeTonetworkData
564
+ *
565
+ */
566
+ writeTonetworkData = function (publickey, dataShare) {
567
+ this.peerConnect[publickey].write(JSON.stringify(dataShare))
118
568
  }
119
569
 
120
570
  /**
@@ -122,19 +572,12 @@ class NetworkPeers extends EventEmitter {
122
572
  * @method writeToPublicLibrary
123
573
  *
124
574
  */
125
- writeToPublicLibrary = function (publickey) {
575
+ writeToPublicLibrary = function (publickey, data) {
126
576
  // check this peer has asked for chart data
127
- let connectTrue = publickey in this.peerConnect
128
- let libraryTrue = publickey in this.peerHolder
129
- if (connectTrue === true && libraryTrue === true) {
130
- let libraryData = this.peerHolder[publickey]
131
577
  let dataShare = {}
132
- dataShare.data = libraryData.data
578
+ dataShare.data = data
133
579
  dataShare.type = 'public-library'
134
580
  this.peerConnect[publickey].write(JSON.stringify(dataShare))
135
- } else {
136
- console.log('no board to write ie share with a peer')
137
- }
138
581
  }
139
582
 
140
583
  /**
@@ -142,37 +585,56 @@ class NetworkPeers extends EventEmitter {
142
585
  * @method writeToCueSpace
143
586
  *
144
587
  */
145
- writeToCueSpace = function (publickey) {
146
- // check this peer has asked for space data
147
- let connectTrue = publickey in this.peerConnect
148
- let spaceTrue = publickey in this.peerHolder
149
- if (connectTrue === true && spaceTrue === true) {
150
- let libraryData = this.peerHolder[publickey]
151
- let dataShare = {}
152
- dataShare.data = libraryData.data
153
- dataShare.type = 'cue-space'
154
- this.peerConnect[publickey].write(JSON.stringify(dataShare))
155
- } else {
156
- console.log('no cuespace to write ie share with a peer')
157
- }
588
+ writeToCueSpace = function (publickey, data) {
589
+ this.peerConnect[publickey].write(JSON.stringify(data))
158
590
  }
159
591
 
160
-
161
592
  /**
162
- * join peer to peer private (server)
593
+ * join peer to peer direct private (server)
163
594
  * @method peerJoin
164
595
  *
165
596
  */
166
597
  peerJoin = function (peerContext) {
167
- console.log('peerJOIn')
168
- console.log(peerContext)
598
+ // set timeer to inform if not connection can be established
599
+ this.checkTimerConnection(peerContext.publickey)
169
600
  this.peerHolder[peerContext.publickey] = peerContext
170
601
  const noisePublicKey = Buffer.from(peerContext.publickey, 'hex') // must be 32 bytes
171
602
  if (noisePublicKey.length === 32) {
172
- const peerConnect = this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
603
+ this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
173
604
  }
174
605
  }
175
606
 
607
+ /**
608
+ * give 2 seconds for connection to establish
609
+ * @method checkTimerConnection
610
+ *
611
+ */
612
+ checkTimerConnection (key) {
613
+ // if peerconnect not set the inform beebee not connection accepted try again
614
+ let localthis = this
615
+ // setTimeout(checkPeerState(localthis, key), 2000)
616
+ setTimeout(() => checkPeerState(localthis, key), 6000)
617
+
618
+ function checkPeerState (localthis, publicKeylive) {
619
+ if (localthis.peerConnect[publicKeylive] === undefined) {
620
+ // failed peer connection
621
+ localthis.emit('peer-share-fail', publicKeylive)
622
+ } else {
623
+ // connnection established
624
+ }
625
+ }
626
+ }
627
+
628
+ /**
629
+ * leave a direct peer connection
630
+ * @method peerLeave
631
+ *
632
+ */
633
+ peerLeave = function (peerLeaveKey) {
634
+ this.peerHolder[peerLeaveKey] = {}
635
+ this.swarm.leavePeer(peerLeaveKey)
636
+ }
637
+
176
638
  /**
177
639
  * already joined but keep track context data
178
640
  * @method peerAlreadyJoinSetData
@@ -180,9 +642,9 @@ class NetworkPeers extends EventEmitter {
180
642
  */
181
643
  peerAlreadyJoinSetData = function (peerContext) {
182
644
  this.peerHolder[peerContext.publickey] = peerContext
645
+ return true
183
646
  }
184
647
 
185
-
186
648
  /**
187
649
  * join peer to peer private (client)
188
650
  * @method peerJoinClient
@@ -193,48 +655,48 @@ class NetworkPeers extends EventEmitter {
193
655
  }
194
656
 
195
657
  /**
196
- * listen for topics as a client
197
- * @method listenClient
658
+ * out message topics as a client
659
+ * @method topicConnect
198
660
  *
199
661
  */
200
- listenClient = async function (topic) {
201
- const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
202
- const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
203
- await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
204
- }
662
+ topicConnect = async function (topic) {
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
+ }
205
673
 
206
674
  /**
207
- *
208
- * @method listen
675
+ * out message topics as a client
676
+ * @method topicListen
209
677
  *
210
678
  */
211
- listen = function () {
212
- console.log('listen')
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
+ }
213
691
  }
214
692
 
215
693
  /**
216
- * setup peers protocol
217
- * @method setupHyerPeers
694
+ * leave topic
695
+ * @method leaveTopic
218
696
  *
219
697
  */
220
- setupHyerPeers = function () {
221
- /* const dht = new DHT()
222
-
223
- // This keypair is your peer identifier in the DHT
224
- const keyPair = DHT.keyPair()
225
-
226
- const server = dht.createServer(conn => {
227
- console.log('got connection!')
228
- process.stdin.pipe(conn).pipe(process.stdout)
229
- })
230
-
231
- server.listen(keyPair).then(() => {
232
- console.log('listening on:', b4a.toString(keyPair.publicKey, 'hex'))
233
- })
234
-
235
- // Unnannounce the public key before exiting the process
236
- // (This is not a requirement, but it helps avoid DHT pollution)
237
- goodbye(() => server.close()) */
698
+ leaveTopic = async function (topic) {
699
+ await this.swarm.leave(topic)
238
700
  }
239
701
 
240
702
  }