Skip to content

Commit

Permalink
Merge pull request #5 from TLX-Protocol/fix-partner-codes
Browse files Browse the repository at this point in the history
Fix Partner Invite Codes Showing as Used
  • Loading branch information
chase-manning authored Jan 30, 2024
2 parents 36fc60d + f3ced9f commit d029709
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions functions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const messageToSign =
export const codeValidChars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
export const codeLength = 4;
export const invitesPerUser = 5;

export const partnerCodes = ["kirbycrypto"];
6 changes: 5 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { onRequest } from "firebase-functions/v2/https";
import admin from "firebase-admin";

import registrationHandler from "./registration";
import wrapHandler, { getUserAddress, validateParams } from "./utils";
import wrapHandler, { getUserAddress, isPartnerCode, validateParams } from "./utils";
import { userExists } from "./db";
import { APIError, SignedParams } from "./types";
import { defineSecret } from "firebase-functions/params";
Expand Down Expand Up @@ -53,6 +53,10 @@ export const hasRegistered = onRequest(
export const inviteCodeUsed = onRequest(
wrapHandler(async (request) => {
const { inviteCode } = validateParams<{ inviteCode: string }>(request.query, "inviteCode");

const isPartner = isPartnerCode(inviteCode);
if (isPartner) return false;

const db = admin.database();
const codeSnapshot = await db.ref("invites").child(inviteCode).get();
if (!codeSnapshot.exists()) {
Expand Down
5 changes: 2 additions & 3 deletions functions/src/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { DiscordService, TwitterService } from "./services";
import { APIError, RegistrationParams, Guild } from "./types";
import { Secrets, invitesPerUser, tlxGuidID } from "./constants";
import { generateAndSaveInviteCodes, useCode, userExists, usernameExists } from "./db";
import { getUserAddress, validateParams } from "./utils";
import { getUserAddress, isPartnerCode, validateParams } from "./utils";

const requiredKeys = ["twitterCode", "discordCode", "signature", "inviteCode"];
const partnerCodes = ["kirbycrypto"];

async function getTwitterUsername(code: string, secrets: Secrets): Promise<string> {
try {
Expand Down Expand Up @@ -50,7 +49,7 @@ export default async function registrationHandler(request: Request, secrets: Sec
}

// Validating invite code
const isPartner = partnerCodes.includes(params.inviteCode);
const isPartner = isPartnerCode(params.inviteCode);
if (!isPartner) {
const codeSnapshot = await db.ref("invites").child(params.inviteCode).get();
if (!codeSnapshot.exists()) {
Expand Down
6 changes: 5 additions & 1 deletion functions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import { codeValidChars, messageToSign } from "./constants";
import { codeValidChars, messageToSign, partnerCodes } from "./constants";
import { APIError, FunctionHandler, HTTPHandler } from "./types";
import * as cors from "cors";

Expand Down Expand Up @@ -45,3 +45,7 @@ export default function wrapHandler<T>(handler: FunctionHandler<T>): HTTPHandler
});
};
}

export function isPartnerCode(code: string): boolean {
return partnerCodes.includes(code.toLowerCase());
}

0 comments on commit d029709

Please sign in to comment.