Files
MikrotikManager/backend/src/services/server-poll-journal-anchor.ts
T

43 lines
2.9 KiB
TypeScript

import { isDeepStrictEqual } from "node:util"
import type { ServerPollExecutionStart } from "./server-poll-execution.js"
import type { ServerPollJournalContext } from "./server-poll-journal.js"
import type { ProbeJournalCrypto } from "./server-probe-journal.js"
const hash = (value: unknown): value is string => typeof value === "string" && /^[a-f0-9]{64}$/.test(value)
const must = (value: unknown): void => { if (!value) throw Error("Invalid immutable poll execution start") }
const record = (value: unknown): value is Record<string, unknown> => !!value && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype
const domain = "execution-start-v1"
type Signer = Pick<ProbeJournalCrypto, "proof">
/** Original context/admission proof remains unchanged. Only version3/4
* encrypted anchor carries an independently purpose-sealed fresh full start.
* No row, quota increase, authority renewal or caller-selected signer. */
export async function encodeServerPollJournalAnchor(context: ServerPollJournalContext, start: ServerPollExecutionStart | undefined, crypto: Signer): Promise<string> {
if (context.plan.version !== 3 && context.plan.version !== 4) { must(start === undefined); return JSON.stringify(context) }
must(start && Object.keys(start).sort().join(",")==="approvalProof,fullProof" && hash(start.fullProof) && hash(start.approvalProof) && start.approvalProof === context.plan.approvalProof)
if (!start) throw Error("Missing immutable poll execution start")
const value = { context, fullProof: start.fullProof, approvalProof: start.approvalProof }
const proof = await crypto.proof(domain, value); must(hash(proof))
return JSON.stringify({ ...context, executionStart: { ...start, proof } })
}
/** Expected context is independently validated by the fixed journal/history
* reader. This function authenticates the extra start; it issues no authority. */
export async function openServerPollJournalAnchor(parsed: unknown, context: ServerPollJournalContext, crypto: Signer): Promise<string> {
must(record(parsed))
if (!record(parsed)) throw Error("Missing poll journal anchor")
if (context.plan.version !== 3 && context.plan.version !== 4) {
must(isDeepStrictEqual(parsed, context)); return context.plan.beforeProof
}
must(Object.keys(parsed).sort().join(",") === "executionStart,identity,plan")
const { executionStart: start, ...original } = parsed
must(isDeepStrictEqual(original, context) && record(start))
if (!record(start)) throw Error("Missing immutable poll execution start")
must(Object.keys(start).sort().join(",") === "approvalProof,fullProof,proof"
&& hash(start.fullProof) && hash(start.approvalProof) && hash(start.proof)
&& start.approvalProof === context.plan.approvalProof)
const expected = await crypto.proof(domain, { context, fullProof: start.fullProof, approvalProof: start.approvalProof })
must(hash(expected) && start.proof === expected)
return start.fullProof as string
}