Skip to content

Commit

Permalink
add safe message time and message validations
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Dec 26, 2023
1 parent c980afa commit 637146d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { randomBytes } from 'crypto';

const MULTISIG_REGEX_PATTERN =
/wants you to sign in with your Ethereum account:.*0x[a-fA-F0-9]+.*URI: https?:\/\/.*Version: \d+.*Chain ID: \d+.*Nonce: .*Issued At: .*/;

export const generateRandomString = (len: number): string => {
return randomBytes(len).toString('hex');
};
Expand All @@ -14,26 +17,40 @@ export const networkIds = {
gnosis: 100,
};

function isValidSafeLoginMessage(safeMessage: any): boolean {
return MULTISIG_REGEX_PATTERN.test(safeMessage.message);
}

export const findObjectByClosestTimestamp = (
target: number,
objects: any[],
) => {
if (objects.length === 0) return null;

const dateLabelObjects = objects.filter(item => item.type !== 'DATE_LABEL');
const safeMessages = objects.filter(item => item.type !== 'DATE_LABEL');
const dateLabelObjects = safeMessages.filter(isValidSafeLoginMessage);

let closestObj = dateLabelObjects[0];
let closestTimestamp = closestObj.creationTimestamp;
let smallestDifference = Math.abs(target - closestTimestamp);
const tenMinutesInMilliseconds = 10 * 60 * 1000;
const currentTime = new Date().getTime();

dateLabelObjects.forEach(obj => {
const difference = Math.abs(target - obj.creationTimestamp);
if (difference < smallestDifference) {
if (
difference <= tenMinutesInMilliseconds &&
difference < smallestDifference
) {
smallestDifference = difference;
closestObj = obj;
closestTimestamp = obj.creationTimestamp;
}
});

if (currentTime - closestTimestamp > tenMinutesInMilliseconds) {
return null;
}

return closestObj;
};

0 comments on commit 637146d

Please sign in to comment.