holepunch-hop 0.1.9 → 0.2.1
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 +1 -1
- package/src/bees.js +121 -25
- package/src/drive.js +0 -2
- package/src/index.js +5 -3
- package/src/peers.js +4 -2
package/package.json
CHANGED
package/src/bees.js
CHANGED
|
@@ -12,15 +12,12 @@ import EventEmitter from 'events'
|
|
|
12
12
|
import Hyperbee from 'hyperbee'
|
|
13
13
|
import b4a from 'b4a'
|
|
14
14
|
|
|
15
|
-
import { Node } from 'hyperbee/lib/messages.js'
|
|
16
|
-
import { start } from 'repl'
|
|
17
|
-
|
|
18
15
|
class HyperBee extends EventEmitter {
|
|
19
16
|
|
|
20
|
-
constructor(
|
|
17
|
+
constructor(store, swarm) {
|
|
21
18
|
super()
|
|
22
19
|
this.hello = 'hyperbee'
|
|
23
|
-
this.
|
|
20
|
+
this.store = store
|
|
24
21
|
this.swarm = swarm
|
|
25
22
|
this.liveBees = {}
|
|
26
23
|
}
|
|
@@ -42,7 +39,7 @@ class HyperBee extends EventEmitter {
|
|
|
42
39
|
setupHyperbee = async function () {
|
|
43
40
|
let beePubkeys = []
|
|
44
41
|
|
|
45
|
-
const core = this.
|
|
42
|
+
const core = this.store.get({ name: 'publiclibrary' })
|
|
46
43
|
this.dbPublicLibrary = new Hyperbee(core, {
|
|
47
44
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
48
45
|
valueEncoding: 'json' // same options as above
|
|
@@ -50,9 +47,18 @@ class HyperBee extends EventEmitter {
|
|
|
50
47
|
await this.dbPublicLibrary.ready()
|
|
51
48
|
beePubkeys.push({ store: 'pubilclibrary', pubkey: b4a.toString(core.key, 'hex')})
|
|
52
49
|
// console.log(this.dbPublicLibrary._feed)
|
|
53
|
-
//
|
|
50
|
+
// allow other peer access to public library (need to check for DDOS ie over asked)
|
|
51
|
+
// join a topic
|
|
52
|
+
console.log('public library discover key')
|
|
53
|
+
console.log(this.dbPublicLibrary.discoveryKey)
|
|
54
|
+
const discovery = this.swarm.join(this.dbPublicLibrary.discoveryKey)
|
|
55
|
+
// Only display the key once the Hyperbee has been announced to the DHT
|
|
56
|
+
discovery.flushed().then(() => {
|
|
57
|
+
console.log('bee key:', b4a.toString(core.key, 'hex'))
|
|
58
|
+
})
|
|
59
|
+
|
|
54
60
|
|
|
55
|
-
const core2 = this.
|
|
61
|
+
const core2 = this.store.get({ name: 'peerlibrary' })
|
|
56
62
|
this.dbPeerLibrary = new Hyperbee(core2, {
|
|
57
63
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
58
64
|
valueEncoding: 'json' // same options as above
|
|
@@ -60,7 +66,7 @@ class HyperBee extends EventEmitter {
|
|
|
60
66
|
await this.dbPeerLibrary.ready()
|
|
61
67
|
beePubkeys.push({store:'peerlibrary', pubkey: b4a.toString(core2.key, 'hex')})
|
|
62
68
|
|
|
63
|
-
const core6 = this.
|
|
69
|
+
const core6 = this.store.get({ name: 'peers' })
|
|
64
70
|
this.dbPeers = new Hyperbee(core6, {
|
|
65
71
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
66
72
|
valueEncoding: 'json' // same options as above
|
|
@@ -68,7 +74,7 @@ class HyperBee extends EventEmitter {
|
|
|
68
74
|
await this.dbPeers.ready()
|
|
69
75
|
beePubkeys.push({store:'peers', pubkey: b4a.toString(core6.key, 'hex')})
|
|
70
76
|
|
|
71
|
-
const core3 = this.
|
|
77
|
+
const core3 = this.store.get({ name: 'bentospaces' })
|
|
72
78
|
this.dbBentospaces = new Hyperbee(core3, {
|
|
73
79
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
74
80
|
valueEncoding: 'json' // same options as above
|
|
@@ -76,7 +82,7 @@ class HyperBee extends EventEmitter {
|
|
|
76
82
|
await this.dbBentospaces.ready()
|
|
77
83
|
beePubkeys.push({store:'bentospaces', pubkey: b4a.toString(core3.key, 'hex')})
|
|
78
84
|
|
|
79
|
-
const core4 = this.
|
|
85
|
+
const core4 = this.store.get({ name: 'hopresults' })
|
|
80
86
|
this.dbHOPresults = new Hyperbee(core4, {
|
|
81
87
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
82
88
|
valueEncoding: 'json' // same options as above
|
|
@@ -85,7 +91,7 @@ class HyperBee extends EventEmitter {
|
|
|
85
91
|
// this.client.replicate(this.dbHOPresults.feed)
|
|
86
92
|
beePubkeys.push({store:'hopresults', pubkey: b4a.toString(core4.key, 'hex')})
|
|
87
93
|
|
|
88
|
-
const core5 = this.
|
|
94
|
+
const core5 = this.store.get({ name: 'kbledger' })
|
|
89
95
|
this.dbKBledger = new Hyperbee(core5, {
|
|
90
96
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
91
97
|
valueEncoding: 'json' // same options as above
|
|
@@ -204,14 +210,25 @@ class HyperBee extends EventEmitter {
|
|
|
204
210
|
return chatData
|
|
205
211
|
}
|
|
206
212
|
|
|
213
|
+
/**
|
|
214
|
+
* save space menu
|
|
215
|
+
* @method saveSpaceHistory
|
|
216
|
+
*
|
|
217
|
+
*/
|
|
218
|
+
saveSpaceHistory = async function (spaceContract) {
|
|
219
|
+
await this.dbBentospaces.put(spaceContract.space.spaceid, spaceContract)
|
|
220
|
+
let checkSave = await this.getBentospace(spaceContract.space.spaceid)
|
|
221
|
+
return checkSave
|
|
222
|
+
}
|
|
223
|
+
|
|
207
224
|
/**
|
|
208
225
|
* save space layout of bentobox
|
|
209
226
|
* @method saveBentospace
|
|
210
227
|
*
|
|
211
228
|
*/
|
|
212
229
|
saveBentospace = async function (spaceContract) {
|
|
213
|
-
await this.dbBentospaces.put(spaceContract.
|
|
214
|
-
let checkSave = await this.getBentospace(spaceContract.
|
|
230
|
+
await this.dbBentospaces.put(spaceContract.spaceid, spaceContract)
|
|
231
|
+
let checkSave = await this.getBentospace(spaceContract.spaceid)
|
|
215
232
|
return checkSave
|
|
216
233
|
}
|
|
217
234
|
|
|
@@ -231,7 +248,7 @@ class HyperBee extends EventEmitter {
|
|
|
231
248
|
*
|
|
232
249
|
*/
|
|
233
250
|
deleteBentospace = async function (space) {
|
|
234
|
-
const deleteStatus = await this.dbBentospaces.del(
|
|
251
|
+
const deleteStatus = await this.dbBentospaces.del(space.spaceid)
|
|
235
252
|
let deleteInfo = {}
|
|
236
253
|
deleteInfo.spaceid = space.spaceid
|
|
237
254
|
return deleteInfo
|
|
@@ -342,12 +359,22 @@ class HyperBee extends EventEmitter {
|
|
|
342
359
|
return ledgerData
|
|
343
360
|
}
|
|
344
361
|
|
|
362
|
+
/**
|
|
363
|
+
* lookup specific result UUID
|
|
364
|
+
* @method peerResultsItem
|
|
365
|
+
*
|
|
366
|
+
*/
|
|
367
|
+
peerResultsItem = async function (dataPrint) {
|
|
368
|
+
const resultsData = this.dbKBledger.get(dataPrint.resultuuid)
|
|
369
|
+
return resultsData
|
|
370
|
+
}
|
|
371
|
+
|
|
345
372
|
/**
|
|
346
373
|
* lookup specific result UUID
|
|
347
374
|
* @method peerResults
|
|
348
375
|
*
|
|
349
376
|
*/
|
|
350
|
-
peerResults = async function (
|
|
377
|
+
peerResults = async function () {
|
|
351
378
|
const nodeData = this.dbKBledger.createReadStream()
|
|
352
379
|
let resData = []
|
|
353
380
|
for await (const { key, value } of nodeData) {
|
|
@@ -387,17 +414,90 @@ class HyperBee extends EventEmitter {
|
|
|
387
414
|
return deleteInfo
|
|
388
415
|
}
|
|
389
416
|
|
|
417
|
+
|
|
390
418
|
/**
|
|
391
419
|
* repicate the publiclibrary peer to peer
|
|
392
420
|
* @method replicatePubliclibrary
|
|
393
421
|
*
|
|
394
422
|
*/
|
|
395
|
-
replicatePubliclibrary = async function (
|
|
396
|
-
|
|
397
|
-
|
|
423
|
+
replicatePubliclibrary = async function (keylib) {
|
|
424
|
+
// create or get the hypercore using the public key supplied as command-line argument
|
|
425
|
+
const coreRep = this.store.get({ key: b4a.from(keylib, 'hex') })
|
|
426
|
+
|
|
427
|
+
// create a hyperbee instance using the hypercore instance
|
|
428
|
+
const beePlib = new Hyperbee(coreRep, {
|
|
429
|
+
keyEncoding: 'utf-8',
|
|
430
|
+
valueEncoding: 'utf-8'
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
// wait till the hypercore properties to be intialized
|
|
434
|
+
await coreRep.ready()
|
|
435
|
+
|
|
436
|
+
// logging the public key of the hypercore instance
|
|
437
|
+
// console.log('core key here is:', coreRep.key.toString('hex'))
|
|
438
|
+
|
|
439
|
+
// Attempt to connect to peers
|
|
440
|
+
this.swarm.join(coreRep.discoveryKey, { server: false, client: true })
|
|
441
|
+
|
|
442
|
+
await coreRep.update()
|
|
443
|
+
|
|
444
|
+
// now ask for whole of public library
|
|
445
|
+
/* read all of repliate connect bee and displays */
|
|
446
|
+
const chathistoryData = beePlib.createReadStream() // { gt: 'a', lt: 'z' }) // anything >a and <z
|
|
447
|
+
let chatData = []
|
|
448
|
+
for await (const { key, value } of chathistoryData) {
|
|
449
|
+
chatData.push({ key, value })
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// now replicate with peer own public library in whole or per nxp
|
|
453
|
+
let savePublib = await this.updatePublicLibrary(beePlib)
|
|
454
|
+
let repMessage = {}
|
|
455
|
+
repMessage.type = 'library'
|
|
456
|
+
repMessage.action = 'replicate-publiclibrary'
|
|
457
|
+
repMessage.task = 'replicate'
|
|
458
|
+
repMessage.reftype = 'publiclibrary'
|
|
459
|
+
repMessage.data = savePublib
|
|
460
|
+
return repMessage
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* update public library from peers public library
|
|
465
|
+
* @method updatePublicLibrary
|
|
466
|
+
*
|
|
467
|
+
*/
|
|
468
|
+
updatePublicLibrary = async function (updateLib) {
|
|
469
|
+
/* read all of repliate connect bee and displays */
|
|
470
|
+
const chathistoryData = updateLib.createReadStream() // { gt: 'a', lt: 'z' }) // anything >a and <z
|
|
471
|
+
let libData = []
|
|
472
|
+
for await (const { key, value } of chathistoryData) {
|
|
473
|
+
libData.push({ key, value })
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// save entries required
|
|
477
|
+
const batch = this.dbPublicLibrary.batch()
|
|
478
|
+
for (const { key, value } of libData) {
|
|
479
|
+
await batch.put(key, JSON.parse(value))
|
|
480
|
+
}
|
|
481
|
+
await batch.flush()
|
|
482
|
+
|
|
483
|
+
// check
|
|
484
|
+
const libUpdatecheck = this.dbPublicLibrary.createReadStream()
|
|
485
|
+
let libConracts = []
|
|
486
|
+
for await (const { key, value } of libUpdatecheck) {
|
|
487
|
+
libConracts.push({ key, value })
|
|
488
|
+
}
|
|
489
|
+
return true
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* repicate the publiclibrary peer to peer
|
|
494
|
+
* @method replicatePubliclibraryOLD
|
|
495
|
+
*
|
|
496
|
+
*/
|
|
497
|
+
replicatePubliclibraryOLD = async function (key) {
|
|
398
498
|
// key = '3ec0f3b78a0cfe574c4be89b1d703a65f018c0b73ad77e52ac65645d8f51676a'
|
|
399
499
|
const store = this.client.corestore('peerspace-hyperbeetemp')
|
|
400
|
-
const core = this.
|
|
500
|
+
const core = this.store.get(key)
|
|
401
501
|
this.dbPublicLibraryTemp = new Hyperbee(core, {
|
|
402
502
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
403
503
|
valueEncoding: 'json' // same options as above
|
|
@@ -419,8 +519,6 @@ class HyperBee extends EventEmitter {
|
|
|
419
519
|
*
|
|
420
520
|
*/
|
|
421
521
|
getReplicatePublicLibrary = async function (nxp) {
|
|
422
|
-
console.log('temp public library get info from peer replicate')
|
|
423
|
-
console.log(nxp)
|
|
424
522
|
// const peerRepData = await this.dbPublicLibraryTemp.get()
|
|
425
523
|
const peerRepData = await this.dbPublicLibraryTemp.createReadStream()
|
|
426
524
|
let contractData = []
|
|
@@ -439,7 +537,7 @@ class HyperBee extends EventEmitter {
|
|
|
439
537
|
const beeKey = 'eff38e0adefd1e1ffcc8dcf4e3413148645a183f2c13679365c431bcc2d26668'
|
|
440
538
|
|
|
441
539
|
const store = this.client.corestore('peerspace-hyperbeetemp')
|
|
442
|
-
const core = this.
|
|
540
|
+
const core = this.store.get(beeKey)
|
|
443
541
|
|
|
444
542
|
// load and read the hyperbee identified by `beeKey`
|
|
445
543
|
const beeResults =new Hyperbee(core, {
|
|
@@ -477,8 +575,6 @@ class HyperBee extends EventEmitter {
|
|
|
477
575
|
*
|
|
478
576
|
*/
|
|
479
577
|
publicLibraryAddentry = async function (nxp) {
|
|
480
|
-
console.log('add entry from nl2')
|
|
481
|
-
console.log(nxp)
|
|
482
578
|
const localthis = this
|
|
483
579
|
const refContract = await this.dbPublicLibraryTemp.get(nxp.nxpID)
|
|
484
580
|
// need to look up individual module contracts and copy them across
|
package/src/drive.js
CHANGED
|
@@ -208,8 +208,6 @@ class HypDrive extends EventEmitter {
|
|
|
208
208
|
*
|
|
209
209
|
*/
|
|
210
210
|
hyperdriveLocalfile = async function (path) {
|
|
211
|
-
console.log('hoelpuch--localfile')
|
|
212
|
-
console.log(path)
|
|
213
211
|
// File reads to buffer and recreate file
|
|
214
212
|
// const bufFromGet2 = await this.drive.get(path)
|
|
215
213
|
const { value: entry } = await this.drive.entry(path)
|
package/src/index.js
CHANGED
|
@@ -47,6 +47,8 @@ class HolepunchWorker extends EventEmitter {
|
|
|
47
47
|
startHolepunch = async function () {
|
|
48
48
|
this.store = new Corestore(os.homedir() + '/.hop-storage')
|
|
49
49
|
this.swarm = new Hyperswarm()
|
|
50
|
+
// make replication possible
|
|
51
|
+
// this.swarm.on('connection', conn => this.store.replicate(conn))
|
|
50
52
|
goodbye(() => this.swarm.destroy())
|
|
51
53
|
this.BeeData = new BeeWorker(this.store, this.swarm)
|
|
52
54
|
this.DriveFiles = new DriveWorker(this.store, this.swarm)
|
|
@@ -71,7 +73,6 @@ class HolepunchWorker extends EventEmitter {
|
|
|
71
73
|
*
|
|
72
74
|
*/
|
|
73
75
|
setWebsocket = function (ws) {
|
|
74
|
-
console.log('webcocket live HP')
|
|
75
76
|
this.wsocket = ws
|
|
76
77
|
this.BeeData.setWebsocket(ws)
|
|
77
78
|
this.DriveFiles.setWebsocket(ws)
|
|
@@ -100,9 +101,10 @@ class HolepunchWorker extends EventEmitter {
|
|
|
100
101
|
this.Peers.on('connect-warm', (data) => {
|
|
101
102
|
let peerId = {}
|
|
102
103
|
peerId.name = 'new-peer'
|
|
103
|
-
peerId.
|
|
104
|
+
peerId.publickey = data
|
|
104
105
|
peerId.datastore = ''
|
|
105
106
|
this.warmPeers.push(peerId)
|
|
107
|
+
this.emit('peer-incoming', peerId)
|
|
106
108
|
})
|
|
107
109
|
}
|
|
108
110
|
|
|
@@ -149,7 +151,7 @@ class HolepunchWorker extends EventEmitter {
|
|
|
149
151
|
this.core3 = this.store.get({ name: 'core-3' })
|
|
150
152
|
await Promise.all([this.core1.ready(), this.core2.ready(), this.core3.ready()])
|
|
151
153
|
|
|
152
|
-
console.log('main core key:', b4a.toString(this.core1.key, 'hex'))
|
|
154
|
+
// console.log('main core key:', b4a.toString(this.core1.key, 'hex'))
|
|
153
155
|
this.discKeypeer = b4a.toString(this.core1.key, 'hex')
|
|
154
156
|
|
|
155
157
|
// Here we'll only join the swarm with the core1's discovery key
|
package/src/peers.js
CHANGED
|
@@ -16,11 +16,11 @@ import b4a from 'b4a'
|
|
|
16
16
|
|
|
17
17
|
class NetworkPeers extends EventEmitter {
|
|
18
18
|
|
|
19
|
-
constructor(
|
|
19
|
+
constructor(store, swarm) {
|
|
20
20
|
super()
|
|
21
21
|
console.log('peer manager')
|
|
22
22
|
this.hello = 'hyperpeers'
|
|
23
|
-
this.
|
|
23
|
+
this.store = store
|
|
24
24
|
this.swarm = swarm
|
|
25
25
|
this.drive = {}
|
|
26
26
|
this.peerHolder = {}
|
|
@@ -53,6 +53,8 @@ class NetworkPeers extends EventEmitter {
|
|
|
53
53
|
listenNetwork = function () {
|
|
54
54
|
console.log('listen network')
|
|
55
55
|
this.swarm.on('connection', (conn, info) => {
|
|
56
|
+
// listen for replication
|
|
57
|
+
this.store.replicate(conn)
|
|
56
58
|
// listener to write message to peers or network partial or broadcast
|
|
57
59
|
console.log('swarm connection recieved')
|
|
58
60
|
let publicKeylive = info.publicKey.toString('hex')
|