hypercore 11.2.0 → 11.3.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.
Files changed (2) hide show
  1. package/index.js +69 -35
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -35,6 +35,37 @@ const inspect = Symbol.for('nodejs.util.inspect.custom')
35
35
  // but we enforce 15mb to ensure smooth replication (each block is transmitted atomically)
36
36
  const MAX_SUGGESTED_BLOCK_SIZE = 15 * 1024 * 1024
37
37
 
38
+ class DefaultEncryption extends HypercoreEncryption {
39
+ constructor (encryptionKey, isBlock, opts) {
40
+ super(opts)
41
+
42
+ this.encryptionKey = encryptionKey
43
+ this.isBlock = !!isBlock
44
+
45
+ this.blockKey = null
46
+ }
47
+
48
+ _init (context) {
49
+ this.blockKey = getLegacyBlockKey(context.key, this.encryptionKey, context.compat, this.isBlock)
50
+ }
51
+
52
+ _getBlockKey (id, context) {
53
+ if (!this.blockKey) this._init(context)
54
+
55
+ return {
56
+ id: 0,
57
+ version: context.manifest.version <= 1 ? 0 : 1,
58
+ key: this.blockKey
59
+ }
60
+ }
61
+
62
+ _getBlindingKey (context) {
63
+ if (!this.blockKey) this._init(context)
64
+
65
+ return crypto.hash(this.blockKey)
66
+ }
67
+ }
68
+
38
69
  class Hypercore extends EventEmitter {
39
70
  constructor (storage, key, opts) {
40
71
  super()
@@ -235,7 +266,7 @@ class Hypercore extends EventEmitter {
235
266
  }
236
267
 
237
268
  setEncryptionKey (encryptionKey, opts) {
238
- const encryption = this._getLegacyEncryption(encryptionKey, !!(opts && opts.block))
269
+ const encryption = this._getEncryptionProvider(encryptionKey, !!(opts && opts.block))
239
270
  return this.setEncryption(encryption, opts)
240
271
  }
241
272
 
@@ -247,7 +278,7 @@ class Hypercore extends EventEmitter {
247
278
  return
248
279
  }
249
280
 
250
- if (!HypercoreEncryption.isHypercoreEncryption(encryption)) {
281
+ if (!(encryption instanceof HypercoreEncryption)) {
251
282
  throw new Error('Expected hypercore encryption provider')
252
283
  }
253
284
 
@@ -328,13 +359,12 @@ class Hypercore extends EventEmitter {
328
359
 
329
360
  if (this.keyPair === null) this.keyPair = opts.keyPair || this.core.header.keyPair
330
361
 
331
- if (!this.core.encryption) {
332
- const e = getEncryptionOption(opts)
333
-
334
- if (HypercoreEncryption.isHypercoreEncryption(e)) {
362
+ const e = getEncryptionOption(opts)
363
+ if (!this.core.encryption && e) {
364
+ if (e instanceof HypercoreEncryption) {
335
365
  this.core.encryption = e
336
- } else if (e) {
337
- this.core.encryption = this._getLegacyEncryption(e.key, e.block)
366
+ } else {
367
+ this.core.encryption = this._getEncryptionProvider(e.key, e.block)
338
368
  }
339
369
  }
340
370
 
@@ -592,12 +622,16 @@ class Hypercore extends EventEmitter {
592
622
  return this.state.fork
593
623
  }
594
624
 
595
- get peers () {
596
- return this.opened === false ? [] : this.core.replicator.peers
625
+ get padding () {
626
+ if (this.encryption && this.key && this.manifest) {
627
+ return this.encryption.padding(this.core)
628
+ }
629
+
630
+ return 0
597
631
  }
598
632
 
599
- get padding () {
600
- return this.encryption === null ? 0 : this.encryption.padding
633
+ get peers () {
634
+ return this.opened === false ? [] : this.core.replicator.peers
601
635
  }
602
636
 
603
637
  get globalCache () {
@@ -700,6 +734,18 @@ class Hypercore extends EventEmitter {
700
734
  if (this.opened === false) await this.opening
701
735
  if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid')
702
736
 
737
+ const activeRequests = (opts && opts.activeRequests) || this.activeRequests
738
+
739
+ if (this.encryption && !this.core.manifest) {
740
+ const req = this.replicator.addUpgrade(activeRequests)
741
+ try {
742
+ await req.promise
743
+ } catch (err) {
744
+ if (isSessionMoved(err)) return this.seek(bytes, opts)
745
+ throw err
746
+ }
747
+ }
748
+
703
749
  const s = MerkleTree.seek(this.state, bytes, this.padding)
704
750
 
705
751
  const offset = await s.update()
@@ -709,7 +755,6 @@ class Hypercore extends EventEmitter {
709
755
 
710
756
  if (!this._shouldWait(opts, this.wait)) return null
711
757
 
712
- const activeRequests = (opts && opts.activeRequests) || this.activeRequests
713
758
  const req = this.core.replicator.addSeek(activeRequests, s)
714
759
 
715
760
  const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
@@ -772,9 +817,7 @@ class Hypercore extends EventEmitter {
772
817
  // Copy the block as it might be shared with other sessions.
773
818
  block = b4a.from(block)
774
819
 
775
- if (this.encryption.compat !== this.core.compat) this._updateEncryption()
776
-
777
- await this.encryption.decrypt(index, block)
820
+ await this.encryption.decrypt(index, block, this.core)
778
821
  }
779
822
 
780
823
  return this._decode(encoding, block)
@@ -923,7 +966,7 @@ class Hypercore extends EventEmitter {
923
966
  blocks = Array.isArray(blocks) ? blocks : [blocks]
924
967
 
925
968
  const preappend = this.encryption && this._preappend
926
- if (preappend) await this.encryption.ready()
969
+ await this._ensureEncryption()
927
970
 
928
971
  const buffers = this.encodeBatch !== null ? this.encodeBatch(blocks) : new Array(blocks.length)
929
972
 
@@ -1052,23 +1095,14 @@ class Hypercore extends EventEmitter {
1052
1095
  return block
1053
1096
  }
1054
1097
 
1055
- _updateEncryption () {
1056
- const e = this.encryption
1057
- if (HypercoreEncryption.isHypercoreEncryption(e)) return
1058
-
1059
- this.encryption = this._getLegacyEncryption(e.key, e.block)
1060
-
1061
- if (e === this.core.encryption) this.core.encryption = this.encryption
1098
+ _ensureEncryption () {
1099
+ if (!this.encryption) return
1100
+ if (this.encryption.version === -1) return this.encryption.load(-1, this.core)
1062
1101
  }
1063
1102
 
1064
- _getLegacyEncryption (encryptionKey, block) {
1103
+ _getEncryptionProvider (encryptionKey, block) {
1065
1104
  if (!encryptionKey) return null
1066
-
1067
- const blockKey = block
1068
- ? encryptionKey
1069
- : getLegacyBlockKey(this.key, encryptionKey, this.core.compat)
1070
-
1071
- return HypercoreEncryption.createLegacyProvider(blockKey)
1105
+ return new DefaultEncryption(encryptionKey, block)
1072
1106
  }
1073
1107
  }
1074
1108
 
@@ -1086,10 +1120,8 @@ async function preappend (blocks) {
1086
1120
  const offset = this.state.length
1087
1121
  const fork = this.state.encryptionFork
1088
1122
 
1089
- if (this.encryption.compat !== this.core.compat) this._updateEncryption()
1090
-
1091
1123
  for (let i = 0; i < blocks.length; i++) {
1092
- await this.encryption.encrypt(offset + i, blocks[i], fork)
1124
+ await this.encryption.encrypt(offset + i, blocks[i], fork, this.core)
1093
1125
  }
1094
1126
  }
1095
1127
 
@@ -1157,7 +1189,9 @@ function getEncryptionOption (opts) {
1157
1189
  return b4a.isBuffer(opts.encryption) ? { key: opts.encryption } : opts.encryption
1158
1190
  }
1159
1191
 
1160
- function getLegacyBlockKey (hypercoreKey, encryptionKey, compat) {
1192
+ function getLegacyBlockKey (hypercoreKey, encryptionKey, compat, isBlock) {
1193
+ if (isBlock) return encryptionKey
1194
+
1161
1195
  const key = b4a.alloc(HypercoreEncryption.KEYBYTES)
1162
1196
 
1163
1197
  if (compat) sodium.crypto_generichash_batch(key, [encryptionKey], hypercoreKey)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.2.0",
3
+ "version": "11.3.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,7 +50,7 @@
50
50
  "fast-fifo": "^1.3.0",
51
51
  "flat-tree": "^1.9.0",
52
52
  "hypercore-crypto": "^3.2.1",
53
- "hypercore-encryption": "^1.0.0",
53
+ "hypercore-encryption": "^2.0.0",
54
54
  "hypercore-errors": "^1.2.0",
55
55
  "hypercore-id-encoding": "^1.2.0",
56
56
  "hypercore-storage": "^1.0.0",