Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(synapse-interface): Dest address screener middleware #2870

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ export const DestinationAddressInput = ({
const adjustInputSize = () => {
const addressInput: HTMLElement = document.getElementById('address-input')

if (!addressInput) return

if (isInputFocused || isInputInvalid) {
addressInput.style.width = '12rem'
} else if (inputValue.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AnalyticsBrowser } from '@segment/analytics-next'
import { getAccount } from '@wagmi/core'
import { createContext, useContext } from 'react'

import { isBlacklisted } from '@/utils/isBlacklisted'
import { screenAddress } from '@/utils/screenAddress'
import { wagmiConfig } from '@/wagmiConfig'

Expand All @@ -24,12 +23,10 @@ export const segmentAnalyticsEvent = (

const { address } = getAccount(wagmiConfig)

if (isBlacklisted(address)) {
document.body = document.createElement('body')
} else {
if (screen) {
screenAddress(address)
}
if (screen && address) {
screenAddress(address).catch((error) => {
console.error('Error screening address:', error)
})
abtestingalpha marked this conversation as resolved.
Show resolved Hide resolved
}

const enrichedEventData = {
Expand Down
12 changes: 6 additions & 6 deletions packages/synapse-interface/contexts/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { setSwapChainId } from '@/slices/swap/reducer'
import { fetchAndStorePortfolioBalances } from '@/slices/portfolio/hooks'
import { useAppDispatch } from '@/store/hooks'
import { resetPortfolioState } from '@/slices/portfolio/actions'
import { isBlacklisted } from '@/utils/isBlacklisted'
import { screenAddress } from '@/utils/screenAddress'

const WalletStatusContext = createContext(undefined)
Expand Down Expand Up @@ -85,11 +84,12 @@ export const UserProvider = ({ children }) => {

useEffect(() => {
if (address) {
if (!isBlacklisted(address)) {
screenAddress(address)
} else {
document.body = document.createElement('body')
}
;(async () => {
const isRisky = await screenAddress(address)
if (isRisky) {
return
}
})()
}
}, [address])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
} from '@/components/Maintenance/Maintenance'
import { wagmiConfig } from '@/wagmiConfig'
import { useStaleQuoteUpdater } from '@/utils/hooks/useStaleQuoteUpdater'
import { screenAddress } from '@/utils/screenAddress'

const StateManagedBridge = () => {
const { address } = useAccount()
Expand Down Expand Up @@ -349,6 +350,14 @@ const StateManagedBridge = () => {

const executeBridge = async () => {
let pendingPopup: any

if (destinationAddress) {
const isRisky = await screenAddress(destinationAddress)
if (isRisky) {
return
}
}

segmentAnalyticsEvent(
`[Bridge] initiates bridge`,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Middleware } from '@reduxjs/toolkit'

import { screenAddress } from '@/utils/screenAddress'
import { setDestinationAddress } from '@/slices/bridge/reducer'

export const destinationAddressMiddleware: Middleware =
(_store) => (next) => async (action) => {
if (setDestinationAddress.match(action) && action.payload !== null) {
const isRisky = await screenAddress(action.payload)
if (isRisky) {
return
Copy link

Choose a reason for hiding this comment

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

Logic: Returning without any action might lead to silent failures. Ensure this behavior is intended and documented.

}
}
return next(action)
}
3 changes: 2 additions & 1 deletion packages/synapse-interface/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { api } from '@/slices/api/slice'
import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'
import { storageKey, persistConfig, persistedReducer } from './reducer'
import { resetReduxCache } from '@/slices/application/actions'
import { destinationAddressMiddleware } from '@/store/destinationAddressMiddleware'

const checkVersionAndResetCache = (): boolean => {
if (typeof window !== 'undefined') {
Expand All @@ -27,7 +28,7 @@ export const store = configureStore({
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}).concat(api.middleware),
}).concat(api.middleware, destinationAddressMiddleware),
})

if (checkVersionAndResetCache()) {
Expand Down
50 changes: 33 additions & 17 deletions packages/synapse-interface/utils/screenAddress.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import { Address } from 'viem'

Check failure on line 1 in packages/synapse-interface/utils/screenAddress.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check failure on line 1 in packages/synapse-interface/utils/screenAddress.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

import { GlobalEventEmitter } from '@/utils/globalEventEmitter'
import { DISCORD_URL } from '@/constants/urls'
import { isBlacklisted } from './isBlacklisted'

const createRiskDetectedEvent = (address: Address | string) => {
return new CustomEvent('riskDetected', {
detail: {
message: `This address ${address} has been flagged for being associated with illicit activities. If you think this is a mistake, please contact <a style="text-decoration: underline; text-underline-offset: 0.2em;" href=${DISCORD_URL} target="_blank" rel="noopener noreferrer">support</a>.`,
},
})
}

export const screenAddress = (address: Address | string) => {
export const screenAddress = async (
address: Address | string
): Promise<boolean> => {
const url = `https://screener.omnirpc.io/fe/address/${address}`

fetch(url, {
method: 'GET',
})
.then((response) => response.json())
.then(({ risk }) => {
if (risk) {
const event = new CustomEvent('riskDetected', {
detail: {
message: `This address has been flagged for being associated with illicit activities. If you think this is a mistake, please contact <a style="text-decoration: underline; text-underline-offset: 0.2em;" href=${DISCORD_URL} target="_blank" rel="noopener noreferrer">support</a>.`,
},
})
if (isBlacklisted(address)) {
const event = createRiskDetectedEvent(address)

GlobalEventEmitter.dispatchEvent(event)
return true
}

try {
const response = await fetch(url, { method: 'GET' })
const { risk } = await response.json()

if (risk) {
const event = createRiskDetectedEvent(address)

GlobalEventEmitter.dispatchEvent(event)
}
})
.catch((error) => console.error('Error:', error))
GlobalEventEmitter.dispatchEvent(event)
return true
}
return false
} catch (error) {
console.error('Error:', error)
return false
}
}
Loading