-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
46 lines (42 loc) · 1.8 KB
/
Copy pathindex.ts
File metadata and controls
46 lines (42 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @verifiable-okf/canon — the shared, version-pinned canonicalization core
* (Verifiable OKF SPEC §5/§6). Consumed identically by signer and verifier;
* any divergence here breaks every signature, so this package is tested hardest.
*/
export const CANON_VERSION = "0.1.0";
export { decodeUtf8, canonicalizeBody, canonicalizeBodyBytes, contentHash } from "./body.js";
export { splitConcept, parseFrontmatter, type SplitConcept } from "./frontmatter.js";
export { jcs } from "./jcs.js";
export { buildSigningPayload, stripSignatureValue } from "./payload.js";
import { canonicalizeBody, contentHash } from "./body.js";
import { splitConcept, parseFrontmatter } from "./frontmatter.js";
import { buildSigningPayload } from "./payload.js";
export interface CanonicalConcept {
readonly hasFrontmatter: boolean;
readonly frontmatter: Record<string, unknown>;
readonly canonicalBody: Uint8Array;
readonly contentHash: string;
/** Present only when frontmatter exists (signing binds the frontmatter). */
readonly signingPayload?: Uint8Array;
}
/**
* Convenience: split a raw concept, canonicalize the body, compute its
* content_hash, parse the frontmatter, and build the signing payload.
* Validates UTF-8 for byte input.
*/
export function canonicalizeConcept(input: Uint8Array | string): CanonicalConcept {
const { hasFrontmatter, frontmatterText, body } = splitConcept(input);
const canonicalBody = canonicalizeBody(body);
const ch = contentHash(canonicalBody);
if (!hasFrontmatter) {
return { hasFrontmatter: false, frontmatter: {}, canonicalBody, contentHash: ch };
}
const frontmatter = parseFrontmatter(frontmatterText);
return {
hasFrontmatter: true,
frontmatter,
canonicalBody,
contentHash: ch,
signingPayload: buildSigningPayload(frontmatter, canonicalBody),
};
}