// A module Worker: no DOM, no window, same CSP as the extension pages. import { accountFromPublicKey, contextForSpec, initWasm, keypairFromSeed, Scheme, sign, verify } from './vendor/quantus-crypto/index.js'; const hex = (u8) => [...u8].map((b) => b.toString(16).padStart(2, '0')).join(''); const lines = []; const t0 = performance.now(); const err = initWasm(); const tInit = performance.now() - t0; lines.push(`hasDOM: ${typeof document !== 'undefined'} hasWindow: ${typeof window !== 'undefined'}`); lines.push(`initWasm: ${err === null ? 'ok' : 'FAILED - ' + err} (${tInit.toFixed(1)} ms cold)`); if (err === null) { const t1 = performance.now(); const pair = keypairFromSeed(new Uint8Array(32), Scheme.MlDsa87); const tKeygen = performance.now() - t1; const account = hex(accountFromPublicKey(pair.publicKey)); lines.push(`keygen: ${tKeygen.toFixed(1)} ms`); lines.push(`account: ${account === '1883df2ae47d1fd428a6b8237ad7b59cf0facccaacac4541ef7758be44b3c333' ? 'matches quantus-cli' : 'MISMATCH ' + account}`); const msg = new TextEncoder().encode('extrinsic payload'); const ctx = contextForSpec(148); const t2 = performance.now(); const sig = sign(msg, pair, ctx, Scheme.MlDsa87); const tSign = performance.now() - t2; const t3 = performance.now(); const ok = verify(msg, sig, pair.publicKey, ctx, Scheme.MlDsa87); const tVerify = performance.now() - t3; lines.push(`sign: ${tSign.toFixed(1)} ms (${sig.length} bytes)`); lines.push(`verify: ${tVerify.toFixed(1)} ms ${ok ? 'ok' : 'FAILED'}`); lines.push(`ctx sep: ${verify(msg, sig, pair.publicKey, contextForSpec(147), Scheme.MlDsa87) ? 'FAILED' : 'ok (rejected under spec-147 ctx)'}`); } postMessage(lines.join('\n'));