mirror of
https://github.com/Discreetly/server.git
synced 2026-01-10 04:57:57 -05:00
feature(bandada) /join route now checks for membership types
chore(crypto) changed some logic in the verifier that was throwing eslint errors
This commit is contained in:
8
package-lock.json
generated
8
package-lock.json
generated
@@ -14,7 +14,7 @@
|
||||
"body-parser": "^1.20.2",
|
||||
"cors": "^2.8.5",
|
||||
"discreetly-claimcodes": "^1.1.5",
|
||||
"discreetly-interfaces": "^0.1.29",
|
||||
"discreetly-interfaces": "^0.1.34",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"express-basic-auth": "^1.2.1",
|
||||
@@ -3470,9 +3470,9 @@
|
||||
"integrity": "sha512-pQueoGtBJk/FrTfGzepjqYfTLaymS+4t11byI4OcfjWQOagRsD7dtavGcowTVQ7Ib/vjKna5T+71WcgWZaAWuA=="
|
||||
},
|
||||
"node_modules/discreetly-interfaces": {
|
||||
"version": "0.1.29",
|
||||
"resolved": "https://registry.npmjs.org/discreetly-interfaces/-/discreetly-interfaces-0.1.29.tgz",
|
||||
"integrity": "sha512-3R63KkmB+wFKFFzD9DixX3VDoLCYkDuMQZucAItmXbjE+0tFgmrK683a1/WBI9VkBhARip6HsVNrjzaGqdR1Aw==",
|
||||
"version": "0.1.34",
|
||||
"resolved": "https://registry.npmjs.org/discreetly-interfaces/-/discreetly-interfaces-0.1.34.tgz",
|
||||
"integrity": "sha512-7purPOWOowVH44ebdweBdZ4z2RsBQy5/H7xi6PdsHkaw1xwg8u3Ev2US5EdavP1igZ+SzebJdK8jT0ZTjzX8Kg==",
|
||||
"dependencies": {
|
||||
"poseidon-lite": "^0.2.0",
|
||||
"rlnjs": "^3.1.4"
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
"body-parser": "^1.20.2",
|
||||
"cors": "^2.8.5",
|
||||
"discreetly-claimcodes": "^1.1.5",
|
||||
"discreetly-interfaces": "^0.1.29",
|
||||
"discreetly-interfaces": "^0.1.34",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"express-basic-auth": "^1.2.1",
|
||||
@@ -64,4 +64,4 @@
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.1.6"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { createRoom } from '../src/data/db';
|
||||
|
||||
function main() {
|
||||
createRoom('1 Second Room', 1000, 1, 10);
|
||||
createRoom('10 Second Room', 10000, 2, 10);
|
||||
createRoom('100 Second Room', 100000, 10, 10);
|
||||
async function main(){
|
||||
await createRoom('1 Second Room', 1000, 1, 10, 20, 'PUBLIC');
|
||||
await createRoom('10 Second Room', 10000, 2, 10, 20, 'PUBLIC');
|
||||
await createRoom('100 Second Room', 100000, 10, 10, 20, 'PUBLIC');
|
||||
}
|
||||
|
||||
main();
|
||||
await main();
|
||||
|
||||
@@ -29,12 +29,20 @@ async function verifyProof(msg: MessageI, room: RoomI, epochErrorRange = 5): Pro
|
||||
// TODO! INTERNAL NULLIFIER (RLNjs cache)
|
||||
|
||||
// Check that the message hash is correct
|
||||
if (msgHash !== msg.proof.snarkProof.publicSignals.x) {
|
||||
let proof: RLNFullProof;
|
||||
|
||||
if (typeof msg.proof === 'string') {
|
||||
proof = JSON.parse(msg.proof) as RLNFullProof;
|
||||
} else {
|
||||
proof = msg.proof;
|
||||
}
|
||||
|
||||
if (msgHash !== proof.snarkProof.publicSignals.x) {
|
||||
console.warn(
|
||||
'Message hash incorrect:',
|
||||
msgHash,
|
||||
'Hash in proof:',
|
||||
msg.proof.snarkProof.publicSignals.x
|
||||
proof.snarkProof.publicSignals.x
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -42,7 +50,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);
|
||||
if (group.root !== msg.proof.snarkProof.publicSignals.root) {
|
||||
if (group.root !== proof.snarkProof.publicSignals.root) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,28 +92,59 @@ export function updateClaimCode(code: string): Promise<RoomsFromClaimCode> {
|
||||
});
|
||||
}
|
||||
|
||||
export function updateRoomIdentities(idc: string, roomIds: string[]): Promise<any> {
|
||||
export async function updateRoomIdentities(idc: string, roomIds: string[]): Promise<any> {
|
||||
return prisma.rooms
|
||||
.findMany({
|
||||
where: { id: { in: roomIds } }
|
||||
})
|
||||
.then((rooms) => {
|
||||
const roomsToUpdate = rooms
|
||||
.filter((room) => !room.identities.includes(idc))
|
||||
const identityListRooms = rooms
|
||||
.filter((room) => room.membershipType === "IDENTITY_LIST" && !room.identities.includes(idc))
|
||||
.map((room) => room.id);
|
||||
|
||||
if (roomsToUpdate) {
|
||||
if (identityListRooms.length > 0) {
|
||||
return prisma.rooms.updateMany({
|
||||
where: { id: { in: roomsToUpdate } },
|
||||
where: { id: { in: identityListRooms } },
|
||||
data: { identities: { push: idc } }
|
||||
});
|
||||
}
|
||||
const bandadaGroupRooms = rooms
|
||||
.filter((room) => room.membershipType === "BANDADA_GROUP" && !room.identities.includes(idc))
|
||||
.map((room) => room);
|
||||
|
||||
if (bandadaGroupRooms.length > 0) {
|
||||
bandadaGroupRooms.forEach((room) => {
|
||||
if (!room.bandadaAPIKey) {
|
||||
console.error("API key is missing for room:", room);
|
||||
return;
|
||||
}
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': room.bandadaAPIKey,
|
||||
},
|
||||
};
|
||||
|
||||
const url = `https://api.bandada.pse.dev/groups/${room.bandadaAddress}/members/${idc}`;
|
||||
|
||||
fetch(url, requestOptions)
|
||||
.then((res) => {
|
||||
if (res.status == 200) {
|
||||
console.log(`Successfully added user to Bandada group ${room.bandadaAddress}`);
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
pp(err, 'error');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function findUpdatedRooms(roomIds: string[]): Promise<RoomI[]> {
|
||||
const rooms = await prisma.rooms.findMany({
|
||||
where: { id: { in: roomIds } }
|
||||
@@ -161,28 +192,34 @@ export function createSystemMessages(message: string, roomId?: string): Promise<
|
||||
* @param {number} [approxNumMockUsers=20] - The approximate number of mock users to generate for the room.
|
||||
*/
|
||||
export async function createRoom(
|
||||
name: string,
|
||||
roomName: string,
|
||||
rateLimit = 1000,
|
||||
userMessageLimit = 1,
|
||||
numClaimCodes = 0,
|
||||
approxNumMockUsers = 20,
|
||||
type: string
|
||||
type: string,
|
||||
bandadaAddress?: string,
|
||||
bandadaAPIKey?: string,
|
||||
membershipType?: string
|
||||
): Promise<boolean> {
|
||||
const claimCodes: { claimcode: string }[] = genClaimCodeArray(numClaimCodes);
|
||||
console.log(claimCodes);
|
||||
const mockUsers: string[] = genMockUsers(approxNumMockUsers);
|
||||
const roomData = {
|
||||
where: {
|
||||
roomId: genId(serverConfig.id as bigint, name).toString()
|
||||
roomId: genId(serverConfig.id as bigint, roomName).toString()
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
roomId: genId(serverConfig.id as bigint, name).toString(),
|
||||
name: name,
|
||||
roomId: genId(serverConfig.id as bigint, roomName).toString(),
|
||||
name: roomName,
|
||||
rateLimit: rateLimit,
|
||||
userMessageLimit: userMessageLimit,
|
||||
identities: mockUsers,
|
||||
type,
|
||||
bandadaAddress,
|
||||
bandadaAPIKey,
|
||||
membershipType,
|
||||
claimCodes: {
|
||||
create: claimCodes
|
||||
}
|
||||
|
||||
@@ -125,6 +125,9 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
numClaimCodes?: number;
|
||||
approxNumMockUsers?: number;
|
||||
roomType?: string;
|
||||
bandadaAddress?: string;
|
||||
bandadaAPIKey?: string;
|
||||
membershipType?: string;
|
||||
}
|
||||
|
||||
/* ~~~~ ADMIN ENDPOINTS ~~~~ */
|
||||
@@ -138,13 +141,19 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
const numClaimCodes = roomMetadata.numClaimCodes ?? 0;
|
||||
const approxNumMockUsers = roomMetadata.approxNumMockUsers;
|
||||
const type = roomMetadata.roomType as unknown as string;
|
||||
const bandadaAddress = roomMetadata.bandadaAddress;
|
||||
const bandadaAPIKey = roomMetadata.bandadaAPIKey;
|
||||
const membershipType = roomMetadata.membershipType;
|
||||
createRoom(
|
||||
roomName,
|
||||
rateLimit,
|
||||
userMessageLimit,
|
||||
numClaimCodes,
|
||||
approxNumMockUsers,
|
||||
type
|
||||
type,
|
||||
bandadaAddress,
|
||||
bandadaAPIKey,
|
||||
membershipType
|
||||
)
|
||||
.then((result) => {
|
||||
console.log(result);
|
||||
|
||||
Reference in New Issue
Block a user