Skip to content
19 changes: 18 additions & 1 deletion src/data/db/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export async function findClaimCode(code: string): Promise<ClaimCodeI | null> {
});
}


/**
* This function finds a gateway identity in the database
* @param {string} identity - The identity of the user to find
* @returns {Promise<GateWayIdentityI | null>} - The gateway identity, if found
*/
export async function findGatewayByIdentity(identity: string): Promise<GateWayIdentityI | null> {
return await prisma.gateWayIdentity.findFirst({
where: {
Expand Down Expand Up @@ -120,6 +126,12 @@ export async function findUpdatedRooms(roomIds: string[]): Promise<RoomI[]> {
});
}

/**
* This function finds a room with a message id
* @param {string} roomId - The id of the room to find
* @param {MessageI} message - The message id to find
* @returns {Promise<MessageI | null>} - A promise that resolves to a message
*/
export async function findRoomWithMessageId(
roomId: string,
message: MessageI
Expand Down Expand Up @@ -154,7 +166,12 @@ export async function findRoomWithMessageId(
}
}

export async function findAllJubmojiNullifiers() {

/**
* This function finds all the used jubmoji nullifiers in the db
* @returns {Promise<string[]>} - A promise that resolves to a list of used sig nullifiers
*/
export async function findAllJubmojiNullifiers(): Promise<string[]> {
const jubmojiNullifiers: Jubmojis[] = await prisma.gateWayIdentity.findMany({
select: {
jubmoji: true
Expand Down
2 changes: 0 additions & 2 deletions src/data/db/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export function removeIdentityFromRoom(
* @param {string} roomId - The id of the room to remove
* @returns {Promise<boolean>} - A promise that resolves to true if the room was removed and false otherwise
* */

export function removeRoom(roomId: string): Promise<boolean> {
return prisma.messages
.deleteMany({
Expand Down Expand Up @@ -82,7 +81,6 @@ export function removeRoom(roomId: string): Promise<boolean> {
* @param {string} messageId - The id of the message to remove
* @returns {Promise<boolean>} - A promise that resolves to true if the message was removed and false otherwise
*/

export function removeMessage(roomId: string, messageId: string) {
return prisma.messages
.deleteMany({
Expand Down
1 change: 0 additions & 1 deletion src/data/db/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ export async function addIdentityToBandadaRooms(
* @param {string} roomId - The ID of the room
* @param {string[]} ethAddresses - The list of Ethereum addresses to add to the group
*/

export async function createEthGroup(
name: string,
roomId: string,
Expand Down
18 changes: 18 additions & 0 deletions src/data/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type EphemeralMessagesI = Record<roomIdT, epoch>;

const ephemeralMessageStore: EphemeralMessagesI = {};

/**
* This code checks the ephemeral message store for a collision.
* @param {string} roomId - The ID of the room to check for collisions in
* @param {MessageI} message - The message to be added to the room
* @returns {MessageI | null} - Returns the old message if there is a collision, and null otherwise
*/
function checkEmphemeralStore(roomId: string, message: MessageI): MessageI | null {
// Check ephemeralMessages
const epoch = message.epoch?.toString();
Expand All @@ -34,6 +40,11 @@ function checkEmphemeralStore(roomId: string, message: MessageI): MessageI | nul
return null;
}

/**
* This code adds a message to the ephemeral message store.
* @param {string} roomId - The ID of the room to add the message to
* @param {MessageI} message - The message to add to the room
*/
function addMessageToEphemeralStore(roomId: string, message: MessageI) {
const currentEpoch = String(message.epoch);

Expand Down Expand Up @@ -137,6 +148,13 @@ export interface validateMessageResult {
idc?: string | bigint;
}

/**
* This code handles the collision check result and adds the message to the room if there is no collision.
* @param {RoomI} room - The room which the message will be added
* @param {MessageI} message - The message to be created
* @param {CollisionCheckResult} collisionResult - The result of the collision check
* @returns {Promise<validateMessageResult>} - A result object which contains a boolean indicating whether the operation was successful
*/
async function handleCollision(
room: RoomI,
message: MessageI,
Expand Down
4 changes: 1 addition & 3 deletions src/endpoints/gateways/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const discordPassword = process.env.DISCORD_PASSWORD

const adminAuth = basicAuth({
users: {
discordAdmin: discordPassword
admin: discordPassword
}
});

Expand Down Expand Up @@ -152,7 +152,6 @@ router.post(
filteredRooms.push(...newRooms);
filteredNames.push(...(newRoomNames as string[]));
}
console.log(filteredRooms);
res.status(200).json({ rooms: filteredRooms, roomNames: filteredNames });
} else {
const roomIds: string[] = [];
Expand Down Expand Up @@ -193,7 +192,6 @@ router.post(
*/
router.post('/rooms', limiter, adminAuth, (req, res) => {
const { discordUserId } = req.body as { discordUserId: string };
console.log('here');
prisma.gateWayIdentity
.findFirst({
where: {
Expand Down
34 changes: 21 additions & 13 deletions src/endpoints/gateways/ethereumGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ const adminAuth = basicAuth({
}
});


const router = express.Router();
const prisma = new PrismaClient();


// Fetches all ethereum groups that exist in the database
router.get('/groups/all', adminAuth, (req: Request, res: Response) => {
prisma.ethereumGroup
Expand Down Expand Up @@ -67,7 +69,7 @@ router.get('/group/:address', limiter, (req, res) => {
}
})
.then((groups) => {
res.status(200).json(groups);
res.status(200).json({status: 'valid', groups: groups});
})
.catch((err) => {
console.error(err);
Expand Down Expand Up @@ -95,15 +97,21 @@ router.post(
name: string;
roomIds: string[];
};
const ethereumGroup = await prisma.ethereumGroup.create({
data: {
name: name,
rooms: {
connect: roomIds.map((roomId) => ({ roomId }))
if (!name) res.status(400).json({ success: false, message: 'Missing name' });
try {
const ethereumGroup = await prisma.ethereumGroup.create({
data: {
name: name,
rooms: {
connect: roomIds.map((roomId) => ({ roomId }))
}
}
}
});
res.json({ success: true, ethereumGroup });
});
res.status(200).json({ success: true, message: 'Ethereum group created', ethereumGroup });
} catch (err) {
console.error(err);
res.status(500).json({ success: false, error: 'Internal Server Error' });
}
})
);

Expand All @@ -119,7 +127,7 @@ router.post(
names: string[];
ethAddresses: string[];
};
if (!names) return;
if (!names) res.status(400).json({ success: false, message: 'Missing group names' });
const groups = await prisma.ethereumGroup.updateMany({
where: {
name: {
Expand All @@ -132,7 +140,7 @@ router.post(
}
}
});
res.json({ success: true, groups });
res.status(200).json({ success: true, groups });
})
);

Expand Down Expand Up @@ -210,7 +218,7 @@ router.post('/group/delete', adminAuth, (req, res) => {
}
})
.then((group) => {
res.status(200).json(group);
res.status(200).json({ success: true, group });
})
.catch((err) => {
console.error(err);
Expand All @@ -231,7 +239,7 @@ router.post('/group/delete', adminAuth, (req, res) => {
* }
*/
router.post(
'/join',
'/message/sign',
limiter,
asyncHandler(async (req: Request, res: Response) => {
const { message, signature } = req.body as {
Expand Down
6 changes: 5 additions & 1 deletion src/gateways/theWord/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { groth16 } from 'snarkjs';
import vkey from './vkey';
import { SNARKProof } from 'idc-nullifier';


/**
* This function verifies a proof generated by theWord.
* @param {SNARKProof} proof - The proof to verify
* @returns {Promise<boolean>} - A promise that resolves to true if the proof is valid and false otherwise
*/
export async function verifyTheWordProof(proof: SNARKProof): Promise<boolean> {

const isValid = groth16.verify(vkey, proof.publicSignals, proof.proof);
Expand Down
Loading