hypercore 10.25.3 → 10.26.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/index.js +10 -5
- package/lib/batch.js +1 -1
- package/lib/block-encryption.js +6 -3
- package/lib/caps.js +9 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -248,9 +248,9 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
248
248
|
return s
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
-
async setEncryptionKey (
|
|
251
|
+
async setEncryptionKey (encryptionKey, opts) {
|
|
252
252
|
if (!this.opened) await this.opening
|
|
253
|
-
this.encryption =
|
|
253
|
+
this.encryption = encryptionKey ? new BlockEncryption(encryptionKey, this.key, { compat: this.core.compat, ...opts }) : null
|
|
254
254
|
}
|
|
255
255
|
|
|
256
256
|
setKeyPair (keyPair) {
|
|
@@ -388,7 +388,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
388
388
|
this.replicator.findingPeers += this._findingPeers
|
|
389
389
|
|
|
390
390
|
if (!this.encryption && opts.encryptionKey) {
|
|
391
|
-
this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
|
|
391
|
+
this.encryption = new BlockEncryption(opts.encryptionKey, this.key, { compat: this.core.compat, isBlockKey: opts.isBlockKey })
|
|
392
392
|
}
|
|
393
393
|
}
|
|
394
394
|
|
|
@@ -651,7 +651,12 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
651
651
|
|
|
652
652
|
if (status & 0b10000) {
|
|
653
653
|
for (let i = 0; i < this.sessions.length; i++) {
|
|
654
|
-
this.sessions[i]
|
|
654
|
+
const s = this.sessions[i]
|
|
655
|
+
|
|
656
|
+
s.emit('manifest')
|
|
657
|
+
if (s.encryption && s.encryption.compat !== this.core.compat) {
|
|
658
|
+
s.encryption = new BlockEncryption(s.encryptionKey, this.key, { compat: this.core.compat, isBlockKey: s.isBlockKey })
|
|
659
|
+
}
|
|
655
660
|
}
|
|
656
661
|
}
|
|
657
662
|
|
|
@@ -1108,5 +1113,5 @@ function ensureEncryption (core, opts) {
|
|
|
1108
1113
|
// Only override the block encryption if it's either not already set or if
|
|
1109
1114
|
// the caller provided a different key.
|
|
1110
1115
|
if (core.encryption && b4a.equals(core.encryption.key, opts.encryptionKey)) return
|
|
1111
|
-
core.encryption = new BlockEncryption(opts.encryptionKey, core.key)
|
|
1116
|
+
core.encryption = new BlockEncryption(opts.encryptionKey, core.key, { compat: core.core.compat, isBlockKey: opts.isBlockKey })
|
|
1112
1117
|
}
|
package/lib/batch.js
CHANGED
|
@@ -183,7 +183,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
183
183
|
if (len < this.length) return b
|
|
184
184
|
|
|
185
185
|
for (let i = 0; i < length - len; i++) {
|
|
186
|
-
b.append(this._appendsActual === this._appends ? blocks[i] : this._encrypt(b.length
|
|
186
|
+
b.append(this._appendsActual === this._appends ? blocks[i] : this._encrypt(b.length, blocks[i]))
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
return b
|
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)
|