-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Safenet Check Summary to Signature Form (#4419)
* Add SafeNet Check Summary to Signature Form This PR adds the SafeNet checks to the transaction execution flow in the wallet interface. Currently, the checks are shown for any Safe that has SafeNet enabled (i.e. has the SafeNet guard set on a supported chain). The checks are done using the SafeNet service backend. Results are grouped into categories which allows the UI to show high level summaries based on a collection of guarantees as well as grouping any unknown guarantees into an "other" category. Note that I implemented SafeTx hash computation as I couldn't find a ulitity method that did it for me anywhere - which was surprising. If there is an existing utility method for this - please let me know and I will change my code to use it. * use Typography instead of p
- Loading branch information
Showing
6 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type ReactElement, useContext } from 'react' | ||
import { SafeNetTxSimulation } from '@/components/tx/security/safenet' | ||
import TxCard from '@/components/tx-flow/common/TxCard' | ||
import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider' | ||
import useIsSafeNetEnabled from '@/hooks/useIsSafeNetEnabled' | ||
import { Typography } from '@mui/material' | ||
import useChainId from '@/hooks/useChainId' | ||
import useSafeAddress from '@/hooks/useSafeAddress' | ||
|
||
const SafeNetTxChecks = (): ReactElement | null => { | ||
const safe = useSafeAddress() | ||
const chainId = useChainId() | ||
const { safeTx } = useContext(SafeTxContext) | ||
const isSafeNetEnabled = useIsSafeNetEnabled() | ||
|
||
if (!isSafeNetEnabled) { | ||
return null | ||
} | ||
|
||
return ( | ||
<TxCard> | ||
<Typography variant="h5">SafeNet checks</Typography> | ||
|
||
<SafeNetTxSimulation safe={safe} chainId={chainId} safeTx={safeTx} /> | ||
</TxCard> | ||
) | ||
} | ||
|
||
export default SafeNetTxChecks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { CircularProgress, List, ListItem, ListItemIcon, ListItemText, Paper, SvgIcon, Typography } from '@mui/material' | ||
import type { SafeTransaction } from '@safe-global/safe-core-sdk-types' | ||
import useDecodeTx from '@/hooks/useDecodeTx' | ||
import CheckIcon from '@/public/images/common/check.svg' | ||
import CloseIcon from '@/public/images/common/close.svg' | ||
import type { SafeNetSimulationResponse } from '@/store/safenet' | ||
import { useLazySimulateSafeNetTxQuery } from '@/store/safenet' | ||
import { useEffect, type ReactElement } from 'react' | ||
import css from './styles.module.css' | ||
import { hashTypedData } from '@/utils/web3' | ||
|
||
export type SafeNetTxSimulationProps = { | ||
safe: string | ||
chainId: string | ||
safeTx?: SafeTransaction | ||
} | ||
|
||
function _getGuaranteeDisplayName(guarantee: string): string { | ||
switch (guarantee) { | ||
case 'no_delegatecall': | ||
return 'Fraud verification' | ||
case 'no_contract_recipient': | ||
case 'recipient_signature': | ||
return 'Recipient verification' | ||
default: | ||
return 'Other' | ||
} | ||
} | ||
|
||
function _groupResultGuarantees({ | ||
results, | ||
}: Pick<SafeNetSimulationResponse, 'results'>): { display: string; success: boolean }[] { | ||
const groups = results.reduce((groups, { guarantee, success }) => { | ||
const display = _getGuaranteeDisplayName(guarantee) | ||
return { | ||
...groups, | ||
[display]: (groups[display] ?? true) && success, | ||
} | ||
}, {} as Record<string, boolean>) | ||
return Object.entries(groups) | ||
.map(([display, success]) => ({ display, success })) | ||
.sort((a, b) => a.display.localeCompare(b.display)) | ||
} | ||
|
||
function _getSafeTxHash({ safe, chainId, safeTx }: Required<SafeNetTxSimulationProps>): string { | ||
return hashTypedData({ | ||
domain: { | ||
chainId, | ||
verifyingContract: safe, | ||
}, | ||
types: { | ||
SafeTx: [ | ||
{ type: 'address', name: 'to' }, | ||
{ type: 'uint256', name: 'value' }, | ||
{ type: 'bytes', name: 'data' }, | ||
{ type: 'uint8', name: 'operation' }, | ||
{ type: 'uint256', name: 'safeTxGas' }, | ||
{ type: 'uint256', name: 'baseGas' }, | ||
{ type: 'uint256', name: 'gasPrice' }, | ||
{ type: 'address', name: 'gasToken' }, | ||
{ type: 'address', name: 'refundReceiver' }, | ||
{ type: 'uint256', name: 'nonce' }, | ||
], | ||
}, | ||
message: { ...safeTx.data }, | ||
}) | ||
} | ||
|
||
const SafeNetTxTxSimulationSummary = ({ simulation }: { simulation: SafeNetSimulationResponse }): ReactElement => { | ||
if (simulation.results.length === 0) { | ||
return <Typography>No SafeNet checks enabled...</Typography> | ||
} | ||
|
||
const guarantees = _groupResultGuarantees(simulation) | ||
|
||
return ( | ||
<Paper variant="outlined" className={css.wrapper}> | ||
{simulation.hasError && ( | ||
<Typography color="error" className={css.errorSummary}> | ||
One or more SafeNet checks failed! | ||
</Typography> | ||
)} | ||
|
||
<List> | ||
{guarantees.map(({ display, success }) => ( | ||
<ListItem key={display}> | ||
<ListItemIcon> | ||
{success ? ( | ||
<SvgIcon component={CheckIcon} inheritViewBox fontSize="small" color="success" /> | ||
) : ( | ||
<SvgIcon component={CloseIcon} inheritViewBox fontSize="small" color="error" /> | ||
)} | ||
</ListItemIcon> | ||
<ListItemText>{display}</ListItemText> | ||
</ListItem> | ||
))} | ||
</List> | ||
</Paper> | ||
) | ||
} | ||
|
||
export const SafeNetTxSimulation = ({ safe, chainId, safeTx }: SafeNetTxSimulationProps): ReactElement | null => { | ||
const [dataDecoded] = useDecodeTx(safeTx) | ||
const [simulate, { data: simulation, status }] = useLazySimulateSafeNetTxQuery() | ||
|
||
useEffect(() => { | ||
if (!safeTx || !dataDecoded) { | ||
return | ||
} | ||
|
||
const safeTxHash = _getSafeTxHash({ safe, chainId, safeTx }) | ||
simulate({ | ||
chainId, | ||
tx: { | ||
safe, | ||
safeTxHash, | ||
to: safeTx.data.to, | ||
value: safeTx.data.value, | ||
data: safeTx.data.data, | ||
operation: safeTx.data.operation, | ||
safeTxGas: safeTx.data.safeTxGas, | ||
baseGas: safeTx.data.baseGas, | ||
gasPrice: safeTx.data.gasPrice, | ||
gasToken: safeTx.data.gasToken, | ||
refundReceiver: safeTx.data.refundReceiver, | ||
// We don't send confirmations, as we want to simulate the transaction before signing. | ||
// In the future, we can consider sending the already collected signatures, but this is not | ||
// necessary at the moment. | ||
confirmations: [], | ||
dataDecoded, | ||
}, | ||
}) | ||
}, [safe, chainId, safeTx, dataDecoded, simulate]) | ||
|
||
switch (status) { | ||
case 'fulfilled': | ||
return <SafeNetTxTxSimulationSummary simulation={simulation!} /> | ||
case 'rejected': | ||
return ( | ||
<Typography color="error"> | ||
<SvgIcon component={CloseIcon} inheritViewBox fontSize="small" sx={{ verticalAlign: 'middle', mr: 1 }} /> | ||
Unexpected error simulating with SafeNet! | ||
</Typography> | ||
) | ||
default: | ||
return <CircularProgress size={22} sx={{ color: ({ palette }) => palette.text.secondary }} /> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
padding: var(--space-1) var(--space-2); | ||
border-width: 1px; | ||
} | ||
|
||
.errorSummary { | ||
padding: calc(var(--space-1) * 2) calc(var(--space-2) * 2) var(--space-1) calc(var(--space-2) * 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import useSafeInfo from '@/hooks/useSafeInfo' | ||
import { useGetSafeNetConfigQuery } from '@/store/safenet' | ||
import { sameAddress } from '@/utils/addresses' | ||
|
||
const useIsSafeNetEnabled = () => { | ||
const { safe } = useSafeInfo() | ||
const { data: safeNetConfig } = useGetSafeNetConfigQuery() | ||
|
||
return sameAddress(safe.guard?.value, safeNetConfig?.guards[safe.chainId]) | ||
} | ||
|
||
export default useIsSafeNetEnabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters