Simplify and harden investigation workflows
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import canonicalize from "canonicalize";
|
||||
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import { EvidenceIntegrityError } from "../domain/errors.js";
|
||||
import { parseEvidence, type Evidence } from "../domain/evidence.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
type EvidenceAuthorityResult = Result<Evidence[], EvidenceIntegrityError>;
|
||||
|
||||
/** Resolve caller-supplied records to their immutable session-owned values. */
|
||||
export const resolveSessionEvidence = (
|
||||
session: BinarySessionPort,
|
||||
input: Evidence | readonly Evidence[],
|
||||
): EvidenceAuthorityResult => {
|
||||
const supplied = Array.isArray(input) ? input : [input];
|
||||
const authoritative: Evidence[] = [];
|
||||
for (const item of supplied) {
|
||||
const parsed = parseEvidence(item);
|
||||
const owned = session.evidenceById(parsed.evidence_id);
|
||||
if (owned === undefined || canonicalJson(owned) !== canonicalJson(parsed))
|
||||
return err(
|
||||
new EvidenceIntegrityError(
|
||||
"Comparison input does not match its session-owned Evidence",
|
||||
),
|
||||
);
|
||||
authoritative.push(owned);
|
||||
}
|
||||
return ok(authoritative);
|
||||
};
|
||||
|
||||
/** Check exact ownership for a single Evidence record. */
|
||||
export const isSessionEvidence = (
|
||||
session: BinarySessionPort,
|
||||
input: Evidence,
|
||||
): boolean => {
|
||||
const owned = session.evidenceById(input.evidence_id);
|
||||
return owned !== undefined && canonicalJson(owned) === canonicalJson(input);
|
||||
};
|
||||
|
||||
const canonicalJson = (value: unknown): string => {
|
||||
const encoded = canonicalize(value);
|
||||
if (encoded === undefined)
|
||||
throw new TypeError("Evidence could not be canonicalized");
|
||||
return encoded;
|
||||
};
|
||||
Reference in New Issue
Block a user