- Added Taskfile.yml to streamline build, development, and testing tasks. - Updated README to reflect new build instructions and development environment setup using `go-task`. - Included `.taskfile.env` and `.task` in .dockerignore and .gitignore for better environment management. - Modified asset loading in verifier.ts to include integrity and cross-origin attributes for security. - Updated SRI generation script to handle both directory and single file inputs for improved flexibility.
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import type { VerificationResult } from './types';
|
|
|
|
export async function loadVerifier() {
|
|
if (typeof window === 'undefined') return null;
|
|
if ((window as any).verifySHA256) return (window as any).verifySHA256;
|
|
|
|
// Dynamically load wasm_exec.js if Go is not defined
|
|
if (!(window as any).Go) {
|
|
await new Promise<void>((resolve, reject) => {
|
|
const script = document.createElement('script');
|
|
script.src = '/verifier/wasm_exec.js';
|
|
script.integrity = 'sha384-PWCs+V4BDf9yY1yjkD/p+9xNEs4iEbuvq+HezAOJiY3XL5GI6VyJXMsvnjiwNbce';
|
|
script.crossOrigin = 'anonymous';
|
|
script.onload = () => resolve();
|
|
script.onerror = () => reject(new Error('Failed to load WASM executor script'));
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
const go = new (window as any).Go();
|
|
const result = await WebAssembly.instantiateStreaming(
|
|
fetch('/verifier/verifier.wasm', {
|
|
integrity: 'sha384-fDQVhNAuumlwh5lh1AT6LiSLer1EQYa1G8TEJLCZvKXeUxYi2gn3QoI5YdNFtKW0',
|
|
crossOrigin: 'anonymous'
|
|
}),
|
|
go.importObject
|
|
);
|
|
go.run(result.instance);
|
|
return (window as any).verifySHA256;
|
|
}
|
|
|
|
export async function verifyAsset(
|
|
data: ArrayBuffer,
|
|
expectedHash: string
|
|
): Promise<VerificationResult> {
|
|
const verify = await loadVerifier();
|
|
if (!verify) {
|
|
return {
|
|
valid: false,
|
|
steps: [],
|
|
error: 'WASM verifier not available',
|
|
};
|
|
}
|
|
|
|
return verify(new Uint8Array(data), expectedHash);
|
|
}
|