Skip to content

Commit ad9e6b6

Browse files
committed
Add compare function for YearMonth and fix filtering of expired identities
1 parent f99b9f4 commit ad9e6b6

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/browser-wallet/src/popup/popupX/pages/CreateAccount/CreateAccount.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ConfirmedIdCard } from '@popup/popupX/shared/IdCard';
99
import Button from '@popup/popupX/shared/Button';
1010
import { generatePath, useNavigate } from 'react-router-dom';
1111
import { absoluteRoutes } from '@popup/popupX/constants/routes';
12+
import { compareYearMonth, getCurrentYearMonth } from 'wallet-common-helpers';
1213

1314
/**
1415
* Get the valid identities, which is Identities that are confirmed by the ID provider and are not
@@ -19,13 +20,14 @@ import { absoluteRoutes } from '@popup/popupX/constants/routes';
1920
function useValidIdentities(): ConfirmedIdentity[] {
2021
const identities = useAtomValue(identitiesAtom);
2122
return useMemo(() => {
22-
const now = new Date();
23+
const now = getCurrentYearMonth();
2324
return identities.flatMap((id) => {
2425
if (id.status !== CreationStatus.Confirmed) {
2526
return [];
2627
}
27-
const validToDate = new Date(id.idObject.value.attributeList.validTo);
28-
if (validToDate < now) {
28+
// Negative number is indicating that `validTo` is before `now`, therefore expired.
29+
const isExpired = compareYearMonth(id.idObject.value.attributeList.validTo, now) < 0;
30+
if (isExpired) {
2931
return [];
3032
}
3133
return [id];

packages/browser-wallet/src/wallet-common-helpers/utils/timeHelpers.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ export function getCurrentYearMonth(): YearMonth {
5555
return date.getFullYear() + month;
5656
}
5757

58+
/**
59+
* Function comparing two YearMonth strings.
60+
*
61+
* Returns a number where:
62+
*
63+
* - A negative value indicates that `left` is before `right`.
64+
* - A positive value indicates that `left` is after `right`.
65+
* - Zero indicates that `left` and `right` are equal.
66+
*/
67+
export function compareYearMonth(left: YearMonth, right: YearMonth): number {
68+
const leftYear = parseInt(left.slice(0, 4), 10);
69+
const rightYear = parseInt(right.slice(0, 4), 10);
70+
if (Number.isNaN(leftYear) || Number.isNaN(rightYear)) {
71+
throw new Error('Invalid input for compareYearMonth, unable to parse integer representing the year');
72+
}
73+
if (leftYear !== rightYear) {
74+
return leftYear - rightYear;
75+
}
76+
const leftMonth = parseInt(left.slice(4, 6), 10);
77+
const rightMonth = parseInt(right.slice(4, 6), 10);
78+
if (Number.isNaN(leftMonth) || Number.isNaN(rightMonth)) {
79+
throw new Error('Invalid input for compareYearMonth, unable to parse integer representing the month');
80+
}
81+
return leftMonth - rightMonth;
82+
}
83+
5884
/**
5985
* Converts a unix timestamp to a Date type.
6086
* @param timestamp the unix timestamp, in seconds or milliseconds.

0 commit comments

Comments
 (0)