From 8da39d01e69c221627b47bfdf0797730b509b13a Mon Sep 17 00:00:00 2001 From: iamacook Date: Tue, 21 Nov 2023 10:31:15 +0100 Subject: [PATCH 1/2] feat: recovery queue --- public/images/common/clock.svg | 4 + public/images/common/recovery-plus.svg | 6 +- src/components/common/Countdown/index.test.ts | 23 ++ src/components/common/Countdown/index.tsx | 49 ++++ .../RecoveryInProgress/index.test.tsx | 191 +++++++------ .../dashboard/RecoveryInProgress/index.tsx | 95 ++----- .../recovery/ExecuteRecoveryButton/index.tsx | 68 +++++ .../recovery/RecoveryDetails/index.tsx | 95 +++++++ .../recovery/RecoveryInfo/index.tsx | 14 + .../recovery/RecoveryList/index.tsx | 28 ++ .../recovery/RecoveryListItem/index.tsx | 22 ++ .../recovery/RecoverySigners/index.tsx | 76 +++++ .../recovery/RecoveryStatus/index.tsx | 33 +++ .../recovery/RecoverySummary/index.tsx | 43 +++ .../recovery/RecoveryType/index.tsx | 20 ++ .../recovery/SkipRecoveryButton/index.tsx | 43 +++ src/components/settings/Recovery/index.tsx | 4 +- .../RecoverAccountFlowReview.tsx | 4 +- src/hooks/__tests__/useLoadRecovery.test.ts | 108 ++++++-- .../__tests__/useRecoveryTxState.test.ts | 130 +++++++++ src/hooks/__tests__/useTimestamp.test.ts | 27 ++ src/hooks/loadables/useLoadRecovery.ts | 66 +++-- src/hooks/useBlockTimestamp.test.ts | 85 ------ src/hooks/useBlockTimestamp.ts | 34 --- src/hooks/useRecoveryTxState.ts | 27 ++ src/hooks/useTimestamp.ts | 17 ++ src/pages/transactions/queue.tsx | 3 + src/services/exceptions/ErrorCodes.ts | 2 + .../recovery/__tests__/recovery-state.test.ts | 260 +++++++++++++++--- .../__tests__/transaction-list.test.ts | 180 ++++++++++++ src/services/recovery/recovery-state.ts | 141 ++++++++-- src/services/recovery/transaction-list.ts | 62 +++++ src/services/tx/tx-sender/dispatch.ts | 21 ++ src/store/__tests__/recoverySlice.test.ts | 96 +++++++ src/store/recoverySlice.ts | 21 +- src/tests/builders/safe.ts | 39 +++ src/utils/date.ts | 2 + src/utils/transaction-calldata.ts | 24 ++ 38 files changed, 1780 insertions(+), 383 deletions(-) create mode 100644 public/images/common/clock.svg create mode 100644 src/components/common/Countdown/index.test.ts create mode 100644 src/components/common/Countdown/index.tsx create mode 100644 src/components/recovery/ExecuteRecoveryButton/index.tsx create mode 100644 src/components/recovery/RecoveryDetails/index.tsx create mode 100644 src/components/recovery/RecoveryInfo/index.tsx create mode 100644 src/components/recovery/RecoveryList/index.tsx create mode 100644 src/components/recovery/RecoveryListItem/index.tsx create mode 100644 src/components/recovery/RecoverySigners/index.tsx create mode 100644 src/components/recovery/RecoveryStatus/index.tsx create mode 100644 src/components/recovery/RecoverySummary/index.tsx create mode 100644 src/components/recovery/RecoveryType/index.tsx create mode 100644 src/components/recovery/SkipRecoveryButton/index.tsx create mode 100644 src/hooks/__tests__/useRecoveryTxState.test.ts create mode 100644 src/hooks/__tests__/useTimestamp.test.ts delete mode 100644 src/hooks/useBlockTimestamp.test.ts delete mode 100644 src/hooks/useBlockTimestamp.ts create mode 100644 src/hooks/useRecoveryTxState.ts create mode 100644 src/hooks/useTimestamp.ts create mode 100644 src/services/recovery/__tests__/transaction-list.test.ts create mode 100644 src/services/recovery/transaction-list.ts create mode 100644 src/store/__tests__/recoverySlice.test.ts create mode 100644 src/tests/builders/safe.ts diff --git a/public/images/common/clock.svg b/public/images/common/clock.svg new file mode 100644 index 0000000000..7e1bcc0df3 --- /dev/null +++ b/public/images/common/clock.svg @@ -0,0 +1,4 @@ + + + + diff --git a/public/images/common/recovery-plus.svg b/public/images/common/recovery-plus.svg index 351202af8e..7189081be6 100644 --- a/public/images/common/recovery-plus.svg +++ b/public/images/common/recovery-plus.svg @@ -1,8 +1,8 @@ - - - + + + diff --git a/src/components/common/Countdown/index.test.ts b/src/components/common/Countdown/index.test.ts new file mode 100644 index 0000000000..7d47e3dbe9 --- /dev/null +++ b/src/components/common/Countdown/index.test.ts @@ -0,0 +1,23 @@ +import { _getCountdown } from '.' + +describe('getCountdown', () => { + it('should convert 0 seconds to 0 days, 0 hours, and 0 minutes', () => { + const result = _getCountdown(0) + expect(result).toEqual({ days: 0, hours: 0, minutes: 0 }) + }) + + it('should convert 3600 seconds to 0 days, 1 hour, and 0 minutes', () => { + const result = _getCountdown(3600) + expect(result).toEqual({ days: 0, hours: 1, minutes: 0 }) + }) + + it('should convert 86400 seconds to 1 day, 0 hours, and 0 minutes', () => { + const result = _getCountdown(86400) + expect(result).toEqual({ days: 1, hours: 0, minutes: 0 }) + }) + + it('should convert 123456 seconds to 1 day, 10 hours, and 17 minutes', () => { + const result = _getCountdown(123456) + expect(result).toEqual({ days: 1, hours: 10, minutes: 17 }) + }) +}) diff --git a/src/components/common/Countdown/index.tsx b/src/components/common/Countdown/index.tsx new file mode 100644 index 0000000000..524356aff7 --- /dev/null +++ b/src/components/common/Countdown/index.tsx @@ -0,0 +1,49 @@ +import { Typography, Box } from '@mui/material' +import type { ReactElement } from 'react' + +export function _getCountdown(seconds: number): { days: number; hours: number; minutes: number } { + const MINUTE_IN_SECONDS = 60 + const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS + const DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS + + const days = Math.floor(seconds / DAY_IN_SECONDS) + + const remainingSeconds = seconds % DAY_IN_SECONDS + const hours = Math.floor(remainingSeconds / HOUR_IN_SECONDS) + const minutes = Math.floor((remainingSeconds % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS) + + return { days, hours, minutes } +} + +export function Countdown({ seconds }: { seconds: number }): ReactElement | null { + if (seconds <= 0) { + return null + } + + const { days, hours, minutes } = _getCountdown(seconds) + + return ( + + + + + + ) +} + +function TimeLeft({ value, unit }: { value: number; unit: string }): ReactElement | null { + if (value === 0) { + return null + } + + return ( +
+ + {value} + {' '} + + {value === 1 ? unit : `${unit}s`} + +
+ ) +} diff --git a/src/components/dashboard/RecoveryInProgress/index.test.tsx b/src/components/dashboard/RecoveryInProgress/index.test.tsx index dd7c7d2584..cc0d8ff299 100644 --- a/src/components/dashboard/RecoveryInProgress/index.test.tsx +++ b/src/components/dashboard/RecoveryInProgress/index.test.tsx @@ -2,7 +2,12 @@ import { render } from '@testing-library/react' import { BigNumber } from 'ethers' import { _RecoveryInProgress } from '.' -import type { RecoveryQueueItem, RecoveryState } from '@/store/recoverySlice' +import { useRecoveryTxState } from '@/hooks/useRecoveryTxState' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +jest.mock('@/hooks/useRecoveryTxState') + +const mockUseRecoveryTxState = useRecoveryTxState as jest.MockedFunction describe('RecoveryInProgress', () => { beforeEach(() => { @@ -10,23 +15,13 @@ describe('RecoveryInProgress', () => { }) it('should return null if the chain does not support recovery', () => { - const result = render( - <_RecoveryInProgress - supportsRecovery={false} - blockTimestamp={0} - recovery={[{ queue: [{ timestamp: 0 } as RecoveryQueueItem] }] as RecoveryState} - />, - ) - - expect(result.container).toBeEmptyDOMElement() - }) + mockUseRecoveryTxState.mockReturnValue({} as any) - it('should return a loader if there is no block timestamp', () => { const result = render( <_RecoveryInProgress supportsRecovery={false} - blockTimestamp={undefined} - recovery={[{ queue: [{ timestamp: 0 } as RecoveryQueueItem] }] as RecoveryState} + timestamp={0} + queuedTxs={[{ timestamp: BigNumber.from(0) } as RecoveryQueueItem]} />, ) @@ -34,67 +29,59 @@ describe('RecoveryInProgress', () => { }) it('should return null if there are no delayed transactions', () => { - const result = render( - <_RecoveryInProgress - supportsRecovery={true} - blockTimestamp={69420} - recovery={[{ queue: [] as Array }] as RecoveryState} - />, - ) + mockUseRecoveryTxState.mockReturnValue({} as any) + + const result = render(<_RecoveryInProgress supportsRecovery={true} timestamp={69420} queuedTxs={[]} />) expect(result.container).toBeEmptyDOMElement() }) it('should return null if all the delayed transactions are expired and invalid', () => { + mockUseRecoveryTxState.mockReturnValue({} as any) + const result = render( <_RecoveryInProgress supportsRecovery={true} - blockTimestamp={69420} - recovery={ - [ - { - queue: [ - { - timestamp: 0, - validFrom: BigNumber.from(69), - expiresAt: BigNumber.from(420), - } as RecoveryQueueItem, - ], - }, - ] as RecoveryState - } + timestamp={69420} + queuedTxs={[ + { + timestamp: BigNumber.from(0), + validFrom: BigNumber.from(69), + expiresAt: BigNumber.from(420), + } as RecoveryQueueItem, + ]} />, ) expect(result.container).toBeEmptyDOMElement() }) - it('should return the countdown of the latest non-expired/invalid transactions if none are non-expired/valid', () => { - const mockBlockTimestamp = 69420 + it('should return the countdown of the next non-expired/invalid transactions if none are non-expired/valid', () => { + mockUseRecoveryTxState.mockReturnValue({ + remainingSeconds: 69 * 420 * 1337, + isExecutable: false, + isNext: true, + } as any) + + const mockBlockTimestamp = BigNumber.from(69420) const { queryByText } = render( <_RecoveryInProgress supportsRecovery={true} - blockTimestamp={mockBlockTimestamp} - recovery={ - [ - { - queue: [ - { - timestamp: mockBlockTimestamp + 1, - validFrom: BigNumber.from(mockBlockTimestamp + 1), // Invalid - expiresAt: BigNumber.from(mockBlockTimestamp + 1), // Non-expired - } as RecoveryQueueItem, - { - // Older - should render this - timestamp: mockBlockTimestamp, - validFrom: BigNumber.from(mockBlockTimestamp * 4), // Invalid - expiresAt: null, // Non-expired - } as RecoveryQueueItem, - ], - }, - ] as RecoveryState - } + timestamp={mockBlockTimestamp.toNumber()} + queuedTxs={[ + { + timestamp: mockBlockTimestamp.add(1), + validFrom: mockBlockTimestamp.add(1), // Invalid + expiresAt: mockBlockTimestamp.add(1), // Non-expired + } as RecoveryQueueItem, + { + // Older - should render this + timestamp: mockBlockTimestamp, + validFrom: mockBlockTimestamp.mul(4), // Invalid + expiresAt: null, // Non-expired + } as RecoveryQueueItem, + ]} />, ) @@ -107,39 +94,35 @@ describe('RecoveryInProgress', () => { expect(queryByText(unit, { exact: false })).toBeInTheDocument() }) // Days - expect(queryByText('2')).toBeInTheDocument() + expect(queryByText('448')).toBeInTheDocument() // Hours - expect(queryByText('9')).toBeInTheDocument() + expect(queryByText('10')).toBeInTheDocument() // Mins expect(queryByText('51')).toBeInTheDocument() }) - it('should return the info of the latest non-expired/valid transactions', () => { - const mockBlockTimestamp = 69420 + it('should return the info of the next non-expired/valid transaction', () => { + mockUseRecoveryTxState.mockReturnValue({ isExecutable: true, remainingSeconds: 0 } as any) + + const mockBlockTimestamp = BigNumber.from(69420) const { queryByText } = render( <_RecoveryInProgress supportsRecovery={true} - blockTimestamp={mockBlockTimestamp} - recovery={ - [ - { - queue: [ - { - timestamp: mockBlockTimestamp - 1, - validFrom: BigNumber.from(mockBlockTimestamp - 1), // Invalid - expiresAt: BigNumber.from(mockBlockTimestamp - 1), // Non-expired - } as RecoveryQueueItem, - { - // Older - should render this - timestamp: mockBlockTimestamp - 2, - validFrom: BigNumber.from(mockBlockTimestamp - 1), // Invalid - expiresAt: null, // Non-expired - } as RecoveryQueueItem, - ], - }, - ] as RecoveryState - } + timestamp={mockBlockTimestamp.toNumber()} + queuedTxs={[ + { + timestamp: mockBlockTimestamp.sub(1), + validFrom: mockBlockTimestamp.sub(1), // Invalid + expiresAt: mockBlockTimestamp.sub(1), // Non-expired + } as RecoveryQueueItem, + { + // Older - should render this + timestamp: mockBlockTimestamp.sub(2), + validFrom: mockBlockTimestamp.sub(1), // Invalid + expiresAt: null, // Non-expired + } as RecoveryQueueItem, + ]} />, ) @@ -150,4 +133,50 @@ describe('RecoveryInProgress', () => { expect(queryByText(unit, { exact: false })).not.toBeInTheDocument() }) }) + + it('should return the intemediary info for of the queued, non-expired/valid transactions', () => { + mockUseRecoveryTxState.mockReturnValue({ + isExecutable: false, + isNext: false, + remainingSeconds: 69 * 420 * 1337, + } as any) + + const mockBlockTimestamp = BigNumber.from(69420) + + const { queryByText } = render( + <_RecoveryInProgress + supportsRecovery={true} + timestamp={mockBlockTimestamp.toNumber()} + queuedTxs={[ + { + timestamp: mockBlockTimestamp.sub(1), + validFrom: mockBlockTimestamp.sub(1), // Invalid + expiresAt: mockBlockTimestamp.sub(1), // Non-expired + } as RecoveryQueueItem, + { + // Older - should render this + timestamp: mockBlockTimestamp.sub(2), + validFrom: mockBlockTimestamp.sub(1), // Invalid + expiresAt: null, // Non-expired + } as RecoveryQueueItem, + ]} + />, + ) + + expect(queryByText('Account recovery in progress')).toBeInTheDocument() + expect( + queryByText( + 'The recovery process has started. This Account can be recovered after previous attempts are executed or skipped and the delay period has passed:', + ), + ) + ;['day', 'hr', 'min'].forEach((unit) => { + // May be pluralised + expect(queryByText(unit, { exact: false })).toBeInTheDocument() + }) + // Days + expect(queryByText('448')).toBeInTheDocument() + // Hours + expect(queryByText('10')).toBeInTheDocument() + // Mins + }) }) diff --git a/src/components/dashboard/RecoveryInProgress/index.tsx b/src/components/dashboard/RecoveryInProgress/index.tsx index 9409a84f1d..5fc9c92698 100644 --- a/src/components/dashboard/RecoveryInProgress/index.tsx +++ b/src/components/dashboard/RecoveryInProgress/index.tsx @@ -1,50 +1,44 @@ -import { Box, Card, Grid, Typography } from '@mui/material' -import { useMemo } from 'react' +import { Card, Grid, Typography } from '@mui/material' import type { ReactElement } from 'react' import { useAppSelector } from '@/store' -import { useBlockTimestamp } from '@/hooks/useBlockTimestamp' +import { useTimestamp } from '@/hooks/useTimestamp' import { WidgetContainer, WidgetBody } from '../styled' import RecoveryPending from '@/public/images/common/recovery-pending.svg' import ExternalLink from '@/components/common/ExternalLink' import { useHasFeature } from '@/hooks/useChains' import { FEATURES } from '@/utils/chains' -import { selectRecovery } from '@/store/recoverySlice' -import type { RecoveryState } from '@/store/recoverySlice' +import { selectAllRecoveryQueues } from '@/store/recoverySlice' import madProps from '@/utils/mad-props' -import { getCountdown } from '@/utils/date' +import { Countdown } from '@/components/common/Countdown' +import { useRecoveryTxState } from '@/hooks/useRecoveryTxState' +import type { RecoveryQueueItem } from '@/store/recoverySlice' export function _RecoveryInProgress({ - blockTimestamp, + timestamp, supportsRecovery, - recovery, + queuedTxs, }: { - blockTimestamp?: number + timestamp: number supportsRecovery: boolean - recovery: RecoveryState + queuedTxs: Array }): ReactElement | null { - const allRecoveryTxs = useMemo(() => { - return recovery.flatMap(({ queue }) => queue).sort((a, b) => a.timestamp - b.timestamp) - }, [recovery]) - - if (!supportsRecovery || !blockTimestamp) { - return null - } - - const nonExpiredTxs = allRecoveryTxs.filter((delayedTx) => { - return delayedTx.expiresAt ? delayedTx.expiresAt.gt(blockTimestamp) : true + const nonExpiredTxs = queuedTxs.filter((queuedTx) => { + return queuedTx.expiresAt ? queuedTx.expiresAt.gt(timestamp) : true }) - if (nonExpiredTxs.length === 0) { + if (!supportsRecovery || nonExpiredTxs.length === 0) { return null } - const nextTx = nonExpiredTxs[0] + // Conditional hook + return <_RecoveryInProgressWidget nextTx={nonExpiredTxs[0]} /> +} - // TODO: Migrate `isValid` components when https://github.com/safe-global/safe-wallet-web/issues/2758 is done - const isValid = nextTx.validFrom.lte(blockTimestamp) - const secondsUntilValid = nextTx.validFrom.sub(blockTimestamp).toNumber() +function _RecoveryInProgressWidget({ nextTx }: { nextTx: RecoveryQueueItem }): ReactElement { + const { isExecutable, isNext, remainingSeconds } = useRecoveryTxState(nextTx) + // TODO: Migrate `isValid` components when https://github.com/safe-global/safe-wallet-web/issues/2758 is done return ( @@ -56,14 +50,18 @@ export function _RecoveryInProgress({ - {isValid ? 'Account recovery possible' : 'Account recovery in progress'} + {isExecutable ? 'Account recovery possible' : 'Account recovery in progress'} - {isValid + {isExecutable ? 'The recovery process is possible. This Account can be recovered.' + : !isNext + ? remainingSeconds > 0 + ? 'The recovery process has started. This Account can be recovered after previous attempts are executed or skipped and the delay period has passed:' + : 'The recovery process has started. This Account can be recovered after previous attempts are executed or skipped.' : 'The recovery process has started. This Account will be ready to recover in:'} - + - - - - - ) -} - -function TimeLeft({ value, unit }: { value: number; unit: string }): ReactElement | null { - if (value === 0) { - return null - } - - return ( -
- - {value} - {' '} - - {value === 1 ? unit : `${unit}s`} - -
- ) -} - // Appease React TypeScript warnings -const _useBlockTimestamp = () => useBlockTimestamp(60_000) // Countdown does not display +const _useTimestamp = () => useTimestamp(60_000) // Countdown does not display const _useSupportsRecovery = () => useHasFeature(FEATURES.RECOVERY) -const _useRecovery = () => useAppSelector(selectRecovery) +const _useQueuedRecoveryTxs = () => useAppSelector(selectAllRecoveryQueues) export const RecoveryInProgress = madProps(_RecoveryInProgress, { - blockTimestamp: _useBlockTimestamp, + timestamp: _useTimestamp, supportsRecovery: _useSupportsRecovery, - recovery: _useRecovery, + queuedTxs: _useQueuedRecoveryTxs, }) diff --git a/src/components/recovery/ExecuteRecoveryButton/index.tsx b/src/components/recovery/ExecuteRecoveryButton/index.tsx new file mode 100644 index 0000000000..a548946f62 --- /dev/null +++ b/src/components/recovery/ExecuteRecoveryButton/index.tsx @@ -0,0 +1,68 @@ +import { Button, SvgIcon, Tooltip } from '@mui/material' +import type { SyntheticEvent, ReactElement } from 'react' + +import RocketIcon from '@/public/images/transactions/rocket.svg' +import IconButton from '@mui/material/IconButton' +import CheckWallet from '@/components/common/CheckWallet' +import { dispatchRecoveryExecution } from '@/services/tx/tx-sender' +import useOnboard from '@/hooks/wallets/useOnboard' +import useSafeInfo from '@/hooks/useSafeInfo' +import { useRecoveryTxState } from '@/hooks/useRecoveryTxState' +import { Errors, logError } from '@/services/exceptions' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +export function ExecuteRecoveryButton({ + recovery, + compact = false, +}: { + recovery: RecoveryQueueItem + compact?: boolean +}): ReactElement { + const { isExecutable } = useRecoveryTxState(recovery) + const onboard = useOnboard() + const { safe } = useSafeInfo() + + const onClick = async (e: SyntheticEvent) => { + e.stopPropagation() + e.preventDefault() + + if (!onboard) { + return + } + + try { + await dispatchRecoveryExecution({ + onboard, + chainId: safe.chainId, + args: recovery.args, + delayModifierAddress: recovery.address, + }) + } catch (e) { + logError(Errors._812, e) + } + } + + return ( + + {(isOk) => { + const isDisabled = !isOk || !isExecutable + + return ( + + + {compact ? ( + + + + ) : ( + + )} + + + ) + }} + + ) +} diff --git a/src/components/recovery/RecoveryDetails/index.tsx b/src/components/recovery/RecoveryDetails/index.tsx new file mode 100644 index 0000000000..46a2773e03 --- /dev/null +++ b/src/components/recovery/RecoveryDetails/index.tsx @@ -0,0 +1,95 @@ +import { Link, Typography } from '@mui/material' +import { useMemo, useState } from 'react' +import { Operation } from '@safe-global/safe-gateway-typescript-sdk' +import type { ReactElement } from 'react' + +import EthHashInfo from '@/components/common/EthHashInfo' +import { dateString } from '@/utils/formatters' +import { generateDataRowValue, TxDataRow } from '@/components/transactions/TxDetails/Summary/TxDataRow' +import { InfoDetails } from '@/components/transactions/InfoDetails' +import { getRecoveredSafeInfo } from '@/services/recovery/transaction-list' +import useSafeInfo from '@/hooks/useSafeInfo' +import ErrorMessage from '@/components/tx/ErrorMessage' +import { RecoverySigners } from '../RecoverySigners' +import { Errors, logError } from '@/services/exceptions' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +import txDetailsCss from '@/components/transactions/TxDetails/styles.module.css' +import summaryCss from '@/components/transactions/TxDetails/Summary/styles.module.css' + +export function RecoveryDetails({ item }: { item: RecoveryQueueItem }): ReactElement { + const { transactionHash, timestamp, validFrom, expiresAt, args, isMalicious, address } = item + const { safe } = useSafeInfo() + + const newSetup = useMemo(() => { + try { + return getRecoveredSafeInfo(safe, { + to: args.to, + value: args.value.toString(), + data: args.data, + }) + } catch (e) { + logError(Errors._811, e) + } + // We only render the threshold and owners + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [args.data, args.to, args.value, safe.threshold, safe.owners]) + + const [expanded, setExpanded] = useState(false) + + const toggleExpanded = () => { + setExpanded((prev) => !prev) + } + + return ( +
+
+
+ {newSetup && !isMalicious ? ( + + {newSetup.owners.map((owner) => ( + + ))} + +
+ + Required confirmations for new transactions: + + + {newSetup.threshold} out of {newSetup.owners.length} owner(s) + +
+
+ ) : ( + This transaction potentially calls malicious actions. We recommend skipping it. + )} +
+ +
+ {generateDataRowValue(transactionHash, 'hash', true)} + {dateString(timestamp.toNumber())} + {dateString(validFrom.toNumber())} + {expiresAt && {dateString(expiresAt.toNumber())}} + + Advanced details + + + {expanded && ( + <> + {generateDataRowValue(address, 'address', true)} + {args.value.toString()} + {`${args.operation} (${Operation[ + args.operation + ].toLowerCase()})`} + {generateDataRowValue(args.data, 'rawData')} + + )} +
+
+ +
+ +
+
+ ) +} diff --git a/src/components/recovery/RecoveryInfo/index.tsx b/src/components/recovery/RecoveryInfo/index.tsx new file mode 100644 index 0000000000..d490267c51 --- /dev/null +++ b/src/components/recovery/RecoveryInfo/index.tsx @@ -0,0 +1,14 @@ +import { SvgIcon, Tooltip } from '@mui/material' +import type { ReactElement } from 'react' + +import WarningIcon from '@/public/images/notifications/warning.svg' + +export const RecoveryInfo = (): ReactElement => { + return ( + + + + + + ) +} diff --git a/src/components/recovery/RecoveryList/index.tsx b/src/components/recovery/RecoveryList/index.tsx new file mode 100644 index 0000000000..c293d2e418 --- /dev/null +++ b/src/components/recovery/RecoveryList/index.tsx @@ -0,0 +1,28 @@ +import type { ReactElement } from 'react' + +import { TxListGrid } from '@/components/transactions/TxList' +import { RecoveryListItem } from '@/components/recovery/RecoveryListItem' +import { selectAllRecoveryQueues } from '@/store/recoverySlice' +import { useAppSelector } from '@/store' + +import labelCss from '@/components/transactions/GroupLabel/styles.module.css' + +export function RecoveryList(): ReactElement | null { + const queue = useAppSelector(selectAllRecoveryQueues) + + if (queue.length === 0) { + return null + } + + return ( + <> +
Pending recovery
+ + + {queue.map((item) => ( + + ))} + + + ) +} diff --git a/src/components/recovery/RecoveryListItem/index.tsx b/src/components/recovery/RecoveryListItem/index.tsx new file mode 100644 index 0000000000..0613ff3502 --- /dev/null +++ b/src/components/recovery/RecoveryListItem/index.tsx @@ -0,0 +1,22 @@ +import { Accordion, AccordionDetails, AccordionSummary } from '@mui/material' +import ExpandMoreIcon from '@mui/icons-material/ExpandMore' +import type { ReactElement } from 'react' + +import txListItemCss from '@/components/transactions/TxListItem/styles.module.css' +import { RecoverySummary } from '../RecoverySummary' +import { RecoveryDetails } from '../RecoveryDetails' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +export function RecoveryListItem({ item }: { item: RecoveryQueueItem }): ReactElement { + return ( + + } sx={{ justifyContent: 'flex-start', overflowX: 'auto' }}> + + + + + + + + ) +} diff --git a/src/components/recovery/RecoverySigners/index.tsx b/src/components/recovery/RecoverySigners/index.tsx new file mode 100644 index 0000000000..1133ab7d45 --- /dev/null +++ b/src/components/recovery/RecoverySigners/index.tsx @@ -0,0 +1,76 @@ +import { Box, List, ListItem, ListItemIcon, ListItemText, SvgIcon, Typography } from '@mui/material' +import type { ReactElement } from 'react' + +import CircleIcon from '@/public/images/common/circle.svg' +import CheckIcon from '@/public/images/common/circle-check.svg' +import EthHashInfo from '@/components/common/EthHashInfo' +import { Countdown } from '@/components/common/Countdown' +import { ExecuteRecoveryButton } from '../ExecuteRecoveryButton' +import { SkipRecoveryButton } from '../SkipRecoveryButton' +import { useRecoveryTxState } from '@/hooks/useRecoveryTxState' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +import txSignersCss from '@/components/transactions/TxSigners/styles.module.css' +import { formatDate } from '@/utils/date' + +export function RecoverySigners({ item }: { item: RecoveryQueueItem }): ReactElement { + const { isExecutable, isNext, remainingSeconds } = useRecoveryTxState(item) + + return ( + <> + + + + palette.background.paper }, + }} + /> + + Created + + + + + + + palette.border.main }} + /> + + Can be executed + + + + + ({ color: palette.border.main, mb: 1 })}> + The recovery can be executed{' '} + {isExecutable ? ( + item.expiresAt ? ( + until ${formatDate(item.expiresAt.toNumber())}. + ) : ( + 'now.' + ) + ) : !isNext ? ( + 'after the previous recovery attempts are executed or skipped and the delay period has passed:' + ) : ( + 'after the delay period:' + )} + + + {isNext && } + + + + + + + + ) +} diff --git a/src/components/recovery/RecoveryStatus/index.tsx b/src/components/recovery/RecoveryStatus/index.tsx new file mode 100644 index 0000000000..3cc690bbd0 --- /dev/null +++ b/src/components/recovery/RecoveryStatus/index.tsx @@ -0,0 +1,33 @@ +import { SvgIcon, Typography } from '@mui/material' +import type { ReactElement } from 'react' + +import ClockIcon from '@/public/images/common/clock.svg' +import { useRecoveryTxState } from '@/hooks/useRecoveryTxState' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +export const RecoveryStatus = ({ recovery }: { recovery: RecoveryQueueItem }): ReactElement => { + const { isExecutable, isExpired } = useRecoveryTxState(recovery) + + return ( + <> + + {isExecutable ? ( + 'Awaiting execution' + ) : isExpired ? ( + 'Expired' + ) : ( + <> + + Pending + + )} + + + ) +} diff --git a/src/components/recovery/RecoverySummary/index.tsx b/src/components/recovery/RecoverySummary/index.tsx new file mode 100644 index 0000000000..4003b3b6a1 --- /dev/null +++ b/src/components/recovery/RecoverySummary/index.tsx @@ -0,0 +1,43 @@ +import { Box } from '@mui/material' +import classNames from 'classnames' +import type { ReactElement } from 'react' + +import { RecoveryType } from '../RecoveryType' +import { RecoveryInfo } from '../RecoveryInfo' +import { RecoveryStatus } from '../RecoveryStatus' +import { ExecuteRecoveryButton } from '../ExecuteRecoveryButton' +import { SkipRecoveryButton } from '../SkipRecoveryButton' +import useWallet from '@/hooks/wallets/useWallet' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +import txSummaryCss from '@/components/transactions/TxSummary/styles.module.css' + +export function RecoverySummary({ item }: { item: RecoveryQueueItem }): ReactElement { + const wallet = useWallet() + const { isMalicious } = item + + return ( + + + + + + {isMalicious && ( + + + + )} + + {wallet && ( + + + + + )} + + + + + + ) +} diff --git a/src/components/recovery/RecoveryType/index.tsx b/src/components/recovery/RecoveryType/index.tsx new file mode 100644 index 0000000000..2fd5b3947c --- /dev/null +++ b/src/components/recovery/RecoveryType/index.tsx @@ -0,0 +1,20 @@ +import { Box, SvgIcon } from '@mui/material' +import type { ReactElement } from 'react' + +import RecoveryPlusIcon from '@/public/images/common/recovery-plus.svg' + +import txTypeCss from '@/components/transactions/TxType/styles.module.css' + +export function RecoveryType({ isMalicious }: { isMalicious: boolean }): ReactElement { + return ( + + palette.warning.main } }} + /> + {isMalicious ? 'Malicious' : 'Account'} recovery + + ) +} diff --git a/src/components/recovery/SkipRecoveryButton/index.tsx b/src/components/recovery/SkipRecoveryButton/index.tsx new file mode 100644 index 0000000000..04b5518dfb --- /dev/null +++ b/src/components/recovery/SkipRecoveryButton/index.tsx @@ -0,0 +1,43 @@ +import { Button, SvgIcon } from '@mui/material' +import { useContext } from 'react' +import type { SyntheticEvent, ReactElement } from 'react' + +import ErrorIcon from '@/public/images/notifications/error.svg' +import IconButton from '@mui/material/IconButton' +import CheckWallet from '@/components/common/CheckWallet' +import { TxModalContext } from '@/components/tx-flow' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +export function SkipRecoveryButton({ + recovery, + compact = false, +}: { + recovery: RecoveryQueueItem + compact?: boolean +}): ReactElement { + const { setTxFlow } = useContext(TxModalContext) + + const onClick = (e: SyntheticEvent) => { + e.stopPropagation() + e.preventDefault() + + // TODO: Implement skip recovery flow + setTxFlow(undefined) + } + + return ( + + {(isOk) => + compact ? ( + + + + ) : ( + + ) + } + + ) +} diff --git a/src/components/settings/Recovery/index.tsx b/src/components/settings/Recovery/index.tsx index b4e34399b6..d10952d251 100644 --- a/src/components/settings/Recovery/index.tsx +++ b/src/components/settings/Recovery/index.tsx @@ -9,12 +9,12 @@ import ExternalLink from '@/components/common/ExternalLink' import { RecoverAccountFlow } from '@/components/tx-flow/flows/RecoverAccount' import useWallet from '@/hooks/wallets/useWallet' import { useAppSelector } from '@/store' -import { selectRecoveryByGuardian } from '@/store/recoverySlice' +import { selectDelayModifierByGuardian } from '@/store/recoverySlice' export function Recovery(): ReactElement { const { setTxFlow } = useContext(TxModalContext) const wallet = useWallet() - const recovery = useAppSelector((state) => selectRecoveryByGuardian(state, wallet?.address ?? '')) + const recovery = useAppSelector((state) => selectDelayModifierByGuardian(state, wallet?.address ?? '')) return ( diff --git a/src/components/tx-flow/flows/RecoverAccount/RecoverAccountFlowReview.tsx b/src/components/tx-flow/flows/RecoverAccount/RecoverAccountFlowReview.tsx index 2d8cdd5575..4bd5aaabf6 100644 --- a/src/components/tx-flow/flows/RecoverAccount/RecoverAccountFlowReview.tsx +++ b/src/components/tx-flow/flows/RecoverAccount/RecoverAccountFlowReview.tsx @@ -18,7 +18,7 @@ import { createMultiSendCallOnlyTx, createTx, dispatchRecoveryProposal } from '@ import { RecoverAccountFlowFields } from '.' import { NewOwnerList } from '../../common/NewOwnerList' import { useAppSelector } from '@/store' -import { selectRecoveryByGuardian } from '@/store/recoverySlice' +import { selectDelayModifierByGuardian } from '@/store/recoverySlice' import useWallet from '@/hooks/wallets/useWallet' import useOnboard from '@/hooks/wallets/useOnboard' import { TxModalContext } from '../..' @@ -41,7 +41,7 @@ export function RecoverAccountFlowReview({ params }: { params: RecoverAccountFlo const { safe } = useSafeInfo() const wallet = useWallet() const onboard = useOnboard() - const recovery = useAppSelector((state) => selectRecoveryByGuardian(state, wallet?.address ?? '')) + const recovery = useAppSelector((state) => selectDelayModifierByGuardian(state, wallet?.address ?? '')) // Proposal const txCooldown = recovery?.txCooldown?.toNumber() diff --git a/src/hooks/__tests__/useLoadRecovery.test.ts b/src/hooks/__tests__/useLoadRecovery.test.ts index f4b693b873..6b8c8e6fcb 100644 --- a/src/hooks/__tests__/useLoadRecovery.test.ts +++ b/src/hooks/__tests__/useLoadRecovery.test.ts @@ -21,6 +21,8 @@ const setupFetchStub = (data: any) => (_url: string) => { }) } +// TODO: Condense test to only check loading logic as `recovery-state.test.ts` covers most + jest.mock('@/hooks/useSafeInfo') jest.mock('@/hooks/wallets/web3') jest.mock('@/hooks/useChains') @@ -43,9 +45,11 @@ describe('useLoadRecovery', () => { }) it('should return the recovery state', async () => { + const safeAddress = faker.finance.ethereumAddress() + // useSafeInfo mockUseSafeInfo.mockReturnValue({ - safeAddress: faker.finance.ethereumAddress(), + safeAddress, safe: { chainId: faker.string.numeric(), modules: [ @@ -62,8 +66,12 @@ describe('useLoadRecovery', () => { } as ChainInfo) // useWeb3ReadOnly + const from = faker.finance.ethereumAddress() const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -78,21 +86,24 @@ describe('useLoadRecovery', () => { const queueNonce = BigNumber.from(3) const transactionsAdded = [ { - getBlock: () => Promise.resolve({ timestamp: 69 }), args: { + to: safeAddress, queueNonce: BigNumber.from(1), + data: '0x', }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 420 }), args: { + to: safeAddress, queueNonce: BigNumber.from(2), + data: '0x', }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 69420 }), args: { + to: faker.finance.ethereumAddress(), queueNonce: BigNumber.from(3), + data: '0x', }, } as unknown, ] as Array @@ -105,6 +116,11 @@ describe('useLoadRecovery', () => { txExpiration: () => Promise.resolve(txExpiration), txCooldown: () => Promise.resolve(txCooldown), txNonce: () => Promise.resolve(txNonce), + txCreatedAt: jest + .fn() + .mockResolvedValueOnce(BigNumber.from(69)) + .mockResolvedValueOnce(BigNumber.from(420)) + .mockResolvedValueOnce(BigNumber.from(69420)), queueNonce: () => Promise.resolve(queueNonce), queryFilter: () => Promise.resolve(transactionsAdded), } as unknown as Delay @@ -121,7 +137,7 @@ describe('useLoadRecovery', () => { [ { address: delayModifier.address, - modules: delayModules, + guardians: delayModules, txExpiration, txCooldown, txNonce, @@ -129,21 +145,27 @@ describe('useLoadRecovery', () => { queue: [ { ...transactionsAdded[0], - timestamp: 69, - validFrom: BigNumber.from(69).add(txCooldown), + timestamp: BigNumber.from(69).mul(1_000), + validFrom: BigNumber.from(69).add(txCooldown).mul(1_000), expiresAt: null, + isMalicious: false, + executor: from, }, { ...transactionsAdded[1], - timestamp: 420, - validFrom: BigNumber.from(420).add(txCooldown), + timestamp: BigNumber.from(420).mul(1_000), + validFrom: BigNumber.from(420).add(txCooldown).mul(1_000), expiresAt: null, + isMalicious: false, + executor: from, }, { ...transactionsAdded[2], - timestamp: 69420, - validFrom: BigNumber.from(69420).add(txCooldown), + timestamp: BigNumber.from(69420).mul(1_000), + validFrom: BigNumber.from(69420).add(txCooldown).mul(1_000), expiresAt: null, + isMalicious: true, + executor: from, }, ], }, @@ -178,7 +200,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -233,7 +258,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -288,7 +316,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -324,7 +355,7 @@ describe('useLoadRecovery', () => { }) }) - it('should poll the recovery state every 5 minutes', async () => { + it.skip('should poll the recovery state every 5 minutes', async () => { jest.useFakeTimers() // useSafeInfo @@ -347,7 +378,11 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: () => + jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -362,21 +397,21 @@ describe('useLoadRecovery', () => { const queueNonce = BigNumber.from(3) const transactionsAdded = [ { - getBlock: () => Promise.resolve({ timestamp: 69 }), args: { queueNonce: BigNumber.from(1), + data: '0x', }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 420 }), args: { queueNonce: BigNumber.from(2), + data: '0x', }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 69420 }), args: { queueNonce: BigNumber.from(3), + data: '0x', }, } as unknown, ] as Array @@ -389,6 +424,11 @@ describe('useLoadRecovery', () => { txExpiration: () => Promise.resolve(txExpiration), txCooldown: () => Promise.resolve(txCooldown), txNonce: () => Promise.resolve(txNonce), + txCreatedAt: jest + .fn() + .mockResolvedValueOnce(BigNumber.from(69)) + .mockResolvedValueOnce(BigNumber.from(420)) + .mockResolvedValueOnce(BigNumber.from(69420)), queueNonce: () => Promise.resolve(queueNonce), queryFilter: () => Promise.resolve(transactionsAdded), } as unknown as Delay @@ -432,7 +472,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -490,7 +533,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -527,7 +573,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -562,7 +611,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -602,7 +654,10 @@ describe('useLoadRecovery', () => { // useWeb3ReadOnly const provider = { - getTransactionReceipt: () => Promise.resolve({ blockHash: `0x${faker.string.hexadecimal}` }), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce({ blockHash: `0x${faker.string.hexadecimal}` }) + .mockResolvedValue({ from: faker.finance.ethereumAddress() }), } as unknown as JsonRpcProvider mockUseWeb3ReadOnly.mockReturnValue(provider) @@ -617,19 +672,16 @@ describe('useLoadRecovery', () => { const queueNonce = BigNumber.from(3) const transactionsAdded = [ { - getBlock: () => Promise.resolve({ timestamp: 69 }), args: { queueNonce: BigNumber.from(1), }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 420 }), args: { queueNonce: BigNumber.from(2), }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 69420 }), args: { queueNonce: BigNumber.from(3), }, diff --git a/src/hooks/__tests__/useRecoveryTxState.test.ts b/src/hooks/__tests__/useRecoveryTxState.test.ts new file mode 100644 index 0000000000..c71d276ac5 --- /dev/null +++ b/src/hooks/__tests__/useRecoveryTxState.test.ts @@ -0,0 +1,130 @@ +import { BigNumber } from 'ethers' + +import { useRecoveryTxState } from '../useRecoveryTxState' +import { renderHook } from '@/tests/test-utils' +import * as store from '@/store' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +describe('useRecoveryTxState', () => { + beforeEach(() => { + jest.useFakeTimers() + }) + + describe('Next', () => { + it('should return correct values when validFrom is in the future and expiresAt is in the future', () => { + jest.setSystemTime(0) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(0), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(1_000) + const expiresAt = BigNumber.from(1_000) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(false) + expect(result.current.remainingSeconds).toBe(1) + expect(result.current.isExpired).toBe(false) + expect(result.current.isNext).toBe(true) + }) + + it('should return correct values when validFrom is in the past and expiresAt is in the future', () => { + jest.setSystemTime(1_000) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(0), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(0) + const expiresAt = BigNumber.from(2_000) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(true) + expect(result.current.remainingSeconds).toBe(0) + expect(result.current.isExpired).toBe(false) + expect(result.current.isNext).toBe(true) + }) + + it('should return correct values when validFrom is in the past and expiresAt is in the past', () => { + jest.setSystemTime(1_000) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(0), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(0) + const expiresAt = BigNumber.from(0) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(false) + expect(result.current.remainingSeconds).toBe(0) + expect(result.current.isExpired).toBe(true) + expect(result.current.isNext).toBe(true) + }) + }) + + describe('Queue', () => { + it('should return correct values when validFrom is in the future and expiresAt is in the future', () => { + jest.setSystemTime(0) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(1), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(1_000) + const expiresAt = BigNumber.from(1_000) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(false) + expect(result.current.remainingSeconds).toBe(1) + expect(result.current.isExpired).toBe(false) + expect(result.current.isNext).toBe(false) + }) + + it('should return correct values when validFrom is in the past and expiresAt is in the future', () => { + jest.setSystemTime(1_000) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(1), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(0) + const expiresAt = BigNumber.from(2_000) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(false) + expect(result.current.remainingSeconds).toBe(0) + expect(result.current.isExpired).toBe(false) + expect(result.current.isNext).toBe(false) + }) + + it('should return correct values when validFrom is in the past and expiresAt is in the past', () => { + jest.setSystemTime(1_000) + + jest.spyOn(store, 'useAppSelector').mockReturnValue({ + txNonce: BigNumber.from(1), + } as unknown as RecoveryQueueItem) + + const validFrom = BigNumber.from(0) + const expiresAt = BigNumber.from(0) + const recoveryQueueItem = { validFrom, expiresAt, args: { queueNonce: BigNumber.from(0) } } as RecoveryQueueItem + + const { result } = renderHook(() => useRecoveryTxState(recoveryQueueItem)) + + expect(result.current.isExecutable).toBe(false) + expect(result.current.remainingSeconds).toBe(0) + expect(result.current.isExpired).toBe(true) + expect(result.current.isNext).toBe(false) + }) + }) +}) diff --git a/src/hooks/__tests__/useTimestamp.test.ts b/src/hooks/__tests__/useTimestamp.test.ts new file mode 100644 index 0000000000..fbcd9400c0 --- /dev/null +++ b/src/hooks/__tests__/useTimestamp.test.ts @@ -0,0 +1,27 @@ +import { renderHook, waitFor } from '@/tests/test-utils' +import { useTimestamp } from '../useTimestamp' + +describe('useTimestamp', () => { + it('should update the timestamp every INTERVAL', async () => { + jest.useFakeTimers() + + const timestamp = 69_420 + jest.setSystemTime(timestamp) + + const { result } = renderHook(() => useTimestamp(1_000)) + + jest.advanceTimersByTime(1_000) + + await waitFor(() => { + expect(result.current).toBe(timestamp + 1_000) + }) + + jest.advanceTimersByTime(1_000) + + await waitFor(() => { + expect(result.current).toBe(timestamp + 2_000) + }) + + jest.useRealTimers() + }) +}) diff --git a/src/hooks/loadables/useLoadRecovery.ts b/src/hooks/loadables/useLoadRecovery.ts index f7e40ca494..530903a3ef 100644 --- a/src/hooks/loadables/useLoadRecovery.ts +++ b/src/hooks/loadables/useLoadRecovery.ts @@ -21,40 +21,50 @@ const useLoadRecovery = (): AsyncResult => { const [counter] = useIntervalCounter(REFRESH_DELAY) const supportsRecovery = useHasFeature(FEATURES.RECOVERY) - const [delayModifiers, delayModifiersError, delayModifiersLoading] = useAsync>(() => { - if (!supportsRecovery || !web3ReadOnly || !safe.modules || safe.modules.length === 0) { - return - } + const [delayModifiers, delayModifiersError, delayModifiersLoading] = useAsync>( + () => { + if (!supportsRecovery || !web3ReadOnly || !safe.modules || safe.modules.length === 0) { + return + } - const isOnlySpendingLimit = - safe.modules.length === 1 && safe.modules[0].value === getSpendingLimitModuleAddress(safe.chainId) + const isOnlySpendingLimit = + safe.modules.length === 1 && safe.modules[0].value === getSpendingLimitModuleAddress(safe.chainId) - if (isOnlySpendingLimit) { - return - } + if (isOnlySpendingLimit) { + return + } - return getDelayModifiers(safe.chainId, safe.modules, web3ReadOnly) + return getDelayModifiers(safe.chainId, safe.modules, web3ReadOnly) + }, // Need to check length of modules array to prevent new request every time Safe info polls // eslint-disable-next-line react-hooks/exhaustive-deps - }, [safeAddress, safe.chainId, safe.modules?.length, web3ReadOnly, supportsRecovery]) - - const [recoveryState, recoveryStateError, recoveryStateLoading] = useAsync(() => { - if (!delayModifiers || delayModifiers.length === 0 || !chain?.transactionService || !web3ReadOnly) { - return - } - - return Promise.all( - delayModifiers.map((delayModifier) => - getRecoveryState({ - delayModifier, - transactionService: chain.transactionService, - safeAddress, - provider: web3ReadOnly, - }), - ), - ) + [safeAddress, safe.chainId, safe.modules?.length, web3ReadOnly, supportsRecovery], + false, + ) + + const [recoveryState, recoveryStateError, recoveryStateLoading] = useAsync( + () => { + if (!delayModifiers || delayModifiers.length === 0 || !chain?.transactionService || !web3ReadOnly) { + return + } + + return Promise.all( + delayModifiers.map((delayModifier) => + getRecoveryState({ + delayModifier, + transactionService: chain.transactionService, + safeAddress, + provider: web3ReadOnly, + chainId: safe.chainId, + version: safe.version, + }), + ), + ) + }, // eslint-disable-next-line react-hooks/exhaustive-deps - }, [delayModifiers, counter, chain?.transactionService, web3ReadOnly, safeAddress]) + [delayModifiers, counter, chain?.transactionService, web3ReadOnly, safeAddress, safe.chainId, safe.version], + false, + ) return [recoveryState, delayModifiersError || recoveryStateError, delayModifiersLoading || recoveryStateLoading] } diff --git a/src/hooks/useBlockTimestamp.test.ts b/src/hooks/useBlockTimestamp.test.ts deleted file mode 100644 index 8c05176e09..0000000000 --- a/src/hooks/useBlockTimestamp.test.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { useWeb3ReadOnly } from '@/hooks/wallets/web3' - -import { useBlockTimestamp } from '@/hooks/useBlockTimestamp' -import { renderHook, waitFor } from '@/tests/test-utils' - -jest.mock('@/hooks/wallets/web3') - -const mockUseWeb3ReadOnly = useWeb3ReadOnly as jest.MockedFunction - -describe('useBlockTimestamp', () => { - const mockGetBlock = jest.fn() - - beforeEach(() => { - mockUseWeb3ReadOnly.mockReturnValue({ - getBlock: mockGetBlock, - } as any) - }) - - afterEach(() => { - jest.clearAllMocks() - }) - - it('should return undefined if web3ReadOnly is not available', () => { - mockUseWeb3ReadOnly.mockReturnValue(undefined) - - const { result } = renderHook(() => useBlockTimestamp()) - - expect(result.current).toBeUndefined() - - expect(mockGetBlock).not.toHaveBeenCalled() - }) - - it('should return the latest block timestamp', async () => { - const timestamp = 69420 - - mockGetBlock.mockResolvedValue({ - timestamp, - } as any) - - const { result } = renderHook(() => useBlockTimestamp()) - - expect(result.current).toBeUndefined() - - await waitFor(() => { - expect(result.current).toBe(timestamp) - }) - - expect(mockGetBlock).toHaveBeenCalledTimes(1) - }) - - it('should update the timestamp every INTERVAL', async () => { - jest.useFakeTimers() - - const timestamp = 69420 - - mockGetBlock.mockResolvedValue({ - timestamp, - } as any) - - const { result } = renderHook(() => useBlockTimestamp()) - - expect(result.current).toBeUndefined() - - await waitFor(() => { - expect(result.current).toBe(timestamp) - }) - - jest.advanceTimersByTime(1_000) - - await waitFor(() => { - expect(result.current).toBe(timestamp + 1) - }) - - jest.advanceTimersByTime(1_000) - - await waitFor(() => { - expect(result.current).toBe(timestamp + 2) - }) - - // Interval is used to update the timestamp after initial getBlock call - expect(mockGetBlock).toHaveBeenCalledTimes(1) - - jest.useRealTimers() - }) -}) diff --git a/src/hooks/useBlockTimestamp.ts b/src/hooks/useBlockTimestamp.ts deleted file mode 100644 index ee6aee2014..0000000000 --- a/src/hooks/useBlockTimestamp.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { useState, useEffect } from 'react' - -import useAsync from './useAsync' - -import { useWeb3ReadOnly } from './wallets/web3' - -export function useBlockTimestamp(interval = 1_000): number | undefined { - const web3ReadOnly = useWeb3ReadOnly() - const [timestamp, setTimestamp] = useState() - - const [block] = useAsync(() => { - return web3ReadOnly?.getBlock('latest') - }, [web3ReadOnly]) - - useEffect(() => { - if (!block) { - return - } - - setTimestamp(block.timestamp) - - const timeout = setInterval(() => { - setTimestamp((prev) => { - return prev ? prev + 1 : block.timestamp - }) - }, interval) - - return () => { - clearInterval(timeout) - } - }, [interval, block]) - - return timestamp -} diff --git a/src/hooks/useRecoveryTxState.ts b/src/hooks/useRecoveryTxState.ts new file mode 100644 index 0000000000..daf282fbc0 --- /dev/null +++ b/src/hooks/useRecoveryTxState.ts @@ -0,0 +1,27 @@ +import { useTimestamp } from './useTimestamp' +import { useAppSelector } from '@/store' +import { selectDelayModifierByTxHash } from '@/store/recoverySlice' +import type { RecoveryQueueItem } from '@/store/recoverySlice' + +// TODO: Test +export function useRecoveryTxState({ validFrom, expiresAt, transactionHash, args }: RecoveryQueueItem): { + isNext: boolean + isExecutable: boolean + isExpired: boolean + remainingSeconds: number +} { + const recovery = useAppSelector((state) => selectDelayModifierByTxHash(state, transactionHash)) + + // We don't display seconds in the interface, so we can use a 60s interval + const timestamp = useTimestamp(60_000) + const remainingMs = validFrom.sub(timestamp) + + const isValid = remainingMs.lte(0) + const isExpired = expiresAt ? expiresAt.toNumber() <= Date.now() : false + const isNext = recovery ? args.queueNonce.eq(recovery.txNonce) : false + const isExecutable = isNext && isValid && !isExpired + + const remainingSeconds = isValid ? 0 : Math.ceil(remainingMs.div(1_000).toNumber()) + + return { isNext, isExecutable, isExpired, remainingSeconds } +} diff --git a/src/hooks/useTimestamp.ts b/src/hooks/useTimestamp.ts new file mode 100644 index 0000000000..43304e29a9 --- /dev/null +++ b/src/hooks/useTimestamp.ts @@ -0,0 +1,17 @@ +import { useState, useEffect } from 'react' + +export function useTimestamp(interval = 1_000): number { + const [timestamp, setTimestamp] = useState(Date.now()) + + useEffect(() => { + const timeout = setInterval(() => { + setTimestamp((prev) => prev + interval) + }, interval) + + return () => { + clearInterval(timeout) + } + }, [interval]) + + return timestamp +} diff --git a/src/pages/transactions/queue.tsx b/src/pages/transactions/queue.tsx index 8c7c8d0b1f..8cb771aa27 100644 --- a/src/pages/transactions/queue.tsx +++ b/src/pages/transactions/queue.tsx @@ -7,6 +7,7 @@ import BatchExecuteButton from '@/components/transactions/BatchExecuteButton' import { Box } from '@mui/material' import { BatchExecuteHoverProvider } from '@/components/transactions/BatchExecuteButton/BatchExecuteHoverProvider' import { usePendingTxsQueue, useShowUnsignedQueue } from '@/hooks/usePendingTxs' +import { RecoveryList } from '@/components/recovery/RecoveryList' const Queue: NextPage = () => { const showPending = useShowUnsignedQueue() @@ -24,6 +25,8 @@ const Queue: NextPage = () => {
+ + {/* Pending unsigned transactions */} {showPending && } diff --git a/src/services/exceptions/ErrorCodes.ts b/src/services/exceptions/ErrorCodes.ts index 8319df5f75..7ce2b41436 100644 --- a/src/services/exceptions/ErrorCodes.ts +++ b/src/services/exceptions/ErrorCodes.ts @@ -62,6 +62,8 @@ enum ErrorCodes { _808 = '808: Failed to get transaction origin', _809 = '809: Failed decoding transaction', _810 = '810: Error executing a recovery proposal transaction', + _811 = '811: Error decoding a recovery proposal transaction', + _812 = '812: Failed to recover', _900 = '900: Error loading Safe App', _901 = '901: Error processing Safe Apps SDK request', diff --git a/src/services/recovery/__tests__/recovery-state.test.ts b/src/services/recovery/__tests__/recovery-state.test.ts index cd7226146c..dbe6c3043e 100644 --- a/src/services/recovery/__tests__/recovery-state.test.ts +++ b/src/services/recovery/__tests__/recovery-state.test.ts @@ -1,12 +1,20 @@ import { faker } from '@faker-js/faker' import { BigNumber, ethers } from 'ethers' import { JsonRpcProvider } from '@ethersproject/providers' +import { cloneDeep } from 'lodash' import type { Delay, TransactionAddedEvent } from '@gnosis.pm/zodiac/dist/cjs/types/Delay' import type { TransactionReceipt } from '@ethersproject/abstract-provider' -import { getRecoveryState, _getRecoveryQueueItem, _getSafeCreationReceipt } from '../recovery-state' +import { + getRecoveryState, + _getRecoveryQueueItemTimestamps, + _getSafeCreationReceipt, + _isMaliciousRecovery, +} from '../recovery-state' import { useWeb3ReadOnly } from '@/hooks/wallets/web3' -import { cloneDeep } from 'lodash' +import { encodeMultiSendData } from '@safe-global/safe-core-sdk/dist/src/utils/transactions/utils' +import { getMultiSendCallOnlyDeployment, getSafeSingletonDeployment } from '@safe-global/safe-deployments' +import { Interface } from 'ethers/lib/utils' jest.mock('@/hooks/wallets/web3') @@ -18,37 +26,189 @@ describe('recovery-state', () => { _getSafeCreationReceipt.cache.clear?.() }) - describe('getRecoveryQueueItem', () => { - it('should return a recovery queue item', async () => { + describe('isMaliciousRecovery', () => { + describe('non-MultiSend', () => { + it('should return true if the transaction is not calling the Safe itself', () => { + const chainId = '5' + const version = '1.3.0' + const safeAddress = faker.finance.ethereumAddress() + + const transaction = { + to: faker.finance.ethereumAddress(), // Not Safe + data: '0x', + } + + expect(_isMaliciousRecovery({ chainId, version, safeAddress, transaction })).toBe(true) + }) + + it('should return false if the transaction is calling the Safe itself', () => { + const chainId = '5' + const version = '1.3.0' + const safeAddress = faker.finance.ethereumAddress() + + const transaction = { + to: safeAddress, // Safe + data: '0x', + } + + expect(_isMaliciousRecovery({ chainId, version, safeAddress, transaction })).toBe(false) + }) + }) + + describe('MultiSend', () => { + it('should return true if the transaction is a not and official MultiSend address', () => { + const chainId = '5' + const version = '1.3.0' + const safeAddress = faker.finance.ethereumAddress() + + const safeAbi = getSafeSingletonDeployment({ network: chainId, version })!.abi + const safeInterface = new Interface(safeAbi) + + const multiSendAbi = getMultiSendCallOnlyDeployment({ network: chainId, version })!.abi + const multiSendInterface = new Interface(multiSendAbi) + + const multiSendData = encodeMultiSendData([ + { + to: safeAddress, + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 1]), + operation: 0, + }, + { + to: safeAddress, + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 2]), + operation: 0, + }, + ]) + + const transaction = { + to: faker.finance.ethereumAddress(), // Not official MultiSend + data: multiSendInterface.encodeFunctionData('multiSend', [multiSendData]), + } + + expect(_isMaliciousRecovery({ chainId, version, safeAddress, transaction })).toBe(true) + }) + + it('should return true if the transaction is an official MultiSend call and not every transaction in the batch calls the Safe itself', () => { + const chainId = '5' + const version = '1.3.0' + const safeAddress = faker.finance.ethereumAddress() + + const safeAbi = getSafeSingletonDeployment({ network: chainId, version })!.abi + const safeInterface = new Interface(safeAbi) + + const multiSendDeployment = getMultiSendCallOnlyDeployment({ network: chainId, version })! + const multiSendInterface = new Interface(multiSendDeployment.abi) + + const multiSendData = encodeMultiSendData([ + { + to: faker.finance.ethereumAddress(), // Not Safe + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 1]), + operation: 0, + }, + { + to: faker.finance.ethereumAddress(), // Not Safe + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 2]), + operation: 0, + }, + ]) + + const transaction = { + to: multiSendDeployment.networkAddresses[chainId], + data: multiSendInterface.encodeFunctionData('multiSend', [multiSendData]), + } + + expect(_isMaliciousRecovery({ chainId, version, safeAddress, transaction })).toBe(true) + }) + + it('should return false if the transaction is an official MultiSend call and every transaction in the batch calls the Safe itself', () => { + const chainId = '5' + const version = '1.3.0' + const safeAddress = faker.finance.ethereumAddress() + + const safeAbi = getSafeSingletonDeployment({ network: chainId, version })!.abi + const safeInterface = new Interface(safeAbi) + + const multiSendDeployment = getMultiSendCallOnlyDeployment({ network: chainId, version })! + const multiSendInterface = new Interface(multiSendDeployment.abi) + + const multiSendData = encodeMultiSendData([ + { + to: safeAddress, + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 1]), + operation: 0, + }, + { + to: safeAddress, + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [faker.finance.ethereumAddress(), 2]), + operation: 0, + }, + ]) + + const transaction = { + to: multiSendDeployment.networkAddresses[chainId], + data: multiSendInterface.encodeFunctionData('multiSend', [multiSendData]), + } + + expect(_isMaliciousRecovery({ chainId, version, safeAddress, transaction })).toBe(false) + }) + }) + }) + + describe('getRecoveryQueueItemTimestamps', () => { + it('should return a recovery queue item timestamps', async () => { + const delayModifier = { + txCreatedAt: () => Promise.resolve(BigNumber.from(1)), + } as unknown as Delay const transactionAdded = { - getBlock: () => Promise.resolve({ timestamp: 1 }), + args: { + queueNonce: BigNumber.from(0), + }, } as TransactionAddedEvent const txCooldown = BigNumber.from(1) const txExpiration = BigNumber.from(2) - const item = await _getRecoveryQueueItem(transactionAdded, txCooldown, txExpiration) + const item = await _getRecoveryQueueItemTimestamps({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, + }) expect(item).toStrictEqual({ - ...transactionAdded, - timestamp: 1, - validFrom: BigNumber.from(2), - expiresAt: BigNumber.from(4), + timestamp: BigNumber.from(1_000), + validFrom: BigNumber.from(2_000), + expiresAt: BigNumber.from(4_000), }) }) - it('should return a recovery queue item with expiresAt null if txExpiration is zero', async () => { + it('should return a recovery queue item timestamps with expiresAt null if txExpiration is zero', async () => { + const delayModifier = { + txCreatedAt: () => Promise.resolve(BigNumber.from(1)), + } as unknown as Delay const transactionAdded = { - getBlock: () => Promise.resolve({ timestamp: 1 }), + args: { + queueNonce: BigNumber.from(0), + }, } as TransactionAddedEvent const txCooldown = BigNumber.from(1) const txExpiration = BigNumber.from(0) - const item = await _getRecoveryQueueItem(transactionAdded, txCooldown, txExpiration) + const item = await _getRecoveryQueueItemTimestamps({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, + }) expect(item).toStrictEqual({ - ...transactionAdded, - timestamp: 1, - validFrom: BigNumber.from(2), + timestamp: BigNumber.from(1_000), + validFrom: BigNumber.from(2_000), expiresAt: null, }) }) @@ -148,11 +308,21 @@ describe('recovery-state', () => { describe('getRecoveryState', () => { it('should return the recovery state from the Safe creation block', async () => { const safeAddress = faker.finance.ethereumAddress() + const chainId = '5' + const version = '1.3.0' const transactionService = faker.internet.url({ appendSlash: false }) const transactionHash = `0x${faker.string.hexadecimal()}` - const blockNumber = faker.number.int() + const safeCreationReceipt = { + blockNumber: faker.number.int(), + } as TransactionReceipt + const transactionAddedReceipt = { + from: faker.finance.ethereumAddress(), + } as TransactionReceipt const provider = { - getTransactionReceipt: () => Promise.resolve({ blockNumber } as TransactionReceipt), + getTransactionReceipt: jest + .fn() + .mockResolvedValueOnce(safeCreationReceipt) + .mockResolvedValue(transactionAddedReceipt), } as unknown as JsonRpcProvider global.fetch = jest.fn().mockImplementation((_url: string) => { @@ -163,23 +333,36 @@ describe('recovery-state', () => { }) }) - const modules = [faker.finance.ethereumAddress()] + const guardians = [faker.finance.ethereumAddress()] const txExpiration = BigNumber.from(0) const txCooldown = BigNumber.from(69420) const txNonce = BigNumber.from(2) const queueNonce = BigNumber.from(4) const transactionsAdded = [ { - getBlock: () => Promise.resolve({ timestamp: 420 }), args: { queueNonce: BigNumber.from(2), + to: safeAddress, + value: BigNumber.from(0), + data: '0x', }, } as unknown, { - getBlock: () => Promise.resolve({ timestamp: 69420 }), args: { queueNonce: BigNumber.from(3), + to: faker.finance.ethereumAddress(), // Malicious + value: BigNumber.from(0), + data: '0x', + }, + } as unknown, + { + args: { + queueNonce: BigNumber.from(4), + to: safeAddress, + value: BigNumber.from(0), + data: '0x', }, + removed: true, // Reorg } as unknown, ] as Array @@ -193,10 +376,15 @@ describe('recovery-state', () => { TransactionAdded: () => cloneDeep(defaultTransactionAddedFilter), }, address: faker.finance.ethereumAddress(), - getModulesPaginated: () => Promise.resolve([modules]), + getModulesPaginated: () => Promise.resolve([guardians]), txExpiration: () => Promise.resolve(txExpiration), txCooldown: () => Promise.resolve(txCooldown), txNonce: () => Promise.resolve(txNonce), + txCreatedAt: jest + .fn() + .mockResolvedValueOnce(BigNumber.from(420)) + .mockResolvedValueOnce(BigNumber.from(69420)) + .mockResolvedValueOnce(BigNumber.from(6942069)), queueNonce: () => Promise.resolve(queueNonce), queryFilter: queryFilterMock.mockImplementation(() => Promise.resolve(transactionsAdded)), } @@ -206,11 +394,13 @@ describe('recovery-state', () => { safeAddress, transactionService, provider, + chainId, + version, }) expect(recoveryState).toStrictEqual({ address: delayModifier.address, - modules, + guardians, txExpiration, txCooldown, txNonce, @@ -218,15 +408,19 @@ describe('recovery-state', () => { queue: [ { ...transactionsAdded[0], - timestamp: 420, - validFrom: BigNumber.from(420).add(txCooldown), + timestamp: BigNumber.from(420).mul(1_000), + validFrom: BigNumber.from(420).add(txCooldown).mul(1_000), expiresAt: null, + isMalicious: false, + executor: transactionAddedReceipt.from, }, { ...transactionsAdded[1], - timestamp: 69420, - validFrom: BigNumber.from(69420).add(txCooldown), + timestamp: BigNumber.from(69420).mul(1_000), + validFrom: BigNumber.from(69420).add(txCooldown).mul(1_000), expiresAt: null, + isMalicious: true, + executor: transactionAddedReceipt.from, }, ], }) @@ -238,17 +432,19 @@ describe('recovery-state', () => { [ethers.utils.hexZeroPad('0x2', 32), ethers.utils.hexZeroPad('0x3', 32)], ], }, - blockNumber, + safeCreationReceipt.blockNumber, 'latest', ) }) it('should not query data if the queueNonce equals the txNonce', async () => { const safeAddress = faker.finance.ethereumAddress() + const chainId = '5' + const version = '1.3.0' const transactionService = faker.internet.url({ appendSlash: true }) const provider = {} as unknown as JsonRpcProvider - const modules = [faker.finance.ethereumAddress()] + const guardians = [faker.finance.ethereumAddress()] const txExpiration = BigNumber.from(0) const txCooldown = BigNumber.from(69420) const txNonce = BigNumber.from(2) @@ -264,7 +460,7 @@ describe('recovery-state', () => { TransactionAdded: () => cloneDeep(defaultTransactionAddedFilter), }, address: faker.finance.ethereumAddress(), - getModulesPaginated: () => Promise.resolve([modules]), + getModulesPaginated: () => Promise.resolve([guardians]), txExpiration: () => Promise.resolve(txExpiration), txCooldown: () => Promise.resolve(txCooldown), txNonce: () => Promise.resolve(txNonce), @@ -277,11 +473,13 @@ describe('recovery-state', () => { safeAddress, transactionService, provider, + chainId, + version, }) expect(recoveryState).toStrictEqual({ address: delayModifier.address, - modules, + guardians, txExpiration, txCooldown, txNonce, diff --git a/src/services/recovery/__tests__/transaction-list.test.ts b/src/services/recovery/__tests__/transaction-list.test.ts new file mode 100644 index 0000000000..7c67e521ac --- /dev/null +++ b/src/services/recovery/__tests__/transaction-list.test.ts @@ -0,0 +1,180 @@ +import { faker } from '@faker-js/faker' +import { getMultiSendCallOnlyDeployment, getSafeSingletonDeployment } from '@safe-global/safe-deployments' +import { Interface } from 'ethers/lib/utils' +import { SENTINEL_ADDRESS } from '@safe-global/safe-core-sdk/dist/src/utils/constants' +import { encodeMultiSendData } from '@safe-global/safe-core-sdk/dist/src/utils/transactions/utils' + +import { safeInfoBuilder } from '@/tests/builders/safe' +import { getRecoveredSafeInfo } from '../transaction-list' +import { checksumAddress, sameAddress } from '@/utils/addresses' + +describe('getRecoveredSafeInfo', () => { + describe('non-MultiSend', () => { + it('returns the added owner and new threshold', () => { + const safe = safeInfoBuilder().with({ chainId: '5' }).build() + + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + const safeInterface = new Interface(safeDeployment!.abi) + + const newOwner = checksumAddress(faker.finance.ethereumAddress()) + const newThreshold = safe.threshold + 1 + + const transaction = { + to: safe.address.value, + value: '0', + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [newOwner, newThreshold]), + } + + expect(getRecoveredSafeInfo(safe, transaction)).toStrictEqual({ + ...safe, + owners: [...safe.owners, { value: newOwner }], + threshold: newThreshold, + }) + }) + + it('returns without an owner and new threshold', () => { + const safe = safeInfoBuilder().with({ chainId: '5' }).build() + + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + const safeInterface = new Interface(safeDeployment!.abi) + + const newThreshold = safe.threshold - 1 + + const transaction = { + to: safe.address.value, + value: '0', + data: safeInterface.encodeFunctionData('removeOwner', [SENTINEL_ADDRESS, safe.owners[0].value, newThreshold]), + } + + expect(getRecoveredSafeInfo(safe, transaction)).toStrictEqual({ + ...safe, + owners: safe.owners.slice(1), + threshold: newThreshold, + }) + }) + + it('returns a swapped owner', () => { + const safe = safeInfoBuilder().with({ chainId: '5' }).build() + + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + const safeInterface = new Interface(safeDeployment!.abi) + + const newOwner = checksumAddress(faker.finance.ethereumAddress()) + + const transaction = { + to: safe.address.value, + value: '0', + data: safeInterface.encodeFunctionData('swapOwner', [SENTINEL_ADDRESS, safe.owners[0].value, newOwner]), + } + + expect(getRecoveredSafeInfo(safe, transaction)).toStrictEqual({ + ...safe, + owners: [{ value: newOwner }, ...safe.owners.slice(1)], + }) + }) + + it('returns a new threshold', () => { + const safe = safeInfoBuilder().with({ chainId: '5' }).build() + + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + const safeInterface = new Interface(safeDeployment!.abi) + + const newThreshold = safe.threshold - 1 + + const transaction = { + to: safe.address.value, + value: '0', + data: safeInterface.encodeFunctionData('changeThreshold', [newThreshold]), + } + + expect(getRecoveredSafeInfo(safe, transaction)).toStrictEqual({ + ...safe, + threshold: newThreshold, + }) + }) + + it('otherwise throws', () => { + const safe = safeInfoBuilder().with({ chainId: '5' }).build() + + const transaction = { + to: safe.address.value, + value: '0', + data: '0x', + } + + expect(() => getRecoveredSafeInfo(safe, transaction)).toThrowError('Unexpected transaction') + }) + }) + + it('handles a MultiSend batch of the above', () => { + const safe = safeInfoBuilder() + .with({ + chainId: '5', + owners: [ + { value: checksumAddress(faker.finance.ethereumAddress()) }, + { value: checksumAddress(faker.finance.ethereumAddress()) }, + { value: checksumAddress(faker.finance.ethereumAddress()) }, + ], + threshold: 2, + }) + .build() + + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + const safeInterface = new Interface(safeDeployment!.abi) + + const multiSendDeployment = getMultiSendCallOnlyDeployment({ + network: safe.chainId, + version: safe.version ?? undefined, + }) + const multiSendAddress = multiSendDeployment!.networkAddresses[safe.chainId] + const multiSendInterface = new Interface(multiSendDeployment!.abi) + + const addedOwner = checksumAddress(faker.finance.ethereumAddress()) + const removedOwner = safe.owners[1].value + const preSwappedOwner = safe.owners[0].value + const postSwappedOwner = checksumAddress(faker.finance.ethereumAddress()) + const newThreshold = safe.threshold + 1 + + const multiSendData = encodeMultiSendData([ + { + data: safeInterface.encodeFunctionData('addOwnerWithThreshold', [addedOwner, safe.threshold]), + value: '0', + to: safe.address.value, + operation: 0, + }, + { + data: safeInterface.encodeFunctionData('removeOwner', [safe.owners[0].value, removedOwner, safe.threshold]), + value: '0', + to: safe.address.value, + operation: 0, + }, + { + data: safeInterface.encodeFunctionData('swapOwner', [SENTINEL_ADDRESS, preSwappedOwner, postSwappedOwner]), + value: '0', + to: safe.address.value, + operation: 0, + }, + { + data: safeInterface.encodeFunctionData('changeThreshold', [newThreshold]), + value: '0', + to: safe.address.value, + operation: 0, + }, + ]) + + const transaction = { + to: multiSendAddress, + value: '0', + data: multiSendInterface.encodeFunctionData('multiSend', [multiSendData]), + } + + expect(getRecoveredSafeInfo(safe, transaction)).toStrictEqual({ + ...safe, + owners: safe.owners + .concat([{ value: addedOwner }]) + .filter((owner) => !sameAddress(owner.value, removedOwner)) + .map((owner) => (sameAddress(owner.value, preSwappedOwner) ? { value: postSwappedOwner } : owner)), + threshold: newThreshold, + }) + }) +}) diff --git a/src/services/recovery/recovery-state.ts b/src/services/recovery/recovery-state.ts index ac0f343497..8af45e5b79 100644 --- a/src/services/recovery/recovery-state.ts +++ b/src/services/recovery/recovery-state.ts @@ -1,33 +1,77 @@ import { SENTINEL_ADDRESS } from '@safe-global/safe-core-sdk/dist/src/utils/constants' -import { BigNumber } from 'ethers' import { memoize } from 'lodash' +import { getMultiSendCallOnlyDeployment } from '@safe-global/safe-deployments' +import { hexZeroPad } from 'ethers/lib/utils' +import type { BigNumber } from 'ethers' +import type { SafeInfo } from '@safe-global/safe-gateway-typescript-sdk' import type { Delay } from '@gnosis.pm/zodiac' import type { TransactionAddedEvent } from '@gnosis.pm/zodiac/dist/cjs/types/Delay' import type { JsonRpcProvider } from '@ethersproject/providers' import type { TransactionReceipt } from '@ethersproject/abstract-provider' -import type { RecoveryQueueItem, RecoveryState } from '@/store/recoverySlice' -import { hexZeroPad } from 'ethers/lib/utils' import { trimTrailingSlash } from '@/utils/url' +import { sameAddress } from '@/utils/addresses' +import { isMultiSendCalldata } from '@/utils/transaction-calldata' +import { decodeMultiSendTxs } from '@/utils/transactions' +import type { RecoveryQueueItem, RecoveryState } from '@/store/recoverySlice' const MAX_PAGE_SIZE = 100 -export const _getRecoveryQueueItem = async ( - transactionAdded: TransactionAddedEvent, - txCooldown: BigNumber, - txExpiration: BigNumber, -): Promise => { - const txBlock = await transactionAdded.getBlock() +export function _isMaliciousRecovery({ + chainId, + version, + safeAddress, + transaction, +}: { + chainId: string + version: SafeInfo['version'] + safeAddress: string + transaction: Pick +}) { + const isMultiSend = isMultiSendCalldata(transaction.data) + const transactions = isMultiSend ? decodeMultiSendTxs(transaction.data) : [transaction] + + if (!isMultiSend) { + // Calling the Safe itself + return !sameAddress(transaction.to, safeAddress) + } + + const multiSendDeployment = getMultiSendCallOnlyDeployment({ network: chainId, version: version ?? undefined }) + + if (!multiSendDeployment) { + return true + } + + const multiSendAddress = multiSendDeployment.networkAddresses[chainId] ?? multiSendDeployment.defaultAddress + + // Calling official MultiSend contract with a batch of transactions to the Safe itself + return ( + !sameAddress(transaction.to, multiSendAddress) || + transactions.some((transaction) => !sameAddress(transaction.to, safeAddress)) + ) +} + +export const _getRecoveryQueueItemTimestamps = async ({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, +}: { + delayModifier: Delay + transactionAdded: TransactionAddedEvent + txCooldown: BigNumber + txExpiration: BigNumber +}): Promise> => { + const timestamp = await delayModifier.txCreatedAt(transactionAdded.args.queueNonce) - const validFrom = BigNumber.from(txBlock.timestamp).add(txCooldown) + const validFrom = timestamp.add(txCooldown) const expiresAt = txExpiration.isZero() ? null // Never expires - : validFrom.add(txExpiration) + : validFrom.add(txExpiration).mul(1_000) return { - ...transactionAdded, - timestamp: txBlock.timestamp, - validFrom, + timestamp: timestamp.mul(1_000), + validFrom: validFrom.mul(1_000), expiresAt, } } @@ -85,18 +129,66 @@ const queryAddedTransactions = async ( return await delayModifier.queryFilter(transactionAddedFilter, blockNumber, 'latest') } +const getRecoveryQueueItem = async ({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, + provider, + chainId, + version, + safeAddress, +}: { + delayModifier: Delay + transactionAdded: TransactionAddedEvent + txCooldown: BigNumber + txExpiration: BigNumber + provider: JsonRpcProvider + chainId: string + version: SafeInfo['version'] + safeAddress: string +}): Promise => { + const [timestamps, receipt] = await Promise.all([ + _getRecoveryQueueItemTimestamps({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, + }), + provider.getTransactionReceipt(transactionAdded.transactionHash), + ]) + + const isMalicious = _isMaliciousRecovery({ + chainId, + version, + safeAddress, + transaction: transactionAdded.args, + }) + + return { + ...transactionAdded, + ...timestamps, + isMalicious, + executor: receipt.from, + } +} + export const getRecoveryState = async ({ delayModifier, transactionService, safeAddress, provider, + chainId, + version, }: { delayModifier: Delay transactionService: string safeAddress: string provider: JsonRpcProvider + chainId: string + version: SafeInfo['version'] }): Promise => { - const [[modules], txExpiration, txCooldown, txNonce, queueNonce] = await Promise.all([ + const [[guardians], txExpiration, txCooldown, txNonce, queueNonce] = await Promise.all([ delayModifier.getModulesPaginated(SENTINEL_ADDRESS, MAX_PAGE_SIZE), delayModifier.txExpiration(), delayModifier.txCooldown(), @@ -114,18 +206,27 @@ export const getRecoveryState = async ({ ) const queue = await Promise.all( - queuedTransactionsAdded.map((transactionAdded) => - _getRecoveryQueueItem(transactionAdded, txCooldown, txExpiration), - ), + queuedTransactionsAdded.map((transactionAdded) => { + return getRecoveryQueueItem({ + delayModifier, + transactionAdded, + txCooldown, + txExpiration, + provider, + chainId, + version, + safeAddress, + }) + }), ) return { address: delayModifier.address, - modules, + guardians, txExpiration, txCooldown, txNonce, queueNonce, - queue, + queue: queue.filter((item) => !item.removed), } } diff --git a/src/services/recovery/transaction-list.ts b/src/services/recovery/transaction-list.ts new file mode 100644 index 0000000000..bf3db0feeb --- /dev/null +++ b/src/services/recovery/transaction-list.ts @@ -0,0 +1,62 @@ +import { sameAddress } from '@/utils/addresses' +import { + isSwapOwnerCalldata, + isAddOwnerWithThresholdCalldata, + isRemoveOwnerCalldata, + isChangeThresholdCalldata, + isMultiSendCalldata, +} from '@/utils/transaction-calldata' +import { decodeMultiSendTxs } from '@/utils/transactions' +import { getSafeSingletonDeployment } from '@safe-global/safe-deployments' +import { Interface } from 'ethers/lib/utils' +import type { BaseTransaction } from '@safe-global/safe-apps-sdk' +import type { SafeInfo } from '@safe-global/safe-gateway-typescript-sdk' + +function decodeOwnerManagementTransaction(safe: SafeInfo, transaction: BaseTransaction): SafeInfo { + const safeDeployment = getSafeSingletonDeployment({ network: safe.chainId, version: safe.version ?? undefined }) + + if (!safeDeployment) { + throw new Error('No Safe deployment found') + } + + const safeInterface = new Interface(safeDeployment.abi) + + let _owners = safe.owners + let _threshold = safe.threshold + + if (isSwapOwnerCalldata(transaction.data)) { + const [, ownerToRemove, ownerToAdd] = safeInterface.decodeFunctionData('swapOwner', transaction.data) + + _owners = safe.owners.map((owner) => (sameAddress(owner.value, ownerToRemove) ? { value: ownerToAdd } : owner)) + } else if (isAddOwnerWithThresholdCalldata(transaction.data)) { + const [ownerToAdd, newThreshold] = safeInterface.decodeFunctionData('addOwnerWithThreshold', transaction.data) + + _owners = _owners.concat({ value: ownerToAdd }) + _threshold = newThreshold.toNumber() + } else if (isRemoveOwnerCalldata(transaction.data)) { + const [, ownerToRemove, newThreshold] = safeInterface.decodeFunctionData('removeOwner', transaction.data) + + _owners = safe.owners.filter((owner) => !sameAddress(owner.value, ownerToRemove)) + _threshold = newThreshold.toNumber() + } else if (isChangeThresholdCalldata(transaction.data)) { + const [newThreshold] = safeInterface.decodeFunctionData('changeThreshold', transaction.data) + + _threshold = newThreshold.toNumber() + } else { + throw new Error('Unexpected transaction') + } + + return { + ...safe, + owners: _owners, + threshold: _threshold, + } +} + +export function getRecoveredSafeInfo(safe: SafeInfo, transaction: BaseTransaction): SafeInfo { + const transactions = isMultiSendCalldata(transaction.data) ? decodeMultiSendTxs(transaction.data) : [transaction] + + return transactions.reduce((acc, cur) => { + return decodeOwnerManagementTransaction(acc, cur) + }, safe) +} diff --git a/src/services/tx/tx-sender/dispatch.ts b/src/services/tx/tx-sender/dispatch.ts index 98b9ce244c..72dee23f94 100644 --- a/src/services/tx/tx-sender/dispatch.ts +++ b/src/services/tx/tx-sender/dispatch.ts @@ -25,6 +25,7 @@ import { type OnboardAPI } from '@web3-onboard/core' import { asError } from '@/services/exceptions/utils' import { getRecoveryProposalTransaction } from '@/services/recovery/transaction' import { getModuleInstance, KnownContracts } from '@gnosis.pm/zodiac' +import type { TransactionAddedEvent } from '@gnosis.pm/zodiac/dist/cjs/types/Delay' /** * Propose a transaction @@ -431,3 +432,23 @@ export async function dispatchRecoveryProposal({ const signer = provider.getSigner() await delayModifier.connect(signer).execTransactionFromModule(to, value, data, OperationType.Call) } + +export async function dispatchRecoveryExecution({ + onboard, + chainId, + args, + delayModifierAddress, +}: { + onboard: OnboardAPI + chainId: string + args: TransactionAddedEvent['args'] + delayModifierAddress: string +}) { + const wallet = await assertWalletChain(onboard, chainId) + const provider = createWeb3(wallet.provider) + + const delayModifier = getModuleInstance(KnownContracts.DELAY, delayModifierAddress, provider) + + const signer = provider.getSigner() + await delayModifier.connect(signer).executeNextTx(args.to, args.value, args.data, args.operation) +} diff --git a/src/store/__tests__/recoverySlice.test.ts b/src/store/__tests__/recoverySlice.test.ts new file mode 100644 index 0000000000..94cb0f6dc2 --- /dev/null +++ b/src/store/__tests__/recoverySlice.test.ts @@ -0,0 +1,96 @@ +import { BigNumber } from 'ethers' +import { faker } from '@faker-js/faker' + +import { selectDelayModifierByGuardian, selectAllRecoveryQueues, selectDelayModifierByTxHash } from '../recoverySlice' +import type { RecoveryState } from '../recoverySlice' +import type { RootState } from '..' + +describe('recoverySlice', () => { + describe('selectDelayModifierByGuardian', () => { + it('should return the Delay Modifier for the given guardian', () => { + const delayModifier1 = { + guardians: [faker.finance.ethereumAddress(), faker.finance.ethereumAddress()], + queue: [{ timestamp: BigNumber.from(1) }], + } as unknown as RecoveryState[number] + + const delayModifier2 = { + guardians: [faker.finance.ethereumAddress()], + } as unknown as RecoveryState[number] + + const delayModifier3 = { + guardians: [faker.finance.ethereumAddress()], + } as unknown as RecoveryState[number] + + const data = [delayModifier1, delayModifier2, delayModifier3] + + expect( + selectDelayModifierByGuardian( + { + recovery: { data }, + } as unknown as RootState, + delayModifier1.guardians[0], + ), + ).toStrictEqual(delayModifier1) + }) + }) + + describe('selectAllRecoveryQueues', () => { + it('should return all recovery queues sorted by timestamp', () => { + const delayModifier1 = { + queue: [{ timestamp: BigNumber.from(1) }, { timestamp: BigNumber.from(3) }], + } as unknown as RecoveryState[number] + + const delayModifier2 = { + queue: [{ timestamp: BigNumber.from(2) }, { timestamp: BigNumber.from(5) }], + } as unknown as RecoveryState[number] + + const delayModifier3 = { + queue: [{ timestamp: BigNumber.from(4) }, { timestamp: BigNumber.from(6) }], + } as unknown as RecoveryState[number] + + const data = [delayModifier1, delayModifier2, delayModifier3] + + expect( + selectAllRecoveryQueues({ + recovery: { data }, + } as unknown as RootState), + ).toStrictEqual([ + { timestamp: BigNumber.from(1) }, + { timestamp: BigNumber.from(2) }, + { timestamp: BigNumber.from(3) }, + { timestamp: BigNumber.from(4) }, + { timestamp: BigNumber.from(5) }, + { timestamp: BigNumber.from(6) }, + ]) + }) + }) + + describe('selectDelayModifierByTxHash', () => { + it('should return the Delay Modifier for the given txHash', () => { + const txHash = faker.string.hexadecimal() + + const delayModifier1 = { + queue: [{ transactionHash: txHash }], + } as unknown as RecoveryState[number] + + const delayModifier2 = { + queue: [{ transactionHash: faker.string.hexadecimal() }], + } as unknown as RecoveryState[number] + + const delayModifier3 = { + queue: [{ transactionHash: faker.string.hexadecimal() }], + } as unknown as RecoveryState[number] + + const data = [delayModifier1, delayModifier2, delayModifier3] + + expect( + selectDelayModifierByTxHash( + { + recovery: { data }, + } as unknown as RootState, + txHash, + ), + ).toStrictEqual(delayModifier1) + }) + }) +}) diff --git a/src/store/recoverySlice.ts b/src/store/recoverySlice.ts index 4a30b615ac..13e614d3ac 100644 --- a/src/store/recoverySlice.ts +++ b/src/store/recoverySlice.ts @@ -7,14 +7,16 @@ import { sameAddress } from '@/utils/addresses' import type { RootState } from '.' export type RecoveryQueueItem = TransactionAddedEvent & { - timestamp: number + timestamp: BigNumber validFrom: BigNumber expiresAt: BigNumber | null + isMalicious: boolean + executor: string } export type RecoveryState = Array<{ address: string - modules: Array + guardians: Array txExpiration: BigNumber txCooldown: BigNumber txNonce: BigNumber @@ -30,9 +32,20 @@ export const recoverySlice = slice export const selectRecovery = createSelector(selector, (recovery) => recovery.data) -export const selectRecoveryByGuardian = createSelector( +export const selectDelayModifierByGuardian = createSelector( [selectRecovery, (_: RootState, walletAddress: string) => walletAddress], (recovery, walletAddress) => { - return recovery.find(({ modules }) => modules.some((module) => sameAddress(module, walletAddress))) + return recovery.find(({ guardians }) => guardians.some((guardian) => sameAddress(guardian, walletAddress))) + }, +) + +export const selectAllRecoveryQueues = createSelector(selectRecovery, (recovery) => { + return recovery.flatMap(({ queue }) => queue).sort((a, b) => a.timestamp.sub(b.timestamp).toNumber()) +}) + +export const selectDelayModifierByTxHash = createSelector( + [selectRecovery, (_: RootState, txHash: string) => txHash], + (recovery, txHash) => { + return recovery.find(({ queue }) => queue.some((item) => item.transactionHash === txHash)) }, ) diff --git a/src/tests/builders/safe.ts b/src/tests/builders/safe.ts new file mode 100644 index 0000000000..f4f3a1b21e --- /dev/null +++ b/src/tests/builders/safe.ts @@ -0,0 +1,39 @@ +import { faker } from '@faker-js/faker' +import { ImplementationVersionState } from '@safe-global/safe-gateway-typescript-sdk' +import type { SafeInfo, AddressEx } from '@safe-global/safe-gateway-typescript-sdk' + +import { Builder } from '../Builder' +import { generateRandomArray } from './utils' +import { LATEST_SAFE_VERSION } from '@/config/constants' +import { checksumAddress } from '@/utils/addresses' +import type { IBuilder } from '../Builder' + +const MAX_OWNERS_LENGTH = 10 + +function addressExBuilder(): IBuilder { + return Builder.new().with({ + value: checksumAddress(faker.finance.ethereumAddress()), + name: faker.word.words(), + logoUri: faker.image.url(), + }) +} + +export function safeInfoBuilder(): IBuilder { + return Builder.new().with({ + address: addressExBuilder().build(), + chainId: faker.string.numeric(), + nonce: faker.number.int(), + threshold: faker.number.int(), + owners: generateRandomArray(() => addressExBuilder().build(), { min: 1, max: MAX_OWNERS_LENGTH }), + implementation: undefined, + implementationVersionState: ImplementationVersionState.UP_TO_DATE, + modules: [], + guard: null, + fallbackHandler: addressExBuilder().build(), + version: LATEST_SAFE_VERSION, + collectiblesTag: faker.string.numeric(), + txQueuedTag: faker.string.numeric(), + txHistoryTag: faker.string.numeric(), + messagesTag: faker.string.numeric(), + }) +} diff --git a/src/utils/date.ts b/src/utils/date.ts index 0c66c1065d..5fdb592450 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -18,6 +18,8 @@ export const formatWithSchema = (timestamp: number, schema: string): string => f export const formatTime = (timestamp: number): string => formatWithSchema(timestamp, 'h:mm a') +export const formatDate = (timestamp: number): string => formatWithSchema(timestamp, 'DD.MM.yyyy') + export const formatDateTime = (timestamp: number): string => formatWithSchema(timestamp, 'MMM d, yyyy - h:mm:ss a') export const formatTimeInWords = (timestamp: number): string => formatDistanceToNow(timestamp, { addSuffix: true }) diff --git a/src/utils/transaction-calldata.ts b/src/utils/transaction-calldata.ts index e2236a4718..2eebf9e918 100644 --- a/src/utils/transaction-calldata.ts +++ b/src/utils/transaction-calldata.ts @@ -6,6 +6,7 @@ import { Multi_send__factory } from '@/types/contracts/factories/@safe-global/sa import { ERC20__factory } from '@/types/contracts/factories/@openzeppelin/contracts/build/contracts/ERC20__factory' import { ERC721__factory } from '@/types/contracts/factories/@openzeppelin/contracts/build/contracts/ERC721__factory' import { decodeMultiSendTxs } from '@/utils/transactions' +import { Safe__factory } from '@/types/contracts' export const isCalldata = (data: string, fragment: FunctionFragment): boolean => { const signature = fragment.format() @@ -37,6 +38,29 @@ const isErc721SafeTransferFromWithBytesCalldata = (data: string): boolean => { return isCalldata(data, safeTransferFromWithBytesFragment) } +// Safe +const safeInterface = Safe__factory.createInterface() + +const addOwnerWithThresholdFragment = safeInterface.getFunction('addOwnerWithThreshold') +export function isAddOwnerWithThresholdCalldata(data: string): boolean { + return isCalldata(data, addOwnerWithThresholdFragment) +} + +const removeOwnerFragment = safeInterface.getFunction('removeOwner') +export function isRemoveOwnerCalldata(data: string): boolean { + return isCalldata(data, removeOwnerFragment) +} + +const swapOwnerFagment = safeInterface.getFunction('swapOwner') +export function isSwapOwnerCalldata(data: string): boolean { + return isCalldata(data, swapOwnerFagment) +} + +const changeThresholdFragment = safeInterface.getFunction('changeThreshold') +export function isChangeThresholdCalldata(data: string): boolean { + return isCalldata(data, changeThresholdFragment) +} + // MultiSend const multiSendInterface = Multi_send__factory.createInterface() const multiSendFragment = multiSendInterface.getFunction('multiSend') From 6896ea6497e6318c8ffb226c27737f612c12f456 Mon Sep 17 00:00:00 2001 From: iamacook Date: Tue, 21 Nov 2023 11:34:08 +0100 Subject: [PATCH 2/2] fix: rename hook + tweak transaction type/tooltip --- src/components/dashboard/RecoveryInProgress/index.tsx | 4 ++-- src/components/recovery/RecoveryInfo/index.tsx | 2 +- src/components/recovery/RecoveryType/index.tsx | 6 ++++-- .../__tests__/{useTimestamp.test.ts => useClock.test.ts} | 6 +++--- src/hooks/{useTimestamp.ts => useClock.ts} | 2 +- src/hooks/useRecoveryTxState.ts | 4 ++-- 6 files changed, 13 insertions(+), 11 deletions(-) rename src/hooks/__tests__/{useTimestamp.test.ts => useClock.test.ts} (77%) rename src/hooks/{useTimestamp.ts => useClock.ts} (84%) diff --git a/src/components/dashboard/RecoveryInProgress/index.tsx b/src/components/dashboard/RecoveryInProgress/index.tsx index 5fc9c92698..c17548bc85 100644 --- a/src/components/dashboard/RecoveryInProgress/index.tsx +++ b/src/components/dashboard/RecoveryInProgress/index.tsx @@ -2,7 +2,7 @@ import { Card, Grid, Typography } from '@mui/material' import type { ReactElement } from 'react' import { useAppSelector } from '@/store' -import { useTimestamp } from '@/hooks/useTimestamp' +import { useClock } from '@/hooks/useClock' import { WidgetContainer, WidgetBody } from '../styled' import RecoveryPending from '@/public/images/common/recovery-pending.svg' import ExternalLink from '@/components/common/ExternalLink' @@ -80,7 +80,7 @@ function _RecoveryInProgressWidget({ nextTx }: { nextTx: RecoveryQueueItem }): R } // Appease React TypeScript warnings -const _useTimestamp = () => useTimestamp(60_000) // Countdown does not display +const _useTimestamp = () => useClock(60_000) // Countdown does not display const _useSupportsRecovery = () => useHasFeature(FEATURES.RECOVERY) const _useQueuedRecoveryTxs = () => useAppSelector(selectAllRecoveryQueues) diff --git a/src/components/recovery/RecoveryInfo/index.tsx b/src/components/recovery/RecoveryInfo/index.tsx index d490267c51..5f2fa6f281 100644 --- a/src/components/recovery/RecoveryInfo/index.tsx +++ b/src/components/recovery/RecoveryInfo/index.tsx @@ -5,7 +5,7 @@ import WarningIcon from '@/public/images/notifications/warning.svg' export const RecoveryInfo = (): ReactElement => { return ( - + diff --git a/src/components/recovery/RecoveryType/index.tsx b/src/components/recovery/RecoveryType/index.tsx index 2fd5b3947c..a1f5fcbf14 100644 --- a/src/components/recovery/RecoveryType/index.tsx +++ b/src/components/recovery/RecoveryType/index.tsx @@ -1,4 +1,4 @@ -import { Box, SvgIcon } from '@mui/material' +import { Box, SvgIcon, Typography } from '@mui/material' import type { ReactElement } from 'react' import RecoveryPlusIcon from '@/public/images/common/recovery-plus.svg' @@ -14,7 +14,9 @@ export function RecoveryType({ isMalicious }: { isMalicious: boolean }): ReactEl fontSize="inherit" sx={{ '& path': { fill: ({ palette }) => palette.warning.main } }} /> - {isMalicious ? 'Malicious' : 'Account'} recovery + + {isMalicious ? 'Malicious transaction' : 'Account recovery'} + ) } diff --git a/src/hooks/__tests__/useTimestamp.test.ts b/src/hooks/__tests__/useClock.test.ts similarity index 77% rename from src/hooks/__tests__/useTimestamp.test.ts rename to src/hooks/__tests__/useClock.test.ts index fbcd9400c0..582dcff3a4 100644 --- a/src/hooks/__tests__/useTimestamp.test.ts +++ b/src/hooks/__tests__/useClock.test.ts @@ -1,14 +1,14 @@ import { renderHook, waitFor } from '@/tests/test-utils' -import { useTimestamp } from '../useTimestamp' +import { useClock } from '../useClock' -describe('useTimestamp', () => { +describe('useClock', () => { it('should update the timestamp every INTERVAL', async () => { jest.useFakeTimers() const timestamp = 69_420 jest.setSystemTime(timestamp) - const { result } = renderHook(() => useTimestamp(1_000)) + const { result } = renderHook(() => useClock(1_000)) jest.advanceTimersByTime(1_000) diff --git a/src/hooks/useTimestamp.ts b/src/hooks/useClock.ts similarity index 84% rename from src/hooks/useTimestamp.ts rename to src/hooks/useClock.ts index 43304e29a9..f50db78d8d 100644 --- a/src/hooks/useTimestamp.ts +++ b/src/hooks/useClock.ts @@ -1,6 +1,6 @@ import { useState, useEffect } from 'react' -export function useTimestamp(interval = 1_000): number { +export function useClock(interval = 1_000): number { const [timestamp, setTimestamp] = useState(Date.now()) useEffect(() => { diff --git a/src/hooks/useRecoveryTxState.ts b/src/hooks/useRecoveryTxState.ts index daf282fbc0..97b6f103eb 100644 --- a/src/hooks/useRecoveryTxState.ts +++ b/src/hooks/useRecoveryTxState.ts @@ -1,4 +1,4 @@ -import { useTimestamp } from './useTimestamp' +import { useClock } from './useClock' import { useAppSelector } from '@/store' import { selectDelayModifierByTxHash } from '@/store/recoverySlice' import type { RecoveryQueueItem } from '@/store/recoverySlice' @@ -13,7 +13,7 @@ export function useRecoveryTxState({ validFrom, expiresAt, transactionHash, args const recovery = useAppSelector((state) => selectDelayModifierByTxHash(state, transactionHash)) // We don't display seconds in the interface, so we can use a 60s interval - const timestamp = useTimestamp(60_000) + const timestamp = useClock(60_000) const remainingMs = validFrom.sub(timestamp) const isValid = remainingMs.lte(0)