1
1
import React , { useCallback , useMemo , useState } from "react" ;
2
2
import styled from "styled-components" ;
3
+
3
4
import { useParams } from "react-router-dom" ;
4
5
import { useLocalStorage } from "react-use" ;
5
- import { keccak256 , encodePacked } from "viem" ;
6
+ import { keccak256 , stringToHex , encodeAbiParameters } from "viem" ;
6
7
import { useWalletClient , usePublicClient , useConfig } from "wagmi" ;
7
8
8
9
import { simulateDisputeKitShutterCastCommitShutter } from "hooks/contracts/generated" ;
9
10
import useSigningAccount from "hooks/useSigningAccount" ;
10
11
import { isUndefined } from "utils/index" ;
11
- import { wrapWithToast } from "utils/wrapWithToast" ;
12
12
import { encrypt } from "utils/shutter" ;
13
+ import { wrapWithToast } from "utils/wrapWithToast" ;
14
+
13
15
import OptionsContainer from "../OptionsContainer" ;
14
16
15
17
const Container = styled . div `
@@ -26,6 +28,25 @@ interface ICommit {
26
28
27
29
const SEPARATOR = "-" ;
28
30
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
+
29
50
const Commit : React . FC < ICommit > = ( { arbitrable, voteIDs, setIsOpen, refetch } ) => {
30
51
const { id } = useParams ( ) ;
31
52
const parsedDisputeID = useMemo ( ( ) => BigInt ( id ?? 0 ) , [ id ] ) ;
@@ -55,9 +76,7 @@ const Commit: React.FC<ICommit> = ({ arbitrable, voteIDs, setIsOpen, refetch })
55
76
const encodedMessage = `${ choice . toString ( ) } ${ SEPARATOR } ${ salt } ${ SEPARATOR } ${ justification } ` ;
56
77
const { encryptedCommitment, identity } = await encrypt ( encodedMessage ) ;
57
78
58
- const commitHash = keccak256 (
59
- encodePacked ( [ "uint256" , "uint256" , "string" ] , [ choice , BigInt ( salt ) , justification ] )
60
- ) ;
79
+ const commitHash = hashVote ( choice , BigInt ( salt ) , justification ) ;
61
80
62
81
const { request } = await simulateDisputeKitShutterCastCommitShutter ( wagmiConfig , {
63
82
args : [ parsedDisputeID , parsedVoteIDs , commitHash , identity as `0x${string } `, encryptedCommitment ] ,
0 commit comments