Files
Shade/packages/shade-vault/tests/vectors.test.ts
Sterister b82a803e0c
Some checks failed
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled
feat(vault): v2 — compressed entries, chunked objects, index pushes, lazy reads, prune (4.14.0)
Entries are deflate-raw compressed when it pays, split into 4 MiB sealed
parts, and manifests are compressed (SVM2). A push with a local index
reuses unchanged files without downloading anything, and a push with no
changes commits nothing. Readers fetch one manifest and only the entries
they need. The relay records what each version references and can prune
old versions signed by the owner. Cross-language vectors in
test-vectors/vault-v2.json.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 01:38:45 +02:00

101 lines
4.2 KiB
TypeScript

/**
* The vault-v2 cross-language vectors, checked against this implementation.
*
* The Kotlin port is written against `test-vectors/vault-v2.json`; this test
* is what stops the file and the TypeScript code from drifting apart, so a
* port that matches the vectors also matches the client that wrote them.
* Regenerate with `bun run scripts/generate-vectors.ts vault-v2.json`.
*/
import { describe, test, expect } from 'bun:test';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { SubtleCryptoProvider } from '@shade/crypto-web';
import { canonicalizePayload } from '@shade/server';
import { deriveVaultKeys } from '../src/client.js';
import {
decodeManifest,
encodeManifest,
openEntryBytes,
openManifest,
packEntry,
toHex,
vaultObjectAad,
} from '../src/crypto.js';
import { sealEntryWithNonces, sealObjectWithNonce } from '../src/testing.js';
import type { VaultEntry } from '../src/types.js';
const crypto = new SubtleCryptoProvider();
const file = JSON.parse(
readFileSync(join(import.meta.dir, '..', '..', '..', 'test-vectors', 'vault-v2.json'), 'utf-8'),
) as { version: number; vectors: any[] };
function fromHex(s: string): Uint8Array {
const out = new Uint8Array(s.length / 2);
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
return out;
}
const byPrefix = (p: string) => file.vectors.filter((v) => v.description.startsWith(p));
describe('vault-v2 vectors', async () => {
const k = byPrefix('vault-v2 keys')[0];
const keys = await deriveVaultKeys(fromHex(k.masterKey), k.app);
test('the file is schema version 2 and has every kind', () => {
expect(file.version).toBe(2);
expect(byPrefix('vault-v2 entry').length).toBe(3);
expect(byPrefix('vault-v2 manifest').length).toBe(2);
expect(byPrefix('vault-v2 signed').length).toBe(2);
});
test('key derivation and AAD', () => {
expect(keys.vaultId).toBe(k.vaultId);
expect(toHex(keys.contentKey)).toBe(k.contentKey);
expect(toHex(keys.signingSeed)).toBe(k.signingSeed);
expect(toHex(keys.publicKey)).toBe(k.publicKey);
expect(toHex(vaultObjectAad(keys.vaultId))).toBe(k.objectAad);
});
test('entries: readers reassemble, writers reproduce every byte', async () => {
for (const v of byPrefix('vault-v2 entry')) {
const entry = v.entry as VaultEntry;
const wires = v.parts.map((p: any) => fromHex(p.wire));
const plain = await openEntryBytes(crypto, keys.contentKey, keys.vaultId, entry, wires);
expect(toHex(plain)).toBe(v.plaintext);
const packed = await packEntry(plain, { chunkSize: v.chunkSize });
expect(toHex(packed.stored)).toBe(v.stored);
expect(packed.enc ?? null).toBe(v.enc);
const resealed = await sealEntryWithNonces(
keys.contentKey,
keys.vaultId,
plain,
(i) => fromHex(v.parts[i].nonce),
{ chunkSize: v.chunkSize },
);
expect(resealed.objects.map((o) => toHex(o.bytes))).toEqual(v.parts.map((p: any) => p.wire));
expect({ path: entry.path, ...resealed.locator, size: plain.length, ...(entry.mode ? { mode: entry.mode } : {}) }).toEqual(entry);
}
});
test('manifests: v2 and v1 both open; encoding reproduces the plaintext', async () => {
for (const v of byPrefix('vault-v2 manifest')) {
const opened = await openManifest(crypto, keys.contentKey, keys.vaultId, v.hash, fromHex(v.wire));
expect(opened).toEqual(v.manifest);
expect(await decodeManifest(fromHex(v.plaintext))).toEqual(v.manifest);
expect(toHex(await encodeManifest(v.manifest))).toBe(v.plaintext);
const sealed = await sealObjectWithNonce(keys.contentKey, keys.vaultId, fromHex(v.plaintext), fromHex(v.nonce));
expect(sealed.hash).toBe(v.hash);
}
});
test('signed bodies: canonical bytes and a deterministic Ed25519 signature', async () => {
for (const v of byPrefix('vault-v2 signed')) {
const canonical = canonicalizePayload({ ...v.body, signedAt: v.signedAt, signature: '' });
expect(toHex(canonical)).toBe(v.canonicalHex);
expect(toHex(await crypto.sign(keys.signingSeed, canonical))).toBe(v.signature);
expect(await crypto.verify(keys.publicKey, canonical, fromHex(v.signature))).toBe(true);
}
});
});