Add WASM verifier loading and asset verification functions
- Introduced loadVerifier function to load the WASM verifier from the server. - Added verifyAsset function to validate asset data against an expected SHA256 hash using the loaded verifier. - Ensured compatibility with server-side rendering by checking for the window object.
This commit is contained in:
19
frontend/src/lib/verifier.ts
Normal file
19
frontend/src/lib/verifier.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export async function loadVerifier() {
|
||||
if (typeof window === 'undefined') return null;
|
||||
if ((window as any).verifySHA256) return (window as any).verifySHA256;
|
||||
|
||||
const go = new (window as any).Go();
|
||||
const result = await WebAssembly.instantiateStreaming(
|
||||
fetch('/verifier/verifier.wasm'),
|
||||
go.importObject
|
||||
);
|
||||
go.run(result.instance);
|
||||
return (window as any).verifySHA256;
|
||||
}
|
||||
|
||||
export async function verifyAsset(data: ArrayBuffer, expectedHash: string): Promise<true | string> {
|
||||
const verify = await loadVerifier();
|
||||
if (!verify) return 'WASM verifier not available';
|
||||
|
||||
return verify(new Uint8Array(data), expectedHash);
|
||||
}
|
||||
Reference in New Issue
Block a user