From e4d58e1e86fb81f6e30c52ea50a2c46a4e544a79 Mon Sep 17 00:00:00 2001 From: Oleh Komendant Date: Fri, 15 Nov 2024 20:05:48 +0200 Subject: [PATCH] Fix code style --- src/core/dependencies/CircomFilesResolver.ts | 5 ++--- src/utils/utils.ts | 23 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/core/dependencies/CircomFilesResolver.ts b/src/core/dependencies/CircomFilesResolver.ts index 056b82e..8ebe015 100644 --- a/src/core/dependencies/CircomFilesResolver.ts +++ b/src/core/dependencies/CircomFilesResolver.ts @@ -19,7 +19,7 @@ import { getRealPath } from "hardhat/internal/util/fs-utils"; import { CircomFilesParser } from "./parser/CircomFilesParser"; import { CIRCOM_FILE_REG_EXP, NODE_MODULES, NODE_MODULES_REG_EXP, URI_SCHEME_REG_EXP } from "../../constants"; -import { getSHA1Hash } from "../../utils"; +import { getFileHash } from "../../utils"; import { HardhatZKitError } from "../../errors"; import { @@ -385,8 +385,7 @@ export class CircomFilesResolver { const stats = await fsExtra.stat(absolutePath); const lastModificationDate = new Date(stats.ctime); - // Add await here - const contentHash = await getSHA1Hash(fsExtra.readFileSync(absolutePath)); + const contentHash = await getFileHash(absolutePath); const fileData = this._parser.parse(rawContent, absolutePath, contentHash); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 7400b36..ab3b2c4 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -8,11 +8,11 @@ import * as snarkjs from "snarkjs"; import { ProvingSystemType } from "@solarity/zkit"; import { Reporter } from "../reporter"; +import { HardhatZKitError } from "../errors"; +import { BN128_CURVE_NAME } from "../constants"; import { ExecCallResult } from "../types/utils"; -import { BN128_CURVE_NAME } from "../constants"; - /** * Downloads a file from the specified URL * @@ -108,14 +108,19 @@ export async function getFileHash(filePath: string): Promise { const hash = createHash("sha1"); const stream = fsExtra.createReadStream(filePath); - stream.on("data", (data) => hash.update(data)); - stream.on("end", () => resolve(hash.digest("hex"))); - stream.on("error", reject); - }); -} + stream.on("data", (data: Buffer) => { + // Add data chunk to the hash object + hash.update(data); + }); + + stream.on("end", () => { + resolve(hash.digest("hex")); + }); -export function getSHA1Hash(rawFileData: Buffer): string { - return createHash("sha1").update(rawFileData).digest().toString("hex"); + stream.on("error", () => { + reject(new HardhatZKitError(`Failed to read ${filePath} file`)); + }); + }); } export function getUniqueProvingSystems(provingSystems: ProvingSystemType | ProvingSystemType[]): ProvingSystemType[] {