refactor(messages) making createMessage into a promise to resolve pro… (#45)

…ofing and collision issues
This commit is contained in:
AtHeartEngineer
2023-08-22 17:54:00 -04:00
committed by GitHub
3 changed files with 71 additions and 50 deletions

View File

@@ -53,7 +53,7 @@ async function verifyProof(msg: MessageI, room: RoomI, epochErrorRange = 5): Pro
// Check that the merkle root is correct
if (room.identities && Array.isArray(room.identities)) {
const group = new Group(room.id, 20, room.identities as bigint[] | undefined);
const group = new Group(room.roomId, 20, room.identities as bigint[] | undefined);
if (group.root !== proof.snarkProof.publicSignals.root) {
console.warn("GROUP ROOT DOESN'T MATCH PROOF ROOT; USE BANDADA");
}

View File

@@ -1,7 +1,10 @@
import { getRoomByID, removeIdentityFromRoom } from './db';
import { PrismaClient } from '@prisma/client';
import { MessageI } from 'discreetly-interfaces';
import { shamirRecovery, getIdentityCommitmentFromSecret } from '../crypto/shamirRecovery';
import {
shamirRecovery,
getIdentityCommitmentFromSecret
} from '../crypto/shamirRecovery';
import { RLNFullProof } from 'rlnjs';
const prisma = new PrismaClient();
@@ -12,7 +15,10 @@ interface CollisionCheckResult {
oldMessage?: MessageI;
}
async function checkRLNCollision(roomId: string, message: MessageI): Promise<CollisionCheckResult> {
async function checkRLNCollision(
roomId: string,
message: MessageI
): Promise<CollisionCheckResult> {
return new Promise((res) => {
prisma.rooms
.findFirst({
@@ -38,8 +44,12 @@ async function checkRLNCollision(roomId: string, message: MessageI): Promise<Col
const oldMessageProof = JSON.parse(
oldMessage.epochs[0].messages[0].proof
) as RLNFullProof;
const oldMessagex2 = BigInt(oldMessageProof.snarkProof.publicSignals.x);
const oldMessagey2 = BigInt(oldMessageProof.snarkProof.publicSignals.y);
const oldMessagex2 = BigInt(
oldMessageProof.snarkProof.publicSignals.x
);
const oldMessagey2 = BigInt(
oldMessageProof.snarkProof.publicSignals.y
);
let proof: RLNFullProof;
@@ -98,45 +108,55 @@ export interface createMessageResult {
idc?: string | bigint;
}
export function createMessage(roomId: string, message: MessageI): createMessageResult {
getRoomByID(roomId)
.then((room) => {
if (room) {
// Todo This should check that there is no duplicate messageId with in this room and epoch,
// if there is, we need to return an error and
// reconstruct the secret from both messages, and ban the user
checkRLNCollision(roomId, message)
.then((collisionResult) => {
if (!collisionResult.collision) {
addMessageToRoom(roomId, message)
.then(() => {
return { success: true };
})
.catch((error) => {
console.error(`Couldn't add message room ${error}`);
return false;
});
} else {
console.debug('Collision found');
const identityCommitment = getIdentityCommitmentFromSecret(collisionResult.secret!);
removeIdentityFromRoom(identityCommitment.toString(), room)
.then(() => {
return { success: false };
})
.catch((error) => {
console.error(`Couldn't remove identity from room ${error}`);
});
}
})
.catch((error) => {
console.error(`Error getting room: ${error}`);
return { success: false };
});
}
})
.catch((error) => {
console.error(`Error getting room: ${error}`);
return { success: false };
});
return { success: false };
export function createMessage(
roomId: string,
message: MessageI
): Promise<createMessageResult> {
return new Promise((resolve, reject) => {
getRoomByID(roomId)
.then(async (room) => {
if (room) {
// Todo This should check that there is no duplicate messageId with in this room and epoch,
// if there is, we need to return an error and
// reconstruct the secret from both messages, and ban the user
await checkRLNCollision(roomId, message)
.then((collisionResult) => {
console.log('HERE', collisionResult);
if (!collisionResult.collision) {
addMessageToRoom(roomId, message)
.then(() => {
console.log('Message added to room');
return resolve({ success: true });
})
.catch((error) => {
console.error(`Couldn't add message room ${error}`);
return reject({ success: false });
});
} else {
console.debug('Collision found');
const identityCommitment = getIdentityCommitmentFromSecret(
collisionResult.secret!
);
removeIdentityFromRoom(identityCommitment.toString(), room)
.then(() => {
return reject({ success: false });
})
.catch((error) => {
console.error(
`Couldn't remove identity from room ${error}`
);
});
}
})
.catch((error) => {
console.error(`Error getting room: ${error}`);
return reject({ success: false });
});
}
})
.catch((error) => {
console.error(`Error getting room: ${error}`);
return reject({ success: false });
});
});
}

View File

@@ -11,20 +11,21 @@ export function websocketSetup(io: SocketIOServer) {
io.on('connection', (socket: Socket) => {
pp('SocketIO: a user connected', 'debug');
socket.on('validateMessage', (msg: MessageI) => {
socket.on('validateMessage', async (msg: MessageI) => {
pp({ 'VALIDATING MESSAGE ID': String(msg.roomId).slice(0, 11), 'MSG:': msg.message });
let validProof: boolean;
getRoomByID(String(msg.roomId))
await getRoomByID(String(msg.roomId))
.then((room: RoomI) => {
if (!room) {
pp('INVALID ROOM', 'warn');
return;
}
verifyProof(msg, room)
.then((v) => {
.then(async (v) => {
console.log('validProof', v)
validProof = v;
// TODO import createMessageResult, and broadcast the idc and message ID that were removed to those room users
const validMessage: createMessageResult = createMessage(String(msg.roomId), msg);
const validMessage: createMessageResult = await createMessage(String(msg.roomId), msg);
if (!validProof || !validMessage.success) {
pp('INVALID MESSAGE', 'warn');
return;