Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Redesign Signature Message date values #27249

Merged
merged 9 commits into from
Sep 20, 2024
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions test/data/confirmations/typed_sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ export const permitSignatureMsg = {
},
} as SignatureRequestType;

export const permitSignatureMsgWithNoDeadline = {
id: '0b1787a0-1c44-11ef-b70d-e7064bd7b659',
securityAlertResponse: {
reason: 'loading',
result_type: 'validation_in_progress',
securityAlertId: 'ab21395f-2190-472f-8cfa-3d224e7529d8',
},
status: 'unapproved',
time: 1716826404122,
type: 'eth_signTypedData',
msgParams: {
data: '{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Permit":[{"name":"owner","type":"address"},{"name":"spender","type":"address"},{"name":"value","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"deadline","type":"uint256"}]},"primaryType":"Permit","domain":{"name":"MyToken","version":"1","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC","chainId":1},"message":{"owner":"0x935e73edb9ff52e23bac7f7e043a1ecd06d05477","spender":"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4","value":3000,"nonce":0,"deadline":-1}}',
from: '0x935e73edb9ff52e23bac7f7e043a1ecd06d05477',
version: 'V4',
signatureMethod: 'eth_signTypedData_v4',
origin: 'https://metamask.github.io',
},
} as SignatureRequestType;

export const permitBatchSignatureMsg = {
id: '0b1787a0-1c44-11ef-b70d-e7064bd7b659',
securityAlertResponse: {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/confirmations/signatures/permit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async function assertInfoValues(driver: Driver) {
});
const value = driver.findElement({ text: '3,000' });
const nonce = driver.findElement({ text: '0' });
const deadline = driver.findElement({ text: '02 August 1971, 16:53' });
const deadline = driver.findElement({ text: '09 June 3554, 16:53' });

assert.ok(await origin, 'origin');
assert.ok(await contractPetName, 'contractPetName');
Expand Down
4 changes: 2 additions & 2 deletions ui/components/app/confirm/info/row/date.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const ConfirmInfoRowDateStory = {
},
};

export const DefaultStory = ({ date }) => <ConfirmInfoRowDate date={date} />;
export const DefaultStory = ({ date }) => <ConfirmInfoRowDate unixTimestamp={date} />;
DefaultStory.args = {
date: 1633019124000,
date: 1633019124,
};

export default ConfirmInfoRowDateStory;
4 changes: 3 additions & 1 deletion ui/components/app/confirm/info/row/date.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { ConfirmInfoRowDate } from './date';

describe('ConfirmInfoRowDate', () => {
it('should match snapshot', () => {
const { getByText } = render(<ConfirmInfoRowDate date={1633019124000} />);
const { getByText } = render(
<ConfirmInfoRowDate unixTimestamp={1633019124} />,
);
expect(getByText('30 September 2021, 16:25')).toBeInTheDocument();
});
});
11 changes: 7 additions & 4 deletions ui/components/app/confirm/info/row/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import {
FlexWrap,
TextColor,
} from '../../../../../helpers/constants/design-system';
import { formatUTCDate } from '../../../../../helpers/utils/util';
import { formatUTCDateFromUnixTimestamp } from '../../../../../helpers/utils/util';
import { Box, Text } from '../../../../component-library';

export type ConfirmInfoRowDateProps = {
date: number;
/** timestamp as seconds since unix epoch e.g. Solidity block.timestamp (type uint256) value */
unixTimestamp: number;
};

export const ConfirmInfoRowDate = ({ date }: ConfirmInfoRowDateProps) => (
export const ConfirmInfoRowDate = ({
unixTimestamp,
}: ConfirmInfoRowDateProps) => (
<Box
display={Display.Flex}
alignItems={AlignItems.center}
flexWrap={FlexWrap.Wrap}
gap={2}
>
<Text color={TextColor.inherit} style={{ whiteSpace: 'pre-wrap' }}>
{formatUTCDate(date)}
{formatUTCDateFromUnixTimestamp(unixTimestamp)}
</Text>
</Box>
);
14 changes: 9 additions & 5 deletions ui/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ export function formatDate(date, format = "M/d/y 'at' T") {
return DateTime.fromMillis(date).toFormat(format);
}

export const formatUTCDate = (dateInMillis) => {
if (!dateInMillis) {
return dateInMillis;
/**
* @param {number} unixTimestamp - timestamp as seconds since unix epoch
* @returns {string} formatted date string e.g. "14 July 2034, 22:22"
*/
export const formatUTCDateFromUnixTimestamp = (unixTimestamp) => {
if (!unixTimestamp) {
return unixTimestamp;
}

return DateTime.fromMillis(dateInMillis)
.setZone('utc')
return DateTime.fromSeconds(unixTimestamp)
.toUTC()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toUTC() is equivalent to .setZone('utc')

.toFormat('dd LLLL yyyy, HH:mm');
};

Expand Down
8 changes: 4 additions & 4 deletions ui/helpers/utils/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,15 +1044,15 @@ describe('util', () => {
});
});

describe('formatUTCDate', () => {
describe('formatUTCDateFromUnixTimestamp', () => {
it('formats passed date string', () => {
expect(util.formatUTCDate(1633019124000)).toStrictEqual(
'30 September 2021, 16:25',
expect(util.formatUTCDateFromUnixTimestamp(2036528542)).toStrictEqual(
'14 July 2034, 22:22',
);
});

it('returns empty string if empty string is passed', () => {
expect(util.formatUTCDate('')).toStrictEqual('');
expect(util.formatUTCDateFromUnixTimestamp('')).toStrictEqual('');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ const SIWESignInfo: React.FC = () => {
</ConfirmInfoRow>
<ConfirmInfoRow label={t('siweIssued')}>
<ConfirmInfoRowDate
date={DateTime.fromISO(issuedAt).toJSDate().getTime()}
unixTimestamp={DateTime.fromISO(issuedAt, {
zone: 'utc',
}).toUnixInteger()}
/>
</ConfirmInfoRow>
{requestId && (
Expand Down
Loading
Loading