diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ff77ff2..872e2c5 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -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'); }; @@ -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; };