hypercore 11.0.10 → 11.0.12

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 CHANGED
@@ -146,6 +146,10 @@ class Hypercore extends EventEmitter {
146
146
  return crypto.discoveryKey(key)
147
147
  }
148
148
 
149
+ static blockEncryptionKey (key, encryptionKey) {
150
+ return BlockEncryption.blockEncryptionKey(key, encryptionKey)
151
+ }
152
+
149
153
  static getProtocolMuxer (stream) {
150
154
  return stream.noiseStream.userData
151
155
  }
@@ -318,7 +322,10 @@ class Hypercore extends EventEmitter {
318
322
  if (e) this.core.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, ...e })
319
323
  }
320
324
 
325
+ const parent = opts.parent || null
326
+
321
327
  if (this.core.encryption) this.encryption = this.core.encryption
328
+ else if (parent && parent.encryption) this.encryption = this.core.encryption = parent.encryption
322
329
 
323
330
  this.writable = this._isWritable()
324
331
 
@@ -329,9 +336,9 @@ class Hypercore extends EventEmitter {
329
336
  this.encodeBatch = opts.encodeBatch
330
337
  }
331
338
 
332
- if (opts.parent) {
333
- if (opts.parent._stateIndex === -1) await opts.parent.ready()
334
- this._setupSession(opts.parent)
339
+ if (parent) {
340
+ if (parent._stateIndex === -1) await parent.ready()
341
+ this._setupSession(parent)
335
342
  }
336
343
 
337
344
  if (opts.exclusive) {
@@ -339,17 +346,17 @@ class Hypercore extends EventEmitter {
339
346
  await this.core.lockExclusive()
340
347
  }
341
348
 
342
- const parent = opts.parent || this.core
349
+ const parentState = parent ? parent.state : this.core.state
343
350
  const checkout = opts.checkout === undefined ? -1 : opts.checkout
344
351
  const state = this.state
345
352
 
346
353
  if (opts.atom) {
347
- this.state = await parent.state.createSession(null, false, opts.atom)
354
+ this.state = await parentState.createSession(null, false, opts.atom)
348
355
  if (state) state.unref()
349
356
  } else if (opts.name) {
350
357
  // todo: need to make named sessions safe before ready
351
358
  // atm we always copy the state in passCapabilities
352
- this.state = await parent.state.createSession(opts.name, !!opts.overwrite, null)
359
+ this.state = await parentState.createSession(opts.name, !!opts.overwrite, null)
353
360
  if (state) state.unref() // ref'ed above in setup session
354
361
  }
355
362
 
@@ -23,6 +23,12 @@ module.exports = class BlockEncryption {
23
23
  sodium.crypto_generichash(this.blindingKey, this.blockKey)
24
24
  }
25
25
 
26
+ static blockEncryptionKey (hypercoreKey, encryptionKey) {
27
+ const blockKey = b4a.alloc(sodium.crypto_stream_KEYBYTES)
28
+ sodium.crypto_generichash_batch(blockKey, [BLOCK_ENCRYPTION, hypercoreKey, encryptionKey])
29
+ return blockKey
30
+ }
31
+
26
32
  encrypt (index, block, fork) {
27
33
  const padding = block.subarray(0, this.padding)
28
34
  block = block.subarray(this.padding)
package/lib/core.js CHANGED
@@ -258,10 +258,13 @@ module.exports = class Core {
258
258
  header.userData.push({ key, value: unslab(value) })
259
259
  }
260
260
 
261
- // compat from earlier version that do not store contig length
262
- // if (header.hints.contiguousLength === 0) {
263
- // while (bitfield.get(header.hints.contiguousLength)) header.hints.contiguousLength++
264
- // }
261
+ const len = bitfield.findFirst(false, header.hints.contiguousLength)
262
+ if (header.hints.contiguousLength !== len) {
263
+ header.hints.contiguousLength = len
264
+ const tx = storage.write()
265
+ tx.setHints({ contiguousLength: len })
266
+ await tx.flush()
267
+ }
265
268
 
266
269
  // to unslab
267
270
  if (header.manifest) header.manifest = Verifier.createManifest(header.manifest)
@@ -757,7 +760,7 @@ function parseHeader (info) {
757
760
  tree: info.head || getDefaultTree(),
758
761
  hints: {
759
762
  reorgs: [],
760
- contiguousLength: 0
763
+ contiguousLength: info.hints ? info.hints.contiguousLength : 0
761
764
  }
762
765
  }
763
766
  }
@@ -769,11 +772,13 @@ async function getCoreInfo (storage) {
769
772
 
770
773
  const auth = r.getAuth()
771
774
  const head = r.getHead()
775
+ const hints = r.getHints()
772
776
 
773
777
  r.tryFlush()
774
778
 
775
779
  return {
776
780
  ...await auth,
777
- head: await head
781
+ head: await head,
782
+ hints: await hints
778
783
  }
779
784
  }
@@ -537,7 +537,7 @@ module.exports = class SessionState {
537
537
  return
538
538
  }
539
539
 
540
- if (p.drop) {
540
+ if (p.drop || p.truncated !== -1) {
541
541
  if (p.truncated !== b.start + b.length) throw INVALID_OPERATION('Atomic truncations must be contiguous')
542
542
  p.truncated = b.start
543
543
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.0.10",
3
+ "version": "11.0.12",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {