-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathshutter.builder.ts
More file actions
49 lines (38 loc) · 1.94 KB
/
shutter.builder.ts
File metadata and controls
49 lines (38 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Hex } from "viem";
import { getVoteKey } from "actions/helpers/storage/getVoteKey";
import { disputeKitShutterAbi, disputeKitShutterAddress } from "hooks/contracts/generated";
import { hashJustification } from "utils/crypto/hashJustification";
import { hashVote } from "utils/crypto/hashVote";
import { encrypt } from "utils/crypto/shutter";
import { ShutterCommitDeps } from "../deps";
import { encodeShutterMessage } from "../helpers";
import { ShutterCommitParams } from "../params";
import { defineCommitBuilder } from "./baseBuilder";
export const shutterCommitBuilder = defineCommitBuilder({
builderDeps: {
encrypt,
},
build: async (params: ShutterCommitParams, context, deps: ShutterCommitDeps) => {
if (!import.meta.env.REACT_APP_SHUTTER_API || import.meta.env.REACT_APP_SHUTTER_API.trim() === "") {
console.error("REACT_APP_SHUTTER_API environment variable is not set or is empty");
throw new Error("Cannot commit vote: REACT_APP_SHUTTER_API environment variable is required but not set");
}
const { disputeId, voteIds, choice, salt, decryptionDelay, justification, roundIndex } = params;
const { chain, account } = context;
const key = getVoteKey(disputeId, roundIndex, voteIds);
deps.storeCommitData(key, { choice, salt, justification });
const encodedMessage = encodeShutterMessage(choice, salt, justification);
const { encryptedCommitment, identity } = await deps.encrypt(encodedMessage, decryptionDelay);
const choiceCommit = hashVote(choice, BigInt(salt));
const justificationCommit = hashJustification(BigInt(salt), justification);
const chainKey = chain.id as keyof typeof disputeKitShutterAddress;
return {
account,
address: disputeKitShutterAddress[chainKey],
abi: disputeKitShutterAbi,
functionName: "castCommitShutter",
args: [disputeId, voteIds, choiceCommit, justificationCommit, identity as Hex, encryptedCommitment],
chain,
};
},
});