hypercore 10.0.0-alpha.20 → 10.0.0-alpha.21
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/lib/protocol.js +39 -2
- package/package.json +1 -1
package/lib/protocol.js
CHANGED
|
@@ -2,9 +2,19 @@ const { uint, from: fromEncoding } = require('compact-encoding')
|
|
|
2
2
|
const b4a = require('b4a')
|
|
3
3
|
const safetyCatch = require('safety-catch')
|
|
4
4
|
const codecs = require('codecs')
|
|
5
|
+
const sodium = require('sodium-universal')
|
|
5
6
|
|
|
6
7
|
const messages = require('./messages')
|
|
7
8
|
|
|
9
|
+
const slab = b4a.alloc(96)
|
|
10
|
+
const NS = slab.subarray(0, 32)
|
|
11
|
+
const NS_HYPERCORE_INITIATOR = slab.subarray(32, 64)
|
|
12
|
+
const NS_HYPERCORE_RESPONDER = slab.subarray(64, 96)
|
|
13
|
+
|
|
14
|
+
sodium.crypto_generichash(NS, b4a.from('hypercore'))
|
|
15
|
+
sodium.crypto_generichash(NS_HYPERCORE_INITIATOR, b4a.from([0]), NS)
|
|
16
|
+
sodium.crypto_generichash(NS_HYPERCORE_RESPONDER, b4a.from([1]), NS)
|
|
17
|
+
|
|
8
18
|
class Extension {
|
|
9
19
|
constructor (protocol, type, name, handlers) {
|
|
10
20
|
this.protocol = protocol
|
|
@@ -257,10 +267,14 @@ module.exports = class Protocol {
|
|
|
257
267
|
this._remoteExtensions = []
|
|
258
268
|
this._extensions = new Map()
|
|
259
269
|
this._keepAliveInterval = null
|
|
270
|
+
this._pendingCaps = []
|
|
260
271
|
|
|
261
272
|
this._destroyer = this._safeDestroy.bind(this)
|
|
262
273
|
this.noiseStream.on('data', this.onmessage.bind(this))
|
|
263
274
|
this.noiseStream.on('end', this.noiseStream.end) // no half open
|
|
275
|
+
this.noiseStream.on('finish', () => {
|
|
276
|
+
this.setKeepAlive(false)
|
|
277
|
+
})
|
|
264
278
|
this.noiseStream.on('close', () => {
|
|
265
279
|
this.setKeepAlive(false)
|
|
266
280
|
// TODO: If the stream was destroyed with an error, we probably want to forward it here
|
|
@@ -346,6 +360,11 @@ module.exports = class Protocol {
|
|
|
346
360
|
|
|
347
361
|
if (batch.length === 0) return
|
|
348
362
|
|
|
363
|
+
while (this._pendingCaps.length > 0) {
|
|
364
|
+
const [key, cap] = this._pendingCaps.pop()
|
|
365
|
+
hypercoreCapability(this.noiseStream.isInitiator, this.noiseStream.handshakeHash, key, cap)
|
|
366
|
+
}
|
|
367
|
+
|
|
349
368
|
const state = { start: 0, end: 0, buffer: null }
|
|
350
369
|
const lens = new Array(batch.length)
|
|
351
370
|
|
|
@@ -386,10 +405,18 @@ module.exports = class Protocol {
|
|
|
386
405
|
}
|
|
387
406
|
|
|
388
407
|
_announceCore (alias, key, discoveryKey) {
|
|
408
|
+
const cap = b4a.alloc(32)
|
|
409
|
+
|
|
410
|
+
if (!this.noiseStream.handshakeHash) {
|
|
411
|
+
this._pendingCaps.push([key, cap]) // encode it later...
|
|
412
|
+
} else {
|
|
413
|
+
hypercoreCapability(this.noiseStream.isInitiator, this.noiseStream.handshakeHash, key, cap)
|
|
414
|
+
}
|
|
415
|
+
|
|
389
416
|
this.send(2, messages.core, -1, {
|
|
390
417
|
alias: alias,
|
|
391
418
|
discoveryKey: discoveryKey,
|
|
392
|
-
capability:
|
|
419
|
+
capability: cap
|
|
393
420
|
})
|
|
394
421
|
}
|
|
395
422
|
|
|
@@ -469,7 +496,11 @@ module.exports = class Protocol {
|
|
|
469
496
|
if (m.alias === this._remoteAliases.length) this._remoteAliases.push(null)
|
|
470
497
|
|
|
471
498
|
if (peer) {
|
|
472
|
-
|
|
499
|
+
const expectedCap = hypercoreCapability(!this.noiseStream.isInitiator, this.noiseStream.handshakeHash, peer.key)
|
|
500
|
+
if (!b4a.equals(expectedCap, m.capability)) {
|
|
501
|
+
this.destroy(new Error('Remote sent an invalid capability'))
|
|
502
|
+
return
|
|
503
|
+
}
|
|
473
504
|
|
|
474
505
|
if (m.alias >= this._remoteAliases.length) {
|
|
475
506
|
this.destroy(new Error('Remote alias out of bounds'))
|
|
@@ -544,3 +575,9 @@ function noop () {}
|
|
|
544
575
|
function isPromise (p) {
|
|
545
576
|
return !!p && typeof p.then === 'function'
|
|
546
577
|
}
|
|
578
|
+
|
|
579
|
+
function hypercoreCapability (initiator, handshakeHash, key, cap = b4a.alloc(32)) {
|
|
580
|
+
const ns = initiator ? NS_HYPERCORE_INITIATOR : NS_HYPERCORE_RESPONDER
|
|
581
|
+
sodium.crypto_generichash_batch(cap, [handshakeHash, key], ns)
|
|
582
|
+
return cap
|
|
583
|
+
}
|