hypercore-protocol / corestore-next

Easily manage storage/replication for large collections of Hypercores
10 stars 7 forks source link

Failing unit tests for opening an encrypted core from storage #16

Closed Nuhvi closed 2 years ago

Nuhvi commented 2 years ago

I don't know how to fix this, but this unit test will fail even with hypercore@10.0.0-alpha40

test('reopen encrypted core with correct options if it is already opened through replication', async function (t) {
  // Create an encrypted drive
  const originStore = new Corestore(tmpdir())
  const encryptionKey = randomBytes(32)
  const origin = originStore.get({ name: 'origin', encryptionKey })
  await origin.ready()

  const originContent = b4a.from(JSON.stringify({ foo: 'bar' }))
  await origin.append(originContent)

  const coreOpts = {
    key: origin.key,
    encryptionKey: origin.encryptionKey
  }

  t.is(coreOpts.key.length, 32)
  t.is(coreOpts.encryptionKey, encryptionKey)

  const cloneDir = tmpdir()
  // Clone to storage then close cloneStore
  await openClone()
  // open clone from storage
  await openClone()

  async function openClone () {
    const cloneStore = new Corestore(cloneDir)
    const s = cloneStore.replicate(true)
    s.pipe(originStore.replicate(false)).pipe(s)

    // Ensure that the clone is opened by discoveryKey (no options)
    await cloneStore.ready()

    const clone = cloneStore.get(coreOpts)
    await clone.ready()

    t.is(clone.encryptionKey, encryptionKey, 'encryptionKey is missing')

    const cloneContent = await clone.get(0)
    t.is(cloneContent.toString(), originContent.toString())

    await cloneStore.close()
  }
})
Nuhvi commented 2 years ago

I think the issue here is that sessions are used for replication with different options, but they aren't really returned to the user through store.get() and they kinda can't without converting store.get() to an async function to do await core.ready(); return core.session(opts)

Nuhvi commented 2 years ago

I found the issue and will open a PR in Hypercore in a minute