Ratatoskr/src/app/features/desk/optionsHash.ts
Void 3436f1874a
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m8s
desk: read the gamecult.fork.* contract as Bifrost serves it
Replace the org.gamecult.feedback projection types (feedback.ts) with forkContract.ts, a read-only
mirror of fork-card.md (list, entry, card, record, desk_answer). The desk renders the record as served:
counted_answer_id marks the counted answer, answers keep admission order, a note shows only when
non-null, and nothing is re-derived or folded per account. The navigator count reads list.open_count.
The options-hash verifier now reads card.{title,context,options,recommendation,depends}, pinned to the
0c19eddb vectors. The demo seed is contract-shaped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 21:05:07 +00:00

51 lines
2.1 KiB
TypeScript

import { ForkCard, ForkEntry } from './forkContract';
// The hash and its canonical bytes are specified in CultLib contracts/cultnet/governance/fork-card.md
// ("Options hash", rule owner: Heimdall's binding file 1.2); the vectors in optionsHashVectors.json pin this code to it.
const jstr = (s: string): string => {
let out = '"';
// eslint-disable-next-line no-restricted-syntax
for (const ch of s.normalize('NFC')) {
const o = ch.codePointAt(0) as number;
const named: Record<number, string> = { 8: '\\b', 9: '\\t', 10: '\\n', 12: '\\f', 13: '\\r' };
if (ch === '"') out += '\\"';
else if (ch === '\\') out += '\\\\';
else if (named[o]) out += named[o];
else out += o < 0x20 ? `\\u${o.toString(16).padStart(4, '0')}` : ch;
}
return `${out}"`;
};
export type Shown = Pick<ForkCard, 'title' | 'context' | 'options' | 'recommendation' | 'depends'>;
/** The five shown members as RFC 8785 JSON, keys already in sorted order. */
export const canonicalShown = (c: Shown): string =>
`{"context":${jstr(c.context)},"depends":[${c.depends.map(jstr).join(',')}],"options":[${c.options
.map((o) => `{"id":${jstr(o.id)},"text":${jstr(o.text)}}`)
.join(',')}],"recommendation":${jstr(c.recommendation)},"title":${jstr(c.title)}}`;
export const shownOf = (c: ForkCard): Shown => ({
title: c.title,
context: c.context,
options: c.options,
recommendation: c.recommendation,
depends: c.depends,
});
export const optionsHash = async (c: Shown): Promise<string> => {
const bytes = new TextEncoder().encode(canonicalShown(c));
const digest = await crypto.subtle.digest('SHA-256', bytes);
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join('');
};
/** Fork ids whose published `options_hash` is not the hash of what the card shows. */
export const unverifiedForks = async (forks: ForkEntry[]): Promise<Set<string>> => {
const bad = new Set<string>();
await Promise.all(
forks.map(async ({ card }) => {
if ((await optionsHash(shownOf(card))) !== card.options_hash) bad.add(card.fork_id);
})
);
return bad;
};