Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage stores first opened discovery key as default #12

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const TL = {
LOCAL_SEED: 1,
DKEYS: 2,
CORE: 3,
DATA: 4
DATA: 4,
DEFAULT_KEY: 5
}

// core prefixes
Expand Down Expand Up @@ -276,6 +277,7 @@ class HypercoreStorage {
constructor (db, mutex, discoveryKey) {
this.db = db
this.mutex = mutex

this.discoveryKey = discoveryKey

// pointers
Expand All @@ -284,15 +286,22 @@ class HypercoreStorage {
}

async open () {
if (!this.discoveryKey) {
const discoveryKey = await getDefaultKey(this.db)
if (!discoveryKey) throw new Error('No discovery key was provided')

this.discoveryKey = discoveryKey
}

const val = await this.db.get(encodeDiscoveryKey(this.discoveryKey))
if (val === null) return false
if (val === null) return null

const { core, data } = c.decode(m.CorePointer, val)

this.corePointer = core
this.dataPointer = data

return true
return this.getCoreInfo()
}

async create ({ key, manifest, keyPair, encryptionKey }) {
Expand All @@ -306,8 +315,16 @@ class HypercoreStorage {
return false
}

if (!key) throw new Error('No key was provided')

let info = await getStorageInfo(this.db)

const write = this.db.write()
const info = (await getStorageInfo(this.db)) || { free: 0, total: 0 }

if (!info) {
write.tryPut(Buffer.from([TL.DEFAULT_KEY]), this.discoveryKey)
info = { free: 0, total: 0 }
}

const core = info.total++
const data = info.free++
Expand Down Expand Up @@ -430,6 +447,24 @@ class HypercoreStorage {
return p
}

async getCoreInfo () {
const r = this.createReadBatch()

const auth = r.getCoreAuth()
const localKeyPair = r.getLocalKeyPair()
const encryptionKey = r.getEncryptionKey()
const head = r.getCoreHead()

await r.flush()

return {
auth: await auth,
localKeyPair: await localKeyPair,
encryptionKey: await encryptionKey,
head: await head
}
}

getBitfieldPage (index) {
const b = this.createReadBatch()
const p = b.getBitfieldPage(index)
Expand Down Expand Up @@ -486,6 +521,10 @@ function mapOnlyDiscoveryKey (data) {
return data.key.subarray(1)
}

async function getDefaultKey (db) {
return db.get(Buffer.from([TL.DEFAULT_KEY]))
}

async function getStorageInfo (db) {
const value = await db.get(Buffer.from([TL.STORAGE_INFO]))
if (value === null) return null
Expand Down
34 changes: 33 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,38 @@ test('user data', async function (t) {
t.alike(await c.getUserData('hej'), Buffer.from('verden'))
})

test('reopen default core', async function (t) {
const dir = await tmp(t)

const manifest = Buffer.from('manifest')
const keyPair = {
publicKey: Buffer.alloc(32, 2),
secretKey: Buffer.alloc(32, 3)
}
const encryptionKey = Buffer.alloc(32, 4)

const s1 = await getStorage(t, dir)
const c1 = s1.get(DK_0)
if (!(await c1.open())) await c1.create({ key: DK_1, manifest, keyPair, encryptionKey })

await s1.close()

const s2 = await getStorage(t, dir)
const c2 = s2.get()

t.alike(await c2.open(), {
auth: {
key: DK_1,
manifest
},
localKeyPair: keyPair,
encryptionKey,
head: null
})

t.alike(c2.discoveryKey, DK_0)
})

async function getStorage (t, dir) {
if (!dir) dir = await tmp(t)
const s = new CoreStorage(dir)
Expand All @@ -609,7 +641,7 @@ async function getCore (t) {
const s = await getStorage(t)
const c = s.get(DK_0)

t.is(await c.open(), false)
t.is(await c.open(), null)
await c.create({ key: DK_0 })

return c
Expand Down
Loading