Skip to content

Commit bee72e6

Browse files
Set manifest if not present in header (#486)
* add test for opening existing core with manifest * set manifest if provided and not present in header
1 parent de55e09 commit bee72e6

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/core.js

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ module.exports = class Core {
124124
if (!opts.key && !Verifier.isValidManifest(header.key, Verifier.createManifest(opts.manifest))) {
125125
throw STORAGE_CONFLICT('Manifest does not hash to provided key')
126126
}
127+
128+
if (!header.manifest) header.manifest = opts.manifest
127129
}
128130

129131
if (opts.key && !b4a.equals(header.key, opts.key)) {

test/manifest.js

+43
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,49 @@ test('manifest encoding', t => {
14121412
}
14131413
})
14141414

1415+
test('create verifier - open existing core with manifest', async function (t) {
1416+
const keyPair = crypto.keyPair()
1417+
1418+
const manifest = Verifier.createManifest({
1419+
quorum: 1,
1420+
signers: [{
1421+
signature: 'ed25519',
1422+
publicKey: keyPair.publicKey
1423+
}]
1424+
})
1425+
1426+
const key = Verifier.manifestHash(manifest)
1427+
1428+
const storage = ram.reusable()
1429+
const core = new Hypercore(storage, key, { compat: false })
1430+
await core.ready()
1431+
1432+
t.is(core.manifest, null)
1433+
t.is(core.core.header.manifest, null)
1434+
t.alike(core.key, key)
1435+
1436+
await core.close()
1437+
1438+
manifest.signers[0].publicKey = b4a.alloc(32, 0)
1439+
1440+
const wrongCore = new Hypercore(storage, null, { manifest, compat: false })
1441+
await t.exception(wrongCore.ready(), /STORAGE_CONFLICT/)
1442+
1443+
manifest.signers[0].publicKey = keyPair.publicKey
1444+
1445+
const manifestCore = new Hypercore(storage, null, { manifest, compat: false })
1446+
await manifestCore.ready()
1447+
1448+
t.not(manifestCore.manifest, null)
1449+
t.not(manifestCore.core.header.manifest, null)
1450+
t.alike(manifestCore.key, key)
1451+
1452+
await manifestCore.close()
1453+
1454+
const compatCore = new Hypercore(storage, null, { manifest, compat: true })
1455+
await t.execution(compatCore.ready()) // compat flag is unset internally
1456+
})
1457+
14151458
function createMultiManifest (signers, prologue = null) {
14161459
return {
14171460
hash: 'blake2b',

0 commit comments

Comments
 (0)