Skip to content

Commit

Permalink
Remove hiddenChars and use privateKeyToAccount to validate PK found (#54
Browse files Browse the repository at this point in the history
)
  • Loading branch information
damianmarti authored Jul 25, 2024
1 parent 6aac4b3 commit 3af9ce4
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 37 deletions.
9 changes: 3 additions & 6 deletions packages/backend/controllers/Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -54,7 +54,6 @@ export const createGame = async (req: Request, res: Response) => {
mode,
privateKey,
hiddenPrivateKey,
hiddenChars,
});

let token;
Expand All @@ -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);

Expand All @@ -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";
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions packages/backend/models/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const gameSchema = new mongoose.Schema(
type: String,
required: true,
},
hiddenChars: {
type: Object,
required: true,
},
players: {
type: [String],
default: [],
Expand Down
4 changes: 0 additions & 4 deletions packages/nextjs/components/dicedemo/GameCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface FormData {
mode: "auto" | "manual" | "brute";
privateKey: string;
hiddenPrivateKey: string;
hiddenChars: { [key: number]: string };
adminAddress: string | undefined;
}

Expand All @@ -27,7 +26,6 @@ const GameCreationForm = () => {
mode: "manual",
hiddenPrivateKey: "*" + initialPrivateKey.slice(1),
privateKey: initialPrivateKey,
hiddenChars: { 0: initialPrivateKey.charAt(0) },
adminAddress,
});

Expand All @@ -53,7 +51,6 @@ const GameCreationForm = () => {

setFormData(formData => ({
...formData,
hiddenChars: characterObject,
diceCount: selectedSlots.length,
hiddenPrivateKey: "*".repeat(selectedSlots.length) + privateKey.slice(selectedSlots.length),
}));
Expand Down Expand Up @@ -86,7 +83,6 @@ const GameCreationForm = () => {
diceCount: 0,
mode: "auto",
privateKey: loadBurnerSK(),
hiddenChars: {},
hiddenPrivateKey: "",
adminAddress,
});
Expand Down
3 changes: 0 additions & 3 deletions packages/nextjs/components/dicedemo/HostAnnouncement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const HostAnnouncement = ({
</label>
<p className="text-center">The Winner is</p>
<Address address={game.winner as string} format="long" />
<p className="mt-5 text-2xl text-center">
The hidden characters are: {Object.values(game.hiddenChars).join(", ").toUpperCase()}
</p>
</div>
</div>
)}
Expand Down
4 changes: 0 additions & 4 deletions packages/nextjs/components/dicedemo/RestartWithNewPk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface FormData {
diceCount: number;
privateKey: string;
hiddenPrivateKey: string;
hiddenChars: { [key: number]: string };
adminAddress: string | undefined;
}

Expand All @@ -29,7 +28,6 @@ const RestartWithNewPk = ({ isOpen, setIsOpen }: { isOpen: boolean; setIsOpen: D
diceCount: 0,
hiddenPrivateKey: "",
privateKey: "",
hiddenChars: {},
adminAddress: undefined,
});

Expand Down Expand Up @@ -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),
}));
};
Expand All @@ -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
}, []);
Expand Down
21 changes: 12 additions & 9 deletions packages/nextjs/pages/game/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -477,8 +480,8 @@ function GamePage() {
game.mode === "auto"
? setAutoRolling(true)
: game.mode === "brute"
? setBruteRolling(true)
: rollTheDice();
? setBruteRolling(true)
: rollTheDice();
}}
disabled={
isRolling ||
Expand All @@ -502,12 +505,12 @@ function GamePage() {
</div>
)}
<div className="flex flex-wrap justify-center gap-2 mt-8 py-2">
{Object.entries(game.hiddenChars).map(([key], index) =>
{rolls.map((value, index) =>
rolled ? (
isUnitRolling[index] || (isRolling && game.mode == "brute") ? (
<Image
className="transition duration-500 opacity-100 rounded-lg"
key={key}
key={index}
src="/rolls-gif/Spin.gif"
alt="spinning"
width={length}
Expand All @@ -516,8 +519,8 @@ function GamePage() {
) : (
<Image
className="transition duration-500 ease-in rounded-lg"
key={key}
src={`/rolls-jpg/${rolls[index]}.jpg`}
key={index}
src={`/rolls-jpg/${value}.jpg`}
alt="rolled"
width={length}
height={length}
Expand All @@ -526,7 +529,7 @@ function GamePage() {
) : (
<Image
className="rounded-lg"
key={key}
key={index}
src={`/rolls-jpg/0.jpg`}
alt="zero roll"
width={length}
Expand Down
1 change: 0 additions & 1 deletion packages/nextjs/types/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface Game {
mode: "auto" | "manual" | "brute";
privateKey: string;
hiddenPrivateKey: string;
hiddenChars: Record<string, any>;
players: string[];
winner?: string | null;
}
7 changes: 1 addition & 6 deletions packages/nextjs/utils/diceDemo/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}

Expand All @@ -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");
Expand Down

0 comments on commit 3af9ce4

Please sign in to comment.