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