hypercore 11.0.3 → 11.0.5
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 +15 -8
- package/lib/block-encryption.js +3 -4
- package/lib/core.js +1 -1
- package/lib/session-state.js +12 -1
- 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
|
|
@@ -341,14 +341,14 @@ class Hypercore extends EventEmitter {
|
|
|
341
341
|
|
|
342
342
|
const parent = opts.parent || this.core
|
|
343
343
|
const checkout = opts.checkout === undefined ? -1 : opts.checkout
|
|
344
|
+
const state = this.state
|
|
344
345
|
|
|
345
346
|
if (opts.atom) {
|
|
346
347
|
this.state = await parent.state.createSession(null, checkout, false, opts.atom)
|
|
348
|
+
if (state) state.unref()
|
|
347
349
|
} else if (opts.name) {
|
|
348
350
|
// todo: need to make named sessions safe before ready
|
|
349
351
|
// atm we always copy the state in passCapabilities
|
|
350
|
-
const state = this.state
|
|
351
|
-
|
|
352
352
|
this.state = await parent.state.createSession(opts.name, checkout, !!opts.overwrite, null)
|
|
353
353
|
if (state) state.unref() // ref'ed above in setup session
|
|
354
354
|
|
|
@@ -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
|
}
|
|
@@ -235,6 +241,8 @@ module.exports = class SessionState {
|
|
|
235
241
|
}
|
|
236
242
|
|
|
237
243
|
this._moveToCore(src.core)
|
|
244
|
+
|
|
245
|
+
await this.storage.close()
|
|
238
246
|
}
|
|
239
247
|
|
|
240
248
|
const truncated = (bitfield && bitfield.truncated !== -1)
|
|
@@ -792,8 +800,9 @@ module.exports = class SessionState {
|
|
|
792
800
|
const treeLength = this.length
|
|
793
801
|
|
|
794
802
|
if (!this.isSnapshot()) {
|
|
795
|
-
const
|
|
803
|
+
const oldStorage = this.storage
|
|
796
804
|
|
|
805
|
+
const truncation = length < this.length ? await truncateAndFlush(this, length) : null
|
|
797
806
|
const treeInfo = truncation ? truncation.tree : await state._getTreeHeadAt(this.length)
|
|
798
807
|
|
|
799
808
|
treeInfo.fork = truncation ? this.fork + 1 : this.fork
|
|
@@ -821,6 +830,8 @@ module.exports = class SessionState {
|
|
|
821
830
|
if (dependency) this.storage.updateDependencyLength(this.dependencyLength)
|
|
822
831
|
this.ontruncate(tree, tree.length, treeLength, flushed)
|
|
823
832
|
}
|
|
833
|
+
|
|
834
|
+
await oldStorage.close()
|
|
824
835
|
}
|
|
825
836
|
|
|
826
837
|
if (!this.storage.atom) {
|