Skip to content

Commit

Permalink
v1 cores generate the blob store from the manifest (#366)
Browse files Browse the repository at this point in the history
* v1 cores generate the blob store from the manifest

* needs hypercore also

* add missing fields to the generated manifest
  • Loading branch information
mafintosh authored Feb 13, 2024
1 parent 983210e commit 1afd22a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 36 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const MirrorDrive = require('mirror-drive')
const SubEncoder = require('sub-encoder')
const ReadyResource = require('ready-resource')
const safetyCatch = require('safety-catch')
const crypto = require('hypercore-crypto')
const Hypercore = require('hypercore')
const { BLOCK_NOT_AVAILABLE } = require('hypercore-errors')

const keyEncoding = new SubEncoder('files', 'utf-8')

const [BLOBS] = crypto.namespace('hyperdrive', 1)

module.exports = class Hyperdrive extends ReadyResource {
constructor (corestore, key, opts = {}) {
super()
Expand Down Expand Up @@ -39,6 +43,27 @@ module.exports = class Hyperdrive extends ReadyResource {
return this.entries()[Symbol.asyncIterator]()
}

_generateBlobsManifest () {
const m = this.db.core.manifest
if (m.version < 1 || this.db.core.core.compat) return null

const signers = []

for (const s of m.signers) {
const namespace = crypto.hash([BLOBS, this.core.key, s.namespace])
signers.push({ ...s, namespace })
}

return {
version: m.version,
hash: 'blake2b',
allowPatch: m.allowPatch,
quorum: m.quorum,
signers,
prologue: null // TODO: could be configurable through the header still...
}
}

get id () {
return this.core.id
}
Expand Down Expand Up @@ -126,14 +151,16 @@ module.exports = class Hyperdrive extends ReadyResource {

if (this.blobs) return true

const blobsKey = header.metadata && header.metadata.contentFeed.subarray(0, 32)
const contentKey = header.metadata && header.metadata.contentFeed && header.metadata.contentFeed.subarray(0, 32)
const blobsKey = contentKey || Hypercore.key(this._generateBlobsManifest())
if (!blobsKey || blobsKey.length < 32) throw new Error('Invalid or no Blob store key set')

const blobsCore = this.corestore.get({
key: blobsKey,
cache: false,
onwait: this._onwait,
encryptionKey: this.encryptionKey
encryptionKey: this.encryptionKey,
keyPair: (!contentKey && this.db.core.writable) ? this.db.core.keyPair : null
})
await blobsCore.ready()

Expand All @@ -160,17 +187,21 @@ module.exports = class Hyperdrive extends ReadyResource {
await this._openBlobsFromHeader({ wait: false })

if (this.db.core.writable && !this.blobs) {
const m = this._generateBlobsManifest()
const blobsCore = this.corestore.get({
name: this.db.core.id + '/blobs', // simple trick to avoid blobs clashing if no namespace is provided...
manifest: m,
name: m ? null : this.db.core.id + '/blobs', // simple trick to avoid blobs clashing if no namespace is provided...
cache: false,
onwait: this._onwait,
encryptionKey: this.encryptionKey,
compat: this.db.core.core.compat
compat: this.db.core.core.compat,
keyPair: (m && this.db.core.writable) ? this.db.core.keyPair : null
})
await blobsCore.ready()

this.blobs = new Hyperblobs(blobsCore)
getBee(this.db).metadata.contentFeed = this.blobs.core.key

if (!m) getBee(this.db).metadata.contentFeed = this.blobs.core.key

this.emit('blobs', this.blobs)
this.emit('content-key', blobsCore.key)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"hyperbee": "^2.11.1",
"hyperblobs": "^2.3.0",
"hypercore": "^10.33.0",
"hypercore-errors": "^1.0.0",
"is-options": "^1.0.2",
"mirror-drive": "^1.2.0",
Expand All @@ -36,7 +37,7 @@
"b4a": "^1.6.0",
"brittle": "^3.1.0",
"corestore": "^6.8.1",
"hypercore-crypto": "^3.2.1",
"hypercore-crypto": "^3.4.0",
"hyperdht": "^6.6.0",
"hyperswarm": "^4.0.0",
"random-access-memory": "^6.0.0",
Expand Down

0 comments on commit 1afd22a

Please sign in to comment.