Skip to content

Commit 8bf3772

Browse files
committed
fix: vote hashing during commitment must follow DisputeKitShutter.hashVote()
1 parent 729777d commit 8bf3772

File tree

1 file changed

+24
-5
lines changed
  • web/src/pages/Cases/CaseDetails/Voting/Shutter

1 file changed

+24
-5
lines changed

web/src/pages/Cases/CaseDetails/Voting/Shutter/Commit.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import React, { useCallback, useMemo, useState } from "react";
22
import styled from "styled-components";
3+
34
import { useParams } from "react-router-dom";
45
import { useLocalStorage } from "react-use";
5-
import { keccak256, encodePacked } from "viem";
6+
import { keccak256, stringToHex, encodeAbiParameters } from "viem";
67
import { useWalletClient, usePublicClient, useConfig } from "wagmi";
78

89
import { simulateDisputeKitShutterCastCommitShutter } from "hooks/contracts/generated";
910
import useSigningAccount from "hooks/useSigningAccount";
1011
import { isUndefined } from "utils/index";
11-
import { wrapWithToast } from "utils/wrapWithToast";
1212
import { encrypt } from "utils/shutter";
13+
import { wrapWithToast } from "utils/wrapWithToast";
14+
1315
import OptionsContainer from "../OptionsContainer";
1416

1517
const Container = styled.div`
@@ -26,6 +28,25 @@ interface ICommit {
2628

2729
const SEPARATOR = "-";
2830

31+
/**
32+
* This hashing function must be follow the same logic as DisputeKitClassic.hashVote()
33+
*/
34+
const hashVote = (choice: bigint, salt: bigint, justification: string): `0x${string}` => {
35+
const justificationHash = keccak256(stringToHex(justification));
36+
37+
// Encode and hash the parameters together (mimics Solidity's abi.encode)
38+
const encodedParams = encodeAbiParameters(
39+
[
40+
{ type: "uint256" }, // choice
41+
{ type: "uint256" }, // salt
42+
{ type: "bytes32" }, // justificationHash
43+
],
44+
[choice, salt, justificationHash]
45+
);
46+
47+
return keccak256(encodedParams);
48+
};
49+
2950
const Commit: React.FC<ICommit> = ({ arbitrable, voteIDs, setIsOpen, refetch }) => {
3051
const { id } = useParams();
3152
const parsedDisputeID = useMemo(() => BigInt(id ?? 0), [id]);
@@ -55,9 +76,7 @@ const Commit: React.FC<ICommit> = ({ arbitrable, voteIDs, setIsOpen, refetch })
5576
const encodedMessage = `${choice.toString()}${SEPARATOR}${salt}${SEPARATOR}${justification}`;
5677
const { encryptedCommitment, identity } = await encrypt(encodedMessage);
5778

58-
const commitHash = keccak256(
59-
encodePacked(["uint256", "uint256", "string"], [choice, BigInt(salt), justification])
60-
);
79+
const commitHash = hashVote(choice, BigInt(salt), justification);
6180

6281
const { request } = await simulateDisputeKitShutterCastCommitShutter(wagmiConfig, {
6382
args: [parsedDisputeID, parsedVoteIDs, commitHash, identity as `0x${string}`, encryptedCommitment],

0 commit comments

Comments
 (0)