Skip to content

Commit

Permalink
add get db key as a static method (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas authored Aug 27, 2024
1 parent 53b4980 commit ccc96d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ module.exports = class Hyperdrive extends ReadyResource {
return this.entries()[Symbol.asyncIterator]()
}

static async getDriveKey (corestore) {
const core = makeBee(undefined, corestore)
await core.ready()
const key = core.key
await core.close()
return key
}

static getContentKey (m, key) {
if (m instanceof Hypercore) {
if (m.core.compat) return null
Expand Down Expand Up @@ -634,7 +642,7 @@ function shallowReadStream (files, folder, keys) {
})
}

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

Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,22 @@ test('truncate throws when truncating future version)', async t => {
)
})

test('get drive key without using the constructor', async (t) => {
t.plan(1)
const corestore = new Corestore(RAM.reusable())
const key = await Hyperdrive.getDriveKey(corestore.session())
const drive = new Hyperdrive(corestore.session())

t.teardown(() => {
corestore.close()
drive.close()
})

await drive.ready()

t.is(key.toString('hex'), drive.key.toString('hex'))
})

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

0 comments on commit ccc96d4

Please sign in to comment.