From da3b4d7393dc2f895f9a8a237a5c168a8fa5e57c Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Tue, 27 Feb 2024 15:05:37 +0000 Subject: [PATCH] add helper for generating content key --- index.js | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index d16e9b2a..938b551b 100644 --- a/index.js +++ b/index.js @@ -43,25 +43,23 @@ 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 + static getContentKey (m, key) { + if (m instanceof Hypercore) { + if (m.core.compat) return null + return Hyperdrive.getContentKey(m.manifest, m.key) + } - const signers = [] + const manifest = generateContentManifest(m, key) + if (!manifest) return null - for (const s of m.signers) { - const namespace = crypto.hash([BLOBS, this.core.key, s.namespace]) - signers.push({ ...s, namespace }) - } + return Hypercore.key(manifest) + } - return { - version: m.version, - hash: 'blake2b', - allowPatch: m.allowPatch, - quorum: m.quorum, - signers, - prologue: null // TODO: could be configurable through the header still... - } + _generateBlobsManifest () { + const m = this.db.core.manifest + if (this.db.core.core.compat) return null + + return generateContentManifest(m, this.core.key) } get id () { @@ -625,3 +623,25 @@ function prefixRange (name, prev = '/') { // '0' is binary +1 of / return { gt: name + prev, lt: name + '0' } } + +function generateContentManifest (m, key) { + if (m.version < 1) return null + + const signers = [] + + if (!key) key = Hypercore.key(m) + + for (const s of m.signers) { + const namespace = crypto.hash([BLOBS, 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... + } +}