hypercore 10.25.4 → 10.26.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/index.js +14 -8
- package/lib/block-encryption.js +6 -3
- package/lib/caps.js +9 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -167,15 +167,16 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
167
167
|
if (!noiseStream.userData) {
|
|
168
168
|
const protocol = Protomux.from(noiseStream)
|
|
169
169
|
|
|
170
|
-
if (opts.ondiscoverykey) {
|
|
171
|
-
protocol.pair({ protocol: 'hypercore/alpha' }, opts.ondiscoverykey)
|
|
172
|
-
}
|
|
173
170
|
if (opts.keepAlive !== false) {
|
|
174
171
|
noiseStream.setKeepAlive(5000)
|
|
175
172
|
}
|
|
176
173
|
noiseStream.userData = protocol
|
|
177
174
|
}
|
|
178
175
|
|
|
176
|
+
if (opts.ondiscoverykey) {
|
|
177
|
+
noiseStream.userData.pair({ protocol: 'hypercore/alpha' }, opts.ondiscoverykey)
|
|
178
|
+
}
|
|
179
|
+
|
|
179
180
|
return outerStream
|
|
180
181
|
}
|
|
181
182
|
|
|
@@ -248,9 +249,9 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
248
249
|
return s
|
|
249
250
|
}
|
|
250
251
|
|
|
251
|
-
async setEncryptionKey (
|
|
252
|
+
async setEncryptionKey (encryptionKey, opts) {
|
|
252
253
|
if (!this.opened) await this.opening
|
|
253
|
-
this.encryption =
|
|
254
|
+
this.encryption = encryptionKey ? new BlockEncryption(encryptionKey, this.key, { compat: this.core.compat, ...opts }) : null
|
|
254
255
|
}
|
|
255
256
|
|
|
256
257
|
setKeyPair (keyPair) {
|
|
@@ -388,7 +389,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
388
389
|
this.replicator.findingPeers += this._findingPeers
|
|
389
390
|
|
|
390
391
|
if (!this.encryption && opts.encryptionKey) {
|
|
391
|
-
this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
|
|
392
|
+
this.encryption = new BlockEncryption(opts.encryptionKey, this.key, { compat: this.core.compat, isBlockKey: opts.isBlockKey })
|
|
392
393
|
}
|
|
393
394
|
}
|
|
394
395
|
|
|
@@ -651,7 +652,12 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
651
652
|
|
|
652
653
|
if (status & 0b10000) {
|
|
653
654
|
for (let i = 0; i < this.sessions.length; i++) {
|
|
654
|
-
this.sessions[i]
|
|
655
|
+
const s = this.sessions[i]
|
|
656
|
+
|
|
657
|
+
s.emit('manifest')
|
|
658
|
+
if (s.encryption && s.encryption.compat !== this.core.compat) {
|
|
659
|
+
s.encryption = new BlockEncryption(s.encryptionKey, this.key, { compat: this.core.compat, isBlockKey: s.isBlockKey })
|
|
660
|
+
}
|
|
655
661
|
}
|
|
656
662
|
}
|
|
657
663
|
|
|
@@ -1108,5 +1114,5 @@ function ensureEncryption (core, opts) {
|
|
|
1108
1114
|
// Only override the block encryption if it's either not already set or if
|
|
1109
1115
|
// the caller provided a different key.
|
|
1110
1116
|
if (core.encryption && b4a.equals(core.encryption.key, opts.encryptionKey)) return
|
|
1111
|
-
core.encryption = new BlockEncryption(opts.encryptionKey, core.key)
|
|
1117
|
+
core.encryption = new BlockEncryption(opts.encryptionKey, core.key, { compat: core.core.compat, isBlockKey: opts.isBlockKey })
|
|
1112
1118
|
}
|
package/lib/block-encryption.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
const sodium = require('sodium-universal')
|
|
2
2
|
const c = require('compact-encoding')
|
|
3
3
|
const b4a = require('b4a')
|
|
4
|
+
const { BLOCK_ENCRYPTION } = require('./caps')
|
|
4
5
|
|
|
5
6
|
const nonce = b4a.alloc(sodium.crypto_stream_NONCEBYTES)
|
|
6
7
|
|
|
7
8
|
module.exports = class BlockEncryption {
|
|
8
|
-
constructor (encryptionKey, hypercoreKey) {
|
|
9
|
+
constructor (encryptionKey, hypercoreKey, { isBlockKey = false, compat = false } = {}) {
|
|
9
10
|
const subKeys = b4a.alloc(2 * sodium.crypto_stream_KEYBYTES)
|
|
10
11
|
|
|
11
12
|
this.key = encryptionKey
|
|
12
|
-
this.blockKey = subKeys.subarray(0, sodium.crypto_stream_KEYBYTES)
|
|
13
|
+
this.blockKey = isBlockKey ? encryptionKey : subKeys.subarray(0, sodium.crypto_stream_KEYBYTES)
|
|
13
14
|
this.blindingKey = subKeys.subarray(sodium.crypto_stream_KEYBYTES)
|
|
14
15
|
this.padding = 8
|
|
16
|
+
this.compat = compat
|
|
17
|
+
this.isBlockKey = isBlockKey
|
|
15
18
|
|
|
16
|
-
sodium.
|
|
19
|
+
if (!isBlockKey) sodium.crypto_generichash_batch(this.blockKey, compat ? [encryptionKey] : [BLOCK_ENCRYPTION, encryptionKey], hypercoreKey)
|
|
17
20
|
sodium.crypto_generichash(this.blindingKey, this.blockKey)
|
|
18
21
|
}
|
|
19
22
|
|
package/lib/caps.js
CHANGED
|
@@ -6,10 +6,18 @@ const c = require('compact-encoding')
|
|
|
6
6
|
// TODO: rename this to "crypto" and move everything hashing related etc in here
|
|
7
7
|
// Also lets move the tree stuff from hypercore-crypto here
|
|
8
8
|
|
|
9
|
-
const [
|
|
9
|
+
const [
|
|
10
|
+
TREE,
|
|
11
|
+
REPLICATE_INITIATOR,
|
|
12
|
+
REPLICATE_RESPONDER,
|
|
13
|
+
MANIFEST,
|
|
14
|
+
DEFAULT_NAMESPACE,
|
|
15
|
+
BLOCK_ENCRYPTION
|
|
16
|
+
] = crypto.namespace('hypercore', 6)
|
|
10
17
|
|
|
11
18
|
exports.MANIFEST = MANIFEST
|
|
12
19
|
exports.DEFAULT_NAMESPACE = DEFAULT_NAMESPACE
|
|
20
|
+
exports.BLOCK_ENCRYPTION = BLOCK_ENCRYPTION
|
|
13
21
|
|
|
14
22
|
exports.replicate = function (isInitiator, key, handshakeHash) {
|
|
15
23
|
const out = b4a.allocUnsafe(32)
|