Fix session encryption
My previous pull request got `this` and `s` mixed up
MODIFY
index.js
index.js
@@ -195,10 +195,10 @@ module.exports = class Hypercore extends EventEmitter {
// Only override the block encryption if its either not already set or if
// the caller provided a different key.
if (
- !this.encryption ||
- !b4a.equals(this.encryption.key, opts.encryptionKey)
+ !s.encryption ||
+ !b4a.equals(s.encryption.key, opts.encryptionKey)
) {
- this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
+ s.encryption = new BlockEncryption(opts.encryptionKey, s.key)
}
}

MODIFY
test/encryption.js
test/encryption.js
@@ -126,6 +126,9 @@ test('encrypted session on unencrypted core', async function (t) {
const a = await create()
const s = a.session({ encryptionKey })
+ t.alike(s.encryptionKey, encryptionKey)
+ t.unlike(s.encryptionKey, a.encryptionKey)
+
await s.append(['hello'])
const unencrypted = await s.get(0)
@@ -135,10 +138,25 @@ test('encrypted session on unencrypted core', async function (t) {
t.absent(encrypted.includes('hello'))
})
+test('encrypted session on encrypted core, same key', async function (t) {
+ const a = await create({ encryptionKey })
+ const s = a.session({ encryptionKey })
+
+ t.alike(s.encryptionKey, a.encryptionKey)
+
+ await s.append(['hello'])
+
+ const unencrypted = await s.get(0)
+ t.alike(unencrypted, Buffer.from('hello'))
+ t.alike(unencrypted, await a.get(0))
+})
+
test('encrypted session on encrypted core, different keys', async function (t) {
const a = await create({ encryptionKey: Buffer.alloc(32, 'a') })
const s = a.session({ encryptionKey: Buffer.alloc(32, 's') })
+ t.unlike(s.encryptionKey, a.encryptionKey)
+
await s.append(['hello'])
const unencrypted = await s.get(0)

Fix session encryption (#101)

My previous pull request got `this` and `s` mixed up