/** Verify inspector: recompute and show every check, Tufte-clean. */
import { icon } from './icons.js';
import { esc, sparkPath, esriThumb, haptic } from './dom.js';
import { createSheet } from './sheet.js';
import { verify } from '../capture/provenance.js';
import { get } from '../capture/db.js';
const GLYPH = {
pass: 'checkmark',
warn: 'hazards',
fail: 'xmark',
na: 'minus',
};
export function createInspector() {
const sheet = createSheet({
label: 'Verify capture',
detents: ['large'],
modal: true,
dismissible: true,
closeButton: true,
className: 'opaque inspector',
heights: { large: (H) => Math.round(H * 0.94) },
});
let urls = [];
function small(label, values, unit) {
const v = values.filter(Number.isFinite);
if (v.length < 3)
return `
${label} —
`;
const lo = Math.min(...v);
const hi = Math.max(...v);
return `${label} ${lo.toFixed(1)}–${hi.toFixed(1)}${unit} `;
}
async function open(bundle) {
urls.forEach((u) => URL.revokeObjectURL(u));
urls = [];
const rec = await get('media', bundle.id).catch(() => null);
const blob = rec?.blob || null;
const url = blob ? URL.createObjectURL(blob) : '';
if (url) urls.push(url);
const isVideo = /video/.test(bundle.media.type);
const when = new Date(bundle.sealedAt);
sheet.head.innerHTML = `
${url ? (isVideo ? `
` : `
`) : ''}
${icon('sealFill')}
Capture #${bundle.seq} ${esc(when.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'medium' }))}${bundle.context?.testFrame ? ' · Test frame ' : ''}
`;
sheet.body.innerHTML = ` Recomputing hashes and checking the signature…
`;
sheet.open('large');
const t0 = performance.now();
const res = await verify(bundle, blob);
const ms = Math.round(performance.now() - t0);
const fails = res.checks.filter((c) => c.status === 'fail').length;
const warns = res.checks.filter((c) => c.status === 'warn').length;
haptic(fails ? 'warn' : 'success');
const imu = bundle.session?.imu || [];
const gps = bundle.session?.gps || [];
const f = gps[gps.length - 1];
sheet.body.innerHTML = `
${icon(fails ? 'xmark' : 'seal')}
${fails ? `${fails} check${fails > 1 ? 's' : ''} failed` : warns ? `Verified with ${warns} caveat${warns > 1 ? 's' : ''}` : 'All checks passed'} Recomputed on this device in ${ms} ms. None of this proves truth; it raises the cost of lying.
${res.checks
.map(
(c) =>
`${icon(GLYPH[c.status] || 'minus')} ${esc(c.label)} ${esc(c.detail)}
`,
)
.join('')}
Motion during capture${imu.length} samples
${small(
'α',
imu.map((s) => s.α),
'°',
)}${small(
'β',
imu.map((s) => s.β),
'°',
)}${small(
'γ',
imu.map((s) => s.γ),
'°',
)}${small(
'|a|',
imu.map((s) => Math.hypot(s.ax ?? NaN, s.ay ?? NaN, s.az ?? NaN)),
'',
)}
${f ? `Where it was sealed±${Math.round(f.acc)} m
${f.lat.toFixed(5)}, ${f.lon.toFixed(5)} ` : ''}
Bundle
hash ${esc(bundle.hash)}
media ${esc(bundle.media.sha256)}
prev ${esc(bundle.prev || 'null (genesis)')}
key ${esc(bundle.keyId)} · P-256
sig ${esc(bundle.signature.slice(0, 48))}…
device ${esc(bundle.context?.device || '')} · ${esc(bundle.context?.timezone || '')}
${icon('download')}Export bundle ${icon('refresh')}Verify again
`;
sheet.body.querySelector('[data-insp="export"]').onclick = () => {
const a = document.createElement('a');
const u = URL.createObjectURL(
new Blob([JSON.stringify(bundle, null, 2)], {
type: 'application/json',
}),
);
a.href = u;
a.download = `${bundle.id}.bundle.json`;
a.click();
setTimeout(() => URL.revokeObjectURL(u), 2000);
};
sheet.body.querySelector('[data-insp="reverify"]').onclick = () =>
open(bundle);
}
return { open, sheet };
}