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 231e779
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { randomBytes } from 'crypto';

const MULTISIG_MESSAGE = 'Login into Giveth services';

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

function isValidSafeLoginMessage(safeMessage: any): boolean {
return safeMessage.message.includes(MULTISIG_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(obj =>
isValidSafeLoginMessage(obj),
);

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 231e779

Please sign in to comment.