Skip to content

Commit

Permalink
Merge pull request #24 from Giveth/hotfix_multisig_session_state
Browse files Browse the repository at this point in the history
add safe message time and message validations
  • Loading branch information
CarlosQ96 authored Dec 26, 2023
2 parents c980afa + 231e779 commit 07c674a
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 07c674a

Please sign in to comment.