holepunchto / hyperdrive

Hyperdrive is a secure, real time distributed file system
Apache License 2.0
1.86k stars 135 forks source link

Hypercode v10 doesn't pass secretKey correctly #255

Closed mjp0 closed 4 years ago

mjp0 commented 4 years ago

I'm testing with hypercore-8 branch and I ran into a regression that passing secretKey in opts doesn't produce writable hyperdrive anymore.

Weird thing is that provide keypair test in test/basic.js works fine, but when you copy/paste the test code as a normal js file, it fails and shows the problem.

This code fails for me with Not writable error:

const sodium = require('sodium-universal')
const ram = require('random-access-memory')
const hyperdrive = require('hyperdrive')

function create(key, opts) {
  if (key && !(key instanceof Buffer)) {
    opts = key
    key = null
  }
  return hyperdrive((opts && opts.corestore) || ram, key, opts)
}

var publicKey = Buffer.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = Buffer.allocUnsafe(sodium.crypto_sign_SECRETKEYBYTES)

sodium.crypto_sign_keypair(publicKey, secretKey)

var drive = create(publicKey, { secretKey: secretKey })

drive.on('ready', function () {
  t.ok(drive.writable)
  t.ok(drive.metadata.writable)
  t.ok(publicKey.equals(drive.key))

  drive.writeFile('/hello.txt', 'world', function (err) {
    t.error(err, 'no error')
    drive.readFile('/hello.txt', function (err, buf) {
      t.error(err, 'no error')
      t.same(buf, Buffer.from('world'))
      t.end()
    })
  })
})

P.S. Yes, I know there's tape's t method there but it doesn't matter because the code doesn't pass drive.on('ready').

mjp0 commented 4 years ago

Solved this. The test above was from master branch and not hypercore-8. In hypercore-8 you apparently need to pass keyPair from hypercore-crypto to opts and plain secretKey doesn't work anymore.

mafintosh commented 4 years ago

That's most likely a regression. Will take a look on Monday, but good you found a workaround.