Jobs of fixed types with strict argument schemas, signed by the Hub with Ed25519 over canonical JSON. Agent requests signed with the server's Ed25519 key. Config sealed to the server's X25519 key (X25519 + HKDF + AES-256-GCM; Node has no XSalsa20). Exports encrypted with a key that only travels in the download link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
/**
|
|
* Canonical JSON for signatures: object keys sorted (by UTF-16 code units, as JavaScript sorts),
|
|
* no whitespace, `undefined` properties left out. Arrays keep their order. Only plain JSON values
|
|
* (no Dates, Maps or functions: convert them first).
|
|
*/
|
|
export function canonicalJson(value: unknown): string {
|
|
if (value === null || typeof value === "boolean" || typeof value === "string") return JSON.stringify(value);
|
|
if (typeof value === "number") {
|
|
if (!Number.isFinite(value)) throw new Error("canonicalJson: numbers must be finite");
|
|
return JSON.stringify(value);
|
|
}
|
|
if (Array.isArray(value)) return `[${value.map((v) => (v === undefined ? "null" : canonicalJson(v))).join(",")}]`;
|
|
if (typeof value === "object") {
|
|
const proto = Object.getPrototypeOf(value) as unknown;
|
|
if (proto !== Object.prototype && proto !== null) throw new Error("canonicalJson: only plain objects");
|
|
const obj = value as Record<string, unknown>;
|
|
const entries = Object.keys(obj)
|
|
.filter((k) => obj[k] !== undefined)
|
|
.sort()
|
|
.map((k) => `${JSON.stringify(k)}:${canonicalJson(obj[k])}`);
|
|
return `{${entries.join(",")}}`;
|
|
}
|
|
throw new Error(`canonicalJson: can't encode a ${typeof value}`);
|
|
}
|