From f207efbc9c44a1aca2bc68fa861329b817a37f90 Mon Sep 17 00:00:00 2001 From: Aristides Staffieri Date: Thu, 5 Sep 2024 11:25:44 -0600 Subject: [PATCH] adds failed simulation state to tx scan label, updates tx scan response types, updates non json response error --- .../components/WarningMessages/index.tsx | 70 ++++++++++++++----- .../components/WarningMessages/styles.scss | 1 + .../SendConfirm/TransactionDetails/index.tsx | 8 +-- extension/src/popup/helpers/blockaid.ts | 41 +++++++++-- extension/src/popup/helpers/fetch.ts | 2 +- .../src/popup/views/SignTransaction/index.tsx | 8 +-- 6 files changed, 93 insertions(+), 37 deletions(-) diff --git a/extension/src/popup/components/WarningMessages/index.tsx b/extension/src/popup/components/WarningMessages/index.tsx index 9c6fd8290..3028b4e62 100644 --- a/extension/src/popup/components/WarningMessages/index.tsx +++ b/extension/src/popup/components/WarningMessages/index.tsx @@ -1147,33 +1147,65 @@ export const BlockAidSiteScanLabel = ({ return ; }; -export const BlockaidMaliciousTxSignWarning = ({ - type, +export const BlockaidTxScanLabel = ({ + scanResult, }: { - type: BlockAidScanTxResult["validation"]["result_type"]; + scanResult: BlockAidScanTxResult; }) => { const { t } = useTranslation(); - const details = - type === "Malicious" - ? { + const { simulation, validation } = scanResult; + + if ("error" in simulation) { + return ( + +
+

{t(simulation.error)}

+
+
+ ); + } + + let message = null; + if ("result_type" in validation) { + switch (validation.result_type) { + case "Malicious": { + message = { header: "This contract was flagged as malicious", variant: WarningMessageVariant.highAlert, - message: - "Proceed with caution. Blockaid has flagged this transaction as malicious.", - } - : { + message: validation.description, + }; + return ( + +
+

{t(message.message)}

+
+
+ ); + } + + case "Warning": { + message = { header: "This contract was flagged as suspicious", variant: WarningMessageVariant.warning, - message: - "Proceed with caution. Blockaid has flagged this transaction as suspicious.", + message: validation.description, }; - return ( - -
-

{t(details.message)}

-
-
- ); + return ( + +
+

{t(message.message)}

+
+
+ ); + } + + case "Benign": + default: + } + } + return <>; }; export const BlockaidMaliciousTxInternalWarning = ({ diff --git a/extension/src/popup/components/WarningMessages/styles.scss b/extension/src/popup/components/WarningMessages/styles.scss index bea0544fa..ed9f3a3a8 100644 --- a/extension/src/popup/components/WarningMessages/styles.scss +++ b/extension/src/popup/components/WarningMessages/styles.scss @@ -93,6 +93,7 @@ &__children-wrapper { flex-grow: 1; + word-break: break-word; a { color: var(--color-white); diff --git a/extension/src/popup/components/sendPayment/SendConfirm/TransactionDetails/index.tsx b/extension/src/popup/components/sendPayment/SendConfirm/TransactionDetails/index.tsx index 15383fbc5..0e20c8cc2 100644 --- a/extension/src/popup/components/sendPayment/SendConfirm/TransactionDetails/index.tsx +++ b/extension/src/popup/components/sendPayment/SendConfirm/TransactionDetails/index.tsx @@ -63,7 +63,7 @@ import { import { HardwareSign } from "popup/components/hardwareConnect/HardwareSign"; import { useScanAsset, useScanTx } from "popup/helpers/blockaid"; import { - BlockaidMaliciousTxInternalWarning, + BlockaidTxScanLabel, FlaggedWarningMessage, } from "popup/components/WarningMessages"; import { View } from "popup/basics/layout/View"; @@ -620,11 +620,7 @@ export const TransactionDetails = ({ goBack }: { goBack: () => void }) => { )} - {scanResult && scanResult.validation.result_type !== "Benign" && ( - - )} + {scanResult && } {submission.submitStatus === ActionStatus.IDLE && ( { + if (validation.status === "Error") { + return validation as ValidationResult; + } + return validation as ValidationError; +}; + +export const castSimulation = (simulation: Simulation) => { + if (simulation.status === "Error") { + return simulation as SimulationResult; + } + return simulation as SimulationError; +}; + export interface BlockAidScanTxResult { - simulation: object; - validation: { - result_type: "Benign" | "Warning" | "Malicious"; - description: string; - }; + simulation: Simulation; + validation: Validation; } export const useScanSite = () => { diff --git a/extension/src/popup/helpers/fetch.ts b/extension/src/popup/helpers/fetch.ts index 5bde9ac53..b057eb5bd 100644 --- a/extension/src/popup/helpers/fetch.ts +++ b/extension/src/popup/helpers/fetch.ts @@ -6,7 +6,7 @@ export const fetchJson = async (url: string, options?: RequestInit) => { if (!res.headers.get("content-type")?.includes("application/json")) { const content = await res.text(); - throw new Error(content); + throw new Error(`Did not receive json error:${content}`); } const data = (await res.json()) as T; diff --git a/extension/src/popup/views/SignTransaction/index.tsx b/extension/src/popup/views/SignTransaction/index.tsx index e0960b775..964a0df9d 100644 --- a/extension/src/popup/views/SignTransaction/index.tsx +++ b/extension/src/popup/views/SignTransaction/index.tsx @@ -53,7 +53,7 @@ import { FirstTimeWarningMessage, MemoWarningMessage, SSLWarningMessage, - BlockaidMaliciousTxSignWarning, + BlockaidTxScanLabel, } from "popup/components/WarningMessages"; import { HardwareSign } from "popup/components/hardwareConnect/HardwareSign"; import { KeyIdenticon } from "popup/components/identicons/KeyIdenticon"; @@ -328,11 +328,7 @@ export const SignTransaction = () => { {!isDomainListedAllowed && !isSubmitDisabled ? ( ) : null} - {scanResult && scanResult.validation.result_type !== "Benign" && ( - - )} + {scanResult && } {renderTabBody()} );