Skip to content

Commit

Permalink
Cleanup code (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianBouron authored Feb 18, 2024
1 parent 56d600e commit 7e86b2b
Showing 14 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"cSpell.words": ["gemwallet", "Trustline", "xrpl"],
"cSpell.words": ["deepcode", "gemwallet", "testid", "Trustline", "xrpl"],
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
13 changes: 7 additions & 6 deletions packages/extension/cypress/e2e/delete_account.cy.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import { Chain, XRPLNetwork } from '@gemwallet/constants';

describe('Delete account', () => {
// deepcode ignore NoHardcodedPasswords: <Test file, not a real password>
const PASSWORD = 'SECRET_PASSWORD';

beforeEach(() => {
@@ -98,21 +99,21 @@ describe('Delete account', () => {
const navigate = (url: string, password: string) => {
cy.visit(url, {
onBeforeLoad(win) {
(win as any).chrome = (win as any).chrome || {};
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
win['chrome'] = win['chrome'] || {};
win['chrome'].runtime = {
sendMessage() {}
};

(win as any).chrome.storage = {
win['chrome'].storage = {
local: {
get(key, cb) {},
get() {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
cy.stub(win['chrome'].runtime, 'sendMessage').resolves({});
}
});

1 change: 1 addition & 0 deletions packages/extension/cypress/e2e/offers.cy.ts
Original file line number Diff line number Diff line change
@@ -290,6 +290,7 @@ describe('Offers', () => {
cy.contains('Sequence')
.next()
.invoke('text')
// deepcode ignore PromiseNotCaughtGeneral: No need to have a catch here as this is a test
.then((sequence) => {
cy.wrap(sequence).as('sequence');
});
2 changes: 1 addition & 1 deletion packages/extension/cypress/e2e/submit_raw_json_tx.cy.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ import { Chain, XRPLNetwork } from '@gemwallet/constants';

import { navigate } from '../utils/navigation';

// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const SUBMIT_RAW_TRANSACTION_PATH = 'http://localhost:3000/build-transaction?transaction=buildRaw';
// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const PASSWORD = 'SECRET_PASSWORD';

beforeEach(() => {
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { FC } from 'react';

import { Paper, Tooltip, Typography, TypographyProps } from '@mui/material';
import { Paper, Tooltip, Typography } from '@mui/material';

interface KeyValueDisplayProps {
keyName: string;
value: string;
keyTypographyProps?: TypographyProps;
valueTypographyProps?: TypographyProps;
hasTooltip?: boolean;
useLegacy?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FC } from 'react';

import { TypographyProps } from '@mui/material';
import { convertHexToString, Currency } from 'xrpl';
import { Amount, Memo, Signer } from 'xrpl/dist/npm/models/common';
import { GlobalFlags } from 'xrpl/dist/npm/models/transactions/common';
@@ -176,12 +175,11 @@ export const TransactionDisplay: FC<XRPLTxProps> = ({
const renderAmount = (params: {
title: string;
value: Amount;
valueTypographyProps?: TypographyProps;
useLegacy: boolean;
hasMultipleAmounts?: boolean;
mainToken?: string;
}) => {
const { title, value, valueTypographyProps, useLegacy } = params;
const { title, value, useLegacy } = params;
const res = parseAmountObject(value, mainToken);
if (hasMultipleAmounts) {
const formattedValue = res.issuer
@@ -194,7 +192,6 @@ export const TransactionDisplay: FC<XRPLTxProps> = ({
<KeyValueDisplay
keyName={title}
value={formattedValue}
valueTypographyProps={valueTypographyProps}
useLegacy={useLegacy}
hasTooltip={hasTooltip}
/>
@@ -206,14 +203,12 @@ export const TransactionDisplay: FC<XRPLTxProps> = ({
<KeyValueDisplay
keyName={title}
value={`${res.amount} ${res.currency}`}
valueTypographyProps={valueTypographyProps}
useLegacy={useLegacy}
/>
{res.issuer ? (
<KeyValueDisplay
keyName="Trustline"
value={res.issuer}
valueTypographyProps={valueTypographyProps}
hasTooltip={true}
useLegacy={useLegacy}
/>
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { SECONDARY_GRAY } from '../../../constants';

export interface TransactionHeaderProps {
title: string;
favicon?: string | undefined;
favicon?: string;
url?: string | null;
}

12 changes: 5 additions & 7 deletions packages/extension/src/components/pages/Login/Login.tsx
Original file line number Diff line number Diff line change
@@ -154,13 +154,11 @@ export const Login: FC = () => {
useEffect(() => {
const loadTimerData = async () => {
const storedTimerData = await loadFromChromeLocalStorage('disabledLoginTimer');
if (storedTimerData !== null) {
if (storedTimerData > Date.now()) {
setDisableLogin(true);
setPasswordError('Too many attempts, please try again later');
} else {
setDisableLogin(false);
}
if (typeof storedTimerData === 'number' && storedTimerData > Date.now()) {
setDisableLogin(true);
setPasswordError('Too many attempts, please try again later');
} else {
setDisableLogin(false);
}
};
loadTimerData();
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ export const SignMessage: FC = () => {
return {
id: Number(urlParams.get('id')) || 0,
url,
favicon: favicon || undefined
favicon: favicon ?? undefined
};
}, []);

@@ -178,6 +178,7 @@ export const SignMessage: FC = () => {
<TransactionPage
title="Sign Message"
description="Signing this message will prove your ownership of the wallet."
// deepcode ignore OR: This URL is not used to redirect the user but only displayed as stringified
url={url}
favicon={favicon}
actionButtonsDescription="Only sign messages with a website you trust."
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@ export const TransactionsDisplay: FC<TransactionsDisplayProps> = ({
const { ID, ...txWithoutID } = tx;
return (
<DataCard
key={ID}
dataName={`${Number(key) + 1} - ${tx.TransactionType}`}
isExpanded={expandedStates[key] ?? areAllExpanded}
setIsExpanded={() => handleToggleExpand(key)}
Original file line number Diff line number Diff line change
@@ -4,12 +4,11 @@ import * as Sentry from '@sentry/react';

export interface CloseProps {
windowId: number;
callback?: Function;
}

interface ContextType {
window?: chrome.windows.Window;
closeExtension: ({ windowId, callback }: CloseProps) => void;
closeExtension: ({ windowId }: CloseProps) => void;
}

const BrowserContext = createContext<ContextType>({
@@ -34,14 +33,10 @@ const BrowserProvider: FC<Props> = ({ children }) => {
getCurrentWindow();
}, [getCurrentWindow]);

const closeExtension = useCallback(({ windowId, callback }: CloseProps) => {
const closeExtension = useCallback(({ windowId }: CloseProps) => {
if (chrome?.windows) {
chrome.storage.local.remove('currentWindowId');
chrome.windows.remove(windowId).then(() => {
if (callback) {
callback();
}
});
chrome.windows.remove(windowId);
}
}, []);

3 changes: 2 additions & 1 deletion packages/extension/src/utils/NFTViewer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NFTData } from '@gemwallet/constants';
import { IPFSResolverPrefix } from '@gemwallet/constants/src/xrpl/nft.constant';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const parseImage = (NFTData: any, URL: string): string => {
if (NFTData.image) {
return replaceIPFS(NFTData.image);
@@ -13,7 +14,7 @@ export const parseImage = (NFTData: any, URL: string): string => {
return URL.replace('.json', '.png');
};

export const parseJSON = async (URL: any, NFTokenID: string): Promise<NFTData> => {
export const parseJSON = async (URL: string, NFTokenID: string): Promise<NFTData> => {
const NFTData = await fetch(URL)
.then((res) => res.json())
.catch(() => {
4 changes: 3 additions & 1 deletion packages/extension/src/utils/login.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,9 @@ export const saveRememberSessionState = (checked: boolean) => {
export const loadRememberSessionState = async (): Promise<boolean> => {
try {
const storedState = await loadFromChromeLocalStorage(STORAGE_REMEMBER_SESSION);
return storedState ? JSON.parse(storedState) === true : false;
return storedState
? typeof storedState === 'string' && JSON.parse(storedState) === true
: false;
} catch (e) {
Sentry.captureException(e);
return false;
6 changes: 2 additions & 4 deletions packages/extension/src/utils/storageChromeLocal.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
* Manages the storage in Chrome local storage
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const saveInChromeLocalStorage = (key: string, value: any): Promise<void> => {
return new Promise((resolve, reject) => {
if (process.env.NODE_ENV === 'production') {
@@ -18,10 +19,7 @@ export const saveInChromeLocalStorage = (key: string, value: any): Promise<void>
});
};

export const loadFromChromeLocalStorage = (
key: string,
deleteAfterLoad: boolean = false
): Promise<any> => {
export const loadFromChromeLocalStorage = (key: string, deleteAfterLoad: boolean = false) => {
return new Promise((resolve, reject) => {
if (process.env.NODE_ENV === 'production') {
chrome.storage.local.get(key, (result) => {

0 comments on commit 7e86b2b

Please sign in to comment.