import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { load, walk, verifyRecord, marketContractHeld, recordCid } from "./index.js"; await load(readFileSync(new URL("./pkg/identlink_bg.wasm", import.meta.url))); const v = JSON.parse(readFileSync(new URL("../vectors.json", import.meta.url))); const entry = (map, key) => { if (map[key] === "error") throw new Error(`${key}: error`); return map[key] ?? null; }; const io = { resolveDid: async (did) => entry(v.dids, did), getRecord: async (repo, collection, rkey) => entry(v.dids, repo) === null ? null : entry(v.records, `at://${repo}/${collection}/${rkey}`), contractHeld: async (c, authorities) => entry(v.held, c.uri) === true && authorities.includes(c.uri.slice(5).split("/")[0]), now: () => Date.parse(v.now), }; const time = (s) => (s === undefined ? undefined : Date.parse(s)); const norm = (w) => ({ ancestors: w.ancestors.map((a) => ({ did: a.did, depth: a.depth, expiresAt: time(a.expiresAt) })), roots: w.roots, truncated: w.truncated, expiresAt: time(w.expiresAt), errors: typeof w.errors === "number" ? w.errors : (w.errors ?? []).length, }); for (const c of v.cases) { const got = await walk(io, c.agent, c.options); assert.deepEqual(norm(got), norm({ errors: 0, ...c.want }), c.name); } const { contractHeld, ...reader } = io; for (const c of v.market) { const got = await marketContractHeld(reader, c.contract, c.authorities).catch(() => "error"); assert.equal(got, c.want, c.name); } const did = "did:plc:ocnuqjlzgohypj3zoznermms"; const car = readFileSync(new URL("../tests/fixtures/profile.car", import.meta.url)); const doc = JSON.parse(readFileSync(new URL("../tests/fixtures/did.json", import.meta.url))); const profile = await verifyRecord(car, doc, did, "app.bsky.actor.profile", "self"); assert.equal(profile.$type, "app.bsky.actor.profile"); assert.equal(await recordCid(profile), "bafyreihjykthjmsbjlbjnahdvj3hj7oqhn75r67n6uerkrclbq5t3odhha"); car[car.length - 1] ^= 1; await assert.rejects(() => verifyRecord(car, doc, did, "app.bsky.actor.profile", "self")); console.log(`ok ${v.cases.length} vectors, ${v.market.length} market vectors, CAR verify`);