fix signal hash

This commit is contained in:
2023-08-22 14:46:04 -04:00
parent 3310a65b82
commit ce87d6cb04
4 changed files with 21 additions and 2 deletions

3
package-lock.json generated
View File

@@ -9,6 +9,9 @@
"version": "0.1.2",
"license": "ISC",
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@ethersproject/strings": "^5.7.0",
"@faker-js/faker": "^8.0.2",
"@prisma/client": "^5.0.0",
"body-parser": "^1.20.2",

View File

@@ -23,6 +23,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@ethersproject/strings": "^5.7.0",
"@faker-js/faker": "^8.0.2",
"@prisma/client": "^5.0.0",
"body-parser": "^1.20.2",

13
src/crypto/signalHash.ts Normal file
View File

@@ -0,0 +1,13 @@
import { hexlify } from '@ethersproject/bytes';
import { toUtf8Bytes } from '@ethersproject/strings';
import { keccak256 } from '@ethersproject/keccak256';
/**
* Hashes a signal string with Keccak256.
* @param signal The RLN signal.
* @returns The signal hash.
*/
export function calculateSignalHash(signal: string): bigint {
const converted = hexlify(toUtf8Bytes(signal));
return BigInt(keccak256(converted)) >> BigInt(8);
}

View File

@@ -1,8 +1,8 @@
import type { MessageI, RoomI } from 'discreetly-interfaces';
import { str2BigInt } from 'discreetly-interfaces';
import { RLNFullProof, RLNVerifier } from 'rlnjs';
import vkey from './verification_key';
import { Group } from '@semaphore-protocol/group';
import { calculateSignalHash } from './signalHash';
const v = new RLNVerifier(vkey);
@@ -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 = str2BigInt(msg.message);
const msgHash = calculateSignalHash(msg.message);
let proof: RLNFullProof | undefined;
// Check that the epoch falls within the range for the room
const epoch = BigInt(msg.epoch);