All files / app-leaderboard/src/submission documents.ts

83.33% Statements 5/6
100% Branches 7/7
100% Functions 1/1
83.33% Lines 5/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                      353x 4x     349x 349x       349x    
import { computeFingerprintOfDocument, type BeMusicFingerprintDocument } from '@be-music/chart-model';
 
export type DocumentCheck = { ok: true } | { ok: false; reason: 'not-a-document' | 'fingerprint-mismatch' };
 
/**
 * Checks a fingerprint document a client sent against the fingerprint it claims (verification layer L3,
 * docs/leaderboard.md section 5.3). The document is canonicalized and hashed exactly like the chart package hashes the
 * documents it builds, so any edit to it, or a document of another chart, fails. This checks integrity only; whether
 * the document describes the replay's notes is a later layer.
 */
export function checkFingerprintDocument(document: unknown, claimedFingerprint: string): DocumentCheck {
  if (typeof document !== 'object' || document === null || Array.isArray(document)) {
    return { ok: false, reason: 'not-a-document' };
  }
  let actual: string;
  try {
    actual = computeFingerprintOfDocument(document as BeMusicFingerprintDocument);
  } catch {
    return { ok: false, reason: 'not-a-document' };
  }
  return actual === claimedFingerprint ? { ok: true } : { ok: false, reason: 'fingerprint-mismatch' };
}