From 8645885bf33e06ff1f778b89670f887f5e778674 Mon Sep 17 00:00:00 2001 From: Damian Date: Wed, 24 Jul 2024 16:40:11 -0300 Subject: [PATCH] Remove hiddenChars and use privateKeyToAccount to validate PK found --- packages/backend/controllers/Admin.ts | 9 +++----- packages/backend/models/Game.ts | 4 ---- .../components/dicedemo/GameCreateForm.tsx | 4 ---- .../components/dicedemo/HostAnnouncement.tsx | 3 --- .../components/dicedemo/RestartWithNewPk.tsx | 4 ---- packages/nextjs/pages/game/[id].tsx | 21 +++++++++++-------- packages/nextjs/types/game/game.ts | 1 - packages/nextjs/utils/diceDemo/apiUtils.ts | 7 +------ 8 files changed, 16 insertions(+), 37 deletions(-) diff --git a/packages/backend/controllers/Admin.ts b/packages/backend/controllers/Admin.ts index 221bd27..6c372f6 100644 --- a/packages/backend/controllers/Admin.ts +++ b/packages/backend/controllers/Admin.ts @@ -41,7 +41,7 @@ async function generateUniqueInvite(length: number) { export const createGame = async (req: Request, res: Response) => { try { - const { diceCount, hiddenChars, privateKey, hiddenPrivateKey, mode, adminAddress } = req.body; + const { diceCount, privateKey, hiddenPrivateKey, mode, adminAddress } = req.body; const salt = await bcrypt.genSalt(); // const privateKeyHash = await bcrypt.hash(privateKey, salt); @@ -54,7 +54,6 @@ export const createGame = async (req: Request, res: Response) => { mode, privateKey, hiddenPrivateKey, - hiddenChars, }); let token; @@ -70,7 +69,7 @@ export const createGame = async (req: Request, res: Response) => { export const restartWithNewPk = async (req: Request, res: Response) => { try { - const { diceCount, hiddenChars, privateKey, hiddenPrivateKey, adminAddress } = req.body; + const { diceCount, privateKey, hiddenPrivateKey, adminAddress } = req.body; const { id } = req.params; const game = await Game.findById(id); @@ -79,7 +78,6 @@ export const restartWithNewPk = async (req: Request, res: Response) => { } game.diceCount = diceCount; - game.hiddenChars = hiddenChars; game.privateKey = privateKey; game.hiddenPrivateKey = hiddenPrivateKey; game.mode = "manual"; @@ -247,7 +245,7 @@ export const kickPlayer = async (req: Request, res: Response) => { export const varyHiddenPrivatekey = async (req: Request, res: Response) => { try { const { id } = req.params; - const { hiddenChars, hiddenPrivateKey, diceCount } = req.body; + const { hiddenPrivateKey, diceCount } = req.body; const game = await Game.findById(id); if (!game) { @@ -258,7 +256,6 @@ export const varyHiddenPrivatekey = async (req: Request, res: Response) => { return res.status(400).json({ error: "Invalid dice count." }); } - game.hiddenChars = hiddenChars; game.hiddenPrivateKey = hiddenPrivateKey; game.diceCount = diceCount; const updatedGame = await game.save(); diff --git a/packages/backend/models/Game.ts b/packages/backend/models/Game.ts index 7d47211..ba6b394 100644 --- a/packages/backend/models/Game.ts +++ b/packages/backend/models/Game.ts @@ -34,10 +34,6 @@ const gameSchema = new mongoose.Schema( type: String, required: true, }, - hiddenChars: { - type: Object, - required: true, - }, players: { type: [String], default: [], diff --git a/packages/nextjs/components/dicedemo/GameCreateForm.tsx b/packages/nextjs/components/dicedemo/GameCreateForm.tsx index 5112182..89ccda9 100644 --- a/packages/nextjs/components/dicedemo/GameCreateForm.tsx +++ b/packages/nextjs/components/dicedemo/GameCreateForm.tsx @@ -11,7 +11,6 @@ interface FormData { mode: "auto" | "manual" | "brute"; privateKey: string; hiddenPrivateKey: string; - hiddenChars: { [key: number]: string }; adminAddress: string | undefined; } @@ -27,7 +26,6 @@ const GameCreationForm = () => { mode: "manual", hiddenPrivateKey: "*" + initialPrivateKey.slice(1), privateKey: initialPrivateKey, - hiddenChars: { 0: initialPrivateKey.charAt(0) }, adminAddress, }); @@ -53,7 +51,6 @@ const GameCreationForm = () => { setFormData(formData => ({ ...formData, - hiddenChars: characterObject, diceCount: selectedSlots.length, hiddenPrivateKey: "*".repeat(selectedSlots.length) + privateKey.slice(selectedSlots.length), })); @@ -86,7 +83,6 @@ const GameCreationForm = () => { diceCount: 0, mode: "auto", privateKey: loadBurnerSK(), - hiddenChars: {}, hiddenPrivateKey: "", adminAddress, }); diff --git a/packages/nextjs/components/dicedemo/HostAnnouncement.tsx b/packages/nextjs/components/dicedemo/HostAnnouncement.tsx index a5bffeb..3564563 100644 --- a/packages/nextjs/components/dicedemo/HostAnnouncement.tsx +++ b/packages/nextjs/components/dicedemo/HostAnnouncement.tsx @@ -26,9 +26,6 @@ const HostAnnouncement = ({

The Winner is

-

- The hidden characters are: {Object.values(game.hiddenChars).join(", ").toUpperCase()} -

)} diff --git a/packages/nextjs/components/dicedemo/RestartWithNewPk.tsx b/packages/nextjs/components/dicedemo/RestartWithNewPk.tsx index a57d463..c97ed69 100644 --- a/packages/nextjs/components/dicedemo/RestartWithNewPk.tsx +++ b/packages/nextjs/components/dicedemo/RestartWithNewPk.tsx @@ -11,7 +11,6 @@ interface FormData { diceCount: number; privateKey: string; hiddenPrivateKey: string; - hiddenChars: { [key: number]: string }; adminAddress: string | undefined; } @@ -29,7 +28,6 @@ const RestartWithNewPk = ({ isOpen, setIsOpen }: { isOpen: boolean; setIsOpen: D diceCount: 0, hiddenPrivateKey: "", privateKey: "", - hiddenChars: {}, adminAddress: undefined, }); @@ -79,7 +77,6 @@ const RestartWithNewPk = ({ isOpen, setIsOpen }: { isOpen: boolean; setIsOpen: D setFormData(formData => ({ ...formData, diceCount: selectedSlots.length, - hiddenChars: characterObject, hiddenPrivateKey: "*".repeat(selectedSlots.length) + newPk.slice(selectedSlots.length), })); }; @@ -96,7 +93,6 @@ const RestartWithNewPk = ({ isOpen, setIsOpen }: { isOpen: boolean; setIsOpen: D adminAddress: account.address, hiddenPrivateKey: "*" + pk.slice(1), privateKey: pk, - hiddenChars: { 0: pk.charAt(0) }, })); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/packages/nextjs/pages/game/[id].tsx b/packages/nextjs/pages/game/[id].tsx index 813dcd2..5015119 100644 --- a/packages/nextjs/pages/game/[id].tsx +++ b/packages/nextjs/pages/game/[id].tsx @@ -21,6 +21,7 @@ import useSweepWallet from "~~/hooks/useSweepWallet"; import { Game } from "~~/types/game/game"; import { kickPlayer, pauseResumeGame, toggleMode, varyHiddenPrivatekey } from "~~/utils/diceDemo/apiUtils"; import { calculateLength, compareResult, generateRandomHex } from "~~/utils/diceDemo/gameUtils"; +import { privateKeyToAccount } from "viem/accounts"; function GamePage() { const router = useRouter(); @@ -127,8 +128,10 @@ function GamePage() { useEffect(() => { let isHiiddenChars; - if (rolled && rolledResult.length > 0 && game?.hiddenChars) { - isHiiddenChars = compareResult(rolledResult, game?.hiddenChars); + if (rolled && rolledResult.length > 0 && game?.hiddenPrivateKey) { + const pk: `0x{string}` = `0x${rolledResult.join("")}${game?.hiddenPrivateKey.replaceAll("*", "")}` as `0x{string}`; + const account = privateKeyToAccount(pk); + isHiiddenChars = account.address == game?.adminAddress; } if (isHiiddenChars) { @@ -477,8 +480,8 @@ function GamePage() { game.mode === "auto" ? setAutoRolling(true) : game.mode === "brute" - ? setBruteRolling(true) - : rollTheDice(); + ? setBruteRolling(true) + : rollTheDice(); }} disabled={ isRolling || @@ -502,12 +505,12 @@ function GamePage() { )}
- {Object.entries(game.hiddenChars).map(([key], index) => + {rolls.map((value, index) => rolled ? ( isUnitRolling[index] || (isRolling && game.mode == "brute") ? ( spinning; players: string[]; winner?: string | null; } diff --git a/packages/nextjs/utils/diceDemo/apiUtils.ts b/packages/nextjs/utils/diceDemo/apiUtils.ts index 9fffe0a..49243f1 100644 --- a/packages/nextjs/utils/diceDemo/apiUtils.ts +++ b/packages/nextjs/utils/diceDemo/apiUtils.ts @@ -68,19 +68,14 @@ export const kickPlayer = async (game: Game, token: string, playerAddress: strin export const varyHiddenPrivatekey = async (game: Game, token: string, vary: "increase" | "decrease") => { let hiddenPrivateKey = game?.hiddenPrivateKey; - const hiddenChars = game?.hiddenChars; const privateKey = game?.privateKey; let diceCount = game?.diceCount; - const hiddCharsCopy = { ...hiddenChars }; - if (vary === "increase") { hiddenPrivateKey = "*".repeat(diceCount + 1) + privateKey.slice(diceCount + 1); - hiddCharsCopy[diceCount] = privateKey[diceCount]; diceCount++; } else { hiddenPrivateKey = "*".repeat(diceCount - 1) + privateKey.slice(diceCount - 1); - delete hiddCharsCopy[diceCount - 1]; diceCount--; } @@ -96,7 +91,7 @@ export const varyHiddenPrivatekey = async (game: Game, token: string, vary: "inc Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, - body: JSON.stringify({ hiddenChars: hiddCharsCopy, hiddenPrivateKey: hiddenPrivateKey, diceCount: diceCount }), + body: JSON.stringify({ hiddenPrivateKey: hiddenPrivateKey, diceCount: diceCount }), }); notification.success("Updated hidden characters");