Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add commitment.ts #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ts/src/commitment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ZkWasmServiceHelper, ZkWasmUtil } from "zkwasm-service-helper";

export const endpoint = "https://rpc.zkwasmhub.com:8090";

export function commitmentHexToHexString(x: string, y: string): string[] {
const hexString1 = "0x" + x.slice(12, 66);
const hexString2 = "0x" + y.slice(39) + "00000000000000000" + x.slice(2, 12);
const hexString3 = "0x" + y.slice(2, 39);
return [hexString1, hexString2, hexString3];
}


export async function getImageCommitmentHexStrings(imageHash: string): Promise<string[]> {
const helper = new ZkWasmServiceHelper(endpoint, "", "");
const imageInfo = await helper.queryImage(imageHash);

if (!imageInfo || !imageInfo.checksum) {
throw new Error(`Image not found: ${imageHash}`);
}

const { x, y } = imageInfo.checksum;
const xHexString = ZkWasmUtil.bytesToHexStrings(x);
const yHexString = ZkWasmUtil.bytesToHexStrings(y);

return commitmentHexToHexString(
"0x" + xHexString[0].slice(2).padStart(64, "0"),
"0x" + yHexString[0].slice(2).padStart(64, "0")
);
}