sign every write as an MMUR2 claim (POST /api/claims); the guessed title is parsedTitle
Build and Push Docker Image / build (push) Successful in 47s

This commit is contained in:
Worph
2026-10-25 17:36:44 +00:00
parent 8194541f1c
commit ecb30e1633
7 changed files with 597 additions and 52 deletions
+91
View File
@@ -0,0 +1,91 @@
/**
* `addToSet` keeps the legacy csv-set shape under claims: it rewrites THIS
* signer's own comma-joined vote, ignoring other signers' claims. fetch is
* stubbed with a tiny meta-core.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { MetaCoreClient, ClaimSigner, verifyClaim, resetSignerForTests } from '../src/meta-core-client.js';
const KEY = '01'.repeat(32);
const own = new ClaimSigner(KEY);
const other = new ClaimSigner('02'.repeat(32));
/** Claims meta-core holds for the cid, and every batch posted. */
let stored: Array<Record<string, unknown>>;
let posted: Array<Array<Record<string, unknown>>>;
let claimReads: string[];
function json(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } });
}
beforeEach(() => {
process.env.METAMESH_SIGNING_KEY = KEY;
resetSignerForTests();
stored = [];
posted = [];
claimReads = [];
vi.stubGlobal('fetch', vi.fn(async (url: string, init?: RequestInit) => {
const u = new URL(url);
if (init?.method === 'GET' && u.pathname.endsWith('/claims')) {
claimReads.push(u.pathname + u.search);
const key = u.searchParams.get('key');
return json({ claims: stored.filter((c) => c.key === key) });
}
if (init?.method === 'POST' && u.pathname === '/api/claims') {
const batch = JSON.parse(String(init.body)) as Array<Record<string, unknown>>;
posted.push(batch);
return json(batch.map(() => ({ status: 'accepted' })));
}
return json({ error: 'no route' }, 404);
}));
});
afterEach(() => {
vi.unstubAllGlobals();
delete process.env.METAMESH_SIGNING_KEY;
resetSignerForTests();
});
const client = () => new MetaCoreClient('http://core');
const claimOf = (s: ClaimSigner, value: string, ts: number) =>
({ cid: 'bafycid', uid: s.uid, key: 'genres', value, ts, sig: 'x' });
function postedValue(): string {
expect(posted).toHaveLength(1);
const [c] = posted[0];
expect(c.key).toBe('genres');
expect(c.uid).toBe(own.uid);
expect(verifyClaim({ ...(c as never), sig: Buffer.from(String(c.sig), 'base64') })).toBe(true);
return String(c.value);
}
describe('addToSet (csv-set over this signer\'s own claim)', () => {
it('writes the first member when this signer has no claim yet', async () => {
stored = [claimOf(other, 'Comedy', 5)];
expect(await client().addToSet('bafycid', 'genres', 'Drama')).toBe(true);
expect(claimReads).toEqual(['/api/meta/bafycid/claims?key=genres']);
expect(postedValue()).toBe('Drama');
});
it('appends to its own newest value and ignores other signers', async () => {
stored = [claimOf(own, 'Action', 1), claimOf(own, 'Drama, Thriller', 9), claimOf(other, 'Comedy,Horror', 20)];
expect(await client().addToSet('bafycid', 'genres', 'Sci-Fi/Fantasy')).toBe(true);
expect(postedValue()).toBe('Drama,Thriller,Sci-Fi/Fantasy');
});
it('writes nothing when the member is already in its own value', async () => {
stored = [claimOf(own, 'Drama,Thriller', 9)];
expect(await client().addToSet('bafycid', 'genres', 'Thriller')).toBe(true);
expect(posted).toHaveLength(0);
});
it('refuses to write when its own claims cannot be read', async () => {
vi.stubGlobal('fetch', vi.fn(async (url: string, init?: RequestInit) => {
if (init?.method === 'GET') return json({ error: 'nope' }, 500);
return json([{ status: 'accepted' }]);
}));
expect(await client().addToSet('bafycid', 'genres', 'Drama')).toBe(false);
});
});
+50
View File
@@ -0,0 +1,50 @@
/**
* MMUR2 conformance of the shared meta-core-client.ts against meta-core's
* golden vectors (test/claim-vectors.json, canonical copy in meta-core
* internal/claim/testdata). Node signs with a random nonce, so our own
* signatures cannot equal sigHex — they must verify and be low-S instead.
*/
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'fs';
import { signedBytes, verifyClaim, ClaimSigner, type ClaimValue } from '../src/meta-core-client.js';
const f = JSON.parse(readFileSync(new URL('./claim-vectors.json', import.meta.url), 'utf8'));
const HALF_N = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141') >> 1n;
const valueOf = (v: { value?: string | null; valueBytesHex?: string }): ClaimValue =>
v.valueBytesHex ? Buffer.from(v.valueBytesHex, 'hex') : (v.value ?? null);
describe('MMUR2 claim vectors', () => {
const signer = new ClaimSigner(f.privateKeyHex);
it('derives the vector uid from the test key', () => {
expect(signer.uid).toBe(f.uid);
});
for (const v of f.vectors) {
it(`${v.name}: canonical bytes, reference signature, own signature`, () => {
const value = valueOf(v);
expect(signedBytes(v.cid, v.uid, v.key, value, v.ts).toString('hex')).toBe(v.signedBytesHex);
expect(verifyClaim({ cid: v.cid, uid: v.uid, key: v.key, value, ts: v.ts, sig: Buffer.from(v.sigHex, 'hex') })).toBe(true);
const own = signer.sign(v.cid, v.key, value, v.ts);
expect(verifyClaim(own)).toBe(true);
expect(BigInt('0x' + Buffer.from(own.sig).subarray(32).toString('hex')) <= HALF_N).toBe(true);
});
}
it('refuses the high-S twin of a valid signature', () => {
const v = f.vectors.find((x: { name: string }) => x.name === f.highS.vector);
expect(verifyClaim({ cid: v.cid, uid: v.uid, key: v.key, value: valueOf(v), ts: v.ts, sig: Buffer.from(f.highS.sigHex, 'hex') })).toBe(false);
});
it('refuses a tampered value', () => {
const c = signer.sign('bafy', 'title', 'A');
expect(verifyClaim({ ...c, value: 'B' })).toBe(false);
});
it('issues strictly increasing ts', () => {
const a = signer.nextTs();
expect(signer.nextTs()).toBeGreaterThan(a);
});
});
+93
View File
@@ -0,0 +1,93 @@
{
"_comment": "MMUR2 golden vectors. Canonical copy: meta-core internal/claim/testdata. Do not edit by hand; see claim_test.go.",
"privateKeyHex": "0101010101010101010101010101010101010101010101010101010101010101",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"vectors": [
{
"name": "text",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "title",
"value": "The Matrix",
"ts": 1793001234567,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a657469746c656a546865204d61747269781b000001a17733a087",
"sigHex": "3ecac653f63cfc981564b5a5cc0db0459f2b4f19b810b6391d8f487216da0f3d2e560a2b61f4a81e320287cde753e1990d7949f4f518127f6505b8fb698ee8a8"
},
{
"name": "empty-text",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "title",
"value": "",
"ts": 1,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a657469746c656001",
"sigHex": "6eeb9ace8afe3987006c166eb0ec564b71e23d6a1c6211fdd55e8b4779c12dbb4f38c443faa3255c2ce7c6592e9d024e4ea57b5fa23446d1d4df647947e37642"
},
{
"name": "unicode",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "titles/jpn/攻殻機動隊",
"value": "true",
"ts": 1793001234567,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a781a7469746c65732f6a706e2fe694bbe6aebbe6a99fe58b95e99a8a64747275651b000001a17733a087",
"sigHex": "64ec6342c598782df278b49636a34da4f4c009423a584454a3284efad5426aa517d83873b09a4fdd3fd32a5e9a5fef0880126edd11db773045caeb6e634bc207"
},
{
"name": "keyset-member",
"cid": "bagacbabaec7v3",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "cids/bafkreih5kapbjzq",
"value": "true",
"ts": 23,
"signedBytesHex": "4d4d5552320a856e6261676163626162616563377633782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a75636964732f6261666b72656968356b6170626a7a71647472756517",
"sigHex": "8f707847895ccb522f1507dbab23330bd2c567d6fd1882df3bfae65bef6601024832f0b181146dcfb089aae91f5beac47da970373ab14370c4ac41410f3bf761"
},
{
"name": "bytes",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "seek",
"value": null,
"valueBytesHex": "00ff10ab",
"ts": 24,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a647365656b4400ff10ab1818",
"sigHex": "09671b27c03edd510029ec550b35e95e3900dd37a5d688ea04c3f0409979c7236bc4a1b5fa9d1b76265da85df24693a51f0ced528a6171c90a70b92db9f246e4"
},
{
"name": "retraction",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "genres/Drama",
"value": null,
"ts": 255,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a6c67656e7265732f4472616d61f618ff",
"sigHex": "d0c29d8edac4b78005eaf895cd9d534cb080b22ba9f7e79a5abf4d9bd906dc494464a2b388653452bca16e351132a9a92370953fa047e693459e55b7965449ac"
},
{
"name": "long-value",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "plot/eng",
"value": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"ts": 65536,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a68706c6f742f656e6779012c6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161611a00010000",
"sigHex": "383b2e40f913eb5240e1eae2cfa392f967321d3701623c2b3df344984686d888198bce1c78847d1898b35f991ec3465c382c45e03b9a77f2ae7d1fd740391dd2"
},
{
"name": "ts-64bit",
"cid": "bafkreih5kapbjzq",
"uid": "zvYNYVRtXSSDCi1rZtPP3ieuoh8cG5AscesGPYESa4VpJ",
"key": "year",
"value": "1999",
"ts": 1099511627776,
"signedBytesHex": "4d4d5552320a85706261666b72656968356b6170626a7a71782d7a76594e5956527458535344436931725a745050336965756f683863473541736365734750594553613456704a647965617264313939391b0000010000000000",
"sigHex": "2b9b42ef4ebd7fc8d3ca0a514679ca4104f2e627da3a84c7b9664b27ebbdf73946cc37a04e4ea15dbed23a0e48e8797c92fcba5598b012305a314bf776398e0d"
}
],
"highS": {
"note": "same claim as `vector`, S replaced by n-S: valid ECDSA, but MMUR2 verifiers must refuse it",
"vector": "text",
"sigHex": "3ecac653f63cfc981564b5a5cc0db0459f2b4f19b810b6391d8f487216da0f3dd1a9f5d49e0b57e1cdfd783218ac1e65ad3592f1ba308dbc5acca59166a75899"
}
}
+2 -1
View File
@@ -64,7 +64,8 @@ describe('Filename Parser Plugin Integration Tests', () => {
});
it('declares correct schema', () => {
expect(manifest.schema).toHaveProperty('originalTitle');
expect(manifest.schema).toHaveProperty('parsedTitle');
expect(manifest.schema).not.toHaveProperty('originalTitle');
expect(manifest.schema).toHaveProperty('videoType');
expect(manifest.schema).toHaveProperty('season');
expect(manifest.schema).toHaveProperty('episode');