papertrail/lib/signatures/certificate.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

38 lines
1.4 KiB
TypeScript

import { createHash } from "node:crypto";
import type { SignatureRequest, SignatureSigner } from "@/lib/signatures/types";
// Tamper-evident certificate digest for a completed signature request. Mirrors
// the compliance module's evidence pattern: a deterministic SHA-256 over a
// canonical, ordered projection of the request and its signer trail. Any change
// to who signed, in what order, or when, changes the hash.
interface CertificateInput {
request: Pick<
SignatureRequest,
"id" | "orgId" | "entityType" | "entityId" | "title"
>;
signers: readonly Pick<
SignatureSigner,
"userId" | "orderIndex" | "signedAt"
>[];
}
// Produces the canonical string that gets hashed. Signers are sorted by
// order_index so serialization is stable regardless of input ordering.
export function canonicalPayload(input: CertificateInput): string {
const signers = [...input.signers]
.sort((a, b) => a.orderIndex - b.orderIndex)
.map((s) => `${s.orderIndex}:${s.userId}:${s.signedAt ?? ""}`);
return [
`request:${input.request.id}`,
`org:${input.request.orgId}`,
`entity:${input.request.entityType}/${input.request.entityId}`,
`title:${input.request.title}`,
`signers:${signers.join("|")}`,
].join("\n");
}
export function computeCertHash(input: CertificateInput): string {
return createHash("sha256").update(canonicalPayload(input), "utf8").digest("hex");
}