/** Local bytes only; importing this module performs no I/O and cannot authorize work. */ import { createHash } from 'node:crypto'; type Flat = Record; type Bytes = Record; const PUBLIC = ['generation', 'binding_sha256', 'public_api_sha256', 'public_dictionary_sha256', 'public_errors_sha256']; const COMMON = [...new Set(['profile', 'active', 'generation', 'api_sha256', 'mcp_sha256', 'dictionary_sha256', 'errors_sha256', ...PUBLIC])]; const GENERIC = [...COMMON, 'status']; const COMPILED = [...COMMON, 'limits_sha256', 'full_profile_source_sha256', 'adapters_sha256']; const ARTIFACTS: Record = {'openapi.json': 'public_api_sha256', 'dictionary.json': 'public_dictionary_sha256', 'errors.json': 'public_errors_sha256'}; const fail = (reason: string): never => { throw new Error(reason); }; const object = (x: unknown): x is Record => x !== null && typeof x === 'object' && !Array.isArray(x) && Object.getPrototypeOf(x) === Object.prototype; const sameKeys = (x: Record, keys: string[]) => Object.keys(x).length === keys.length && keys.every(k => Object.hasOwn(x, k)); const scalar = (x: unknown): x is string => typeof x === 'string' && !/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(? typeof x === 'string' && /^[a-f0-9]{64}$/.test(x) && x.length === 64; const utf8 = (x: string) => new TextEncoder().encode(x); const equal = (a: Uint8Array, b: Uint8Array) => a.length === b.length && a.every((x, i) => x === b[i]); const digest = (x: unknown): string => x instanceof Uint8Array ? createHash('sha256').update(x).digest('hex') : fail('artifact_must_be_bytes'); export function canonicalBinding(value: unknown): Uint8Array { if (!object(value) || Object.entries(value).some(([k, v]) => !scalar(k) || !(v === null || typeof v === 'boolean' || scalar(v)))) fail('unsupported_binding_shape'); // Python sorts Unicode code points. Do not use JavaScript's UTF-16 default sort. const compare = (a: string, b: string) => { const aa = Array.from(a, x => x.codePointAt(0)!), bb = Array.from(b, x => x.codePointAt(0)!); for (let i = 0; i < Math.min(aa.length, bb.length); i++) if (aa[i] !== bb[i]) return aa[i] - bb[i]; return aa.length - bb.length; }; const v = value as Flat; // JSON.stringify is used ONLY for one scalar, never as an object canonicalizer. return utf8('{' + Object.keys(v).sort(compare).map(k => JSON.stringify(k) + ':' + JSON.stringify(v[k])).join(',') + '}'); } export function verifyBundle(binding: unknown, canonical: Bytes, snapshot: unknown, readable?: Bytes, readableHashes?: Record) { const encoded = canonicalBinding(binding); const b = binding as Flat; const generic = sameKeys(b, GENERIC); if (!generic && !sameKeys(b, COMPILED)) fail('unsupported_binding_fields'); if (typeof b.active !== 'boolean' || b.generation !== 'ARCH-COMPLETE-01') fail('unknown_generation_or_invalid_active'); if (generic ? b.profile !== null || b.status !== 'generic_template_not_released' || b.active : !scalar(b.profile) || !b.profile) fail(generic ? 'invalid_generic_binding' : 'invalid_profile'); if (Object.entries(b).some(([k, v]) => k.endsWith('_sha256') && !hex(v))) fail('invalid_digest'); const names = [...Object.keys(ARTIFACTS), 'binding.json']; if (!object(canonical) || !sameKeys(canonical, names.map(n => n + '.bytes'))) fail('missing_or_unknown_canonical_artifact'); if (!(canonical['binding.json.bytes'] instanceof Uint8Array) || !equal(canonical['binding.json.bytes'], encoded)) fail('binding_canonical_bytes_mismatch'); const withoutSelf = Object.fromEntries(Object.entries(b).filter(([k]) => k !== 'binding_sha256')); if (digest(canonicalBinding(withoutSelf)) !== b.binding_sha256) fail('binding_digest_mismatch'); for (const [name, field] of Object.entries(ARTIFACTS)) if (digest(canonical[name + '.bytes']) !== b[field]) fail('public_artifact_digest_mismatch'); if (!object(snapshot) || !sameKeys(snapshot, ['profile_id', 'revision', 'contract_binding'])) fail('invalid_snapshot_fields'); const s = snapshot as Record; if (!scalar(s.profile_id) || !s.profile_id || !scalar(s.revision) || !s.revision) fail('invalid_snapshot_identity'); const cb = s.contract_binding; if (!object(cb) || !sameKeys(cb, PUBLIC) || PUBLIC.some(k => cb[k] !== b[k])) fail('capability_binding_mismatch'); if (b.profile !== null && s.profile_id !== b.profile) fail('capability_profile_mismatch'); let readableIntegrity = 'not_verified'; if (readableHashes !== undefined) { if (!object(readableHashes) || !sameKeys(readableHashes, names) || !object(readable) || !sameKeys(readable, names)) fail('incomplete_readable_file_records'); for (const name of names) if (!hex(readableHashes[name]) || digest(readable![name]) !== readableHashes[name]) fail('readable_file_digest_mismatch'); readableIntegrity = 'exact_file_hashes_verified'; } const reasons = ['offline_integrity_is_not_release_or_entitlement', 'profile_revision_not_in_bundle']; if (!b.active) reasons.push('inactive_binding'); if (b.profile === null) reasons.push('generic_template_not_profile'); return {canonical_integrity: 'verified', readable_integrity: readableIntegrity, generation: b.generation, profile_id: s.profile_id, supplied_revision: s.revision, revision_independently_verified: false, new_work_allowed: false, reasons}; }