-
-
Notifications
You must be signed in to change notification settings - Fork 279
feat: add getApprovals to PhishingController #8074
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
Changes from all commits
296ae1a
ab33cb8
3ea4dbf
1722073
ed84f19
a21d206
2770750
bdeb1ba
d18256e
e2ad990
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 |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ import type { | |
| TokenScanApiResponse, | ||
| AddressScanCacheData, | ||
| AddressScanResult, | ||
| ApprovalsResponse, | ||
| } from './types'; | ||
| import { | ||
| applyDiffs, | ||
|
|
@@ -51,6 +52,7 @@ import { | |
| splitCacheHits, | ||
| resolveChainName, | ||
| getPathnameFromUrl, | ||
| isApprovalSupportedChain, | ||
| } from './utils'; | ||
|
|
||
| export const PHISHING_CONFIG_BASE_URL = | ||
|
|
@@ -71,6 +73,7 @@ export const SECURITY_ALERTS_BASE_URL = | |
| 'https://security-alerts.api.cx.metamask.io'; | ||
| export const TOKEN_BULK_SCANNING_ENDPOINT = '/token/scan-bulk'; | ||
| export const ADDRESS_SCAN_ENDPOINT = '/address/evm/scan'; | ||
| export const APPROVALS_ENDPOINT = '/address/evm/approvals'; | ||
|
|
||
| // Cache configuration defaults | ||
| export const DEFAULT_URL_SCAN_CACHE_TTL = 1 * 60; // 1 minute in seconds | ||
|
|
@@ -388,6 +391,7 @@ const MESSENGER_EXPOSED_METHODS = [ | |
| 'bulkScanUrls', | ||
| 'bulkScanTokens', | ||
| 'scanAddress', | ||
| 'getApprovals', | ||
| ] as const; | ||
|
|
||
| /** | ||
|
|
@@ -1233,6 +1237,66 @@ export class PhishingController extends BaseController< | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get token approvals for an EVM address with security enrichments. | ||
| * | ||
| * @param chainId - The chain ID in hex format (e.g., '0x1' for Ethereum). | ||
| * @param address - The address to get approvals for. | ||
| * @returns The approvals response containing approval data, or empty approvals on error. | ||
| */ | ||
| getApprovals = async ( | ||
|
Contributor
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. The flow duplicates
Contributor
Author
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. Agreed there's duplication. Similar to the test scaffold |
||
| chainId: string, | ||
| address: string, | ||
| ): Promise<ApprovalsResponse> => { | ||
| if (!address || !chainId) { | ||
| return { approvals: [] }; | ||
| } | ||
|
|
||
| const normalizedChainId = chainId.toLowerCase(); | ||
| const normalizedAddress = address.toLowerCase(); | ||
| const chain = resolveChainName(normalizedChainId); | ||
|
|
||
| if (!chain || !isApprovalSupportedChain(chain)) { | ||
| return { approvals: [] }; | ||
| } | ||
|
|
||
| const apiResponse = await safelyExecuteWithTimeout( | ||
| async () => { | ||
| const res = await fetch( | ||
| `${SECURITY_ALERTS_BASE_URL}${APPROVALS_ENDPOINT}`, | ||
| { | ||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| chain, | ||
| address: normalizedAddress, | ||
| }), | ||
| }, | ||
| ); | ||
| if (!res.ok) { | ||
| return { error: `${res.status} ${res.statusText}` }; | ||
| } | ||
| const data: ApprovalsResponse = await res.json(); | ||
|
imblue-dabadee marked this conversation as resolved.
|
||
| return data; | ||
| }, | ||
| true, | ||
| 5000, | ||
| ); | ||
|
|
||
| if ( | ||
| !apiResponse || | ||
| 'error' in apiResponse || | ||
| !Array.isArray(apiResponse.approvals) | ||
| ) { | ||
| return { approvals: [] }; | ||
| } | ||
|
|
||
| return apiResponse; | ||
| }; | ||
|
|
||
| /** | ||
| * Scan multiple tokens for malicious activity in bulk. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.