holepunch-hop 0.3.8 → 0.4.0

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.8",
3
+ "version": "0.4.0",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -504,7 +504,6 @@ class HyperBee extends EventEmitter {
504
504
  savePeer = async function (peerInfo) {
505
505
  await this.dbPeers.put(peerInfo.publickey, peerInfo)
506
506
  let checkSave = await this.getPeer(peerInfo.publickey)
507
- console.log('save peer over---------')
508
507
  return checkSave
509
508
  }
510
509
 
@@ -876,7 +875,7 @@ class HyperBee extends EventEmitter {
876
875
  */
877
876
  replicatePubliclibrary = async function (dataIn) {
878
877
  // create or get the hypercore using the public key supplied as command-line argument
879
- const coreRep = this.store.get({ key: b4a.from(dataIn.data.datastores, 'hex') })
878
+ const coreRep = this.store.get({ key: b4a.from(dataIn.data.data.datastores, 'hex') })
880
879
 
881
880
  // create a hyperbee instance using the hypercore instance
882
881
  const beePlib = new Hyperbee(coreRep, {
@@ -896,7 +895,7 @@ class HyperBee extends EventEmitter {
896
895
  await coreRep.update()
897
896
 
898
897
  // if provided with specific boarnxp key then just get the contract, extract module contracts and get those contracts and then inform the peer and save to their public library
899
- const boardNXPcontract = await beePlib.get(dataIn.data.boardID)
898
+ const boardNXPcontract = await beePlib.get(dataIn.data.data.boardID)
900
899
  let unString = JSON.parse(boardNXPcontract.value)
901
900
  let moduleContracts = []
902
901
  for (let mod of unString.modules) {
@@ -935,8 +934,8 @@ class HyperBee extends EventEmitter {
935
934
  holderConfirm.boardNXP = [boardNXPcontract]
936
935
  holderConfirm.modules = moduleContracts
937
936
  holderConfirm.refcontracts = referenceContracts
938
- this.confirmPubLibList[dataIn.data.datastores] = holderConfirm
939
- this.emit('publibbeebee-notification', dataIn.data)
937
+ this.confirmPubLibList[dataIn.data.data.datastores] = holderConfirm
938
+ this.emit('publibbeebee-notification', dataIn)
940
939
  }
941
940
  // or
942
941
  // now ask for whole of public library
package/src/index.js CHANGED
@@ -52,6 +52,16 @@ class HolepunchWorker extends EventEmitter {
52
52
  // make replication possible
53
53
  this.swarm.on('connection', conn => this.store.replicate(conn))
54
54
  goodbye(() => this.swarm.destroy())
55
+
56
+ this.swarm.on('update', () => {
57
+ // this.Peers.updateListen(data)
58
+ console.log('update-------')
59
+ console.log(this.swarm.connecting)
60
+ console.log(this.swarm.connections.size)
61
+ console.log(this.swarm.peers.size)
62
+ })
63
+
64
+
55
65
  this.BeeData = new BeeWorker(this.store, this.swarm)
56
66
  this.DriveFiles = new DriveWorker(this.store, this.swarm)
57
67
  this.Peers = new PeerWorker(this.store, this.swarm)
@@ -134,8 +144,8 @@ class HolepunchWorker extends EventEmitter {
134
144
  this.Peers.on('cuespace-notification', (data) => {
135
145
  this.emit('peer-cuespace', data)
136
146
  // check if bentoboxN1 included?
137
- if (data.data.content.bbn1.publicN1contracts.length > 0) {
138
- for (let n1Cont of data.data.content.bbn1.publicN1contracts) {
147
+ if (data.data.data.content.bbn1.publicN1contracts.length > 0) {
148
+ for (let n1Cont of data.data.data.content.bbn1.publicN1contracts) {
139
149
  this.BeeData.replicatePubliclibrary({ data: n1Cont })
140
150
  }
141
151
  }
@@ -181,6 +191,9 @@ class HolepunchWorker extends EventEmitter {
181
191
  peerLive.publickey = data
182
192
  this.emit('peer-live-notify', peerLive)
183
193
  })
194
+ this.Peers.on('peer-disconnect', (data) => {
195
+ this.emit('peer-disconnect-notify', data)
196
+ })
184
197
  // drive listener
185
198
  this.DriveFiles.on('largefile-save', (data) => {
186
199
  this.emit('drive-save-large', data)
@@ -192,7 +205,7 @@ class HolepunchWorker extends EventEmitter {
192
205
  * @method networkPath
193
206
  *
194
207
  */
195
- networkPath = function (message) {
208
+ networkPath = async function (message) {
196
209
  if (message.action === 'share') {
197
210
  // check if joined now?
198
211
  let reEstablishShort = this.Peers.checkConnectivityStatus(message, this.warmPeers, 'invite-gen')
@@ -270,6 +283,16 @@ class HolepunchWorker extends EventEmitter {
270
283
  this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
271
284
  }
272
285
  }
286
+ } else if (message.action === 'retry') {
287
+ console.log('retry')
288
+ let peerDefaults = this.Peers.peerMatchTopic(message.data.key)
289
+ this.Peers.discoveryMatch(message.data.key)
290
+ } else if (message.action === 'peer-closed') {
291
+ console.log('distroy swarm connections')
292
+ console.log(message)
293
+ this.flushConnections()
294
+ await this.swarm.destroy()
295
+ // this.flushConnections()
273
296
  }
274
297
  }
275
298
 
@@ -319,21 +342,6 @@ class HolepunchWorker extends EventEmitter {
319
342
  // what type of data being shared?
320
343
  // check for data along with new peer?
321
344
  this.Peers.routeDataPath(data, peerDataExist)
322
- /*
323
- if (peerDataExist.data !== undefined) {
324
- if (peerDataExist.data.type === 'private-chart') {
325
- this.Peers.writeTonetworkData(data, peerDataExist.data)
326
- } else if (peerDataExist.data.type === 'private-cue-space') {
327
- this.Peers.writeToCueSpace(this.Peers.peerHolder[peerFirstID].publickey)
328
- } else if (peerDataExist.data.type === 'public-n1-experiment') {
329
- this.Peers.writeTonetworkData(data, peerDataExist.data)
330
- } else if (peerDataExist.data.type === 'public-library') {
331
- this.Peers.writeToPublicLibrary(data)
332
- } else if (peerDataExist.data.type === 'text-message') {
333
- // simpole text message
334
- this.Peers.writeTonetwork(data)
335
- }
336
- } */
337
345
  }
338
346
  }
339
347
 
@@ -432,6 +440,21 @@ class HolepunchWorker extends EventEmitter {
432
440
  this.readcore = lastBlock
433
441
  }
434
442
 
443
+ /**
444
+ * refresh connection i.e. which peers are live
445
+ * @method flushConnections
446
+ *
447
+ */
448
+ flushConnections = async function () {
449
+ console.log('flush connections')
450
+ console.log(this.swarm.connections.size)
451
+ console.log(this.swarm.connecting)
452
+ console.log(this.swarm.peers.size)
453
+ await this.swarm.flush()
454
+ await Promise.all(Array.from(this.swarm.connections).map(e => e.flush()))
455
+ await new Promise(resolve => setImmediate(resolve))
456
+ }
457
+
435
458
  }
436
459
 
437
460
  export default HolepunchWorker
package/src/peers.js CHANGED
@@ -27,6 +27,7 @@ class NetworkPeers extends EventEmitter {
27
27
  this.topicHolder = {}
28
28
  this.sendTopicHolder = []
29
29
  this.peersRole = []
30
+ this.discoveryList = []
30
31
  }
31
32
 
32
33
  /**
@@ -82,8 +83,8 @@ class NetworkPeers extends EventEmitter {
82
83
  for (let sPeer of this.peerNetwork) {
83
84
  // sPeer.value.key = publicKeylive
84
85
  if (sPeer.value.settopic === true) {
85
- // client role
86
- this.topicConnect(sPeer.value.topic)
86
+ // client role need to pass on peerUniqueID
87
+ this.topicConnect(sPeer.key, sPeer.value.topic)
87
88
  } else {
88
89
  // server role
89
90
  this.topicListen(sPeer.value.topic, sPeer.key)
@@ -100,8 +101,6 @@ class NetworkPeers extends EventEmitter {
100
101
  this.peerEstContext[pubKey] = established
101
102
  }
102
103
 
103
-
104
-
105
104
  /**
106
105
  * connection listen
107
106
  * @method listenNetwork
@@ -228,11 +227,41 @@ class NetworkPeers extends EventEmitter {
228
227
  // assess data
229
228
  this.assessData(publicKeylive, data)
230
229
  )
231
- //conn.end()
230
+
231
+ conn.on('error', data => {
232
+ let connectLivekeys = Object.keys(this.peerConnect)
233
+ // console.log(this.peerNetwork)
234
+ for (let peer of this.peerNetwork) {
235
+ for (let pconn of connectLivekeys) {
236
+ if (peer.value.livePeerkey === pconn) {
237
+ // check if connect is close?
238
+ let keysNoise = Object.keys(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
239
+ // console.log(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
240
+ let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
241
+ if (closeStatus === true) {
242
+ // remove peer & inform beebee
243
+ this.emit('peer-disconnect', { publickey: peer.key })
244
+ }
245
+ }
246
+ }
247
+ }
248
+ })
249
+
250
+ // conn.end()
232
251
  })
233
252
  }
234
253
 
235
- /**
254
+ /**
255
+ *
256
+ * @method updateListen
257
+ *
258
+ */
259
+ updateListen = function (data) {
260
+ console.log('update listen')
261
+ console.log(data)
262
+ }
263
+
264
+ /**
236
265
  *
237
266
  * @method assessData data and act
238
267
  *
@@ -241,16 +270,19 @@ class NetworkPeers extends EventEmitter {
241
270
  if (Buffer.isBuffer(data)) {
242
271
  try {
243
272
  let dataShareIn = JSON.parse(data.toString())
273
+ // match current public key to base id of peer
274
+ let peerMatch = this.peerMatchbase(peer)
244
275
  if (dataShareIn.type === 'private-chart') {
245
- this.emit('beebee-data', dataShareIn)
276
+ this.emit('beebee-data', { publickey: peerMatch, data: dataShareIn })
277
+ // two types of chart share above html answer sharing and below from a full bentoboxN1 experiment TODO
246
278
  // need to look at NXP, modules and within for reference contracts.
247
279
  // Need to replicate public library for contracts (repliate hyberbee)
248
280
  // Need to ask for data source e.g. file (replicate hyberdrive)
249
281
  // Lastly put together SafeFlowECS query to produce chart
250
282
  } else if (dataShareIn.type === 'private-cue-space') {
251
- this.emit('cuespace-notification', dataShareIn)
283
+ this.emit('cuespace-notification', { publickey: peerMatch, data: dataShareIn })
252
284
  } else if (dataShareIn.type === 'public-library') {
253
- this.emit('publiclibrarynotification', dataShareIn)
285
+ this.emit('publiclibrarynotification', { publickey: peerMatch, data: dataShareIn })
254
286
  } else if (dataShareIn.type === 'peer') {
255
287
  } else if (dataShareIn.type === 'peer-codename-inform') {
256
288
  // all peer to match publicke to codename then update save and infom beebee
@@ -374,6 +406,56 @@ class NetworkPeers extends EventEmitter {
374
406
  return roleMatch
375
407
  }
376
408
 
409
+ /**
410
+ * match peer key to settings
411
+ * @method peerMatchTopic
412
+ *
413
+ */
414
+ peerMatchTopic = function (pubKey) {
415
+ // first match live pubkey to topic and then use topic to get original
416
+ let peerSettings = {}
417
+ for (let savePeer of this.peerNetwork) {
418
+ if (savePeer.key === pubKey) {
419
+ peerSettings = savePeer
420
+ }
421
+ }
422
+ return peerSettings
423
+ }
424
+
425
+ /**
426
+ * match discovery peer reconnect
427
+ * @method discoveryMatch
428
+ *
429
+ */
430
+ discoveryMatch = function (pubKey) {
431
+ // first match live pubkey to topic and then use topic to get original
432
+ let discoverySettings = {}
433
+ for (let savePeer of this.discoveryList) {
434
+ if (savePeer.peerKey === pubKey) {
435
+ discoverySettings = savePeer.discovery
436
+ }
437
+ }
438
+ // now refresh
439
+ discoverySettings.refresh()
440
+ return true
441
+ }
442
+
443
+ /**
444
+ * match current publickey to peer id i.e. estbalshed on first connect and maps to 'peername' in UX
445
+ * @method peerMatchbase
446
+ *
447
+ */
448
+ peerMatchbase = function (currPubKey) {
449
+ // first match live pubkey to topic and then use topic to get original
450
+ let originalKey = ''
451
+ for (let savePeer of this.peerNetwork) {
452
+ if (savePeer.value.livePeerkey === currPubKey) {
453
+ originalKey = savePeer.key
454
+ }
455
+ }
456
+ return originalKey
457
+ }
458
+
377
459
  /**
378
460
  * split invte code string
379
461
  * @method inviteDecoded
@@ -493,13 +575,29 @@ class NetworkPeers extends EventEmitter {
493
575
  let discovery = this.swarm.status(noisePublicKey)
494
576
  let discoverTopic = discovery.topic.toString('hex')
495
577
  if (discoverTopic === topicH.topic) {
578
+ // do the peerid match?
496
579
  discovery.swarm.connections.forEach((value, key) => {
497
580
  checkDiscoveryTopic.server = value.publicKey.toString('hex')
498
581
  checkDiscoveryTopic.client = value.remotePublicKey.toString('hex')
499
582
  checkDiscoveryTopic.topic = discoverTopic
500
583
  })
584
+ if (checkDiscoveryTopic.client === topicH.peerKey) {
585
+ return checkDiscoveryTopic
586
+ } else {
587
+ if (checkDiscoveryTopic.topic === topicH.topic) {
588
+ return checkDiscoveryTopic
589
+ } else {
590
+ checkDiscoveryTopic.topic = ''
591
+ }
592
+ }
593
+ } else {
594
+
501
595
  }
502
596
  }
597
+ } else {
598
+ checkDiscoveryTopic.server = ''
599
+ checkDiscoveryTopic.client = ''
600
+ checkDiscoveryTopic.topic = ''
503
601
  }
504
602
  return checkDiscoveryTopic
505
603
  }
@@ -665,14 +763,16 @@ class NetworkPeers extends EventEmitter {
665
763
  * @method topicConnect
666
764
  *
667
765
  */
668
- topicConnect = async function (topic) {
766
+ topicConnect = async function (peerID, topic) {
767
+ console.log('topic connect')
669
768
  // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
670
769
  const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
671
770
  if (noisePublicKey.length === 32) {
672
771
  let topicKeylive = noisePublicKey.toString('hex')
673
772
  this.topicHolder[topic] = { role: 'server', livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic, key: topicKeylive, timestamp: '' }
674
- this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic })
773
+ this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), peerKey: peerID, topic: topic })
675
774
  const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
775
+ this.discoveryList.push({ peerKey: peerID, topic: topic, discovery: peerConnect })
676
776
  await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
677
777
  }
678
778
  }
@@ -683,6 +783,7 @@ class NetworkPeers extends EventEmitter {
683
783
  *
684
784
  */
685
785
  topicListen = async function (topic, peerKey) {
786
+ console.log('topic listen')
686
787
  // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
687
788
  // let topicKeylive = noisePublicKey.toString('hex')
688
789
  const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
@@ -690,7 +791,8 @@ class NetworkPeers extends EventEmitter {
690
791
  let topicKeylive = noisePublicKey.toString('hex')
691
792
  this.topicHolder[topic] = { role: 'client', topic: topic, key: topicKeylive, peerKey: peerKey, timestamp: '' }
692
793
  const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
693
- await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
794
+ this.discoveryList.push({ peerKey: peerKey, topic: topic, discovery: peerConnect })
795
+ await this.swarm.flush() // Waits for the topic to be fully announced on the DHT
694
796
  } else {
695
797
  console.log('key lenght issue')
696
798
  }