hypercore 10.0.0-alpha.8 → 10.0.0-alpha.9

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
@@ -394,12 +394,13 @@ module.exports = class Hypercore extends EventEmitter {
394
394
 
395
395
  if (this.core.bitfield.get(index)) {
396
396
  block = await this.core.blocks.get(index)
397
+ if (this.encryption) this.encryption.decrypt(index, block)
397
398
  } else {
398
399
  if (opts && opts.onwait) opts.onwait(index)
400
+ // Note that the _oncoreupdate handler decrypts inplace so we should not decrypt here
399
401
  block = await this.replicator.requestBlock(index)
400
402
  }
401
403
 
402
- if (this.encryption) this.encryption.decrypt(index, block)
403
404
  return this._decode(encoding, block)
404
405
  }
405
406
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.8",
3
+ "version": "10.0.0-alpha.9",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,4 +1,6 @@
1
1
  const test = require('brittle')
2
+ const RAM = require('random-access-memory')
3
+ const Hypercore = require('..')
2
4
  const { create, replicate } = require('./helpers')
3
5
 
4
6
  const encryptionKey = Buffer.alloc(32, 'hello world')
@@ -14,10 +16,10 @@ test('encrypted append and get', async function (t) {
14
16
  t.is(a.core.tree.byteLength, 5 + a.padding)
15
17
 
16
18
  const unencrypted = await a.get(0)
17
- const encrypted = await a.core.blocks.get(0)
18
-
19
19
  t.alike(unencrypted, Buffer.from('hello'))
20
- t.unlike(unencrypted, encrypted)
20
+
21
+ const encrypted = await a.core.blocks.get(0)
22
+ t.absent(encrypted.includes('hello'))
21
23
  })
22
24
 
23
25
  test('encrypted seek', async function (t) {
@@ -74,12 +76,46 @@ test('encrypted replication', async function (t) {
74
76
  })
75
77
  })
76
78
 
77
- test('encrypted sessions', async function (t) {
79
+ test('encrypted session', async function (t) {
78
80
  const a = await create({ encryptionKey })
79
81
 
80
82
  await a.append(['hello'])
81
83
 
82
- const session = a.session()
84
+ const s = a.session()
85
+
86
+ t.alike(a.encryptionKey, s.encryptionKey)
87
+ t.alike(await s.get(0), Buffer.from('hello'))
88
+
89
+ await s.append(['world'])
90
+
91
+ const unencrypted = await s.get(1)
92
+ t.alike(unencrypted, Buffer.from('world'))
93
+ t.alike(await a.get(1), unencrypted)
94
+
95
+ const encrypted = await s.core.blocks.get(1)
96
+ t.absent(encrypted.includes('world'))
97
+ t.alike(await a.core.blocks.get(1), encrypted)
98
+ })
99
+
100
+ test('encrypted session before ready core', async function (t) {
101
+ const a = new Hypercore(RAM, { encryptionKey })
102
+ const s = a.session()
103
+
104
+ await a.ready()
105
+
106
+ t.alike(a.encryptionKey, s.encryptionKey)
107
+
108
+ await a.append(['hello'])
109
+ t.alike(await s.get(0), Buffer.from('hello'))
110
+ })
111
+
112
+ test('replicating encrypted block returns expected data', async function (t) {
113
+ const a = await create({ encryptionKey })
114
+ const b = await create(a.key, { encryptionKey })
115
+
116
+ replicate(a, b, t)
83
117
 
84
- t.alike(await session.get(0), Buffer.from('hello'))
118
+ await a.append('hej')
119
+ const blk = await b.get(0)
120
+ t.alike(blk, Buffer.from('hej'), 'freshly replicated block is decrypted')
85
121
  })