From 67944d9eafbb85ea0c27bdc33bca437cc636b688 Mon Sep 17 00:00:00 2001 From: Tanner Shaw Date: Thu, 24 Aug 2023 23:48:44 -0500 Subject: [PATCH] refactor(claimCodes) fixed the /addcode route to properly generate codes for all rooms or a single room --- package-lock.json | 11 ++-- package.json | 4 +- src/crypto/verifier.ts | 2 +- src/data/messages.ts | 2 +- src/endpoints/index.ts | 121 +++++++++++++++++++++++------------------ 5 files changed, 80 insertions(+), 60 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0267183..5cf2862 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "body-parser": "^1.20.2", "cors": "^2.8.5", "discreetly-claimcodes": "^1.1.5", - "discreetly-interfaces": "^0.1.37", + "discreetly-interfaces": "^0.1.38", "dotenv": "^16.3.1", "express": "^4.18.2", "express-basic-auth": "^1.2.1", @@ -3469,10 +3469,13 @@ "integrity": "sha512-pQueoGtBJk/FrTfGzepjqYfTLaymS+4t11byI4OcfjWQOagRsD7dtavGcowTVQ7Ib/vjKna5T+71WcgWZaAWuA==" }, "node_modules/discreetly-interfaces": { - "version": "0.1.37", - "resolved": "https://registry.npmjs.org/discreetly-interfaces/-/discreetly-interfaces-0.1.37.tgz", - "integrity": "sha512-qUbUcmckpuiFggVfkQqDA7e56qhMEp7X8Ug9xbU/xG6P5rQK7PewqCDYUixXJFfby3RHByGNju9xPOtv1FsW0g==", + "version": "0.1.38", + "resolved": "https://registry.npmjs.org/discreetly-interfaces/-/discreetly-interfaces-0.1.38.tgz", + "integrity": "sha512-lsRQgKNe7hc8kwA0cBMItUBXHHdgZr95vb9F1YWjS+01rzL2RWo96+4G1wQSBMK3p22VllWILeCAgMLn2ClA9Q==", "dependencies": { + "@ethersproject/bytes": "^5.7.0", + "@ethersproject/keccak256": "^5.7.0", + "@ethersproject/strings": "^5.7.0", "poseidon-lite": "^0.2.0", "rlnjs": "^3.1.4" } diff --git a/package.json b/package.json index c0cb2bf..cd0c0f8 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "body-parser": "^1.20.2", "cors": "^2.8.5", "discreetly-claimcodes": "^1.1.5", - "discreetly-interfaces": "^0.1.37", + "discreetly-interfaces": "^0.1.38", "dotenv": "^16.3.1", "express": "^4.18.2", "express-basic-auth": "^1.2.1", @@ -68,4 +68,4 @@ "ts-node": "^10.9.1", "typescript": "^5.1.6" } -} \ No newline at end of file +} diff --git a/src/crypto/verifier.ts b/src/crypto/verifier.ts index 63c3a80..b4c6add 100644 --- a/src/crypto/verifier.ts +++ b/src/crypto/verifier.ts @@ -16,7 +16,7 @@ async function verifyProof(msg: MessageI, room: RoomI, epochErrorRange = 5): Pro const rateLimit = room.rateLimit ? room.rateLimit : 1000; const currentEpoch = Math.floor(timestamp / rateLimit); const rlnIdentifier = BigInt(msg.roomId); - const msgHash = calculateSignalHash(msg.message); + const msgHash = calculateSignalHash(JSON.stringify(msg.message)); let proof: RLNFullProof | undefined; // Check that the epoch falls within the range for the room const epoch = BigInt(msg.epoch); diff --git a/src/data/messages.ts b/src/data/messages.ts index 50c4311..b6ebe49 100644 --- a/src/data/messages.ts +++ b/src/data/messages.ts @@ -110,7 +110,7 @@ function addMessageToRoom(roomId: string, message: MessageI): Promise { epoch: String(message.epoch), messages: { create: { - message: message.message ? message.message.toString() : '', + message: message.message ? JSON.stringify(message.message) : '', messageId: message.messageId ? message.messageId.toString() : '', proof: JSON.stringify(message.proof), roomId: roomId diff --git a/src/endpoints/index.ts b/src/endpoints/index.ts index 78ef269..c01e316 100644 --- a/src/endpoints/index.ts +++ b/src/endpoints/index.ts @@ -51,7 +51,10 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { .then((room: RoomI) => { if (!room) { // This is set as a timeout to prevent someone from trying to brute force room ids - setTimeout(() => res.status(500).json({ error: 'Internal Server Error' }), 1000); + setTimeout( + () => res.status(500).json({ error: 'Internal Server Error' }), + 1000 + ); } else { const { roomId, @@ -133,7 +136,9 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { const parsedBody: JoinData = req.body as JoinData; if (!parsedBody.code || !parsedBody.idc) { - res.status(400).json({ message: '{code: string, idc: string} expected' }); + res + .status(400) + .json({ message: '{code: string, idc: string} expected' }); } const { code, idc } = parsedBody; console.debug('Invite Code:', code); @@ -311,24 +316,29 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { const codes = genClaimCodeArray(numCodes); return await prisma.rooms.findMany(query).then((rooms) => { const roomIds = rooms.map((room) => room.id); - - const createCodes = codes.map(async (code, index) => { - return await prisma.claimCodes.create({ - data: { - claimcode: code.claimcode, - claimed: false, - roomIds: roomIds, - rooms: { - connect: { - roomId: rooms[index].roomId ? rooms[index].roomId : undefined + console.log(roomIds); + console.log(codes); + const createCodes = rooms.flatMap((room) => + codes.map((code) => + prisma.claimCodes.create({ + data: { + claimcode: code.claimcode, + claimed: false, + roomIds: [room.id], + rooms: { + connect: { + roomId: room.roomId + } } } - } - }); - }); + }) + ) + ); return Promise.all(createCodes) .then(() => { - res.status(200).json({ message: 'Claim codes added successfully', codes }); + res + .status(200) + .json({ message: 'Claim codes added successfully', codes }); }) .catch((err) => { console.error(err); @@ -349,46 +359,50 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { * } */ - app.post(['/room/:roomId/addcode', '/api/room/:roomId/addcode'], adminAuth, (req, res) => { - const { roomId } = req.params; - const { numCodes } = req.body as { numCodes: number }; - const codes = genClaimCodeArray(numCodes); + app.post( + ['/room/:roomId/addcode', '/api/room/:roomId/addcode'], + adminAuth, + (req, res) => { + const { roomId } = req.params; + const { numCodes } = req.body as { numCodes: number }; + const codes = genClaimCodeArray(numCodes); - prisma.rooms - .findUnique({ - where: { roomId: roomId }, - include: { claimCodes: true } - }) - .then((room) => { - if (!room) { - res.status(404).json({ error: 'Room not found' }); - return; - } - // Map over the codes array and create a claim code for each code - const createCodes = codes.map((code) => { - return prisma.claimCodes.create({ - data: { - claimcode: code.claimcode, - claimed: false, - rooms: { - connect: { - roomId: roomId + prisma.rooms + .findUnique({ + where: { roomId: roomId }, + include: { claimCodes: true } + }) + .then((room) => { + if (!room) { + res.status(404).json({ error: 'Room not found' }); + return; + } + // Map over the codes array and create a claim code for each code + const createCodes = codes.map((code) => { + return prisma.claimCodes.create({ + data: { + claimcode: code.claimcode, + claimed: false, + rooms: { + connect: { + roomId: roomId + } } } - } + }); }); - }); - return Promise.all(createCodes); - }) - .then(() => { - res.status(200).json({ message: 'Claim codes added successfully' }); - }) - .catch((err) => { - console.error(err); - res.status(500).json({ error: 'Internal Server Error' }); - }); - }); + return Promise.all(createCodes); + }) + .then(() => { + res.status(200).json({ message: 'Claim codes added successfully' }); + }) + .catch((err) => { + console.error(err); + res.status(500).json({ error: 'Internal Server Error' }); + }); + } + ); // This code fetches the claim codes from the database. @@ -434,7 +448,10 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { '/admin/message', adminAuth, asyncHandler(async (req: Request, res: Response) => { - const { message, roomId } = req.body as { message: string; roomId?: string }; + const { message, roomId } = req.body as { + message: string; + roomId?: string; + }; try { // Function to send system messages