import { MerkleProofResult, ProcessState } from '../../pkg/sdk_client'; // Type for WasmService proxy (passed from Core Worker) type WasmServiceProxy = { hashValue(fileBlob: { type: string; data: Uint8Array }, commitedIn: string, label: string): Promise; getMerkleProof(processState: any, attributeName: string): Promise; validateMerkleProof(proof: any, hash: string): Promise; }; export class CryptoService { constructor(private wasm: WasmServiceProxy) {} public hexToBlob(hexString: string): Blob { const uint8Array = this.hexToUInt8Array(hexString); return new Blob([uint8Array as any], { type: 'application/octet-stream' }); } public hexToUInt8Array(hexString: string): Uint8Array { if (hexString.length % 2 !== 0) throw new Error('Invalid hex string'); const uint8Array = new Uint8Array(hexString.length / 2); for (let i = 0; i < hexString.length; i += 2) { uint8Array[i / 2] = parseInt(hexString.substr(i, 2), 16); } return uint8Array; } public async blobToHex(blob: Blob): Promise { const buffer = await blob.arrayBuffer(); const bytes = new Uint8Array(buffer); return Array.from(bytes) .map((byte) => byte.toString(16).padStart(2, '0')) .join(''); } public async getHashForFile(commitedIn: string, label: string, fileBlob: { type: string; data: Uint8Array }): Promise { return await this.wasm.hashValue(fileBlob, commitedIn, label); } public async getMerkleProofForFile(processState: ProcessState, attributeName: string): Promise { return await this.wasm.getMerkleProof(processState, attributeName); } public async validateMerkleProof(proof: MerkleProofResult, hash: string): Promise { return await this.wasm.validateMerkleProof(proof, hash); } public splitData(obj: Record) { const jsonCompatibleData: Record = {}; const binaryData: Record = {}; for (const [key, value] of Object.entries(obj)) { if (this.isFileBlob(value)) { binaryData[key] = value; } else { jsonCompatibleData[key] = value; } } return { jsonCompatibleData, binaryData }; } private isFileBlob(value: any): value is { type: string; data: Uint8Array } { return typeof value === 'object' && value !== null && typeof value.type === 'string' && value.data instanceof Uint8Array; } }