diff --git a/lib/core.js b/lib/core.js index 606b069e..c06343bc 100644 --- a/lib/core.js +++ b/lib/core.js @@ -194,7 +194,7 @@ module.exports = class Core { } } - const keyPair = opts.keyPair || (opts.key ? null : crypto.keyPair()) + const keyPair = opts.keyPair || (opts.key || opts.manifest ? null : crypto.keyPair()) const defaultManifest = !opts.manifest && diff --git a/test/manifest.js b/test/manifest.js index 0ab7406c..26cf7bca 100644 --- a/test/manifest.js +++ b/test/manifest.js @@ -1518,21 +1518,55 @@ test('manifest - persist if manifest is updated', async function (t) { } }) -test('manifest - invalid signature fails', async function (t) { - const core = await create(t, { compat: false }) +test('manifest - writable', async function (t) { + const keyPair = crypto.keyPair() + + const manifest = Verifier.createManifest({ + quorum: 1, + signers: [ + { + signature: 'ed25519', + publicKey: keyPair.publicKey + } + ] + }) - t.ok(core.writable) + const key = Verifier.manifestHash(manifest) + + // correct keyPair + const writer = await create(t, { compat: false, manifest, keyPair }) + t.alike(writer.key, key) + + t.ok(writer.keyPair) + t.ok(writer.writable) const signature = b4a.alloc(32) - await t.exception(core.append('hello', { signature })) + await t.execution(writer.append('hello')) - t.is(core.length, 0) + t.is(writer.length, 1) - await core.append(['a', 'b']) + // no keyPair + const reader = await create(t, { compat: false, manifest }) - t.is(core.length, 2) + t.alike(reader.key, key) + + t.absent(reader.keyPair) + t.absent(reader.writable) + + await t.exception(reader.append('hello')) + + t.is(reader.length, 0) + + // bad keyPair + const imposter = await create(t, { compat: false, manifest, keyPair: crypto.keyPair() }) + t.alike(imposter.key, key) + + t.ok(imposter.keyPair) + t.ok(imposter.writable) + + await t.exception(imposter.append('hello')) - await t.exception(core.truncate(1, { signature })) + t.is(imposter.length, 0) }) function createMultiManifest(signers, prologue = null) {