hypercore 11.0.11 → 11.0.13

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
  }
@@ -226,6 +230,7 @@ class Hypercore extends EventEmitter {
226
230
 
227
231
  async setEncryptionKey (encryptionKey, opts) {
228
232
  if (!this.opened) await this.opening
233
+ if (this.core.unencrypted) return
229
234
  this.encryption = encryptionKey ? new BlockEncryption(encryptionKey, this.key, { compat: this.core.compat, ...opts }) : null
230
235
  if (!this.core.encryption) this.core.encryption = this.encryption
231
236
  }
@@ -313,7 +318,7 @@ class Hypercore extends EventEmitter {
313
318
 
314
319
  if (this.keyPair === null) this.keyPair = opts.keyPair || this.core.header.keyPair
315
320
 
316
- if (!this.core.encryption) {
321
+ if (!this.core.encryption && !this.core.unencrypted) {
317
322
  const e = getEncryptionOption(opts)
318
323
  if (e) this.core.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, ...e })
319
324
  }
@@ -755,7 +760,8 @@ class Hypercore extends EventEmitter {
755
760
  block = b4a.from(block)
756
761
 
757
762
  if (this.encryption.compat !== this.core.compat) this._updateEncryption()
758
- this.encryption.decrypt(index, block)
763
+ if (this.core.unencrypted) this.encryption = null
764
+ else this.encryption.decrypt(index, block)
759
765
  }
760
766
 
761
767
  return this._decode(encoding, block)
@@ -899,13 +905,13 @@ class Hypercore extends EventEmitter {
899
905
  const defaultKeyPair = this.state.name === null ? this.keyPair : null
900
906
 
901
907
  const { keyPair = defaultKeyPair, signature = null } = opts
902
- const writable = !isDefault || !!signature || !!(keyPair && keyPair.secretKey)
908
+ const writable = !isDefault || !!signature || !!(keyPair && keyPair.secretKey) || opts.writable === true
903
909
 
904
910
  if (this._readonly || writable === false) throw SESSION_NOT_WRITABLE()
905
911
 
906
912
  blocks = Array.isArray(blocks) ? blocks : [blocks]
907
913
 
908
- const preappend = this.encryption && this._preappend
914
+ const preappend = this.core.unencrypted ? null : (this.encryption && this._preappend)
909
915
 
910
916
  const buffers = this.encodeBatch !== null ? this.encodeBatch(blocks) : new Array(blocks.length)
911
917
 
@@ -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
@@ -41,6 +41,7 @@ module.exports = class Core {
41
41
  this.verifier = null
42
42
  this.truncating = 0
43
43
  this.updating = false
44
+ this.unencrypted = false
44
45
  this.skipBitfield = null
45
46
  this.globalCache = opts.globalCache || null
46
47
  this.autoClose = opts.autoClose !== false
@@ -258,13 +259,19 @@ module.exports = class Core {
258
259
  header.userData.push({ key, value: unslab(value) })
259
260
  }
260
261
 
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
- // }
262
+ const len = bitfield.findFirst(false, header.hints.contiguousLength)
263
+ if (header.hints.contiguousLength !== len) {
264
+ header.hints.contiguousLength = len
265
+ const tx = storage.write()
266
+ tx.setHints({ contiguousLength: len })
267
+ await tx.flush()
268
+ }
265
269
 
266
270
  // to unslab
267
- if (header.manifest) header.manifest = Verifier.createManifest(header.manifest)
271
+ if (header.manifest) {
272
+ header.manifest = Verifier.createManifest(header.manifest)
273
+ this.unencrypted = header.manifest.unencrypted
274
+ }
268
275
 
269
276
  const verifier = header.manifest ? new Verifier(header.key, header.manifest, { crypto, legacy }) : null
270
277
 
@@ -329,6 +336,7 @@ module.exports = class Core {
329
336
  if (verifier.prologue) this.state.prologue = Object.assign({}, verifier.prologue)
330
337
 
331
338
  this.manifest = this.header.manifest = manifest
339
+ this.unencrypted = this.manifest.unencrypted
332
340
 
333
341
  tx.setAuth({
334
342
  key: this.header.key,
@@ -757,7 +765,7 @@ function parseHeader (info) {
757
765
  tree: info.head || getDefaultTree(),
758
766
  hints: {
759
767
  reorgs: [],
760
- contiguousLength: 0
768
+ contiguousLength: info.hints ? info.hints.contiguousLength : 0
761
769
  }
762
770
  }
763
771
  }
@@ -769,11 +777,13 @@ async function getCoreInfo (storage) {
769
777
 
770
778
  const auth = r.getAuth()
771
779
  const head = r.getHead()
780
+ const hints = r.getHints()
772
781
 
773
782
  r.tryFlush()
774
783
 
775
784
  return {
776
785
  ...await auth,
777
- head: await head
786
+ head: await head,
787
+ hints: await hints
778
788
  }
779
789
  }
package/lib/messages.js CHANGED
@@ -136,7 +136,8 @@ const manifestv0 = {
136
136
  prologue: {
137
137
  hash: c.fixed32.decode(state),
138
138
  length: 0
139
- }
139
+ },
140
+ unencrypted: false
140
141
  }
141
142
  }
142
143
 
@@ -147,7 +148,8 @@ const manifestv0 = {
147
148
  allowPatch: false,
148
149
  quorum: 1,
149
150
  signers: [signer.decode(state)],
150
- prologue: null
151
+ prologue: null,
152
+ unencrypted: false
151
153
  }
152
154
  }
153
155
 
@@ -159,7 +161,8 @@ const manifestv0 = {
159
161
  allowPatch: (flags & 1) !== 0,
160
162
  quorum: c.uint.decode(state),
161
163
  signers: signerArray.decode(state),
162
- prologue: null
164
+ prologue: null,
165
+ unencrypted: false
163
166
  }
164
167
  }
165
168
  }
@@ -180,7 +183,7 @@ const manifest = exports.manifest = {
180
183
  c.uint.encode(state, m.version)
181
184
  if (m.version === 0) return manifestv0.encode(state, m)
182
185
 
183
- c.uint.encode(state, (m.allowPatch ? 1 : 0) | (m.prologue ? 2 : 0))
186
+ c.uint.encode(state, (m.allowPatch ? 1 : 0) | (m.prologue ? 2 : 0) | (m.unencrypted ? 4 : 0))
184
187
  hashes.encode(state, m.hash)
185
188
 
186
189
  c.uint.encode(state, m.quorum)
@@ -196,6 +199,7 @@ const manifest = exports.manifest = {
196
199
  const hash = hashes.decode(state)
197
200
  const quorum = c.uint.decode(state)
198
201
  const signers = signerArray.decode(state)
202
+ const unencrypted = (flags & 4) !== 0
199
203
 
200
204
  return {
201
205
  version: 1,
@@ -203,7 +207,8 @@ const manifest = exports.manifest = {
203
207
  allowPatch: (flags & 1) !== 0,
204
208
  quorum,
205
209
  signers,
206
- prologue: (flags & 2) === 0 ? null : prologue.decode(state)
210
+ prologue: (flags & 2) === 0 ? null : prologue.decode(state),
211
+ unencrypted
207
212
  }
208
213
  }
209
214
  }
@@ -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/lib/verifier.js CHANGED
@@ -200,7 +200,8 @@ module.exports = class Verifier {
200
200
  allowPatch: !!inp.allowPatch,
201
201
  quorum: defaultQuorum(inp),
202
202
  signers: inp.signers ? inp.signers.map(parseSigner) : [],
203
- prologue: null
203
+ prologue: null,
204
+ unencrypted: !!inp.unencrypted
204
205
  }
205
206
 
206
207
  if (inp.hash && inp.hash !== 'blake2b') throw BAD_ARGUMENT('Only Blake2b hashes are supported')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.0.11",
3
+ "version": "11.0.13",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {