hypercore 11.0.2 → 11.0.4
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 +13 -6
- package/lib/block-encryption.js +3 -4
- package/lib/core.js +1 -1
- package/lib/session-state.js +10 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -69,7 +69,6 @@ class Hypercore extends EventEmitter {
|
|
|
69
69
|
this.closed = false
|
|
70
70
|
this.weak = !!opts.weak
|
|
71
71
|
this.snapshotted = !!opts.snapshot
|
|
72
|
-
this.draft = !!opts.draft
|
|
73
72
|
this.onwait = opts.onwait || null
|
|
74
73
|
this.wait = opts.wait !== false
|
|
75
74
|
this.timeout = opts.timeout || 0
|
|
@@ -314,8 +313,9 @@ class Hypercore extends EventEmitter {
|
|
|
314
313
|
|
|
315
314
|
if (this.keyPair === null) this.keyPair = opts.keyPair || this.core.header.keyPair
|
|
316
315
|
|
|
317
|
-
if (!this.core.encryption
|
|
318
|
-
|
|
316
|
+
if (!this.core.encryption) {
|
|
317
|
+
const e = getEncryptionOption(opts)
|
|
318
|
+
if (e) this.core.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, ...e })
|
|
319
319
|
}
|
|
320
320
|
|
|
321
321
|
if (this.core.encryption) this.encryption = this.core.encryption
|
|
@@ -896,7 +896,7 @@ class Hypercore extends EventEmitter {
|
|
|
896
896
|
const defaultKeyPair = this.state.name === null ? this.keyPair : null
|
|
897
897
|
|
|
898
898
|
const { keyPair = defaultKeyPair, signature = null } = opts
|
|
899
|
-
const writable =
|
|
899
|
+
const writable = !isDefault || !!signature || !!(keyPair && keyPair.secretKey)
|
|
900
900
|
|
|
901
901
|
if (this._readonly || writable === false) throw SESSION_NOT_WRITABLE()
|
|
902
902
|
|
|
@@ -1012,7 +1012,7 @@ class Hypercore extends EventEmitter {
|
|
|
1012
1012
|
|
|
1013
1013
|
_updateEncryption () {
|
|
1014
1014
|
const e = this.encryption
|
|
1015
|
-
this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat,
|
|
1015
|
+
this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, block: e.block })
|
|
1016
1016
|
if (e === this.core.encryption) this.core.encryption = this.encryption
|
|
1017
1017
|
}
|
|
1018
1018
|
}
|
|
@@ -1029,7 +1029,7 @@ function toHex (buf) {
|
|
|
1029
1029
|
|
|
1030
1030
|
function preappend (blocks) {
|
|
1031
1031
|
const offset = this.state.length
|
|
1032
|
-
const fork = this.state.
|
|
1032
|
+
const fork = this.state.encryptionFork
|
|
1033
1033
|
|
|
1034
1034
|
if (this.encryption.compat !== this.core.compat) this._updateEncryption()
|
|
1035
1035
|
|
|
@@ -1093,3 +1093,10 @@ function maybeAddMonitor (name) {
|
|
|
1093
1093
|
function isSessionMoved (err) {
|
|
1094
1094
|
return err.code === 'SESSION_MOVED'
|
|
1095
1095
|
}
|
|
1096
|
+
|
|
1097
|
+
function getEncryptionOption (opts) {
|
|
1098
|
+
// old style, supported for now but will go away
|
|
1099
|
+
if (opts.encryptionKey) return { key: opts.encryptionKey, block: !!opts.isBlockKey }
|
|
1100
|
+
if (!opts.encryption) return null
|
|
1101
|
+
return b4a.isBuffer(opts.encryption) ? { key: opts.encryption } : opts.encryption
|
|
1102
|
+
}
|
package/lib/block-encryption.js
CHANGED
|
@@ -6,17 +6,16 @@ const { BLOCK_ENCRYPTION } = require('./caps')
|
|
|
6
6
|
const nonce = b4a.alloc(sodium.crypto_stream_NONCEBYTES)
|
|
7
7
|
|
|
8
8
|
module.exports = class BlockEncryption {
|
|
9
|
-
constructor (encryptionKey, hypercoreKey, {
|
|
9
|
+
constructor (encryptionKey, hypercoreKey, { block = false, compat = true } = {}) {
|
|
10
10
|
const subKeys = b4a.alloc(2 * sodium.crypto_stream_KEYBYTES)
|
|
11
11
|
|
|
12
12
|
this.key = encryptionKey
|
|
13
|
-
this.blockKey =
|
|
13
|
+
this.blockKey = block ? encryptionKey : subKeys.subarray(0, sodium.crypto_stream_KEYBYTES)
|
|
14
14
|
this.blindingKey = subKeys.subarray(sodium.crypto_stream_KEYBYTES)
|
|
15
15
|
this.padding = 8
|
|
16
16
|
this.compat = compat
|
|
17
|
-
this.isBlockKey = isBlockKey
|
|
18
17
|
|
|
19
|
-
if (!
|
|
18
|
+
if (!block) {
|
|
20
19
|
if (compat) sodium.crypto_generichash_batch(this.blockKey, [encryptionKey], hypercoreKey)
|
|
21
20
|
else sodium.crypto_generichash_batch(this.blockKey, [BLOCK_ENCRYPTION, hypercoreKey, encryptionKey])
|
|
22
21
|
}
|
package/lib/core.js
CHANGED
|
@@ -374,7 +374,7 @@ module.exports = class Core {
|
|
|
374
374
|
|
|
375
375
|
async _validateCommit (state, treeLength) {
|
|
376
376
|
if (this.state.length > state.length) {
|
|
377
|
-
return false // TODO: partial commit in the future
|
|
377
|
+
return false // TODO: partial commit and truncation possible in the future
|
|
378
378
|
}
|
|
379
379
|
|
|
380
380
|
if (this.state.length > treeLength) {
|
package/lib/session-state.js
CHANGED
|
@@ -25,6 +25,8 @@ module.exports = class SessionState {
|
|
|
25
25
|
this.blocks = blocks
|
|
26
26
|
this.tree = tree
|
|
27
27
|
|
|
28
|
+
this.treeFork = -1 // only updated if truncated below dependency
|
|
29
|
+
|
|
28
30
|
// merkle state
|
|
29
31
|
this.roots = []
|
|
30
32
|
this.length = 0
|
|
@@ -101,6 +103,10 @@ module.exports = class SessionState {
|
|
|
101
103
|
this.length = MerkleTree.span(roots) / 2
|
|
102
104
|
}
|
|
103
105
|
|
|
106
|
+
get encryptionFork () {
|
|
107
|
+
return this.treeFork === -1 ? this.core.header.tree.fork : this.treeFork
|
|
108
|
+
}
|
|
109
|
+
|
|
104
110
|
get byteLength () {
|
|
105
111
|
return MerkleTree.size(this.roots)
|
|
106
112
|
}
|
|
@@ -218,6 +224,10 @@ module.exports = class SessionState {
|
|
|
218
224
|
signature: this.signature
|
|
219
225
|
}
|
|
220
226
|
|
|
227
|
+
if (src.dependencyLength > this.dependencyLength) {
|
|
228
|
+
this.storage.updateDependencyLength(src.dependencyLength)
|
|
229
|
+
}
|
|
230
|
+
|
|
221
231
|
// handle migration
|
|
222
232
|
if (src.core !== this.core) {
|
|
223
233
|
this.prologue = src.prologue
|