Skip to content

Commit

Permalink
feat: add whitelist feature
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Jan 5, 2025
1 parent 797cb13 commit 6ca52e1
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ dist-ssr
*.sln
*.sw?
.vercel
whitelist-addresses.txt
4 changes: 3 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --fix",
"preview": "vite preview",
"codegen": "graphql-codegen"
"codegen": "graphql-codegen",
"generate-whitelist": "tsx script/generateWhitelistHashes.ts"
},
"dependencies": {
"@apollo/client": "^3.8.8",
Expand Down Expand Up @@ -90,6 +91,7 @@
"eslint-plugin-react-refresh": "^0.3.4",
"postcss": "^8.4.31",
"tailwindcss": "^3.3.5",
"tsx": "^4.19.2",
"typescript": "^5.4.5",
"vite": "^4.3.9",
"vitest": "^1.3.1"
Expand Down
59 changes: 59 additions & 0 deletions packages/client/script/generateWhitelistHashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { hash } from "starknet";
import * as fs from "fs/promises";
import * as path from "path";

function generateHash(address: string): string {
// Convert address to decimal if it's in hex format
const addressBN = BigInt(address);
return hash.computeHashOnElements([addressBN.toString()]).toString();
}

async function readAddresses(): Promise<string[]> {
const addressFile = path.join(process.cwd(), "script/whitelist-addresses.txt");
const content = await fs.readFile(addressFile, "utf8");

return content
.split("\n")
.map(line => line.trim())
.filter(line => line && !line.startsWith("#")); // Remove empty lines and comments
}

async function main() {
const walletAddresses = await readAddresses();

if (walletAddresses.length === 0) {
console.log("Please add wallet addresses to script/whitelist-addresses.txt");
console.log("Each address should be on a new line");
console.log("Lines starting with # are treated as comments");
return;
}

const hashes = walletAddresses.map((address) => ({
address,
hash: generateHash(address),
}));

// Create the whitelist file content
const whitelistContent = `// This file is auto-generated by generateWhitelistHashes.ts
// DO NOT EDIT DIRECTLY
export const whitelistedHashes: string[] = [
${hashes.map((item) => ` "${item.hash}", // ${item.address}`).join("\n")}
];
`;

// Save to utils directory
const outputFile = path.join(process.cwd(), "src/utils/whitelist.generated.ts");
await fs.writeFile(outputFile, whitelistContent, "utf8");

// Print summary
console.log("Generated hashes for", hashes.length, "addresses");
if (hashes.length > 0) {
console.log("\nSample output:");
console.log("Address:", hashes[0].address);
console.log("Hash:", hashes[0].hash);
}
console.log("\nWhitelist saved to:", outputFile);
}

main().catch(console.error);
22 changes: 19 additions & 3 deletions packages/client/src/ui/features/login/ConnectWalletDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { braavos, argent, useAccount, useConnect } from "@starknet-react/core";
import {
braavos,
argent,
useAccount,
useConnect,
useDisconnect,
} from "@starknet-react/core";
import { ShowItem, usePersistUIStore, useUIStore } from "../../../store";
import { Dialog } from "../../components/Dialog";
import { useMemo } from "react";
import { usePlayerProfile } from "../../hooks/usePlayerProfile";
import { logDebug } from "../../lib/utils";
import { isInWhiteList } from "../../../utils/validateWhitelist";

export function ConnectWalletDialog() {
const show = useUIStore((state) =>
Expand All @@ -14,7 +21,8 @@ export function ConnectWalletDialog() {
const setLoggedIn = usePersistUIStore((state) => state.setLoggedIn);

const { connect } = useConnect();
const { isConnected } = useAccount();
const { disconnect } = useDisconnect();
const { isConnected, account } = useAccount();
const { playerName } = usePlayerProfile();

logDebug(
Expand All @@ -29,14 +37,22 @@ export function ConnectWalletDialog() {

useMemo(() => {
if (isConnected) {
if (!isInWhiteList(account)) {
alert(
"Sorry, your wallet is not in the whitelist yet. Please stay tuned for future updates!"
);
disconnect();
setShow(ShowItem.ConnectWalletDialog, false);
return;
}
setLoggedIn(true);
setShow(ShowItem.ConnectWalletDialog, false);
if (!playerName) {
logDebug("show SessionWalletCreate");
setShow(ShowItem.SessionWalletCreate, true);
}
}
}, [isConnected, setLoggedIn, setShow, playerName]);
}, [isConnected, setLoggedIn, setShow, playerName, account, disconnect]);

if (!show) return null;

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/ui/features/login/UnConnected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export function UnConnected() {
<img src="/assets/ui/zenith.png" />
</div>

<GreenButton
{/* <GreenButton
className="self-center w-[60%] h-16 mt-20 text-xl"
onClick={() => {
setShow(ShowItem.GuestTips, true);
}}
>
Login As Guest
</GreenButton>
</GreenButton> */}

<GreenButton
className="self-center w-[60%] h-16 mt-12 text-xl"
Expand Down
13 changes: 13 additions & 0 deletions packages/client/src/utils/validateWhitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AccountInterface } from "starknet";
import { hash } from "starknet";
import { whitelistedHashes } from "./whitelist.generated";

export function isInWhiteList(account: AccountInterface | undefined): boolean {
if (!account) {
return false;
}
// Convert address to checksum format and hash it
const addressHash = hash.computeHashOnElements([account.address]);
// Check if the hash exists in the whitelist
return whitelistedHashes.includes(addressHash.toString());
}
6 changes: 6 additions & 0 deletions packages/client/src/utils/whitelist.generated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is auto-generated by generateWhitelistHashes.ts
// DO NOT EDIT DIRECTLY

export const whitelistedHashes: string[] = [
"0x2917789a9bdf8816372c5563cc384c57f18283c45ed4839cfa7e65832d705a7", // 0x061aaefa3a5025ff8350642b71328a2eb0c9cdddc6f9aed360ebd39dba481eec
];
7 changes: 5 additions & 2 deletions packages/client/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"target": "ES2020",
"lib": ["ES2020"]
},
"include": ["vite.config.ts"]
"include": ["vite.config.ts", "script/generateWhitelistHashes.ts"]
}
Loading

0 comments on commit 6ca52e1

Please sign in to comment.