Skip to content
Open
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
33 changes: 32 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
48 changes: 48 additions & 0 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Comment on lines +1822 to +1824

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test misses unsaved manifests

The “manifest persisted” check can pass even if nothing was saved. again.ready() runs recovery again when storage still has no manifest, so removing the recovery write would leave this assertion green. Check the stored getAuth() result after the first recovery, or reopen with read-only storage so recovery cannot hide a missing write.

Prompt To Fix With AI
This is a comment left during a code review.
Path: test/manifest.js
Line: 1822-1824

Comment:
**Test misses unsaved manifests**

The “manifest persisted” check can pass even if nothing was saved. `again.ready()` runs recovery again when storage still has no manifest, so removing the recovery write would leave this assertion green. Check the stored `getAuth()` result after the first recovery, or reopen with read-only storage so recovery cannot hide a missing write.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

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()
}
Loading