Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

add encryption option #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module.exports = class Hyperdrive extends ReadyResource {
this._batching = !!(opts._checkout === null && opts._db)
this._checkout = opts._checkout || null

this._encryptionKey = opts.encryptionKey

this.ready().catch(safetyCatch)
}

Expand Down Expand Up @@ -128,7 +130,8 @@ module.exports = class Hyperdrive extends ReadyResource {
const blobsCore = this.corestore.get({
key: blobsKey,
cache: false,
onwait: this._onwait
onwait: this._onwait,
encryptionKey: this._encryptionKey
})
await blobsCore.ready()

Expand Down Expand Up @@ -158,7 +161,8 @@ module.exports = class Hyperdrive extends ReadyResource {
const blobsCore = this.corestore.get({
name: 'blobs',
cache: false,
onwait: this._onwait
onwait: this._onwait,
encryptionKey: this._encryptionKey
})
await blobsCore.ready()

Expand Down Expand Up @@ -545,7 +549,7 @@ function shallowReadStream (files, folder, keys) {

function makeBee (key, corestore, opts) {
const name = key ? undefined : 'db'
const core = corestore.get({ key, name, cache: true, exclusive: true, onwait: opts.onwait })
const core = corestore.get({ key, name, cache: true, exclusive: true, onwait: opts.onwait, encryptionKey: opts.encryptionKey })

return new Hyperbee(core, {
keyEncoding: 'utf-8',
Expand Down
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,35 @@ test('non-existing follow entry', async function (t) {
await drive.close()
})

test('encryptionKey', async function (t) {
const encryptionKey = Buffer.alloc(32).fill(0)

const storage = createTmpDir(t)

const store = new Corestore(storage)
const writer = new Hyperdrive(store, { encryptionKey })

await writer.ready()
t.alike(writer.core.encryptionKey, encryptionKey)
t.alike(writer.blobs.core.encryptionKey, encryptionKey)

await writer.close()

{
const store = new Corestore(storage)

const reader = new Hyperdrive(store, writer.key, { encryptionKey })

await reader.ready()
t.alike(reader.core.encryptionKey, encryptionKey)

await reader.getBlobs()
t.alike(reader.blobs.core.encryptionKey, encryptionKey)

await reader.close()
}
})

async function testenv (teardown) {
const corestore = new Corestore(RAM)
await corestore.ready()
Expand Down