-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: add utility function to get supported chains from the Security Alerts API #10267
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,12 @@ import { | |
import { WALLET_CONNECT_ORIGIN } from '../../util/walletconnect'; | ||
import AppConstants from '../../core/AppConstants'; | ||
import { | ||
getSupportedChains, | ||
isSecurityAlertsAPIEnabled, | ||
validateWithSecurityAlertsAPI, | ||
} from './security-alerts-api'; | ||
import { PPOMController } from '@metamask/ppom-validator'; | ||
import { Hex } from '@metamask/utils'; | ||
|
||
export interface PPOMRequest { | ||
method: string; | ||
|
@@ -65,7 +67,7 @@ async function validateRequest(req: PPOMRequest, transactionId?: string) { | |
|
||
const chainId = NetworkController.state.providerConfig.chainId; | ||
const isConfirmationMethod = CONFIRMATION_METHODS.includes(req.method); | ||
const isSupportedChain = BLOCKAID_SUPPORTED_CHAIN_IDS.includes(chainId); | ||
const isSupportedChain = await isChainSupported(chainId); | ||
|
||
const isSecurityAlertsEnabled = | ||
PreferencesController.state.securityAlertsEnabled; | ||
|
@@ -119,6 +121,20 @@ async function validateRequest(req: PPOMRequest, transactionId?: string) { | |
} | ||
} | ||
|
||
async function isChainSupported(chainId: Hex): Promise<boolean> { | ||
try { | ||
const supportedChains = isSecurityAlertsAPIEnabled() | ||
? await getSupportedChains() | ||
: BLOCKAID_SUPPORTED_CHAIN_IDS; | ||
return supportedChains.includes(chainId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but could we remove some duplication with something like:
|
||
} catch (e) { | ||
Logger.log( | ||
`Error fetching supported chains from security alerts API: ${e}`, | ||
); | ||
return BLOCKAID_SUPPORTED_CHAIN_IDS.includes(chainId); | ||
} | ||
} | ||
|
||
async function validateWithController( | ||
ppomController: PPOMController, | ||
request: PPOMRequest, | ||
|
@@ -193,4 +209,4 @@ function normalizeRequest(request: PPOMRequest): PPOMRequest { | |
}; | ||
} | ||
|
||
export default { validateRequest }; | ||
export default { validateRequest, isChainSupported }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { Hex } from '@metamask/utils'; | ||
import { SecurityAlertResponse } from '../../components/Views/confirmations/components/BlockaidBanner/BlockaidBanner.types'; | ||
|
||
const ENDPOINT_VALIDATE = 'validate'; | ||
const ENDPOINT_SUPPORTED_CHAINS = 'supportedChains'; | ||
|
||
export interface SecurityAlertsAPIRequest { | ||
method: string; | ||
|
@@ -19,6 +21,10 @@ export async function validateWithSecurityAlertsAPI( | |
return postRequest(endpoint, request); | ||
} | ||
|
||
export async function getSupportedChains(): Promise<Hex[]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
return getRequest(ENDPOINT_SUPPORTED_CHAINS); | ||
} | ||
|
||
async function postRequest(endpoint: string, body: unknown) { | ||
const url = getUrl(endpoint); | ||
|
||
|
@@ -39,6 +45,20 @@ async function postRequest(endpoint: string, body: unknown) { | |
return response.json(); | ||
} | ||
|
||
async function getRequest(endpoint: string) { | ||
const url = getUrl(endpoint); | ||
|
||
const response = await fetch(url); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`Security alerts API request failed with status: ${response.status}`, | ||
); | ||
} | ||
|
||
return response.json(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the duplication and create a |
||
} | ||
|
||
function getUrl(endpoint: string) { | ||
const host = process.env.SECURITY_ALERTS_API_URL; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but could we create a persistent variable for this such as
getSupportedChainsMock
like we do forisSecurityAlertsEnabledMock
?