From 865dd413adb8e21223facfcb88197c54c93b863c Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Fri, 13 Nov 2026 11:28:15 +0000 Subject: [PATCH] backfill missing compat manifest on open Cores migrated from hypercore 10 storage can have blocks but no stored manifest. On open, if the stored tree signature verifies against the default signer manifest for the key, persist that manifest. Co-Authored-By: Claude Opus 5.5 --- lib/core.js | 33 ++++++++++++++++++++++++++++++++- test/manifest.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/lib/core.js b/lib/core.js index 6c1b5353..4b796359 100644 --- a/lib/core.js +++ b/lib/core.js @@ -3,7 +3,7 @@ const b4a = require('b4a') const unslab = require('unslab') const z32 = require('z32') const Mutex = require('./mutex') -const { MerkleTree, ReorgBatch } = require('./merkle-tree') +const { MerkleTree, MerkleTreeBatch, ReorgBatch } = require('./merkle-tree') const BitInterlude = require('./bit-interlude') const Bitfield = require('./bitfield') const RemoteBitfield = require('./remote-bitfield') @@ -348,6 +348,23 @@ module.exports = class Core { } } + if (!header.manifest && !storage.readOnly) { + const manifest = recoverCompatManifest(header, treeInfo) + if (manifest) { + header.manifest = manifest + compat = true + + const tx = storage.write() + tx.setAuth({ + key: header.key, + discoveryKey: crypto.discoveryKey(header.key), + manifest: header.manifest, + keyPair: header.keyPair + }) + await tx.flush() + } + } + const verifier = header.manifest ? new Verifier(header.key, header.manifest, { crypto, legacy }) : null @@ -1101,3 +1118,17 @@ async function getCoreInfo(storage) { group: groupInfo } } + +function recoverCompatManifest(header, treeInfo) { + if (!treeInfo.length || !treeInfo.signature || !treeInfo.roots.length) return null + + const manifest = Verifier.defaultSignerManifest(header.key) + const batch = new MerkleTreeBatch(treeInfo) + + for (const legacy of [false, true]) { + const verifier = new Verifier(header.key, manifest, { legacy }) + if (verifier.verify(batch, treeInfo.signature)) return manifest + } + + return null +} diff --git a/test/manifest.js b/test/manifest.js index a0718345..d5ff8728 100644 --- a/test/manifest.js +++ b/test/manifest.js @@ -1802,3 +1802,51 @@ async function partialCoreSignature(core, s, len) { const proof = await partialSignature(s.core, index, len, s.core.state.length, sig) return proof } + +test('missing compat manifest is recovered from the stored signature', async function (t) { + const open = await createStored(t) + + const core = await open({ compat: true }) + await core.append(['a', 'b', 'c']) + await dropManifest(core) + const key = core.key + await core.close() + + const reopened = await open(key) + await reopened.ready() + + t.alike(reopened.manifest, Verifier.defaultSignerManifest(key)) + t.alike(await reopened.get(2), b4a.from('c')) + await reopened.close() + + const again = await open(key) + await again.ready() + t.ok(again.core.header.manifest, 'manifest persisted') + await again.close() +}) + +test('missing non-compat manifest is not recovered', async function (t) { + const open = await createStored(t) + const keyPair = crypto.keyPair() + + const core = await open({ + keyPair, + manifest: { version: 1, signers: [{ publicKey: keyPair.publicKey }] } + }) + await core.append(['a', 'b', 'c']) + await dropManifest(core) + const key = core.key + await core.close() + + const reopened = await open(key) + await reopened.ready() + + t.is(reopened.manifest, null) + await reopened.close() +}) + +async function dropManifest(core) { + const tx = core.core.storage.write() + tx.setAuth({ key: core.key, discoveryKey: core.discoveryKey, manifest: null, keyPair: null }) + await tx.flush() +}